123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256 |
- <?php
- namespace Illuminate\Tests\Database;
- use Exception;
- use Illuminate\Database\Capsule\Manager as DB;
- use Illuminate\Database\DatabaseTransactionsManager;
- use Mockery as m;
- use PHPUnit\Framework\TestCase;
- use Throwable;
- class DatabaseTransactionsTest extends TestCase
- {
- /**
- * Setup the database schema.
- *
- * @return void
- */
- protected function setUp(): void
- {
- $db = new DB;
- $db->addConnection([
- 'driver' => 'sqlite',
- 'database' => ':memory:',
- ]);
- $db->addConnection([
- 'driver' => 'sqlite',
- 'database' => ':memory:',
- ], 'second_connection');
- $db->setAsGlobal();
- $this->createSchema();
- }
- protected function createSchema()
- {
- foreach (['default', 'second_connection'] as $connection) {
- $this->schema($connection)->create('users', function ($table) {
- $table->increments('id');
- $table->string('name')->nullable();
- $table->string('value')->nullable();
- });
- }
- }
- /**
- * Tear down the database schema.
- *
- * @return void
- */
- protected function tearDown(): void
- {
- foreach (['default', 'second_connection'] as $connection) {
- $this->schema($connection)->drop('users');
- }
- m::close();
- }
- public function testTransactionIsRecordedAndCommitted()
- {
- $transactionManager = m::mock(new DatabaseTransactionsManager);
- $transactionManager->shouldReceive('begin')->once()->with('default', 1);
- $transactionManager->shouldReceive('commit')->once()->with('default');
- $this->connection()->setTransactionManager($transactionManager);
- $this->connection()->table('users')->insert([
- 'name' => 'zain', 'value' => 1,
- ]);
- $this->connection()->transaction(function () {
- $this->connection()->table('users')->where(['name' => 'zain'])->update([
- 'value' => 2,
- ]);
- });
- }
- public function testTransactionIsRecordedAndCommittedUsingTheSeparateMethods()
- {
- $transactionManager = m::mock(new DatabaseTransactionsManager);
- $transactionManager->shouldReceive('begin')->once()->with('default', 1);
- $transactionManager->shouldReceive('commit')->once()->with('default');
- $this->connection()->setTransactionManager($transactionManager);
- $this->connection()->table('users')->insert([
- 'name' => 'zain', 'value' => 1,
- ]);
- $this->connection()->beginTransaction();
- $this->connection()->table('users')->where(['name' => 'zain'])->update([
- 'value' => 2,
- ]);
- $this->connection()->commit();
- }
- public function testNestedTransactionIsRecordedAndCommitted()
- {
- $transactionManager = m::mock(new DatabaseTransactionsManager);
- $transactionManager->shouldReceive('begin')->once()->with('default', 1);
- $transactionManager->shouldReceive('begin')->once()->with('default', 2);
- $transactionManager->shouldReceive('commit')->once()->with('default');
- $this->connection()->setTransactionManager($transactionManager);
- $this->connection()->table('users')->insert([
- 'name' => 'zain', 'value' => 1,
- ]);
- $this->connection()->transaction(function () {
- $this->connection()->table('users')->where(['name' => 'zain'])->update([
- 'value' => 2,
- ]);
- $this->connection()->transaction(function () {
- $this->connection()->table('users')->where(['name' => 'zain'])->update([
- 'value' => 2,
- ]);
- });
- });
- }
- public function testNestedTransactionIsRecordeForDifferentConnectionsdAndCommitted()
- {
- $transactionManager = m::mock(new DatabaseTransactionsManager);
- $transactionManager->shouldReceive('begin')->once()->with('default', 1);
- $transactionManager->shouldReceive('begin')->once()->with('second_connection', 1);
- $transactionManager->shouldReceive('begin')->once()->with('second_connection', 2);
- $transactionManager->shouldReceive('commit')->once()->with('default');
- $transactionManager->shouldReceive('commit')->once()->with('second_connection');
- $this->connection()->setTransactionManager($transactionManager);
- $this->connection('second_connection')->setTransactionManager($transactionManager);
- $this->connection()->table('users')->insert([
- 'name' => 'zain', 'value' => 1,
- ]);
- $this->connection()->transaction(function () {
- $this->connection()->table('users')->where(['name' => 'zain'])->update([
- 'value' => 2,
- ]);
- $this->connection('second_connection')->transaction(function () {
- $this->connection('second_connection')->table('users')->where(['name' => 'zain'])->update([
- 'value' => 2,
- ]);
- $this->connection('second_connection')->transaction(function () {
- $this->connection('second_connection')->table('users')->where(['name' => 'zain'])->update([
- 'value' => 2,
- ]);
- });
- });
- });
- }
- public function testTransactionIsRolledBack()
- {
- $transactionManager = m::mock(new DatabaseTransactionsManager);
- $transactionManager->shouldReceive('begin')->once()->with('default', 1);
- $transactionManager->shouldReceive('rollback')->once()->with('default', 0);
- $transactionManager->shouldNotReceive('commit');
- $this->connection()->setTransactionManager($transactionManager);
- $this->connection()->table('users')->insert([
- 'name' => 'zain', 'value' => 1,
- ]);
- try {
- $this->connection()->transaction(function () {
- $this->connection()->table('users')->where(['name' => 'zain'])->update([
- 'value' => 2,
- ]);
- throw new Exception;
- });
- } catch (Throwable $e) {
- }
- }
- public function testTransactionIsRolledBackUsingSeparateMethods()
- {
- $transactionManager = m::mock(new DatabaseTransactionsManager);
- $transactionManager->shouldReceive('begin')->once()->with('default', 1);
- $transactionManager->shouldReceive('rollback')->once()->with('default', 0);
- $transactionManager->shouldNotReceive('commit');
- $this->connection()->setTransactionManager($transactionManager);
- $this->connection()->table('users')->insert([
- 'name' => 'zain', 'value' => 1,
- ]);
- $this->connection()->beginTransaction();
- $this->connection()->table('users')->where(['name' => 'zain'])->update([
- 'value' => 2,
- ]);
- $this->connection()->rollBack();
- }
- public function testNestedTransactionsAreRolledBack()
- {
- $transactionManager = m::mock(new DatabaseTransactionsManager);
- $transactionManager->shouldReceive('begin')->once()->with('default', 1);
- $transactionManager->shouldReceive('begin')->once()->with('default', 2);
- $transactionManager->shouldReceive('rollback')->once()->with('default', 1);
- $transactionManager->shouldReceive('rollback')->once()->with('default', 0);
- $transactionManager->shouldNotReceive('commit');
- $this->connection()->setTransactionManager($transactionManager);
- $this->connection()->table('users')->insert([
- 'name' => 'zain', 'value' => 1,
- ]);
- try {
- $this->connection()->transaction(function () {
- $this->connection()->table('users')->where(['name' => 'zain'])->update([
- 'value' => 2,
- ]);
- $this->connection()->transaction(function () {
- $this->connection()->table('users')->where(['name' => 'zain'])->update([
- 'value' => 2,
- ]);
- throw new Exception;
- });
- });
- } catch (Throwable $e) {
- }
- }
- /**
- * Get a schema builder instance.
- *
- * @return \Illuminate\Database\Schema\Builder
- */
- protected function schema($connection = 'default')
- {
- return $this->connection($connection)->getSchemaBuilder();
- }
- public function connection($name = 'default')
- {
- return DB::connection($name);
- }
- }
|