Skip to content

Commit

Permalink
Merge pull request doctrine#3582 from jwage/phpstan-tests
Browse files Browse the repository at this point in the history
Enable phpstan in DBAL tests
  • Loading branch information
morozov committed Aug 26, 2019
2 parents e13540c + 65ce317 commit c02c3ea
Show file tree
Hide file tree
Showing 51 changed files with 496 additions and 229 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@
"require-dev": {
"doctrine/coding-standard": "^6.0",
"jetbrains/phpstorm-stubs": "^2019.1",
"phpstan/phpstan": "^0.11.3",
"phpstan/phpstan": "^0.11.6",
"phpstan/phpstan-phpunit": "^0.11",
"phpunit/phpunit": "^8.3.3",
"symfony/console": "^2.0.5|^3.0|^4.0|^5.0"
},
Expand Down
59 changes: 58 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 24 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ parameters:
level: 7
paths:
- %currentWorkingDirectory%/lib
- %currentWorkingDirectory%/tests
autoload_files:
- %currentWorkingDirectory%/tests/phpstan-polyfill.php
reportUnmatchedIgnoredErrors: false
Expand Down Expand Up @@ -63,3 +64,26 @@ parameters:
-
message: '~^Strict comparison using === between string|false and null will always evaluate to false\.~'
path: %currentWorkingDirectory%/lib/Doctrine/DBAL/Driver/PDOStatement.php

# impossible inference for covariance
- '~^Property Doctrine\\Tests\\DBAL\\Types\\\S+Test::\$type \(Doctrine\\DBAL\\Types\\\S+Type\) does not accept Doctrine\\DBAL\\Types\\Type\.\z~'
- '~^Property Doctrine\\Tests\\DBAL\\Tools\\Console\\RunSqlCommandTest::\$command \(Doctrine\\DBAL\\Tools\\Console\\Command\\RunSqlCommand\) does not accept Symfony\\Component\\Console\\Command\\Command\.\z~'

# https://github.com/phpstan/phpstan-phpunit/pull/28
-
message: '~Call to method expects\(\) on an unknown class \S+~'
path: %currentWorkingDirectory%/tests/Doctrine/Tests/DBAL/Platforms/AbstractPlatformTestCase.php
-
message: '~Call to method expects\(\) on an unknown class \S+~'
path: %currentWorkingDirectory%/tests/Doctrine/Tests/DBAL/ConnectionTest.php
-
message: '~Call to method expects\(\) on an unknown class \S+~'
path: %currentWorkingDirectory%/tests/Doctrine/Tests/DBAL/Functional/Schema/SchemaManagerFunctionalTestCase.php

# https://github.com/doctrine/dbal/pull/3582/files#r290847062
-
message: '~Parameter #3 \$type of method Doctrine\\DBAL\\Driver\\Statement::bindValue\(\) expects int, string given\.~'
path: %currentWorkingDirectory%/tests/Doctrine/Tests/DBAL/Functional/DataAccessTest.php
includes:
- vendor/phpstan/phpstan-phpunit/extension.neon
- vendor/phpstan/phpstan-phpunit/rules.neon
4 changes: 2 additions & 2 deletions tests/Doctrine/Tests/DBAL/Cache/ArrayStatementTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,8 @@ public function testFetchColumn() : void

$statement = $this->createTestArrayStatement();

self::assertSame(true, $statement->fetchColumn(1));
self::assertSame(false, $statement->fetchColumn(1));
self::assertTrue($statement->fetchColumn(1));
self::assertFalse($statement->fetchColumn(1));
}

private function createTestArrayStatement() : ArrayStatement
Expand Down
2 changes: 1 addition & 1 deletion tests/Doctrine/Tests/DBAL/Cache/QueryCacheProfileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class QueryCacheProfileTest extends DbalTestCase
/** @var int[] */
private $params = [666];

/** @var string[] */
/** @var int[] */
private $types = [ParameterType::INTEGER];

/** @var string[] */
Expand Down
3 changes: 3 additions & 0 deletions tests/Doctrine/Tests/DBAL/Driver/AbstractDriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,9 @@ abstract protected function createPlatform() : AbstractPlatform;
*/
abstract protected function createSchemaManager(Connection $connection) : AbstractSchemaManager;

/**
* @return Connection|MockObject
*/
protected function getConnectionMock() : Connection
{
return $this->getMockBuilder(Connection::class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public function testRestoresErrorHandlerOnException() : void
// Do nothing
}

self::assertSame($handler, set_error_handler($default_handler), 'Restoring error handler failed.');
self::assertSame($handler, set_error_handler($handler), 'Restoring error handler failed.');
restore_error_handler();
restore_error_handler();
}
Expand Down
3 changes: 1 addition & 2 deletions tests/Doctrine/Tests/DBAL/Driver/PDOExceptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use Doctrine\DBAL\Driver\PDOException;
use Doctrine\Tests\DbalTestCase;
use PHPUnit\Framework\MockObject\MockObject;

/**
* @requires extension pdo
Expand All @@ -29,7 +28,7 @@ class PDOExceptionTest extends DbalTestCase
/**
* The wrapped PDO exception mock.
*
* @var \PDOException|MockObject
* @var \PDOException
*/
private $wrappedException;

Expand Down
20 changes: 12 additions & 8 deletions tests/Doctrine/Tests/DBAL/Driver/StatementIteratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
use Doctrine\Tests\DbalTestCase;
use IteratorAggregate;
use PHPUnit\Framework\MockObject\MockObject;
use Traversable;
use function extension_loaded;

