Skip to content

Commit

Permalink
Merge pull request #24 from lcobucci/remove-mutations
Browse files Browse the repository at this point in the history
Remove escaped mutations
  • Loading branch information
lcobucci committed Jun 23, 2019
2 parents 7c0a596 + 5697cb3 commit 0efd798
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 55 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ jobs:
- mv ~/.phpenv/versions/$(phpenv version-name)/etc/conf.d/xdebug.ini{.disabled,}
- if [[ ! $(php -m | grep -si xdebug) ]]; then echo "xdebug required for mutation"; exit 1; fi
script:
- ./vendor/bin/infection -s --min-msi=90 --min-covered-msi=91 --threads=$(nproc)
- ./vendor/bin/infection -s --min-msi=100 --min-covered-msi=100 --threads=$(nproc)

- stage: Quality
env: STATIC_ANALYSIS=1
Expand Down
4 changes: 0 additions & 4 deletions infection.json.dist
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,5 @@
},
"logs": {
"text": "infection.log"
},
"mutators": {
"@default": true,
"@function_signature": true
}
}
26 changes: 11 additions & 15 deletions src/Config/ContainerConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,26 +105,24 @@ public function addPackage(string $className, array $constructArguments = []): v

public function getFiles(): Generator
{
foreach ($this->filterModules(FileListProvider::class) as $module) {
assert($module instanceof FileListProvider);
foreach ($this->filterPackages(FileListProvider::class) as $package) {
assert($package instanceof FileListProvider);

yield from $module->getFiles();
yield from $package->getFiles();
}

foreach ($this->files as $file) {
yield $file;
}
yield from $this->files;
}

/**
* @return Package[]
*/
private function filterModules(string $moduleType): array
private function filterPackages(string $packageType): array
{
return array_filter(
$this->getPackages(),
static function (Package $module) use ($moduleType): bool {
return $module instanceof $moduleType;
static function (Package $package) use ($packageType): bool {
return $package instanceof $packageType;
}
);
}
Expand All @@ -136,15 +134,13 @@ public function addFile(string $file): void

public function getPassList(): Generator
{
foreach ($this->filterModules(CompilerPassListProvider::class) as $module) {
assert($module instanceof CompilerPassListProvider);
foreach ($this->filterPackages(CompilerPassListProvider::class) as $package) {
assert($package instanceof CompilerPassListProvider);

yield from $module->getCompilerPasses();
yield from $package->getCompilerPasses();
}

foreach ($this->passList as $compilerPass) {
yield $compilerPass;
}
yield from $this->passList;
}

public function addPass(
Expand Down
10 changes: 6 additions & 4 deletions src/ContainerBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\Compiler\PassConfig;
use Symfony\Component\DependencyInjection\ContainerInterface;
use function assert;
use function is_bool;

final class ContainerBuilder implements Builder
{
Expand Down Expand Up @@ -141,12 +143,12 @@ public function setBaseClass(string $class): Builder

public function getContainer(): ContainerInterface
{
$devMode = $this->parameterBag->get('app.devmode');
assert(is_bool($devMode));

return $this->generator->generate(
$this->config,
new ConfigCache(
$this->config->getDumpFile(),
(bool) $this->parameterBag->get('app.devmode')
)
new ConfigCache($this->config->getDumpFile(), $devMode)
);
}

Expand Down
4 changes: 2 additions & 2 deletions src/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ abstract class Generator
*/
private $compiler;

public function __construct(?Compiler $compiler = null)
public function __construct()
{
$this->compiler = $compiler ?? new Compiler();
$this->compiler = new Compiler();
}

/**
Expand Down
47 changes: 39 additions & 8 deletions test/CompilerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Lcobucci\DependencyInjection\Compiler\ParameterBag;
use Lcobucci\DependencyInjection\Config\ContainerConfiguration;
use Lcobucci\DependencyInjection\Generators\Yaml;
use Lcobucci\DependencyInjection\Testing\MakeServicesPublic;
use org\bovigo\vfs\vfsStream;
use org\bovigo\vfs\vfsStreamDirectory;
use org\bovigo\vfs\vfsStreamFile;
Expand All @@ -21,6 +22,12 @@

final class CompilerTest extends TestCase
{
private const EXPECTED_FILES = [
'getTestingService.php',
'container.php',
'container.php.meta',
];

/**
* @var vfsStreamDirectory
*/
Expand All @@ -44,13 +51,14 @@ public function configureDependencies(): void
$this->root = vfsStream::setup(
'tests',
null,
['services.yml' => 'services: { testing: { class: stdClass, public: true } }']
['services.yml' => 'services: { testing: { class: stdClass } }']
);

$this->config = new ContainerConfiguration(
[vfsStream::url('tests/services.yml')],
[
[new ParameterBag(['app.devmode' => true]), PassConfig::TYPE_BEFORE_OPTIMIZATION],
[[MakeServicesPublic::class, []], PassConfig::TYPE_BEFORE_OPTIMIZATION],
]
);

Expand All @@ -66,19 +74,15 @@ public function configureDependencies(): void
* @uses \Lcobucci\DependencyInjection\Config\ContainerConfiguration
* @uses \Lcobucci\DependencyInjection\Generator
* @uses \Lcobucci\DependencyInjection\Generators\Yaml
* @uses \Lcobucci\DependencyInjection\Testing\MakeServicesPublic
*/
public function compileShouldCreateMultipleFiles(): void
{
$compiler = new Compiler();
$compiler->compile($this->config, $this->dump, new Yaml());

$expectedFiles = [
'removed-ids.php',
'getTestingService.php',
$this->config->getClassName() . '.php',
'container.php',
'container.php.meta',
];
$expectedFiles = self::EXPECTED_FILES;
$expectedFiles[] = $this->config->getClassName() . '.php';

$expectedPermissions = 0666 & ~umask();
$generatedFiles = iterator_to_array($this->getGeneratedFiles($this->root));
Expand All @@ -93,6 +97,33 @@ public function compileShouldCreateMultipleFiles(): void
}
}

/**
* @test
*
* @covers \Lcobucci\DependencyInjection\Compiler
*
* @uses \Lcobucci\DependencyInjection\Compiler\ParameterBag
* @uses \Lcobucci\DependencyInjection\Config\ContainerConfiguration
* @uses \Lcobucci\DependencyInjection\Generator
* @uses \Lcobucci\DependencyInjection\Generators\Yaml
* @uses \Lcobucci\DependencyInjection\Testing\MakeServicesPublic
*/
public function compileShouldAllowForLazyServices(): void
{
file_put_contents(
vfsStream::url('tests/services.yml'),
'services: { testing: { class: stdClass, lazy: true } }'
);

$compiler = new Compiler();
$compiler->compile($this->config, $this->dump, new Yaml());

$expectedFiles = self::EXPECTED_FILES;
$expectedFiles[] = $this->config->getClassName() . '.php';

self::assertCount(count($expectedFiles) + 1, iterator_to_array($this->getGeneratedFiles($this->root)));
}

/**
* @test
*
Expand Down
49 changes: 35 additions & 14 deletions test/Config/ContainerConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function configureDependencies(): void
*
* @covers \Lcobucci\DependencyInjection\Config\ContainerConfiguration::__construct
* @covers \Lcobucci\DependencyInjection\Config\ContainerConfiguration::getFiles
* @covers \Lcobucci\DependencyInjection\Config\ContainerConfiguration::filterModules
* @covers \Lcobucci\DependencyInjection\Config\ContainerConfiguration::filterPackages
*
* @uses \Lcobucci\DependencyInjection\Config\ContainerConfiguration::getPackages
*/
Expand All @@ -53,22 +53,35 @@ public function getFilesShouldReturnTheFileList(): void
* @test
*
* @covers \Lcobucci\DependencyInjection\Config\ContainerConfiguration::getFiles
* @covers \Lcobucci\DependencyInjection\Config\ContainerConfiguration::filterModules
* @covers \Lcobucci\DependencyInjection\Config\ContainerConfiguration::filterPackages
*
* @uses \Lcobucci\DependencyInjection\Config\ContainerConfiguration::getPackages
* @uses \Lcobucci\DependencyInjection\Config\ContainerConfiguration::__construct
*/
public function getFilesShouldYieldTheFilesFromPackagesFirst(): void
{
$package = new class implements FileListProvider
$package1 = new class implements CompilerPassListProvider
{
public function getCompilerPasses(): Generator
{
yield [CompilerPassInterface::class, 'beforeOptimization'];
}
};

$package2 = new class implements FileListProvider
{
public function getFiles(): Generator
{
yield 'services2.xml';
}
};

$config = new ContainerConfiguration(['services.xml'], [], [], [[get_class($package), []]]);
$config = new ContainerConfiguration(
['services.xml'],
[],
[],
[[get_class($package1), []], [get_class($package2), []]]
);

self::assertSame(['services2.xml', 'services.xml'], iterator_to_array($config->getFiles(), false));
}
Expand All @@ -91,7 +104,7 @@ public function addFileShouldAppendANewFileToTheList(): void
* @test
*
* @covers \Lcobucci\DependencyInjection\Config\ContainerConfiguration::getPassList
* @covers \Lcobucci\DependencyInjection\Config\ContainerConfiguration::filterModules
* @covers \Lcobucci\DependencyInjection\Config\ContainerConfiguration::filterPackages
*
* @uses \Lcobucci\DependencyInjection\Config\ContainerConfiguration::getPackages
* @uses \Lcobucci\DependencyInjection\Config\ContainerConfiguration::__construct
Expand All @@ -107,26 +120,34 @@ public function getPassListShouldReturnTheHandlersList(): void
* @test
*
* @covers \Lcobucci\DependencyInjection\Config\ContainerConfiguration::getPassList
* @covers \Lcobucci\DependencyInjection\Config\ContainerConfiguration::filterModules
* @covers \Lcobucci\DependencyInjection\Config\ContainerConfiguration::filterPackages
*
* @uses \Lcobucci\DependencyInjection\Config\ContainerConfiguration::getPackages
* @uses \Lcobucci\DependencyInjection\Config\ContainerConfiguration::__construct
*/
public function getPassListShouldYieldTheCompilerPassesFromPackagesFirst(): void
{
$package = new class implements CompilerPassListProvider
$package1 = new class implements CompilerPassListProvider
{
public function getCompilerPasses(): Generator
{
yield [CompilerPassInterface::class, 'beforeOptimization'];
}
};

$package2 = new class implements FileListProvider
{
public function getFiles(): Generator
{
yield 'services2.xml';
}
};

$config = new ContainerConfiguration(
[],
[[$this->pass, 'beforeOptimization']],
[],
[[get_class($package), []]]
[[get_class($package1), []], [get_class($package2), []]]
);

self::assertSame(
Expand All @@ -149,7 +170,7 @@ public function addPassShouldAppendANewHandlerToTheList(): void
$config = new ContainerConfiguration();
$config->addPass($this->pass);

$expected = $config = new ContainerConfiguration(
$expected = new ContainerConfiguration(
[],
[[$this->pass, PassConfig::TYPE_BEFORE_OPTIMIZATION, 0]]
);
Expand All @@ -168,7 +189,7 @@ public function addPassCanReceiveTheTypeAndPriority(): void
$config = new ContainerConfiguration();
$config->addPass($this->pass, PassConfig::TYPE_AFTER_REMOVING, 1);

$expected = $config = new ContainerConfiguration(
$expected = new ContainerConfiguration(
[],
[[$this->pass, PassConfig::TYPE_AFTER_REMOVING, 1]]
);
Expand All @@ -187,7 +208,7 @@ public function addDelayedPassShouldAppendANewCompilerPassToTheList(): void
$config = new ContainerConfiguration();
$config->addDelayedPass(ParameterBag::class, ['a' => 'b']);

$expected = $config = new ContainerConfiguration(
$expected = new ContainerConfiguration(
[],
[[[ParameterBag::class, ['a' => 'b']], PassConfig::TYPE_BEFORE_OPTIMIZATION, 0]]
);
Expand All @@ -206,7 +227,7 @@ public function addDelayedPassCanReceiveTheTypeAndPriority(): void
$config = new ContainerConfiguration();
$config->addDelayedPass(ParameterBag::class, ['a' => 'b'], PassConfig::TYPE_AFTER_REMOVING, 1);

$expected = $config = new ContainerConfiguration(
$expected = new ContainerConfiguration(
[],
[[[ParameterBag::class, ['a' => 'b']], PassConfig::TYPE_AFTER_REMOVING, 1]]
);
Expand All @@ -226,7 +247,7 @@ public function addPackageShouldAppendThePackageConfigurationToTheList(): void
$config = new ContainerConfiguration();
$config->addPackage($package, ['a' => 'b']);

$expected = $config = new ContainerConfiguration(
$expected = new ContainerConfiguration(
[],
[],
[],
Expand Down Expand Up @@ -293,7 +314,7 @@ public function addPathShouldAppendANewPathToTheList(): void
$config = new ContainerConfiguration();
$config->addPath('services');

$expected = $config = new ContainerConfiguration(
$expected = new ContainerConfiguration(
[],
[],
['services']
Expand Down
8 changes: 1 addition & 7 deletions test/GeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,12 @@ final class GeneratorTest extends TestCase
*/
private $generator;

/**
* @var Compiler
*/
private $compiler;

/**
* @before
*/
public function configureDependencies(): void
{
$this->compiler = new Compiler();
$this->generator = $this->getMockForAbstractClass(Generator::class, [$this->compiler]);
$this->generator = $this->getMockForAbstractClass(Generator::class);
}

/**
Expand Down

0 comments on commit 0efd798

Please sign in to comment.