Skip to content

Commit

Permalink
Merge pull request #765 from doctrine/phpstan-tests
Browse files Browse the repository at this point in the history
Enable PHPStan in tests
  • Loading branch information
jwage authored Dec 5, 2018
2 parents 32cca71 + 8327d63 commit 5650e82
Show file tree
Hide file tree
Showing 64 changed files with 720 additions and 621 deletions.
12 changes: 7 additions & 5 deletions lib/Doctrine/Migrations/MigrationRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,12 @@
use function array_map;
use function array_search;
use function array_unshift;
use function assert;
use function class_exists;
use function count;
use function end;
use function get_class;
use function implode;
use function is_array;
use function is_int;
use function ksort;
use function sprintf;
use function substr;
Expand Down Expand Up @@ -345,14 +343,18 @@ public function getRelativeVersion(string $version, int $delta) : ?string

$offset = array_search($version, $versions, true);

assert($offset === false || is_int($offset));
if ($offset === false) {
return null;
}

$relativeVersion = ((int) $offset) + $delta;

if ($offset === false || ! isset($versions[$offset + $delta])) {
if (! isset($versions[$relativeVersion])) {
// Unknown version or delta out of bounds.
return null;
}

return $versions[$offset + $delta];
return $versions[$relativeVersion];
}

public function getDeltaVersion(string $delta) : ?string
Expand Down
11 changes: 10 additions & 1 deletion lib/Doctrine/Migrations/OutputWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,14 @@ class OutputWriter
/** @var callable */
private $callback;

/** @var string */
private $lastMessage = '';

public function __construct(?callable $callback = null)
{
if ($callback === null) {
$callback = static function ($message) : void {
$callback = function (string $message) : void {
$this->lastMessage = $message;
};
}

Expand All @@ -31,4 +35,9 @@ public function write(string $message) : void
{
($this->callback)($message);
}

public function getLastMessage() : string
{
return $this->lastMessage;
}
}
2 changes: 1 addition & 1 deletion lib/Doctrine/Migrations/Tools/BytesFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ public static function formatBytes(int $size, int $precision = 2) : string
$base = log($size, 1024);
$suffixes = ['', 'K', 'M', 'G', 'T'];

return round(pow(1024, $base - floor($base)), $precision) . $suffixes[floor($base)];
return round(pow(1024, $base - floor($base)), $precision) . $suffixes[(int) floor($base)];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,4 @@ private function createDirIfNotExists(string $dir) : void

mkdir($dir, 0755, true);
}

public function getName() : string
{
return 'MigrationDirectory';
}
}
14 changes: 12 additions & 2 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,23 @@ parameters:
level: 7
paths:
- %currentWorkingDirectory%/lib
- %currentWorkingDirectory%/tests
autoload_directories:
- %currentWorkingDirectory%/tests/Doctrine/Migrations/Tests/Finder/_features
- %currentWorkingDirectory%/tests/Doctrine/Migrations/Tests/Finder/_files
- %currentWorkingDirectory%/tests/Doctrine/Migrations/Tests/Finder/_regression
- %currentWorkingDirectory%/tests/Doctrine/Migrations/Tests/Functional/_files
autoload_files:
- %currentWorkingDirectory%/tests/Doctrine/Migrations/Tests/Configuration/ConfigurationTestSource/Migrations/Version123.php
- %currentWorkingDirectory%/tests/Doctrine/Migrations/Tests/realpath.php
ignoreErrors:
# Ignore proxy manager magic
- '~ProxyManager\\Proxy\\VirtualProxyInterface~'
- '~Parameter #1 \$files of method Doctrine\\Migrations\\Finder\\Finder::loadMigrationClasses\(\) expects array<string>, array<int, string\|false> given~'
#'
- '~^Parameter #1 \$files of method Doctrine\\Migrations\\Finder\\Finder::loadMigrationClasses\(\) expects array<string>, array<int, string\|false> given\.\z~'
- '~^Class Doctrine\\Migrations\\Tests\\DoesNotExistAtAll not found\.\z~'

