From 021d4480583fe22a771a3eca3720aa75dc86eb25 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o=20FIDRY?= Date: Sat, 1 Jan 2022 18:36:21 +0100 Subject: [PATCH] Fix the tests (#11) --- .github/workflows/ci.yaml | 4 +- bin/clear_cache.sh | 2 +- bin/console | 8 +-- composer.json | 7 ++- fixtures/Functional/AppKernel.php | 22 ++------ .../MakeServicesPublicCompilerPass.php | 56 +++++++++++++++++++ fixtures/Functional/SimpleKernel.php | 32 +---------- fixtures/Functional/config/doctrine.yml | 9 +-- fixtures/Logger/FakeLogger.php | 18 +++--- phpunit.xml.dist | 3 +- .../DoctrineOrmLoadDataFixturesCommand.php | 4 +- src/Locator/EnvDirectoryLocator.php | 2 +- ...LoadDataFixturesCommandIntegrationTest.php | 5 -- .../Doctrine/LoadDataFixturesCommandTest.php | 5 +- .../DependencyInjection/ConfigurationTest.php | 11 ---- .../HautelookAliceBundleTest.php | 21 ++----- .../EnvironmentlessFilesLocatorTest.php | 3 + .../Resolver/Bundle/NoBundleResolverTest.php | 3 + .../Resolver/File/KernelFileResolverTest.php | 48 +--------------- 19 files changed, 104 insertions(+), 159 deletions(-) create mode 100644 fixtures/Functional/MakeServicesPublicCompilerPass.php diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 0276fbfc..f77e174f 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -5,7 +5,9 @@ on: push: branches: [ master ] +# this fixes the input device is not a TTY .. see https://github.com/docker/compose/issues/5696 env: + MYSQL_HOST: '127.0.0.1' SHELL_VERBOSITY: 1 jobs: @@ -36,7 +38,7 @@ jobs: run: php-cs-fixer fix --dry-run --diff --ansi phpunit: - name: PHPUnit (PHP ${{ matrix.php }}) + name: PHPUnit (PHP ${{ matrix.php }}) ${{ matrix.variant }} runs-on: ubuntu-latest strategy: matrix: diff --git a/bin/clear_cache.sh b/bin/clear_cache.sh index a91056c3..f5b9b525 100755 --- a/bin/clear_cache.sh +++ b/bin/clear_cache.sh @@ -9,4 +9,4 @@ # file that was distributed with this source code. # -rm -rf fixtures/Functional/cache/*ch +rm -rf fixtures/Functional/cache/* diff --git a/bin/console b/bin/console index 0af275ce..d37a941b 100755 --- a/bin/console +++ b/bin/console @@ -15,20 +15,16 @@ set_time_limit(0); require_once __DIR__.'/../vendor/autoload.php'; +use Hautelook\AliceBundle\Functional\AppKernel; use Symfony\Bundle\FrameworkBundle\Console\Application; use Symfony\Component\Console\Input\ArgvInput; -use Symfony\Component\Debug\Debug; $input = new ArgvInput(); $env = $input->getParameterOption(array('--env', '-e'), getenv('SYMFONY_ENV') ?: 'dev'); $debug = getenv('SYMFONY_DEBUG') !== '0' && !$input->hasParameterOption(array('--no-debug', '')) && $env !== 'prod'; -if ($debug) { - Debug::enable(); -} - error_reporting(E_ALL & ~E_USER_DEPRECATED); -$kernel = new Hautelook\AliceBundle\Functional\AppKernel($env, $debug); +$kernel = new AppKernel($env, $debug); $application = new Application($kernel); $application->run($input); diff --git a/composer.json b/composer.json index 230f29ef..4b8f0e69 100644 --- a/composer.json +++ b/composer.json @@ -37,14 +37,15 @@ "doctrine/orm": "^2.10.0", "doctrine/persistence": "^2.2", "psr/log": "^1.0 || ^2.0 || ^3.0", - "symfony/finder": "^5.4 || ^6.0", - "symfony/framework-bundle": "^5.4 || ^6.0", + "symfony/finder": "^5.4.2 || ^6.0", + "symfony/framework-bundle": "^5.4.2 || ^6.0", "theofidry/alice-data-fixtures": "^1.5" }, "require-dev": { "phpunit/phpunit": "^9.5", "phpspec/prophecy": "^1.7", - "symfony/phpunit-bridge": "^6.0" + "symfony/phpunit-bridge": "^6.0", + "phpspec/prophecy-phpunit": "^2.0" }, "extra": { diff --git a/fixtures/Functional/AppKernel.php b/fixtures/Functional/AppKernel.php index 3dbc83af..e2d2b01b 100644 --- a/fixtures/Functional/AppKernel.php +++ b/fixtures/Functional/AppKernel.php @@ -19,7 +19,6 @@ use Nelmio\Alice\Bridge\Symfony\NelmioAliceBundle; use Symfony\Bundle\FrameworkBundle\FrameworkBundle; use Symfony\Component\Config\Loader\LoaderInterface; -use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; use Symfony\Component\DependencyInjection\Compiler\PassConfig; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\HttpKernel\Kernel; @@ -64,22 +63,9 @@ public function build(ContainerBuilder $container) return; } - $container->addCompilerPass(new class() implements CompilerPassInterface { - public function process(ContainerBuilder $container) - { - foreach ($container->getDefinitions() as $id => $definition) { - if ('slugger' === $id) { - continue; - } - $definition->setPublic(true); - } - foreach ($container->getAliases() as $id => $definition) { - if ('Symfony\Component\String\Slugger\SluggerInterface' === $id) { - continue; - } - $definition->setPublic(true); - } - } - }, PassConfig::TYPE_OPTIMIZE); + $container->addCompilerPass( + new MakeServicesPublicCompilerPass(), + PassConfig::TYPE_OPTIMIZE, + ); } } diff --git a/fixtures/Functional/MakeServicesPublicCompilerPass.php b/fixtures/Functional/MakeServicesPublicCompilerPass.php new file mode 100644 index 00000000..03bcdd01 --- /dev/null +++ b/fixtures/Functional/MakeServicesPublicCompilerPass.php @@ -0,0 +1,56 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); + +namespace Hautelook\AliceBundle\Functional; + +use function array_flip; +use function array_key_exists; +use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; +use Symfony\Component\DependencyInjection\ContainerBuilder; +use Symfony\Component\DependencyInjection\Definition; +use Symfony\Component\String\Slugger\SluggerInterface; + +final class MakeServicesPublicCompilerPass implements CompilerPassInterface +{ + private const IGNORED_SERVICE_IDS = [ + 'slugger', + SluggerInterface::class, + 'test.private_services_locator', + ]; + + public function process(ContainerBuilder $container): void + { + self::setDefinitionsAsPublic( + $container->getDefinitions(), + $container->getAliases(), + ); + } + + /** + * @param list> $sources + */ + private static function setDefinitionsAsPublic(array $sources): void + { + $ignoredServiceIdsAsKeys = array_flip(self::IGNORED_SERVICE_IDS); + + foreach ($sources as $source) { + foreach ($source as $id => $definition) { + if ($definition instanceof Definition + && !array_key_exists($id, $ignoredServiceIdsAsKeys) + ) { + $definition->setPublic(true); + } + } + } + } +} diff --git a/fixtures/Functional/SimpleKernel.php b/fixtures/Functional/SimpleKernel.php index be193139..899abf4a 100644 --- a/fixtures/Functional/SimpleKernel.php +++ b/fixtures/Functional/SimpleKernel.php @@ -22,11 +22,6 @@ */ class SimpleKernel extends Kernel { - /** - * @var bool|null - */ - private $overrideFirst; - /** * {@inheritdoc} */ @@ -37,31 +32,8 @@ public function registerBundles(): iterable ]; } - /** - * {@inheritdoc} - */ - public function registerContainerConfiguration(LoaderInterface $loader) - { - } - - public function setLocateResourceFirst(bool $overrideFirst = null) + public function registerContainerConfiguration(LoaderInterface $loader): void { - $this->overrideFirst = $overrideFirst; - } - - /** - * {@inheritdoc} - */ - public function locateResource($name, $dir = null, $first = true): string - { - if (null !== $this->overrideFirst) { - $first = $this->overrideFirst; - } - - if (Kernel::VERSION_ID >= 50000) { - return parent::locateResource($name); - } - - return parent::locateResource($name, $dir, $first); + // Nothing to do } } diff --git a/fixtures/Functional/config/doctrine.yml b/fixtures/Functional/config/doctrine.yml index d5985893..9434312d 100644 --- a/fixtures/Functional/config/doctrine.yml +++ b/fixtures/Functional/config/doctrine.yml @@ -1,12 +1,5 @@ doctrine: dbal: - default_connection: default - connections: - default: - driver: pdo_mysql - dbname: hautelook_alice_bundle - port: 3307 - user: root - password: password + url: 'mysql://root:password@127.0.0.1:3307/hautelook_alice_bundle?serverVersion=8.0' orm: auto_mapping: true diff --git a/fixtures/Logger/FakeLogger.php b/fixtures/Logger/FakeLogger.php index 020ec271..411f32a5 100644 --- a/fixtures/Logger/FakeLogger.php +++ b/fixtures/Logger/FakeLogger.php @@ -26,7 +26,7 @@ class FakeLogger implements LoggerInterface /** * {@inheritdoc} */ - public function emergency($message, array $context = []) + public function emergency($message, array $context = []): void { $this->__call(__METHOD__, \func_get_args()); } @@ -34,7 +34,7 @@ public function emergency($message, array $context = []) /** * {@inheritdoc} */ - public function alert($message, array $context = []) + public function alert($message, array $context = []): void { $this->__call(__METHOD__, \func_get_args()); } @@ -42,7 +42,7 @@ public function alert($message, array $context = []) /** * {@inheritdoc} */ - public function critical($message, array $context = []) + public function critical($message, array $context = []): void { $this->__call(__METHOD__, \func_get_args()); } @@ -50,7 +50,7 @@ public function critical($message, array $context = []) /** * {@inheritdoc} */ - public function error($message, array $context = []) + public function error($message, array $context = []): void { $this->__call(__METHOD__, \func_get_args()); } @@ -58,7 +58,7 @@ public function error($message, array $context = []) /** * {@inheritdoc} */ - public function warning($message, array $context = []) + public function warning($message, array $context = []): void { $this->__call(__METHOD__, \func_get_args()); } @@ -66,7 +66,7 @@ public function warning($message, array $context = []) /** * {@inheritdoc} */ - public function notice($message, array $context = []) + public function notice($message, array $context = []): void { $this->__call(__METHOD__, \func_get_args()); } @@ -74,7 +74,7 @@ public function notice($message, array $context = []) /** * {@inheritdoc} */ - public function info($message, array $context = []) + public function info($message, array $context = []): void { $this->__call(__METHOD__, \func_get_args()); } @@ -82,7 +82,7 @@ public function info($message, array $context = []) /** * {@inheritdoc} */ - public function debug($message, array $context = []) + public function debug($message, array $context = []): void { $this->__call(__METHOD__, \func_get_args()); } @@ -90,7 +90,7 @@ public function debug($message, array $context = []) /** * {@inheritdoc} */ - public function log($level, $message, array $context = []) + public function log($level, $message, array $context = []): void { $this->__call(__METHOD__, \func_get_args()); } diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 6671c28d..7bd141a0 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -4,7 +4,8 @@ bootstrap="fixtures/Functional/bootstrap.php" colors="true" forceCoversAnnotation="true" - verbose="true"> + verbose="true" + stopOnFailure="true"> diff --git a/src/Console/Command/Doctrine/DoctrineOrmLoadDataFixturesCommand.php b/src/Console/Command/Doctrine/DoctrineOrmLoadDataFixturesCommand.php index e4c26e84..947208a6 100644 --- a/src/Console/Command/Doctrine/DoctrineOrmLoadDataFixturesCommand.php +++ b/src/Console/Command/Doctrine/DoctrineOrmLoadDataFixturesCommand.php @@ -71,7 +71,7 @@ protected function configure() ->addOption( 'no-bundles', null, - InputOption::VALUE_OPTIONAL, + InputOption::VALUE_NONE, 'Fixtures from bundles will not be loaded.' ) ->addOption( @@ -140,7 +140,7 @@ protected function execute(InputInterface $input, OutputInterface $output) } } - $noBundles = $input->getOption('no-bundles') ?? true; + $noBundles = $input->getOption('no-bundles') ?? false; if (!$noBundles) { @trigger_error( 'The configuration parameter hautelook_alice.root_dirs should be used to specify the directories to include. If done or if you do not need to load bundle\'s fixtures, use the --no-bundles option', diff --git a/src/Locator/EnvDirectoryLocator.php b/src/Locator/EnvDirectoryLocator.php index e14b343f..c96b3173 100644 --- a/src/Locator/EnvDirectoryLocator.php +++ b/src/Locator/EnvDirectoryLocator.php @@ -88,7 +88,7 @@ private function doLocateFiles(string $path, string $environment): array // this sort helps to set an order with filename // ( "001-root-level-fixtures.yml", "002-another-level-fixtures.yml", ... ) $files = $files->sort(function ($a, $b) { - return strcasecmp($a, $b); + return strcasecmp((string) $a, (string) $b); }); $fixtureFiles = []; diff --git a/tests/Console/Command/Doctrine/LoadDataFixturesCommandIntegrationTest.php b/tests/Console/Command/Doctrine/LoadDataFixturesCommandIntegrationTest.php index 02047c22..f71a42a2 100644 --- a/tests/Console/Command/Doctrine/LoadDataFixturesCommandIntegrationTest.php +++ b/tests/Console/Command/Doctrine/LoadDataFixturesCommandIntegrationTest.php @@ -13,7 +13,6 @@ namespace Hautelook\AliceBundle\Console\Command\Doctrine; -use Doctrine\Bundle\DoctrineBundle\DoctrineBundle; use Doctrine\DBAL\Sharding\PoolingShardConnection; use Doctrine\ORM\EntityManagerInterface; use Hautelook\AliceBundle\Functional\AppKernel; @@ -56,10 +55,6 @@ class LoadDataFixturesCommandIntegrationTest extends TestCase */ protected function setUp(): void { - if (false === class_exists(DoctrineBundle::class, true)) { - $this->markTestSkipped('DoctrineBundle is not installed.'); - } - $this->kernel = new TestKernel('LoadDataFixturesCommandIntegrationTest', true); $this->kernel->boot(); $this->application = new Application($this->kernel); diff --git a/tests/Console/Command/Doctrine/LoadDataFixturesCommandTest.php b/tests/Console/Command/Doctrine/LoadDataFixturesCommandTest.php index c88b1d6e..bac332a0 100644 --- a/tests/Console/Command/Doctrine/LoadDataFixturesCommandTest.php +++ b/tests/Console/Command/Doctrine/LoadDataFixturesCommandTest.php @@ -22,6 +22,7 @@ use Hautelook\AliceBundle\Persistence\ObjectMapper\FakeEntityManager; use PHPUnit\Framework\TestCase; use Prophecy\Argument; +use Prophecy\PhpUnit\ProphecyTrait; use Prophecy\Prophecy\ObjectProphecy; use Symfony\Bundle\FrameworkBundle\Console\Application as FrameworkBundleConsoleApplication; use Symfony\Component\Console\Application as ConsoleApplication; @@ -35,6 +36,8 @@ */ class LoadDataFixturesCommandTest extends TestCase { + use ProphecyTrait; + public function testIsACommand() { $this->assertTrue(is_a(DoctrineOrmLoadDataFixturesCommand::class, Command::class, true)); @@ -163,7 +166,7 @@ public function testCallCommandWithBundleAndNoBundlesFlags() '--shard' => 'shard_id', '--append' => null, '--purge-with-truncate' => null, - '--no-bundles', + '--no-bundles' => null, ]); $input->setInteractive(false); diff --git a/tests/DependencyInjection/ConfigurationTest.php b/tests/DependencyInjection/ConfigurationTest.php index f3a194f7..dcf70331 100644 --- a/tests/DependencyInjection/ConfigurationTest.php +++ b/tests/DependencyInjection/ConfigurationTest.php @@ -15,7 +15,6 @@ use PHPUnit\Framework\TestCase; use Symfony\Component\Config\Definition\Processor; -use Symfony\Component\HttpKernel\Kernel; /** * @covers \Hautelook\AliceBundle\DependencyInjection\Configuration @@ -30,20 +29,10 @@ public function testDefaultValues() $expected = [ 'fixtures_path' => ['Resources/fixtures'], 'root_dirs' => [ - '%kernel.root_dir%', '%kernel.project_dir%', ], ]; - if (Kernel::VERSION_ID >= 50000) { - $expected = [ - 'fixtures_path' => ['Resources/fixtures'], - 'root_dirs' => [ - '%kernel.project_dir%', - ], - ]; - } - $actual = $processor->processConfiguration($configuration, []); $this->assertEquals($expected, $actual); diff --git a/tests/DependencyInjection/HautelookAliceBundleTest.php b/tests/DependencyInjection/HautelookAliceBundleTest.php index 835d4de9..483fdf12 100644 --- a/tests/DependencyInjection/HautelookAliceBundleTest.php +++ b/tests/DependencyInjection/HautelookAliceBundleTest.php @@ -19,7 +19,6 @@ use Hautelook\AliceBundle\Functional\WithoutDoctrineKernel; use Nelmio\Alice\Bridge\Symfony\NelmioAliceBundle; use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; -use Symfony\Component\HttpKernel\Kernel; /** * @covers \Hautelook\AliceBundle\HautelookAliceBundle @@ -33,14 +32,8 @@ class HautelookAliceBundleTest extends KernelTestCase */ protected function tearDown(): void { - if (Kernel::VERSION_ID < 41000) { - if (null !== self::$kernel) { - self::$kernel->shutdown(); - } - } else { - parent::tearDown(); - static::$class = null; - } + parent::tearDown(); + static::$class = null; } public function testCannotBootIfFidryAliceDataFixturesBundleIsNotRegistered() @@ -65,14 +58,8 @@ public function testWillReplaceFixtureLoadCommandWithErrorInformationCommandIfDo public function testServiceRegistration() { - if (Kernel::VERSION_ID < 41000) { - self::$kernel = new AppKernel('public', true); - self::$kernel->boot(); - $container = self::$kernel->getContainer(); - } else { - parent::bootKernel(['environment' => 'public', 'debug' => true]); - $container = self::$container; - } + parent::bootKernel(['environment' => 'public', 'debug' => true]); + $container = self::getContainer(); // Resolvers $this->assertInstanceOf( diff --git a/tests/Locator/EnvironmentlessFilesLocatorTest.php b/tests/Locator/EnvironmentlessFilesLocatorTest.php index c6bae378..90db2519 100644 --- a/tests/Locator/EnvironmentlessFilesLocatorTest.php +++ b/tests/Locator/EnvironmentlessFilesLocatorTest.php @@ -16,6 +16,7 @@ use Hautelook\AliceBundle\FixtureLocatorInterface; use PHPUnit\Framework\TestCase; use Prophecy\Argument; +use Prophecy\PhpUnit\ProphecyTrait; use Prophecy\Prophecy\ObjectProphecy; use ReflectionClass; @@ -24,6 +25,8 @@ */ class EnvironmentlessFilesLocatorTest extends TestCase { + use ProphecyTrait; + public function testIsAFixtureLocator() { $this->assertTrue(is_a(EnvironmentlessFilesLocator::class, FixtureLocatorInterface::class, true)); diff --git a/tests/Resolver/Bundle/NoBundleResolverTest.php b/tests/Resolver/Bundle/NoBundleResolverTest.php index f84ec148..20ee0d7d 100644 --- a/tests/Resolver/Bundle/NoBundleResolverTest.php +++ b/tests/Resolver/Bundle/NoBundleResolverTest.php @@ -18,6 +18,7 @@ use Hautelook\AliceBundle\Resolver\ResolverKernel; use PHPUnit\Framework\TestCase; use Prophecy\Argument; +use Prophecy\PhpUnit\ProphecyTrait; use ReflectionClass; use Symfony\Bundle\FrameworkBundle\Console\Application; @@ -26,6 +27,8 @@ */ class NoBundleResolverTest extends TestCase { + use ProphecyTrait; + public function testIsABundleResolver() { $this->assertTrue(is_a(NoBundleResolver::class, BundleResolverInterface::class, true)); diff --git a/tests/Resolver/File/KernelFileResolverTest.php b/tests/Resolver/File/KernelFileResolverTest.php index c0243825..e5a958e9 100644 --- a/tests/Resolver/File/KernelFileResolverTest.php +++ b/tests/Resolver/File/KernelFileResolverTest.php @@ -17,8 +17,8 @@ use Hautelook\AliceBundle\Functional\SimpleKernel; use Hautelook\AliceBundle\HttpKernel\DummyKernel; use PHPUnit\Framework\TestCase; +use Prophecy\PhpUnit\ProphecyTrait; use ReflectionClass; -use Symfony\Component\HttpKernel\Kernel; use Symfony\Component\HttpKernel\KernelInterface; /** @@ -26,6 +26,8 @@ */ class KernelFileResolverTest extends TestCase { + use ProphecyTrait; + /** * @var KernelInterface|null */ @@ -89,50 +91,6 @@ public function testThrowsAnExceptionIfFileIsADirectory() $resolver->resolve([__DIR__]); } - public function testResolveFileWithTheKernelIfPossible() - { - if (Kernel::VERSION_ID >= 50000) { - $this->markTestSkipped('Symfony 5 dropped the $first parameter in the locateResource method. This method can no longer return an array'); - } - - $files = [ - '@SimpleBundle/files/foo.yml', - __FILE__, - ]; - - $this->kernel = new SimpleKernel('SimpleKernel_test', true); - $this->kernel->boot(); - - $expected = [ - realpath(__DIR__.'/../../../fixtures/Functional/SimpleBundle/files/foo.yml'), - __FILE__, - ]; - - $resolver = new KernelFileResolver($this->kernel); - $actual = $resolver->resolve($files); - - $this->assertSame($expected, $actual); - } - - public function testThrowsAnErrorIfTheFileResolvedByTheKernelIsNotAString() - { - if (Kernel::VERSION_ID >= 50000) { - $this->markTestSkipped('Symfony 5 dropped the $first parameter in the locateResource method. This method can no longer return an array'); - } - $this->expectException(\TypeError::class); - - $files = [ - '@SimpleBundle/files/foo.yml', - ]; - - $this->kernel = new SimpleKernel('SimpleKernel_test', true); - $this->kernel->setLocateResourceFirst(false); - $this->kernel->boot(); - - $resolver = new KernelFileResolver($this->kernel); - $resolver->resolve($files); - } - public function testThrowsAnExceptionIfFileResolvedByTheKernelDoesNotExist() { $this->expectException(\InvalidArgumentException::class);