From b87a89f1da3865a25b1341104281a09818b77e12 Mon Sep 17 00:00:00 2001 From: Simon Podlipsky Date: Mon, 21 Oct 2024 13:05:27 +0200 Subject: [PATCH] Create stubs instead of mocks (#6564) | Q | A |------------- | ----------- | Type | improvement | Fixed issues | #### Summary _This is a followup of https://github.com/doctrine/dbal/pull/6558_, I have touched only what previous PR touched. @greg0ire [requested](https://github.com/doctrine/dbal/pull/6562#pullrequestreview-2380527773) to use stubs where there are no expectations ![image](https://github.com/user-attachments/assets/f71ad45f-87f7-4575-b10d-2f46b2c6525a) --- tests/ConnectionTest.php | 74 +++++++++---------- .../Expression/ExpressionBuilderTest.php | 2 +- .../Visitor/DropSchemaSqlCollectorTest.php | 2 +- 3 files changed, 39 insertions(+), 39 deletions(-) diff --git a/tests/ConnectionTest.php b/tests/ConnectionTest.php index 10e1ea4d77..5fe70dd9a5 100644 --- a/tests/ConnectionTest.php +++ b/tests/ConnectionTest.php @@ -57,19 +57,19 @@ protected function setUp(): void /** @return Connection&MockObject */ private function getExecuteStatementMockConnection(): Connection { - $driverMock = $this->createMock(Driver::class); + $driver = $this->createStub(Driver::class); - $driverMock + $driver ->method('connect') ->willReturn( - $this->createMock(DriverConnection::class), + $this->createStub(DriverConnection::class), ); $platform = $this->getMockForAbstractClass(AbstractPlatform::class); return $this->getMockBuilder(Connection::class) ->onlyMethods(['executeStatement']) - ->setConstructorArgs([['platform' => $platform], $driverMock]) + ->setConstructorArgs([['platform' => $platform], $driver]) ->getMock(); } @@ -183,13 +183,13 @@ public function testConnectDispatchEvent(): void public function testTransactionBeginDispatchEvent(): void { $eventManager = new EventManager(); - $driverMock = $this->createMock(Driver::class); - $driverMock + $driver = $this->createStub(Driver::class); + $driver ->method('connect') ->willReturn( - $this->createMock(DriverConnection::class), + $this->createStub(DriverConnection::class), ); - $conn = new Connection([], $driverMock, new Configuration(), $eventManager); + $conn = new Connection([], $driver, new Configuration(), $eventManager); $listenerMock = $this->createMock(TransactionBeginDispatchEventListener::class); $listenerMock ->expects(self::exactly(1)) @@ -209,13 +209,13 @@ static function (TransactionBeginEventArgs $eventArgs) use ($conn): bool { public function testTransactionCommitDispatchEvent(): void { $eventManager = new EventManager(); - $driverMock = $this->createMock(Driver::class); - $driverMock + $driver = $this->createStub(Driver::class); + $driver ->method('connect') ->willReturn( - $this->createMock(DriverConnection::class), + $this->createStub(DriverConnection::class), ); - $conn = new Connection([], $driverMock, new Configuration(), $eventManager); + $conn = new Connection([], $driver, new Configuration(), $eventManager); $listenerMock = $this->createMock(TransactionCommitDispatchEventListener::class); $listenerMock ->expects(self::exactly(1)) @@ -236,13 +236,13 @@ static function (TransactionCommitEventArgs $eventArgs) use ($conn): bool { public function testTransactionCommitEventNotCalledAfterRollBack(): void { $eventManager = new EventManager(); - $driverMock = $this->createMock(Driver::class); - $driverMock + $driver = $this->createStub(Driver::class); + $driver ->method('connect') ->willReturn( - $this->createMock(DriverConnection::class), + $this->createStub(DriverConnection::class), ); - $conn = new Connection([], $driverMock, new Configuration(), $eventManager); + $conn = new Connection([], $driver, new Configuration(), $eventManager); $rollBackListenerMock = $this->createMock(TransactionRollBackDispatchEventListener::class); $rollBackListenerMock ->expects(self::exactly(1)) @@ -272,13 +272,13 @@ static function (TransactionRollBackEventArgs $eventArgs) use ($conn): bool { public function testTransactionRollBackDispatchEvent(): void { $eventManager = new EventManager(); - $driverMock = $this->createMock(Driver::class); - $driverMock + $driver = $this->createStub(Driver::class); + $driver ->method('connect') ->willReturn( - $this->createMock(DriverConnection::class), + $this->createStub(DriverConnection::class), ); - $conn = new Connection([], $driverMock, new Configuration(), $eventManager); + $conn = new Connection([], $driver, new Configuration(), $eventManager); $listenerMock = $this->createMock(TransactionRollBackDispatchEventListener::class); $listenerMock ->expects(self::exactly(1)) @@ -306,7 +306,7 @@ public function testEventManagerPassedToPlatform(): void ->method('setEventManager') ->with($eventManager); - $driver = $this->createMock(Driver::class); + $driver = $this->createStub(Driver::class); $driver ->method('getDatabasePlatform') ->willReturn($platform); @@ -383,7 +383,7 @@ public function testConnectStartsTransactionInNoAutoCommitMode(): void $driverMock ->method('connect') ->willReturn( - $this->createMock(DriverConnection::class), + $this->createStub(DriverConnection::class), ); $conn = new Connection([], $driverMock); @@ -398,13 +398,13 @@ public function testConnectStartsTransactionInNoAutoCommitMode(): void public function testCommitStartsTransactionInNoAutoCommitMode(): void { - $driverMock = $this->createMock(Driver::class); - $driverMock + $driver = $this->createStub(Driver::class); + $driver ->method('connect') ->willReturn( - $this->createMock(DriverConnection::class), + $this->createStub(DriverConnection::class), ); - $conn = new Connection([], $driverMock); + $conn = new Connection([], $driver); $conn->setAutoCommit(false); $conn->connect(); @@ -420,12 +420,12 @@ public function testCommitReturn(bool $expectedResult): void $driverConnection->expects(self::once()) ->method('commit')->willReturn($expectedResult); - $driverMock = $this->createMock(Driver::class); - $driverMock + $driver = $this->createStub(Driver::class); + $driver ->method('connect') ->willReturn($driverConnection); - $conn = new Connection([], $driverMock); + $conn = new Connection([], $driver); $conn->connect(); $conn->beginTransaction(); @@ -441,13 +441,13 @@ public static function resultProvider(): array public function testRollBackStartsTransactionInNoAutoCommitMode(): void { - $driverMock = $this->createMock(Driver::class); - $driverMock + $driver = $this->createStub(Driver::class); + $driver ->method('connect') ->willReturn( - $this->createMock(DriverConnection::class), + $this->createStub(DriverConnection::class), ); - $conn = new Connection([], $driverMock); + $conn = new Connection([], $driver); $conn->setAutoCommit(false); $conn->connect(); @@ -458,13 +458,13 @@ public function testRollBackStartsTransactionInNoAutoCommitMode(): void public function testSwitchingAutoCommitModeCommitsAllCurrentTransactions(): void { - $driverMock = $this->createMock(Driver::class); - $driverMock + $driver = $this->createStub(Driver::class); + $driver ->method('connect') ->willReturn( - $this->createMock(DriverConnection::class), + $this->createStub(DriverConnection::class), ); - $conn = new Connection([], $driverMock); + $conn = new Connection([], $driver); $conn->connect(); $conn->beginTransaction(); diff --git a/tests/Query/Expression/ExpressionBuilderTest.php b/tests/Query/Expression/ExpressionBuilderTest.php index 8ccd37994a..11175c5e8b 100644 --- a/tests/Query/Expression/ExpressionBuilderTest.php +++ b/tests/Query/Expression/ExpressionBuilderTest.php @@ -13,7 +13,7 @@ class ExpressionBuilderTest extends TestCase protected function setUp(): void { - $conn = $this->createMock(Connection::class); + $conn = $this->createStub(Connection::class); $this->expr = new ExpressionBuilder($conn); diff --git a/tests/Schema/Visitor/DropSchemaSqlCollectorTest.php b/tests/Schema/Visitor/DropSchemaSqlCollectorTest.php index ddd28fcd00..dd28419d24 100644 --- a/tests/Schema/Visitor/DropSchemaSqlCollectorTest.php +++ b/tests/Schema/Visitor/DropSchemaSqlCollectorTest.php @@ -41,7 +41,7 @@ public function testGetQueriesUsesAcceptedForeignKeys(): void private function getStubKeyConstraint(string $name): ForeignKeyConstraint { - $constraint = $this->createMock(ForeignKeyConstraint::class); + $constraint = $this->createStub(ForeignKeyConstraint::class); $constraint ->method('getName')