class StatementIteratorTest extends DbalTestCase
Expand Down Expand Up @@ -51,10 +50,12 @@ public function testIteratorIterationCallsFetchOncePerStep() : void
*/
public function testStatementIterationCallsFetchOncePerStep(string $class) : void
{
/** @var iterable<int, mixed>|MockObject $stmt */
$stmt = $this->createPartialMock($class, ['fetch']);

$calls = 0;
$this->configureStatement($stmt, $calls);

$this->assertIterationCallsFetchOncePerStep($stmt, $calls);
}

Expand All @@ -73,13 +74,6 @@ private function configureStatement(MockObject $stmt, int &$calls) : void
});
}

private function assertIterationCallsFetchOncePerStep(Traversable $iterator, int &$calls) : void
{
foreach ($iterator as $i => $_) {
$this->assertEquals($i + 1, $calls);
}
}

/**
* @return string[][]
*/
Expand All @@ -102,4 +96,14 @@ public static function statementProvider() : iterable
yield [SQLSrvStatement::class];
}
}

/**
* @param iterable<int, mixed> $iterator
*/
private function assertIterationCallsFetchOncePerStep(iterable $iterator, int &$calls) : void
{
foreach ($iterator as $i => $_) {
$this->assertEquals($i + 1, $calls);
}
}
}
2 changes: 1 addition & 1 deletion tests/Doctrine/Tests/DBAL/DriverManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ public function testDatabaseUrl($url, $expected) : void
}

/**
* @return array<string, array<int, mixed>>
* @return array<string, mixed>
*/
public function databaseUrls() : iterable
{
Expand Down
6 changes: 6 additions & 0 deletions tests/Doctrine/Tests/DBAL/Functional/DataAccessTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,11 @@
use function array_change_key_case;
use function array_filter;
use function array_keys;
use function assert;
use function count;
use function date;
use function implode;
use function is_array;
use function is_numeric;
use function json_encode;
use function property_exists;
Expand Down Expand Up @@ -521,6 +523,8 @@ public function testTrimExpression(string $value, int $position, ?string $char,
'FROM fetch_table';

$row = $this->connection->fetchAssoc($sql);
self::assertIsArray($row);

$row = array_change_key_case($row, CASE_LOWER);

self::assertEquals($expectedResult, $row['trimmed']);
Expand Down Expand Up @@ -931,6 +935,8 @@ public function testLocateExpression() : void
$sql .= 'FROM fetch_table';

$row = $this->connection->fetchAssoc($sql);
assert(is_array($row));

$row = array_change_key_case($row, CASE_LOWER);

self::assertEquals(2, $row['locate1']);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
namespace Doctrine\Tests\DBAL\Functional\Driver\IBMDB2;

use Doctrine\DBAL\Driver\IBMDB2\DB2Driver;
use Doctrine\DBAL\Statement;
use Doctrine\Tests\DbalFunctionalTestCase;
use PHPUnit\Framework\Error\Notice;
use function assert;
use function extension_loaded;

class DB2StatementTest extends DbalFunctionalTestCase
Expand All @@ -29,6 +31,7 @@ protected function setUp() : void
public function testExecutionErrorsAreNotSuppressed() : void
{
$stmt = $this->connection->prepare('SELECT * FROM SYSIBM.SYSDUMMY1 WHERE \'foo\' = ?');
assert($stmt instanceof Statement);

// unwrap the statement to prevent the wrapper from handling the PHPUnit-originated exception
$wrappedStmt = $stmt->getWrappedStatement();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Doctrine\DBAL\Driver\OCI8\OCI8Connection;
use Doctrine\DBAL\Schema\Table;
use Doctrine\Tests\DbalFunctionalTestCase;
use function assert;
use function extension_loaded;

class OCI8ConnectionTest extends DbalFunctionalTestCase
Expand All @@ -27,7 +28,10 @@ protected function setUp() : void
$this->markTestSkipped('oci8 only test.');
}

$this->driverConnection = $this->connection->getWrappedConnection();
$wrappedConnection = $this->connection->getWrappedConnection();
assert($wrappedConnection instanceof OCI8Connection);

$this->driverConnection = $wrappedConnection;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ protected function setUp() : void
{
parent::setUp();

$this->driverConnection = $this->connection->getWrappedConnection();
$wrappedConnection = $this->connection->getWrappedConnection();

if ($this->driverConnection instanceof PDOConnection) {
return;
if (! $wrappedConnection instanceof PDOConnection) {
$this->markTestSkipped('PDO connection only test.');
}

$this->markTestSkipped('PDO connection only test.');
$this->driverConnection = $wrappedConnection;
}

protected function tearDown() : void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function testNonPersistentStatement() : void
self::assertTrue($conn->isConnected(), 'No SQLAnywhere-Connection established');

$prepStmt = $conn->prepare('SELECT 1');
self::assertTrue($prepStmt->execute(), ' Statement non-persistent failed');
$prepStmt->execute();
}

public function testPersistentStatement() : void
Expand All @@ -53,6 +53,6 @@ public function testPersistentStatement() : void
self::assertTrue($conn->isConnected(), 'No SQLAnywhere-Connection established');

$prepStmt = $conn->prepare('SELECT 1');
self::assertTrue($prepStmt->execute(), ' Statement persistent failed');
$prepStmt->execute();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Throwable;
use const CASE_LOWER;
use function array_change_key_case;
use function assert;
use function sprintf;
use function strlen;
use function strtolower;
Expand Down Expand Up @@ -49,7 +50,10 @@ protected function setUp() : void

private function createMasterSlaveConnection(bool $keepSlave = false) : MasterSlaveConnection
{
return DriverManager::getConnection($this->createMasterSlaveConnectionParams($keepSlave));
$connection = DriverManager::getConnection($this->createMasterSlaveConnectionParams($keepSlave));
assert($connection instanceof MasterSlaveConnection);

return $connection;
}

/**
Expand Down
Loading

0 comments on commit c02c3ea

Please sign in to comment.