includes:
- vendor/phpstan/phpstan-deprecation-rules/rules.neon
- vendor/phpstan/phpstan-phpunit/extension.neon
- vendor/phpstan/phpstan-phpunit/rules.neon
- vendor/phpstan/phpstan-strict-rules/rules.neon
7 changes: 6 additions & 1 deletion tests/Doctrine/Migrations/Tests/BoxPharCompileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use PHPUnit\Framework\TestCase;
use Symfony\Component\Process\Process;
use function assert;
use function file_exists;
use function realpath;
use function sprintf;
Expand All @@ -20,18 +21,22 @@ public function testCompile() : void
$boxPharPath = __DIR__ . '/../../../../box.phar';

if (! file_exists($boxPharPath)) {
$this->markTestSkipped('Download box with the ./download-box.sh shell script.');
self::markTestSkipped('Download box with the ./download-box.sh shell script.');
}

$boxPharPath = realpath($boxPharPath);

assert($boxPharPath !== false);

$compilePharCommand = sprintf('php %s compile -vvv', $boxPharPath);

$process = new Process($compilePharCommand);
$process->run();

$doctrinePharPath = realpath(__DIR__ . '/../../../../build/doctrine-migrations.phar');

assert($doctrinePharPath !== false);

self::assertTrue($process->isSuccessful());
self::assertTrue(file_exists($doctrinePharPath));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,19 +156,26 @@ public function testConfigurationFileNotExists() : void

public function testLoadMigrationsList() : void
{
self::assertInstanceOf(AbstractFileConfiguration::class, $this->loadConfiguration('migrations_list'));
self::assertInstanceOf(AbstractFileConfiguration::class, $this->loadConfiguration('migrations_list2'));
$configuration1 = $this->loadConfiguration('migrations_list');

self::assertContains('migrations_list', $configuration1->getFile());

$configuration2 = $this->loadConfiguration('migrations_list2');

self::assertContains('migrations_list2', $configuration2->getFile());
}

/**
* @dataProvider getConfigWithKeysInVariousOrder
*/
public function testThatTheOrderOfConfigKeysDoesNotMatter(string $file) : void
{
self::assertInstanceOf(AbstractFileConfiguration::class, $this->loadConfiguration($file));
$configuration = $this->loadConfiguration($file);

self::assertContains($file, $configuration->getFile());
}

/** @return string[] */
/** @return string[][] */
public function getConfigWithKeysInVariousOrder() : array
{
return [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Doctrine\Migrations\Configuration\AbstractFileConfiguration;
use Doctrine\Migrations\Configuration\Exception\InvalidConfigurationKey;
use PHPUnit\Framework\TestCase;
use function assert;
use function basename;
use function chdir;
use function getcwd;
Expand All @@ -17,20 +18,22 @@ class AbstractFileConfigurationTest extends TestCase
/** @var Connection */
private $connection;

/** @var AbstractFileConfiguration */
/** @var TestAbstractFileConfiguration */
private $fileConfiguration;

public function testLoadChecksCurrentWorkingDirectory() : void
{
$cwd = getcwd();

assert($cwd !== false);

chdir(__DIR__);

$file = basename(__FILE__);

$this->fileConfiguration->load($file);

$this->assertSame(__DIR__ . '/' . $file, $this->fileConfiguration->getFile());
self::assertSame(__DIR__ . '/' . $file, $this->fileConfiguration->getFile());

chdir($cwd);
}
Expand All @@ -52,50 +55,50 @@ public function testSetConfiguration() : void
'setAllOrNothing',
]);

$fileConfiguration->expects($this->once())
$fileConfiguration->expects(self::once())
->method('setMigrationsNamespace')
->with('Doctrine');

$fileConfiguration->expects($this->once())
$fileConfiguration->expects(self::once())
->method('setMigrationsTableName')
->with('migration_version');

$fileConfiguration->expects($this->once())
$fileConfiguration->expects(self::once())
->method('setMigrationsColumnName')
->with('version_number');

$fileConfiguration->expects($this->once())
$fileConfiguration->expects(self::once())
->method('setMigrationsColumnLength')
->with(200);

$fileConfiguration->expects($this->once())
$fileConfiguration->expects(self::once())
->method('setMigrationsExecutedAtColumnName')
->with('executed_at');

$fileConfiguration->expects($this->once())
$fileConfiguration->expects(self::once())
->method('setMigrationsAreOrganizedByYearAndMonth');

$fileConfiguration->expects($this->once())
$fileConfiguration->expects(self::once())
->method('setName')
->with('Migrations Test');

$fileConfiguration->expects($this->once())
$fileConfiguration->expects(self::once())
->method('setMigrationsDirectory')
->with('migrations_directory');

