pdo = $this->getMockBuilder(PDO::class)->disableOriginalConstructor()->getMock(); $this->composer = $this->getMockBuilder(MysqlQueryComposer::class)->disableOriginalConstructor()->getMock(); $this->composer ->expects($this->any()) ->method('composeCreateSavepoint') ->will( $this->returnCallback( function ($name) { return 'SAVEPOINT ' . $name; } ) ); $this->composer ->expects($this->any()) ->method('composeReleaseSavepoint') ->will( $this->returnCallback( function ($name) { return 'RELEASE SAVEPOINT ' . $name; } ) ); $this->composer ->expects($this->any()) ->method('composeRollbackToSavepoint') ->will( $this->returnCallback( function ($name) { return 'ROLLBACK TO SAVEPOINT ' . $name; } ) ); $this->manager = new TransactionManager($this->pdo, $this->composer); } public function testStartOnce() { $this->pdo ->expects($this->once()) ->method('beginTransaction'); $this->manager->start(); } public function testNested() { $this->pdo ->expects($this->exactly(1)) ->method('beginTransaction'); $this->pdo ->expects($this->exactly(4)) ->method('exec') ->withConsecutive( ['SAVEPOINT POINT_1'], ['SAVEPOINT POINT_2'], ['RELEASE SAVEPOINT POINT_2'], ['ROLLBACK TO SAVEPOINT POINT_1'], ); $this->pdo ->expects($this->exactly(1)) ->method('commit'); $this->manager->start(); $this->manager->start(); $this->manager->start(); $this->manager->commit(); $this->manager->rollback(); $this->manager->commit(); } public function testNestedRollback() { $this->pdo ->expects($this->exactly(2)) ->method('exec') ->withConsecutive( ['SAVEPOINT POINT_1'], ['ROLLBACK TO SAVEPOINT POINT_1'], ); $this->pdo ->expects($this->once()) ->method('rollBack'); $this->manager->start(); $this->manager->start(); $this->manager->rollback(); $this->manager->rollback(); } public function testLevel() { $this->assertEquals(0, $this->manager->getLevel()); $this->assertFalse($this->manager->isStarted()); $this->manager->start(); $this->assertEquals(1, $this->manager->getLevel()); $this->assertTrue($this->manager->isStarted()); $this->manager->start(); $this->assertEquals(2, $this->manager->getLevel()); $this->manager->commit(); $this->assertEquals(1, $this->manager->getLevel()); $this->manager->rollback(); $this->assertEquals(0, $this->manager->getLevel()); $this->assertFalse($this->manager->isStarted()); } public function testError1() { $this->expectException(RuntimeException::class); $this->manager->commit(); } public function testError2() { $this->expectException(RuntimeException::class); $this->manager->start(); $this->assertTrue($this->manager->isStarted()); $this->manager->commit(); $this->manager->rollback(); } public function testRunOnce() { $this->pdo ->expects($this->once()) ->method('beginTransaction'); $this->pdo ->expects($this->once()) ->method('commit'); $this->manager->run( function () {} ); } public function testRunNested() { $this->pdo ->expects($this->once()) ->method('beginTransaction'); $this->pdo ->expects($this->exactly(2)) ->method('exec') ->withConsecutive( ['SAVEPOINT POINT_1'], ['RELEASE SAVEPOINT POINT_1'], ); $this->pdo ->expects($this->once()) ->method('commit'); $this->manager->run( function () { $this->manager->run( function () {} ); } ); } public function testRunException() { $this->pdo ->expects($this->once()) ->method('beginTransaction'); $this->pdo ->expects($this->once()) ->method('rollback'); try { $this->manager->run( function () { throw new RuntimeException(); } ); } catch (RuntimeException $e) {} } }