Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Myks92 committed Mar 12, 2024
1 parent f172408 commit 666fbe4
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 20 deletions.
41 changes: 41 additions & 0 deletions tests/Tools/Console/Command/DiffCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,13 @@
use Doctrine\Migrations\Version\Version;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Helper\HelperSet;
use Symfony\Component\Console\Helper\QuestionHelper;
use Symfony\Component\Console\Tester\CommandTester;

use function array_map;
use function explode;
use function sprintf;
use function sys_get_temp_dir;
use function trim;

Expand Down Expand Up @@ -139,6 +142,44 @@ public function testExecutedUnavailableMigrationsCancel(): void
self::assertSame(3, $statusCode);
}

/** @return array<string, int|null, array<string[]>> */
public static function getNamespaceSelected(): array

Check failure on line 146 in tests/Tools/Console/Command/DiffCommandTest.php

View workflow job for this annotation

GitHub Actions / Static Analysis with PHPStan (8.3)

Method Doctrine\Migrations\Tests\Tools\Console\Command\DiffCommandTest::getNamespaceSelected() return type has no value type specified in iterable type array.

Check failure on line 146 in tests/Tools/Console/Command/DiffCommandTest.php

View workflow job for this annotation

GitHub Actions / Static Analysis with PHPStan (8.3)

PHPDoc tag @return contains unresolvable type.
{
return [
'no' => [null, 'FooNs'],
'first' => [0, 'FooNs'],
'two' => [1, 'FooNs2'],
];
}

/** @dataProvider getNamespaceSelected */
public function testExecuteWithMultipleDirectories(int|null $input, string $namespace): void
{
$this->migrationStatusCalculator
->method('getNewMigrations')
->willReturn(new AvailableMigrationsList([]));

$this->migrationStatusCalculator
->method('getExecutedUnavailableMigrations')
->willReturn(new ExecutedMigrationsList([]));

$this->configuration->addMigrationsDirectory('FooNs2', sys_get_temp_dir());

$this->diffCommand->setHelperSet(new HelperSet(['question' => new QuestionHelper()]));

$this->migrationDiffGenerator->expects(self::once())->method('generate');

$this->diffCommandTester->setInputs([$input]);
$this->diffCommandTester->execute([]);

$output = $this->diffCommandTester->getDisplay(true);

self::assertStringContainsString('Please choose a namespace (defaults to the first one)', $output);
self::assertStringContainsString('[0] FooNs', $output);
self::assertStringContainsString('[1] FooNs2', $output);
self::assertStringContainsString(sprintf('You have selected the "%s" namespace', $namespace), $output);
}

protected function setUp(): void
{
$this->migrationDiffGenerator = $this->createMock(DiffGenerator::class);
Expand Down
57 changes: 47 additions & 10 deletions tests/Tools/Console/Command/DumpSchemaCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,13 @@
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use RuntimeException;
use Symfony\Component\Console\Helper\HelperSet;
use Symfony\Component\Console\Helper\QuestionHelper;
use Symfony\Component\Console\Tester\CommandTester;

use function array_map;
use function explode;
use function sprintf;
use function sys_get_temp_dir;
use function trim;

Expand Down Expand Up @@ -62,6 +65,16 @@ public function testExecute(): void
->method('getMigrations')
->willReturn(new AvailableMigrationsSet([]));

$classNameGenerator = $this->createMock(ClassNameGenerator::class);
$classNameGenerator->expects(self::any())
->method('generateClassName')
->with('FooNs')
->willReturn('FooNs\\Version1234');

$this->dependencyFactory->expects(self::any())
->method('getClassNameGenerator')
->willReturn($classNameGenerator);

$this->schemaDumper->expects(self::once())
->method('dump')
->with('FooNs\\Version1234', ['/foo/'], true, 80);
Expand All @@ -88,6 +101,40 @@ public function testExecute(): void
);
}

/** @return array<string, int|null, array<string[]>> */
public static function getNamespaceSelected(): array

Check failure on line 105 in tests/Tools/Console/Command/DumpSchemaCommandTest.php

View workflow job for this annotation

GitHub Actions / Static Analysis with PHPStan (8.3)

Method Doctrine\Migrations\Tests\Tools\Console\Command\DumpSchemaCommandTest::getNamespaceSelected() return type has no value type specified in iterable type array.

Check failure on line 105 in tests/Tools/Console/Command/DumpSchemaCommandTest.php

View workflow job for this annotation

GitHub Actions / Static Analysis with PHPStan (8.3)

PHPDoc tag @return contains unresolvable type.
{
return [
'no' => [null, 'FooNs'],
'first' => [0, 'FooNs'],
'two' => [1, 'FooNs2'],
];
}

