Skip to content

Commit

Permalink
Remove flaky test
Browse files Browse the repository at this point in the history
Depending on the order of the test suite execution we were having issues
as the compiled container for `CompilerTest` was using the same FQCN as
the `GeneratorTest`.

This makes sure we use different namespaces for the generated
containers, avoiding those conflicts.

It also plugs vfsStream to remove the file system dependency on
`CompilerTest`.

Running PHPUnit using the flag `--random-order-seed=1634593025` was
enough to reproduce the issue.
  • Loading branch information
lcobucci committed Oct 18, 2021
1 parent a6169a6 commit 9d997c1
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 21 deletions.
20 changes: 5 additions & 15 deletions test/CompilerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,11 @@
use Symfony\Component\DependencyInjection\Compiler\PassConfig;
use Symfony\Component\DependencyInjection\Container;

use function bin2hex;
use function count;
use function exec;
use function file_get_contents;
use function file_put_contents;
use function iterator_to_array;
use function mkdir;
use function random_bytes;
use function realpath;

/**
* @covers \Lcobucci\DependencyInjection\Compiler
Expand Down Expand Up @@ -53,7 +49,7 @@ final class CompilerTest extends TestCase
public function configureDependencies(): void
{
vfsStream::setup(
'tests',
'tests-compilation',
null,
['services.yml' => 'services: { testing: { class: stdClass } }'],
);
Expand All @@ -67,8 +63,8 @@ public function configureDependencies(): void
$this->dump = new ConfigCache($this->dumpDir . '/AppContainer.php', false);

$this->config = new ContainerConfiguration(
'Me\\MyApp',
[vfsStream::url('tests/services.yml')],
'Me\\CompilationTest',
[vfsStream::url('tests-compilation/services.yml')],
[
[$this->parameters, PassConfig::TYPE_BEFORE_OPTIMIZATION],
[[MakeServicesPublic::class, []], PassConfig::TYPE_BEFORE_OPTIMIZATION],
Expand All @@ -80,18 +76,12 @@ public function configureDependencies(): void

private function createDumpDirectory(): string
{
$dir = __DIR__ . '/../tmp/' . bin2hex(random_bytes(5)) . '/me_myapp';
$dir = vfsStream::url('tests-compilation/tmp/me_myapp');
mkdir($dir, 0777, true);

return $dir;
}

/** @after */
public function cleanUpDumpDirectory(): void
{
exec('rm -rf ' . realpath($this->dumpDir . '/../../'));
}

/** @test */
public function compileShouldCreateMultipleFilesForDevelopmentMode(): void
{
Expand Down Expand Up @@ -143,7 +133,7 @@ public function compileShouldTrackChangesOnTheConfigurationFile(): void
public function compileShouldAllowForLazyServices(): void
{
file_put_contents(
vfsStream::url('tests/services.yml'),
vfsStream::url('tests-compilation/services.yml'),
'services: { testing: { class: stdClass, lazy: true } }',
);

Expand Down
12 changes: 6 additions & 6 deletions test/GeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,27 +79,27 @@ public function initializeContainerCanOptionallyUseACustomClass(): void
public function generateShouldCompileAndLoadTheContainer(): void
{
vfsStream::setup(
'tests',
'tests-generation',
null,
['services.yml' => 'services: { testing: { class: stdClass, public: true } }'],
);

$config = new ContainerConfiguration(
'Me\\MyApp',
[vfsStream::url('tests/services.yml')],
'Me\\GenerationTest',
[vfsStream::url('tests-generation/services.yml')],
[
[new ParameterBag(['app.devmode' => true]), PassConfig::TYPE_BEFORE_OPTIMIZATION],
[
new DumpXmlContainer(
new ConfigCache(vfsStream::url('tests/dump.xml'), true),
new ConfigCache(vfsStream::url('tests-generation/dump.xml'), true),
),
PassConfig::TYPE_AFTER_REMOVING,
-255,
],
],
);

$dump = new ConfigCache(vfsStream::url('tests/container.php'), false);
$dump = new ConfigCache(vfsStream::url('tests-generation/container.php'), false);

$this->generator->method('getLoader')->willReturnCallback(
static function (SymfonyBuilder $container, array $paths): YamlFileLoader {
Expand All @@ -113,6 +113,6 @@ static function (SymfonyBuilder $container, array $paths): YamlFileLoader {
$container = $this->generator->generate($config, $dump);

self::assertInstanceOf(stdClass::class, $container->get('testing'));
self::assertFileExists(vfsStream::url('tests/dump.xml'));
self::assertFileExists(vfsStream::url('tests-generation/dump.xml'));
}
}

0 comments on commit 9d997c1

Please sign in to comment.