From 4a3016a082ee7bb289d20891ce7c117b8d245d3b Mon Sep 17 00:00:00 2001 From: Jakub Zalas Date: Wed, 6 May 2020 13:13:19 +0100 Subject: [PATCH 1/2] Allow PHPUnit 9 --- composer.json | 5 ++-- phpunit.xml.dist | 6 ++--- tests/Cli/ApplicationTest.php | 3 +++ tests/Cli/Command/InstallCommandTest.php | 3 +++ tests/Cli/Command/ListCommandTest.php | 7 ++++-- tests/Cli/Command/TestCommandTest.php | 3 +++ tests/Cli/Runner/DryRunnerTest.php | 3 +++ tests/Cli/ServiceContainer/LazyRunnerTest.php | 3 +++ .../ServiceContainer/RunnerFactoryTest.php | 3 +++ tests/Cli/ServiceContainerTest.php | 3 +++ .../Factory/BoxBuildCommandFactoryTest.php | 2 +- .../ComposerInstallCommandFactoryTest.php | 2 +- tests/Json/JsonToolsTest.php | 8 +++--- tests/Runner/ParametrisedRunnerTest.php | 3 +++ tests/Tool/Command/BoxBuildCommandTest.php | 18 ++++++------- .../Command/ComposerBinPluginCommandTest.php | 8 +++--- .../ComposerBinPluginLinkCommandTest.php | 2 +- .../ComposerGlobalInstallCommandTest.php | 2 +- .../ComposerGlobalMultiInstallCommandTest.php | 2 +- .../Command/ComposerInstallCommandTest.php | 8 +++--- .../Tool/Command/FileDownloadCommandTest.php | 2 +- tests/Tool/Command/MultiStepCommandTest.php | 3 +++ .../OptimisedComposerBinPluginCommandTest.php | 18 ++++++------- .../Tool/Command/PharDownloadCommandTest.php | 2 +- tests/Tool/Command/TestCommandTest.php | 2 +- tests/Tool/FilterTest.php | 3 +++ tests/Tool/ToolTest.php | 3 +++ tests/UseCase/InstallToolsTest.php | 25 +++++++++++-------- tests/UseCase/ListToolsTest.php | 3 +++ tests/UseCase/TestToolsTest.php | 3 +++ 30 files changed, 102 insertions(+), 56 deletions(-) diff --git a/composer.json b/composer.json index 5f4ec534..b338c29c 100644 --- a/composer.json +++ b/composer.json @@ -8,8 +8,9 @@ "psr/container": "^1.0" }, "require-dev": { - "phpunit/phpunit": "^8.4", - "zalas/phpunit-globals": "^1.1" + "phpunit/phpunit": "^8.4 || ^9.1", + "zalas/phpunit-globals": "^2.0", + "phpspec/prophecy-phpunit": "^2.0" }, "autoload": { "psr-4": { diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 53489ad7..bb6d15e6 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -25,9 +25,9 @@ - - - + + + diff --git a/tests/Cli/ApplicationTest.php b/tests/Cli/ApplicationTest.php index b0812102..ef0b7228 100644 --- a/tests/Cli/ApplicationTest.php +++ b/tests/Cli/ApplicationTest.php @@ -4,6 +4,7 @@ use PHPUnit\Framework\TestCase; use Prophecy\Argument; +use Prophecy\PhpUnit\ProphecyTrait; use Prophecy\Prophecy\ObjectProphecy; use Symfony\Component\Console\Application as CliApplication; use Symfony\Component\Console\Input\ArrayInput; @@ -16,6 +17,8 @@ class ApplicationTest extends TestCase { + use ProphecyTrait; + private const VERSION = 'test'; /** diff --git a/tests/Cli/Command/InstallCommandTest.php b/tests/Cli/Command/InstallCommandTest.php index c7e76069..20c8b20a 100644 --- a/tests/Cli/Command/InstallCommandTest.php +++ b/tests/Cli/Command/InstallCommandTest.php @@ -3,6 +3,7 @@ namespace Zalas\Toolbox\Tests\Cli\Command; use Prophecy\Argument; +use Prophecy\PhpUnit\ProphecyTrait; use Prophecy\Prophecy\ObjectProphecy; use Zalas\Toolbox\Cli\Command\InstallCommand; use Zalas\Toolbox\Runner\Runner; @@ -12,6 +13,8 @@ class InstallCommandTest extends ToolboxCommandTestCase { + use ProphecyTrait; + protected const CLI_COMMAND_NAME = InstallCommand::NAME; /** diff --git a/tests/Cli/Command/ListCommandTest.php b/tests/Cli/Command/ListCommandTest.php index 057cbe8d..a45be286 100644 --- a/tests/Cli/Command/ListCommandTest.php +++ b/tests/Cli/Command/ListCommandTest.php @@ -3,6 +3,7 @@ namespace Zalas\Toolbox\Tests\Cli\Command; use Prophecy\Argument; +use Prophecy\PhpUnit\ProphecyTrait; use Prophecy\Prophecy\ObjectProphecy; use Zalas\Toolbox\Cli\Command\ListCommand; use Zalas\Toolbox\Tool\Collection; @@ -12,6 +13,8 @@ class ListCommandTest extends ToolboxCommandTestCase { + use ProphecyTrait; + protected const CLI_COMMAND_NAME = ListCommand::NAME; /** @@ -35,8 +38,8 @@ public function test_it_runs_the_list_tools_use_case() $tester = $this->executeCliCommand(); $this->assertSame(0, $tester->getStatusCode()); - $this->assertRegExp('#Available tools#i', $tester->getDisplay()); - $this->assertRegExp('#Behat.*?Tests business expectations.*?http://behat.org#smi', $tester->getDisplay()); + $this->assertMatchesRegularExpression('#Available tools#i', $tester->getDisplay()); + $this->assertMatchesRegularExpression('#Behat.*?Tests business expectations.*?http://behat.org#smi', $tester->getDisplay()); } public function test_it_filters_by_tags() diff --git a/tests/Cli/Command/TestCommandTest.php b/tests/Cli/Command/TestCommandTest.php index 31dbec3a..43ee76fc 100644 --- a/tests/Cli/Command/TestCommandTest.php +++ b/tests/Cli/Command/TestCommandTest.php @@ -3,6 +3,7 @@ namespace Zalas\Toolbox\Tests\Cli\Command; use Prophecy\Argument; +use Prophecy\PhpUnit\ProphecyTrait; use Prophecy\Prophecy\ObjectProphecy; use Zalas\Toolbox\Cli\Command\TestCommand; use Zalas\Toolbox\Runner\Runner; @@ -12,6 +13,8 @@ class TestCommandTest extends ToolboxCommandTestCase { + use ProphecyTrait; + protected const CLI_COMMAND_NAME = TestCommand::NAME; /** diff --git a/tests/Cli/Runner/DryRunnerTest.php b/tests/Cli/Runner/DryRunnerTest.php index c054b5cd..a0f20a4f 100644 --- a/tests/Cli/Runner/DryRunnerTest.php +++ b/tests/Cli/Runner/DryRunnerTest.php @@ -3,6 +3,7 @@ namespace Zalas\Toolbox\Tests\Cli\Runner; use PHPUnit\Framework\TestCase; +use Prophecy\PhpUnit\ProphecyTrait; use Prophecy\Prophecy\ObjectProphecy; use Symfony\Component\Console\Output\OutputInterface; use Zalas\Toolbox\Cli\Runner\DryRunner; @@ -11,6 +12,8 @@ class DryRunnerTest extends TestCase { + use ProphecyTrait; + /** * @var DryRunner */ diff --git a/tests/Cli/ServiceContainer/LazyRunnerTest.php b/tests/Cli/ServiceContainer/LazyRunnerTest.php index 6c614f29..6c533735 100644 --- a/tests/Cli/ServiceContainer/LazyRunnerTest.php +++ b/tests/Cli/ServiceContainer/LazyRunnerTest.php @@ -3,6 +3,7 @@ namespace Zalas\Toolbox\Tests\Cli\ServiceContainer; use PHPUnit\Framework\TestCase; +use Prophecy\PhpUnit\ProphecyTrait; use Prophecy\Prophecy\ObjectProphecy; use Zalas\Toolbox\Cli\ServiceContainer\LazyRunner; use Zalas\Toolbox\Cli\ServiceContainer\RunnerFactory; @@ -11,6 +12,8 @@ class LazyRunnerTest extends TestCase { + use ProphecyTrait; + /** * @var LazyRunner */ diff --git a/tests/Cli/ServiceContainer/RunnerFactoryTest.php b/tests/Cli/ServiceContainer/RunnerFactoryTest.php index 631acde3..0d415504 100644 --- a/tests/Cli/ServiceContainer/RunnerFactoryTest.php +++ b/tests/Cli/ServiceContainer/RunnerFactoryTest.php @@ -3,6 +3,7 @@ namespace Zalas\Toolbox\Tests\Cli\ServiceContainer; use PHPUnit\Framework\TestCase; +use Prophecy\PhpUnit\ProphecyTrait; use Prophecy\Prophecy\ObjectProphecy; use Psr\Container\ContainerExceptionInterface; use Psr\Container\ContainerInterface; @@ -16,6 +17,8 @@ class RunnerFactoryTest extends TestCase { + use ProphecyTrait; + /** * @var ContainerInterface|ObjectProphecy */ diff --git a/tests/Cli/ServiceContainerTest.php b/tests/Cli/ServiceContainerTest.php index a0efc745..ff1f8030 100644 --- a/tests/Cli/ServiceContainerTest.php +++ b/tests/Cli/ServiceContainerTest.php @@ -3,6 +3,7 @@ namespace Zalas\Toolbox\Tests\Cli; use PHPUnit\Framework\TestCase; +use Prophecy\PhpUnit\ProphecyTrait; use Psr\Container\ContainerExceptionInterface; use Psr\Container\ContainerInterface; use Psr\Container\NotFoundExceptionInterface; @@ -17,6 +18,8 @@ class ServiceContainerTest extends TestCase { + use ProphecyTrait; + /** * @var ServiceContainer */ diff --git a/tests/Json/Factory/BoxBuildCommandFactoryTest.php b/tests/Json/Factory/BoxBuildCommandFactoryTest.php index 0d9ec861..d80f2bdc 100644 --- a/tests/Json/Factory/BoxBuildCommandFactoryTest.php +++ b/tests/Json/Factory/BoxBuildCommandFactoryTest.php @@ -26,7 +26,7 @@ public function test_it_creates_a_command() ]); $this->assertInstanceOf(BoxBuildCommand::class, $command); - $this->assertRegExp('#git checkout '.self::VERSION.'#', (string) $command); + $this->assertMatchesRegularExpression('#git checkout '.self::VERSION.'#', (string) $command); } public function test_the_version_is_not_required() diff --git a/tests/Json/Factory/ComposerInstallCommandFactoryTest.php b/tests/Json/Factory/ComposerInstallCommandFactoryTest.php index 9d8b4cd2..a9c93742 100644 --- a/tests/Json/Factory/ComposerInstallCommandFactoryTest.php +++ b/tests/Json/Factory/ComposerInstallCommandFactoryTest.php @@ -21,7 +21,7 @@ public function test_it_creates_a_command() ]); $this->assertInstanceOf(ComposerInstallCommand::class, $command); - $this->assertRegExp('#git checkout '.self::VERSION.'#', (string) $command); + $this->assertMatchesRegularExpression('#git checkout '.self::VERSION.'#', (string) $command); } /** diff --git a/tests/Json/JsonToolsTest.php b/tests/Json/JsonToolsTest.php index 528282bb..43d50d8b 100644 --- a/tests/Json/JsonToolsTest.php +++ b/tests/Json/JsonToolsTest.php @@ -19,7 +19,7 @@ public function test_it_is_a_tools_repository() public function test_it_throws_an_exception_if_resource_is_missing() { $this->expectException(InvalidArgumentException::class); - $this->expectExceptionMessageRegExp('/Could not read the file/'); + $this->expectExceptionMessageMatches('/Could not read the file/'); $tools = new JsonTools($this->locator(['/foo/tools.json'])); $tools->all($this->filter()); @@ -28,7 +28,7 @@ public function test_it_throws_an_exception_if_resource_is_missing() public function test_it_throws_an_exception_if_resource_contains_invalid_json() { $this->expectException(RuntimeException::class); - $this->expectExceptionMessageRegExp('/Failed to parse json/'); + $this->expectExceptionMessageMatches('/Failed to parse json/'); $tools = new JsonTools($this->locator([__DIR__.'/../resources/invalid.json'])); $tools->all($this->filter()); @@ -37,7 +37,7 @@ public function test_it_throws_an_exception_if_resource_contains_invalid_json() public function test_it_throws_an_exception_if_tools_are_not_present_in_the_resource() { $this->expectException(RuntimeException::class); - $this->expectExceptionMessageRegExp('/Failed to find any tools/'); + $this->expectExceptionMessageMatches('/Failed to find any tools/'); $tools = new JsonTools($this->locator([__DIR__.'/../resources/no-tools.json'])); $tools->all($this->filter()); @@ -46,7 +46,7 @@ public function test_it_throws_an_exception_if_tools_are_not_present_in_the_reso public function test_it_throws_an_exception_if_tools_is_not_a_collection() { $this->expectException(RuntimeException::class); - $this->expectExceptionMessageRegExp('/Failed to find any tools/'); + $this->expectExceptionMessageMatches('/Failed to find any tools/'); $tools = new JsonTools($this->locator([__DIR__.'/../resources/invalid-tools.json'])); $tools->all($this->filter()); diff --git a/tests/Runner/ParametrisedRunnerTest.php b/tests/Runner/ParametrisedRunnerTest.php index 8b14971c..7d25f36f 100644 --- a/tests/Runner/ParametrisedRunnerTest.php +++ b/tests/Runner/ParametrisedRunnerTest.php @@ -4,6 +4,7 @@ use PHPUnit\Framework\TestCase; use Prophecy\Argument; +use Prophecy\PhpUnit\ProphecyTrait; use Prophecy\Prophecy\ObjectProphecy; use Zalas\Toolbox\Runner\ParametrisedRunner; use Zalas\Toolbox\Runner\Runner; @@ -11,6 +12,8 @@ class ParametrisedRunnerTest extends TestCase { + use ProphecyTrait; + /** * @var ParametrisedRunner */ diff --git a/tests/Tool/Command/BoxBuildCommandTest.php b/tests/Tool/Command/BoxBuildCommandTest.php index f06b8d96..8d99fd85 100644 --- a/tests/Tool/Command/BoxBuildCommandTest.php +++ b/tests/Tool/Command/BoxBuildCommandTest.php @@ -27,7 +27,7 @@ public function test_it_is_a_command() self::TMP_DIR, self::VERSION ); - + $this->assertInstanceOf(Command::class, $command); } @@ -40,12 +40,12 @@ public function test_it_generates_the_installation_command() self::TMP_DIR, self::VERSION ); - - $this->assertRegExp('#git clone '.self::REPOSITORY.'#', (string) $command); - $this->assertRegExp('#cd /tools/behat#', (string) $command); - $this->assertRegExp('#git checkout '.self::VERSION.'#', (string) $command); - $this->assertRegExp('#composer install --no-dev --no-suggest --prefer-dist -n#', (string) $command); - $this->assertRegExp('#box-legacy build#', (string) $command); + + $this->assertMatchesRegularExpression('#git clone '.self::REPOSITORY.'#', (string) $command); + $this->assertMatchesRegularExpression('#cd /tools/behat#', (string) $command); + $this->assertMatchesRegularExpression('#git checkout '.self::VERSION.'#', (string) $command); + $this->assertMatchesRegularExpression('#composer install --no-dev --no-suggest --prefer-dist -n#', (string) $command); + $this->assertMatchesRegularExpression('#box-legacy build#', (string) $command); } public function test_it_tries_to_guess_version_number_if_not_given_one() @@ -57,7 +57,7 @@ public function test_it_tries_to_guess_version_number_if_not_given_one() self::TMP_DIR ); - $this->assertRegExp('#git checkout \$\(git describe --tags .*?\)#', (string) $command); + $this->assertMatchesRegularExpression('#git checkout \$\(git describe --tags .*?\)#', (string) $command); } public function test_it_uses_a_generic_directory_if_name_cannot_be_guessed_from_the_repository() @@ -69,6 +69,6 @@ public function test_it_uses_a_generic_directory_if_name_cannot_be_guessed_from_ self::TMP_DIR ); - $this->assertRegExp('#cd /tools/tmp#', (string) $command); + $this->assertMatchesRegularExpression('#cd /tools/tmp#', (string) $command); } } diff --git a/tests/Tool/Command/ComposerBinPluginCommandTest.php b/tests/Tool/Command/ComposerBinPluginCommandTest.php index 5bfd3944..4566939e 100644 --- a/tests/Tool/Command/ComposerBinPluginCommandTest.php +++ b/tests/Tool/Command/ComposerBinPluginCommandTest.php @@ -34,7 +34,7 @@ public function test_it_is_a_command() public function test_it_generates_the_installation_command() { - $this->assertRegExp('#composer global bin tools require .*? phpstan/phpstan#', (string) $this->command); + $this->assertMatchesRegularExpression('#composer global bin tools require .*? phpstan/phpstan#', (string) $this->command); } public function test_it_exposes_the_package_and_namespace() @@ -51,12 +51,12 @@ public function test_it_optionally_creates_a_symlink() $this->command = new ComposerBinPluginCommand(self::PACKAGE, self::NAMESPACE, $links); $this->assertSame($links, $this->command->links()); - $this->assertRegExp('#composer global bin tools require .*? phpstan/phpstan#', (string) $this->command); - $this->assertRegExp('# && ln -sf.*?phpstan /tools/phpstan#', (string) $this->command); + $this->assertMatchesRegularExpression('#composer global bin tools require .*? phpstan/phpstan#', (string) $this->command); + $this->assertMatchesRegularExpression('# && ln -sf.*?phpstan /tools/phpstan#', (string) $this->command); } public function test_it_does_not_create_a_symlink_if_links_option_was_not_given() { - $this->assertNotRegExp('#ln -s#', (string) $this->command); + $this->assertDoesNotMatchRegularExpression('#ln -s#', (string) $this->command); } } diff --git a/tests/Tool/Command/ComposerBinPluginLinkCommandTest.php b/tests/Tool/Command/ComposerBinPluginLinkCommandTest.php index 1a3ca83e..d360d5c4 100644 --- a/tests/Tool/Command/ComposerBinPluginLinkCommandTest.php +++ b/tests/Tool/Command/ComposerBinPluginLinkCommandTest.php @@ -29,6 +29,6 @@ public function test_it_is_a_command() public function test_it_generates_a_symlink_command() { - $this->assertRegExp('#ln -sf \$\{COMPOSER_HOME:-"~/.composer"\}/vendor-bin/tools/vendor/bin/churn /tools/churn#', (string) $this->command); + $this->assertMatchesRegularExpression('#ln -sf \$\{COMPOSER_HOME:-"~/.composer"\}/vendor-bin/tools/vendor/bin/churn /tools/churn#', (string) $this->command); } } diff --git a/tests/Tool/Command/ComposerGlobalInstallCommandTest.php b/tests/Tool/Command/ComposerGlobalInstallCommandTest.php index 970bbda0..748d17ee 100644 --- a/tests/Tool/Command/ComposerGlobalInstallCommandTest.php +++ b/tests/Tool/Command/ComposerGlobalInstallCommandTest.php @@ -32,6 +32,6 @@ public function test_it_exposes_the_package_name() public function test_it_generates_the_installation_command() { - $this->assertRegExp('#composer global require .*? phan/phan#', (string) $this->command); + $this->assertMatchesRegularExpression('#composer global require .*? phan/phan#', (string) $this->command); } } diff --git a/tests/Tool/Command/ComposerGlobalMultiInstallCommandTest.php b/tests/Tool/Command/ComposerGlobalMultiInstallCommandTest.php index 9b8305ad..0a82a4e8 100644 --- a/tests/Tool/Command/ComposerGlobalMultiInstallCommandTest.php +++ b/tests/Tool/Command/ComposerGlobalMultiInstallCommandTest.php @@ -28,7 +28,7 @@ public function test_it_generates_a_single_installation_command() new ComposerGlobalInstallCommand('phpstan/phpstan'), ])); - $this->assertRegExp('#composer global require .*? phan/phan phpstan/phpstan#', (string) $command); + $this->assertMatchesRegularExpression('#composer global require .*? phan/phan phpstan/phpstan#', (string) $command); } public function test_it_throws_an_exception_if_there_is_no_commands() diff --git a/tests/Tool/Command/ComposerInstallCommandTest.php b/tests/Tool/Command/ComposerInstallCommandTest.php index 23197f10..0b860679 100644 --- a/tests/Tool/Command/ComposerInstallCommandTest.php +++ b/tests/Tool/Command/ComposerInstallCommandTest.php @@ -23,15 +23,15 @@ public function test_it_generates_the_installation_command() { $command = new ComposerInstallCommand(self::REPOSITORY, self::TARGET_DIR, self::VERSION); - $this->assertRegExp('#git clone '.self::REPOSITORY.'#', (string) $command); - $this->assertRegExp('#git checkout '.self::VERSION.'#', (string) $command); - $this->assertRegExp('#composer install --no-dev --no-suggest --prefer-dist -n#', (string) $command); + $this->assertMatchesRegularExpression('#git clone '.self::REPOSITORY.'#', (string) $command); + $this->assertMatchesRegularExpression('#git checkout '.self::VERSION.'#', (string) $command); + $this->assertMatchesRegularExpression('#composer install --no-dev --no-suggest --prefer-dist -n#', (string) $command); } public function test_it_tries_to_guess_version_number_if_not_given_one() { $command = new ComposerInstallCommand(self::REPOSITORY, self::TARGET_DIR); - $this->assertRegExp('#git checkout \$\(git describe --tags .*?\)#', (string) $command); + $this->assertMatchesRegularExpression('#git checkout \$\(git describe --tags .*?\)#', (string) $command); } } diff --git a/tests/Tool/Command/FileDownloadCommandTest.php b/tests/Tool/Command/FileDownloadCommandTest.php index cc9c3650..a155f7d5 100644 --- a/tests/Tool/Command/FileDownloadCommandTest.php +++ b/tests/Tool/Command/FileDownloadCommandTest.php @@ -28,6 +28,6 @@ public function test_it_is_a_command() public function test_it_generates_the_installation_command() { - $this->assertRegExp(\sprintf('#curl .*? %s -o %s#', self::URL, self::FILE), (string) $this->command); + $this->assertMatchesRegularExpression(\sprintf('#curl .*? %s -o %s#', self::URL, self::FILE), (string) $this->command); } } diff --git a/tests/Tool/Command/MultiStepCommandTest.php b/tests/Tool/Command/MultiStepCommandTest.php index 3e05e9d0..14e06a11 100644 --- a/tests/Tool/Command/MultiStepCommandTest.php +++ b/tests/Tool/Command/MultiStepCommandTest.php @@ -4,12 +4,15 @@ use InvalidArgumentException; use PHPUnit\Framework\TestCase; +use Prophecy\PhpUnit\ProphecyTrait; use Zalas\Toolbox\Tool\Collection; use Zalas\Toolbox\Tool\Command; use Zalas\Toolbox\Tool\Command\MultiStepCommand; class MultiStepCommandTest extends TestCase { + use ProphecyTrait; + public function test_it_is_a_command() { $command = new MultiStepCommand(Collection::create([$this->command('echo "A"')])); diff --git a/tests/Tool/Command/OptimisedComposerBinPluginCommandTest.php b/tests/Tool/Command/OptimisedComposerBinPluginCommandTest.php index 705e4e4e..c7f4174a 100644 --- a/tests/Tool/Command/OptimisedComposerBinPluginCommandTest.php +++ b/tests/Tool/Command/OptimisedComposerBinPluginCommandTest.php @@ -27,7 +27,7 @@ public function test_it_groups_composer_bin_command_by_namespace() $command = new OptimisedComposerBinPluginCommand(Collection::create($commands)); - $this->assertRegExp('#composer global bin phpstan require .*? phpstan/phpstan && composer global bin tools require .*? phan/phan behat/behat#', (string) $command); + $this->assertMatchesRegularExpression('#composer global bin phpstan require .*? phpstan/phpstan && composer global bin tools require .*? phan/phan behat/behat#', (string) $command); } public function test_it_throws_an_exception_if_there_is_no_commands() @@ -66,12 +66,12 @@ public function test_it_creates_links_to_composer_bin_commands() $command = new OptimisedComposerBinPluginCommand(Collection::create($commands)); - $this->assertRegExp('#composer global bin phpstan require .*? phpstan/phpstan && composer global bin tools require .*? phan/phan behat/behat#', (string) $command); - $this->assertRegExp('# && ln -sf.*?phpstan /tools/phpstan#', (string) $command); - $this->assertRegExp('# && ln -sf.*?phpstan /other/path/phpstan#', (string) $command); - $this->assertRegExp('# && ln -sf.*?phan /tools/phan#', (string) $command); - $this->assertRegExp('# && ln -sf.*?behat /tools/behat#', (string) $command); - $this->assertNotRegExp('#&&\s*&&#', (string) $command, 'It does not generate empty commands'); + $this->assertMatchesRegularExpression('#composer global bin phpstan require .*? phpstan/phpstan && composer global bin tools require .*? phan/phan behat/behat#', (string) $command); + $this->assertMatchesRegularExpression('# && ln -sf.*?phpstan /tools/phpstan#', (string) $command); + $this->assertMatchesRegularExpression('# && ln -sf.*?phpstan /other/path/phpstan#', (string) $command); + $this->assertMatchesRegularExpression('# && ln -sf.*?phan /tools/phan#', (string) $command); + $this->assertMatchesRegularExpression('# && ln -sf.*?behat /tools/behat#', (string) $command); + $this->assertDoesNotMatchRegularExpression('#&&\s*&&#', (string) $command, 'It does not generate empty commands'); } public function test_it_does_not_create_links_if_commands_have_no_links_defined() @@ -84,7 +84,7 @@ public function test_it_does_not_create_links_if_commands_have_no_links_defined( $command = new OptimisedComposerBinPluginCommand(Collection::create($commands)); - $this->assertNotRegExp('#ln -s#', (string) $command); - $this->assertNotRegExp('#&&\s*&&#', (string) $command, 'It does not generate empty commands'); + $this->assertDoesNotMatchRegularExpression('#ln -s#', (string) $command); + $this->assertDoesNotMatchRegularExpression('#&&\s*&&#', (string) $command, 'It does not generate empty commands'); } } diff --git a/tests/Tool/Command/PharDownloadCommandTest.php b/tests/Tool/Command/PharDownloadCommandTest.php index 71af4365..5d8bcbe6 100644 --- a/tests/Tool/Command/PharDownloadCommandTest.php +++ b/tests/Tool/Command/PharDownloadCommandTest.php @@ -28,6 +28,6 @@ public function test_it_is_a_command() public function test_it_generates_the_installation_command() { - $this->assertRegExp(\sprintf('#curl .*? %s -o %s#', self::PHAR, self::BIN), (string) $this->command); + $this->assertMatchesRegularExpression(\sprintf('#curl .*? %s -o %s#', self::PHAR, self::BIN), (string) $this->command); } } diff --git a/tests/Tool/Command/TestCommandTest.php b/tests/Tool/Command/TestCommandTest.php index 603cfec6..2f8c16f0 100644 --- a/tests/Tool/Command/TestCommandTest.php +++ b/tests/Tool/Command/TestCommandTest.php @@ -15,7 +15,7 @@ public function test_it_is_a_command() public function test_it_generates_the_command() { - $this->assertRegExp( + $this->assertMatchesRegularExpression( '#\(\(/usr/bin/true > /dev/null && echo -e .*?✔\.*?\) || \(echo -e .*?✘.*?" && false\)\)#', (string) new TestCommand('/usr/bin/true', 'true') ); diff --git a/tests/Tool/FilterTest.php b/tests/Tool/FilterTest.php index d16ed2bf..5688884c 100644 --- a/tests/Tool/FilterTest.php +++ b/tests/Tool/FilterTest.php @@ -3,11 +3,14 @@ namespace Zalas\Toolbox\Tests\Tool; use PHPUnit\Framework\TestCase; +use Prophecy\PhpUnit\ProphecyTrait; use Zalas\Toolbox\Tool\Filter; use Zalas\Toolbox\Tool\Tool; class FilterTest extends TestCase { + use ProphecyTrait; + public function test_it_returns_true_if_no_excluded_tags_were_defined() { $filter = new Filter([], []); diff --git a/tests/Tool/ToolTest.php b/tests/Tool/ToolTest.php index 74614ef6..2dd24433 100644 --- a/tests/Tool/ToolTest.php +++ b/tests/Tool/ToolTest.php @@ -4,12 +4,15 @@ namespace Zalas\Toolbox\Tests\Tool; use PHPUnit\Framework\TestCase; +use Prophecy\PhpUnit\ProphecyTrait; use TypeError; use Zalas\Toolbox\Tool\Command; use Zalas\Toolbox\Tool\Tool; class ToolTest extends TestCase { + use ProphecyTrait; + public function test_it_exposes_its_properties() { $command = $this->prophesize(Command::class)->reveal(); diff --git a/tests/UseCase/InstallToolsTest.php b/tests/UseCase/InstallToolsTest.php index c810f5b6..9803cee0 100644 --- a/tests/UseCase/InstallToolsTest.php +++ b/tests/UseCase/InstallToolsTest.php @@ -4,6 +4,7 @@ use PHPUnit\Framework\TestCase; use Prophecy\Argument; +use Prophecy\PhpUnit\ProphecyTrait; use Prophecy\Prophecy\ObjectProphecy; use Zalas\Toolbox\Tool\Collection; use Zalas\Toolbox\Tool\Command; @@ -21,6 +22,8 @@ class InstallToolsTest extends TestCase { + use ProphecyTrait; + /** * @var InstallTools */ @@ -57,7 +60,7 @@ public function test_it_groups_composer_global_install_commands() $command = $this->useCase->__invoke($this->filter()); - $this->assertRegExp('#composer global require .* phpstan/phpstan phan/phan#', (string)$command); + $this->assertMatchesRegularExpression('#composer global require .* phpstan/phpstan phan/phan#', (string)$command); } public function test_it_does_not_include_empty_commands() @@ -68,8 +71,8 @@ public function test_it_does_not_include_empty_commands() $command = $this->useCase->__invoke($this->filter()); - $this->assertNotRegExp('#composer global require#', (string)$command, 'Composer commands are not grouped if there is none.'); - $this->assertNotRegExp('#&&\s*$#', (string)$command, 'Empty commands are not included.'); + $this->assertDoesNotMatchRegularExpression('#composer global require#', (string)$command, 'Composer commands are not grouped if there is none.'); + $this->assertDoesNotMatchRegularExpression('#&&\s*$#', (string)$command, 'Empty commands are not included.'); } public function test_it_groups_composer_bin_plugin_commands() @@ -81,7 +84,7 @@ public function test_it_groups_composer_bin_plugin_commands() $command = $this->useCase->__invoke($this->filter()); - $this->assertRegExp('#composer global bin tools require .* phpstan/phpstan phan/phan#', (string)$command); + $this->assertMatchesRegularExpression('#composer global bin tools require .* phpstan/phpstan phan/phan#', (string)$command); } public function test_it_includes_installation_tagged_commands_before_other_ones() @@ -93,8 +96,8 @@ public function test_it_includes_installation_tagged_commands_before_other_ones( $command = $this->useCase->__invoke($this->filter()); - $this->assertRegExp('#echo "Installation".*echo "Foo"#smi', (string)$command, 'Installation commands are included before other ones.'); - $this->assertNotRegExp('#echo "Installation".*echo "Installation"#smi', (string)$command, 'Installation commands are not duplicated.'); + $this->assertMatchesRegularExpression('#echo "Installation".*echo "Foo"#smi', (string)$command, 'Installation commands are included before other ones.'); + $this->assertDoesNotMatchRegularExpression('#echo "Installation".*echo "Installation"#smi', (string)$command, 'Installation commands are not duplicated.'); } public function test_it_includes_shell_commands() @@ -105,7 +108,7 @@ public function test_it_includes_shell_commands() $command = $this->useCase->__invoke($this->filter()); - $this->assertRegExp('#echo "Foo"#', (string)$command); + $this->assertMatchesRegularExpression('#echo "Foo"#', (string)$command); } public function test_it_includes_multi_step_commands() @@ -119,7 +122,7 @@ public function test_it_includes_multi_step_commands() $command = $this->useCase->__invoke($this->filter()); - $this->assertRegExp('#echo "Foo" && echo "Bar"#', (string)$command); + $this->assertMatchesRegularExpression('#echo "Foo" && echo "Bar"#', (string)$command); } public function test_it_includes_composer_install_commands() @@ -130,7 +133,7 @@ public function test_it_includes_composer_install_commands() $command = $this->useCase->__invoke($this->filter()); - $this->assertRegExp('#git clone git@github.com:phpspec/phpspec.git#', (string)$command); + $this->assertMatchesRegularExpression('#git clone git@github.com:phpspec/phpspec.git#', (string)$command); } public function test_it_includes_box_build_commands() @@ -141,7 +144,7 @@ public function test_it_includes_box_build_commands() $command = $this->useCase->__invoke($this->filter()); - $this->assertRegExp('#box-legacy build#', (string)$command); + $this->assertMatchesRegularExpression('#box-legacy build#', (string)$command); } public function test_it_includes_phar_download_commands() @@ -152,7 +155,7 @@ public function test_it_includes_phar_download_commands() $command = $this->useCase->__invoke($this->filter()); - $this->assertRegExp('#curl[^&]*?deptrac-0.2.0.phar#', (string)$command); + $this->assertMatchesRegularExpression('#curl[^&]*?deptrac-0.2.0.phar#', (string)$command); } private function filter(): Filter diff --git a/tests/UseCase/ListToolsTest.php b/tests/UseCase/ListToolsTest.php index 53a4572d..e47bf469 100644 --- a/tests/UseCase/ListToolsTest.php +++ b/tests/UseCase/ListToolsTest.php @@ -3,6 +3,7 @@ namespace Zalas\Toolbox\Tests\UseCase; use PHPUnit\Framework\TestCase; +use Prophecy\PhpUnit\ProphecyTrait; use Zalas\Toolbox\Tool\Collection; use Zalas\Toolbox\Tool\Filter; use Zalas\Toolbox\Tool\Tool; @@ -11,6 +12,8 @@ class ListToolsTest extends TestCase { + use ProphecyTrait; + public function test_it_returns_loaded_tools() { $tools = Collection::create([ diff --git a/tests/UseCase/TestToolsTest.php b/tests/UseCase/TestToolsTest.php index 6e7ed0dd..74ba8f89 100644 --- a/tests/UseCase/TestToolsTest.php +++ b/tests/UseCase/TestToolsTest.php @@ -3,6 +3,7 @@ namespace Zalas\Toolbox\Tests\UseCase; use PHPUnit\Framework\TestCase; +use Prophecy\PhpUnit\ProphecyTrait; use Zalas\Toolbox\Tool\Collection; use Zalas\Toolbox\Tool\Command; use Zalas\Toolbox\Tool\Filter; @@ -12,6 +13,8 @@ class TestToolsTest extends TestCase { + use ProphecyTrait; + public function test_it_returns_test_aggregated_test_command() { $testCommands = [ From 3c8995aa52d8ed1837fa4c2ad9d6e7e18363e5bf Mon Sep 17 00:00:00 2001 From: Jakub Zalas Date: Tue, 12 May 2020 10:13:54 +0100 Subject: [PATCH 2/2] Copy the prophecy trait as phpspec/prophecy-phpunit is not available on PHP 7.1 --- composer.json | 3 +- phpunit.xml.dist | 2 +- tests/Cli/ApplicationTest.php | 4 +- tests/Cli/Command/InstallCommandTest.php | 4 +- tests/Cli/Command/ListCommandTest.php | 4 +- tests/Cli/Command/TestCommandTest.php | 4 +- tests/Cli/Runner/DryRunnerTest.php | 4 +- tests/Cli/ServiceContainer/LazyRunnerTest.php | 4 +- .../ServiceContainer/RunnerFactoryTest.php | 4 +- tests/Cli/ServiceContainerTest.php | 4 +- tests/Prophecy/Prophecy.php | 111 ++++++++++++++++++ tests/Runner/ParametrisedRunnerTest.php | 4 +- tests/Tool/Command/MultiStepCommandTest.php | 4 +- tests/Tool/FilterTest.php | 4 +- tests/Tool/ToolTest.php | 4 +- tests/UseCase/InstallToolsTest.php | 4 +- tests/UseCase/ListToolsTest.php | 4 +- tests/UseCase/TestToolsTest.php | 4 +- 18 files changed, 143 insertions(+), 33 deletions(-) create mode 100644 tests/Prophecy/Prophecy.php diff --git a/composer.json b/composer.json index b338c29c..17253bb1 100644 --- a/composer.json +++ b/composer.json @@ -9,8 +9,7 @@ }, "require-dev": { "phpunit/phpunit": "^8.4 || ^9.1", - "zalas/phpunit-globals": "^2.0", - "phpspec/prophecy-phpunit": "^2.0" + "zalas/phpunit-globals": "^2.0" }, "autoload": { "psr-4": { diff --git a/phpunit.xml.dist b/phpunit.xml.dist index bb6d15e6..fad12eff 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,6 +1,6 @@ recordDoubledType($classOrInterface); + } + + return $this->getProphet()->prophesize($classOrInterface); + } + + /** + * @postCondition + */ + protected function verifyProphecyDoubles(): void + { + if (null === $this->prophet) { + return; + } + + try { + $this->prophet->checkPredictions(); + } catch (PredictionException $e) { + throw new AssertionFailedError($e->getMessage()); + } finally { + $this->countProphecyAssertions(); + } + } + + /** + * @after + */ + protected function tearDownProphecy(): void + { + if (null !== $this->prophet && !$this->prophecyAssertionsCounted) { + // Some Prophecy assertions may have been done in tests themselves even when a failure happened before checking mock objects. + $this->countProphecyAssertions(); + } + + $this->prophet = null; + } + + /** + * @internal + */ + private function countProphecyAssertions(): void + { + \assert($this instanceof TestCase); + $this->prophecyAssertionsCounted = true; + + foreach ($this->prophet->getProphecies() as $objectProphecy) { + foreach ($objectProphecy->getMethodProphecies() as $methodProphecies) { + foreach ($methodProphecies as $methodProphecy) { + \assert($methodProphecy instanceof MethodProphecy); + + $this->addToAssertionCount(\count($methodProphecy->getCheckedPredictions())); + } + } + } + } + + /** + * @internal + */ + private function getProphet(): Prophet + { + if (null === $this->prophet) { + $this->prophet = new Prophet; + } + + return $this->prophet; + } +} diff --git a/tests/Runner/ParametrisedRunnerTest.php b/tests/Runner/ParametrisedRunnerTest.php index 7d25f36f..341f7f86 100644 --- a/tests/Runner/ParametrisedRunnerTest.php +++ b/tests/Runner/ParametrisedRunnerTest.php @@ -4,15 +4,15 @@ use PHPUnit\Framework\TestCase; use Prophecy\Argument; -use Prophecy\PhpUnit\ProphecyTrait; use Prophecy\Prophecy\ObjectProphecy; use Zalas\Toolbox\Runner\ParametrisedRunner; use Zalas\Toolbox\Runner\Runner; +use Zalas\Toolbox\Tests\Prophecy\Prophecy; use Zalas\Toolbox\Tool\Command; class ParametrisedRunnerTest extends TestCase { - use ProphecyTrait; + use Prophecy; /** * @var ParametrisedRunner diff --git a/tests/Tool/Command/MultiStepCommandTest.php b/tests/Tool/Command/MultiStepCommandTest.php index 14e06a11..12961a93 100644 --- a/tests/Tool/Command/MultiStepCommandTest.php +++ b/tests/Tool/Command/MultiStepCommandTest.php @@ -4,14 +4,14 @@ use InvalidArgumentException; use PHPUnit\Framework\TestCase; -use Prophecy\PhpUnit\ProphecyTrait; +use Zalas\Toolbox\Tests\Prophecy\Prophecy; use Zalas\Toolbox\Tool\Collection; use Zalas\Toolbox\Tool\Command; use Zalas\Toolbox\Tool\Command\MultiStepCommand; class MultiStepCommandTest extends TestCase { - use ProphecyTrait; + use Prophecy; public function test_it_is_a_command() { diff --git a/tests/Tool/FilterTest.php b/tests/Tool/FilterTest.php index 5688884c..fd88da0d 100644 --- a/tests/Tool/FilterTest.php +++ b/tests/Tool/FilterTest.php @@ -3,13 +3,13 @@ namespace Zalas\Toolbox\Tests\Tool; use PHPUnit\Framework\TestCase; -use Prophecy\PhpUnit\ProphecyTrait; +use Zalas\Toolbox\Tests\Prophecy\Prophecy; use Zalas\Toolbox\Tool\Filter; use Zalas\Toolbox\Tool\Tool; class FilterTest extends TestCase { - use ProphecyTrait; + use Prophecy; public function test_it_returns_true_if_no_excluded_tags_were_defined() { diff --git a/tests/Tool/ToolTest.php b/tests/Tool/ToolTest.php index 2dd24433..5beaf41e 100644 --- a/tests/Tool/ToolTest.php +++ b/tests/Tool/ToolTest.php @@ -4,14 +4,14 @@ namespace Zalas\Toolbox\Tests\Tool; use PHPUnit\Framework\TestCase; -use Prophecy\PhpUnit\ProphecyTrait; use TypeError; +use Zalas\Toolbox\Tests\Prophecy\Prophecy; use Zalas\Toolbox\Tool\Command; use Zalas\Toolbox\Tool\Tool; class ToolTest extends TestCase { - use ProphecyTrait; + use Prophecy; public function test_it_exposes_its_properties() { diff --git a/tests/UseCase/InstallToolsTest.php b/tests/UseCase/InstallToolsTest.php index 9803cee0..a45f6f5a 100644 --- a/tests/UseCase/InstallToolsTest.php +++ b/tests/UseCase/InstallToolsTest.php @@ -4,8 +4,8 @@ use PHPUnit\Framework\TestCase; use Prophecy\Argument; -use Prophecy\PhpUnit\ProphecyTrait; use Prophecy\Prophecy\ObjectProphecy; +use Zalas\Toolbox\Tests\Prophecy\Prophecy; use Zalas\Toolbox\Tool\Collection; use Zalas\Toolbox\Tool\Command; use Zalas\Toolbox\Tool\Command\BoxBuildCommand; @@ -22,7 +22,7 @@ class InstallToolsTest extends TestCase { - use ProphecyTrait; + use Prophecy; /** * @var InstallTools diff --git a/tests/UseCase/ListToolsTest.php b/tests/UseCase/ListToolsTest.php index e47bf469..511e16e6 100644 --- a/tests/UseCase/ListToolsTest.php +++ b/tests/UseCase/ListToolsTest.php @@ -3,7 +3,7 @@ namespace Zalas\Toolbox\Tests\UseCase; use PHPUnit\Framework\TestCase; -use Prophecy\PhpUnit\ProphecyTrait; +use Zalas\Toolbox\Tests\Prophecy\Prophecy; use Zalas\Toolbox\Tool\Collection; use Zalas\Toolbox\Tool\Filter; use Zalas\Toolbox\Tool\Tool; @@ -12,7 +12,7 @@ class ListToolsTest extends TestCase { - use ProphecyTrait; + use Prophecy; public function test_it_returns_loaded_tools() { diff --git a/tests/UseCase/TestToolsTest.php b/tests/UseCase/TestToolsTest.php index 74ba8f89..34bcebcd 100644 --- a/tests/UseCase/TestToolsTest.php +++ b/tests/UseCase/TestToolsTest.php @@ -3,7 +3,7 @@ namespace Zalas\Toolbox\Tests\UseCase; use PHPUnit\Framework\TestCase; -use Prophecy\PhpUnit\ProphecyTrait; +use Zalas\Toolbox\Tests\Prophecy\Prophecy; use Zalas\Toolbox\Tool\Collection; use Zalas\Toolbox\Tool\Command; use Zalas\Toolbox\Tool\Filter; @@ -13,7 +13,7 @@ class TestToolsTest extends TestCase { - use ProphecyTrait; + use Prophecy; public function test_it_returns_test_aggregated_test_command() {