/** @dataProvider getNamespaceSelected */
public function testExecuteWithMultipleDirectories(int|null $input, string $namespace): void
{
$this->migrationRepository->expects(self::once())
->method('getMigrations')
->willReturn(new AvailableMigrationsSet([]));

$this->configuration->addMigrationsDirectory('FooNs2', sys_get_temp_dir());

$this->dumpSchemaCommand->setHelperSet(new HelperSet(['question' => new QuestionHelper()]));

$this->schemaDumper->expects(self::once())->method('dump');

$this->dumpSchemaCommandTester->setInputs([$input]);
$this->dumpSchemaCommandTester->execute([]);

$output = $this->dumpSchemaCommandTester->getDisplay(true);

self::assertStringContainsString('Please choose a namespace (defaults to the first one)', $output);
self::assertStringContainsString('[0] FooNs', $output);
self::assertStringContainsString('[1] FooNs2', $output);
self::assertStringContainsString(sprintf('You have selected the "%s" namespace', $namespace), $output);
}

protected function setUp(): void
{
$this->configuration = new Configuration();
Expand All @@ -97,16 +144,6 @@ protected function setUp(): void
$this->migrationRepository = $this->createMock(FilesystemMigrationsRepository::class);
$this->schemaDumper = $this->createMock(SchemaDumper::class);

$classNameGenerator = $this->createMock(ClassNameGenerator::class);
$classNameGenerator->expects(self::any())
->method('generateClassName')
->with('FooNs')
->willReturn('FooNs\\Version1234');

$this->dependencyFactory->expects(self::any())
->method('getClassNameGenerator')
->willReturn($classNameGenerator);

$this->dependencyFactory->expects(self::any())
->method('getSchemaDumper')
->willReturn($this->schemaDumper);
Expand Down
51 changes: 41 additions & 10 deletions tests/Tools/Console/Command/GenerateCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,13 @@
use Doctrine\Migrations\Tools\Console\Command\GenerateCommand;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Helper\HelperSet;
use Symfony\Component\Console\Helper\QuestionHelper;
use Symfony\Component\Console\Tester\CommandTester;

use function array_map;
use function explode;
use function sprintf;
use function sys_get_temp_dir;
use function trim;

Expand All @@ -39,6 +42,16 @@ public function testExecute(): void
->with('FooNs\\Version1234')
->willReturn('/path/to/migration.php');

$classNameGenerator = $this->createMock(ClassNameGenerator::class);
$classNameGenerator->expects(self::once())
->method('generateClassName')
->with('FooNs')
->willReturn('FooNs\\Version1234');

$this->dependencyFactory->expects(self::once())
->method('getClassNameGenerator')
->willReturn($classNameGenerator);

$this->generateCommandTest->execute([]);
$output = $this->generateCommandTest->getDisplay(true);

Expand All @@ -51,6 +64,34 @@ public function testExecute(): void
], array_map(trim(...), explode("\n", trim($output))));
}

/** @return array<string, int|null, array<string[]>> */
public static function getNamespaceSelected(): array

Check failure on line 68 in tests/Tools/Console/Command/GenerateCommandTest.php

View workflow job for this annotation

GitHub Actions / Static Analysis with PHPStan (8.3)

Method Doctrine\Migrations\Tests\Tools\Console\Command\GenerateCommandTest::getNamespaceSelected() return type has no value type specified in iterable type array.

Check failure on line 68 in tests/Tools/Console/Command/GenerateCommandTest.php

View workflow job for this annotation

GitHub Actions / Static Analysis with PHPStan (8.3)

PHPDoc tag @return contains unresolvable type.
{
return [
'no' => [null, 'FooNs'],
'first' => [0, 'FooNs'],
'two' => [1, 'FooNs2'],
];
}

/** @dataProvider getNamespaceSelected */
public function testExecuteWithMultipleDirectories(int|null $input, string $namespace): void
{
$this->configuration->addMigrationsDirectory('FooNs2', sys_get_temp_dir());

$this->generateCommand->setHelperSet(new HelperSet(['question' => new QuestionHelper()]));

$this->generateCommandTest->setInputs([$input]);
$this->generateCommandTest->execute([]);

$output = $this->generateCommandTest->getDisplay(true);

self::assertStringContainsString('Please choose a namespace (defaults to the first one)', $output);
self::assertStringContainsString('[0] FooNs', $output);
self::assertStringContainsString('[1] FooNs2', $output);
self::assertStringContainsString(sprintf('You have selected the "%s" namespace', $namespace), $output);
}

protected function setUp(): void
{
$this->configuration = new Configuration();
Expand All @@ -59,16 +100,6 @@ protected function setUp(): void
$this->dependencyFactory = $this->createMock(DependencyFactory::class);
$this->migrationGenerator = $this->createMock(Generator::class);

$classNameGenerator = $this->createMock(ClassNameGenerator::class);
$classNameGenerator->expects(self::once())
->method('generateClassName')
->with('FooNs')
->willReturn('FooNs\\Version1234');

$this->dependencyFactory->expects(self::once())
->method('getClassNameGenerator')
->willReturn($classNameGenerator);

$this->dependencyFactory->expects(self::any())
->method('getConfiguration')
->willReturn($this->configuration);
Expand Down

0 comments on commit 666fbe4

Please sign in to comment.