$fileConfiguration->expects($this->once())
$fileConfiguration->expects(self::once())
->method('registerMigrationsFromDirectory')
->with('migrations_directory');

$fileConfiguration->expects($this->once())
$fileConfiguration->expects(self::once())
->method('registerMigration')
->with('001', 'Test');

$fileConfiguration->expects($this->once())
$fileConfiguration->expects(self::once())
->method('setCustomTemplate')
->with('custom_template');

$fileConfiguration->expects($this->once())
$fileConfiguration->expects(self::once())
->method('setAllOrNothing')
->with(true);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,17 @@ public function testConstructorSetsOutputWriter() : void

public function testOutputWriterIsCreatedIfNotInjected() : void
{
$configuration = new Configuration($this->getConnectionMock());
$dependencyFactory = $this->createMock(DependencyFactory::class);

$outputWriter = $this->getOutputWriterMock();

$dependencyFactory->expects(self::once())
->method('getOutputWriter')
->willReturn($outputWriter);

self::assertInstanceOf(OutputWriter::class, $configuration->getOutputWriter());
$configuration = new Configuration($this->getConnectionMock(), null, null, null, $dependencyFactory);

self::assertSame($outputWriter, $configuration->getOutputWriter());
}

public function testOutputWriterCanBeSet() : void
Expand Down Expand Up @@ -85,7 +93,7 @@ public function testVersionsTryToGetLoadedIfNotAlreadyLoadedWhenAccessingMethodT
$configuration->setMigrationsNamespace(str_replace('\Version1Test', '', Version1Test::class));
$configuration->setMigrationsDirectory(__DIR__ . '/../Stub/Configuration/AutoloadVersions');

$result = call_user_func_array([$configuration, $method], $args);
$result = $configuration->$method(...$args);

if ($method === 'getMigrationsToExecute') {
$result = array_keys($result);
Expand Down Expand Up @@ -122,7 +130,10 @@ public function testVersionsTryToGetLoadedIfNotAlreadyLoadedWhenAccessingMethodT
);
$migrator->migrate('3Test');

$result = call_user_func_array([$configuration, $method], $args);
/** @var callable $callable */
$callable = [$configuration, $method];

$result = call_user_func_array($callable, $args);

if ($method === 'getMigrationsToExecute') {
$result = array_keys($result);
Expand Down Expand Up @@ -167,7 +178,7 @@ public function testMasterSlaveConnectionAlwaysConnectsToMaster() : void
{
$connection = $this->createMock(MasterSlaveConnection::class);

$connection->expects($this->once())
$connection->expects(self::once())
->method('connect')
->with('master')
->willReturn(true);
Expand Down Expand Up @@ -258,7 +269,7 @@ public function testGetQueryWriterCreatesAnInstanceIfItWasNotConfigured() : void

$schemaManager = $this->createMock(AbstractSchemaManager::class);

$conn->expects($this->any())
$conn->expects(self::any())
->method('getSchemaManager')
->willReturn($schemaManager);

Expand Down Expand Up @@ -294,11 +305,11 @@ public function testGetVersionData() : void
'executed_at' => '2018-05-16 11:14:40',
];

$dependencyFactory->expects($this->once())
$dependencyFactory->expects(self::once())
->method('getMigrationRepository')
->willReturn($migrationRepository);

$migrationRepository->expects($this->once())
$migrationRepository->expects(self::once())
->method('getVersionData')
->with($version)
->willReturn($versionData);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,17 @@
use Doctrine\DBAL\Connection;
use Doctrine\Migrations\Configuration\Configuration;
use Doctrine\Migrations\Configuration\Connection\Loader\ConnectionConfigurationLoader;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;

final class ConnectionConfigurationLoaderTest extends TestCase
{
/** @var Configuration */
/** @var Configuration|MockObject */
private $configuration;

/** @var ConnectionConfigurationLoader */
private $connectionConfigurationLoader;

public function testChosenReturnsNull() : void
{
$connectionConfigurationLoader = new ConnectionConfigurationLoader();
Expand All @@ -25,7 +29,7 @@ public function testChosenReturnsConfigurationConnection() : void
{
$connection = $this->createMock(Connection::class);

$this->configuration->expects($this->once())
$this->configuration->expects(self::once())
->method('getConnection')
->willReturn($connection);

Expand Down
Loading

0 comments on commit 5650e82

Please sign in to comment.