Skip to content

Commit

Permalink
Create stubs instead of mocks (#6564)
Browse files Browse the repository at this point in the history
|      Q       |   A
|------------- | -----------
| Type         | improvement
| Fixed issues | <!-- use #NUM format to reference an issue -->

#### Summary

_This is a followup of https://github.com/doctrine/dbal/pull/6558_, I
have touched only what previous PR touched.

@greg0ire
[requested](#6562 (review))
to use stubs where there are no expectations


![image](https://github.com/user-attachments/assets/f71ad45f-87f7-4575-b10d-2f46b2c6525a)
  • Loading branch information
simPod authored Oct 21, 2024
1 parent 9fca0ee commit b87a89f
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 39 deletions.
74 changes: 37 additions & 37 deletions tests/ConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down Expand Up @@ -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))
Expand All @@ -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))
Expand All @@ -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))
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -383,7 +383,7 @@ public function testConnectStartsTransactionInNoAutoCommitMode(): void
$driverMock
->method('connect')
->willReturn(
$this->createMock(DriverConnection::class),
$this->createStub(DriverConnection::class),
);
$conn = new Connection([], $driverMock);

Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion tests/Query/Expression/ExpressionBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
2 changes: 1 addition & 1 deletion tests/Schema/Visitor/DropSchemaSqlCollectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down

0 comments on commit b87a89f

Please sign in to comment.