-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from lcobucci/dump-xml-container
Provide compiler pass for XML dumping
- Loading branch information
Showing
2 changed files
with
121 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Lcobucci\DependencyInjection\Compiler; | ||
|
||
use Symfony\Component\Config\ConfigCache; | ||
use Symfony\Component\Config\ConfigCacheInterface; | ||
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Dumper\XmlDumper; | ||
|
||
final class DumpXmlContainer implements CompilerPassInterface | ||
{ | ||
/** | ||
* @var ConfigCacheInterface | ||
*/ | ||
private $configCache; | ||
|
||
public function __construct(ConfigCacheInterface $configCache) | ||
{ | ||
$this->configCache = $configCache; | ||
} | ||
|
||
public function process(ContainerBuilder $container): void | ||
{ | ||
if (! $container->getParameter('app.devmode') || ! $this->configCache->isFresh()) { | ||
return; | ||
} | ||
|
||
$this->configCache->write((new XmlDumper($container))->dump(), $container->getResources()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Lcobucci\DependencyInjection\Compiler; | ||
|
||
use PHPUnit\Framework\Constraint\StringContains; | ||
use PHPUnit\Framework\MockObject\MockObject; | ||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\Config\ConfigCacheInterface; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag as Parameters; | ||
|
||
final class DumpXmlContainerTest extends TestCase | ||
{ | ||
/** | ||
* @var ConfigCacheInterface|MockObject | ||
*/ | ||
private $configCache; | ||
|
||
/** | ||
* @before | ||
*/ | ||
public function createConfig(): void | ||
{ | ||
$this->configCache = $this->createMock(ConfigCacheInterface::class); | ||
} | ||
|
||
/** | ||
* @test | ||
* | ||
* @covers \Lcobucci\DependencyInjection\Compiler\DumpXmlContainer::__construct | ||
* @covers \Lcobucci\DependencyInjection\Compiler\DumpXmlContainer::process | ||
*/ | ||
public function processShouldBeSkippedWhenDevModeIsNotEnabled(): void | ||
{ | ||
$this->configCache->expects($this->never()) | ||
->method('isFresh'); | ||
|
||
$this->configCache->expects($this->never()) | ||
->method('write'); | ||
|
||
$pass = new DumpXmlContainer($this->configCache); | ||
$pass->process(new ContainerBuilder(new Parameters(['app.devmode' => false]))); | ||
} | ||
|
||
/** | ||
* @test | ||
* | ||
* @covers \Lcobucci\DependencyInjection\Compiler\DumpXmlContainer::__construct | ||
* @covers \Lcobucci\DependencyInjection\Compiler\DumpXmlContainer::process | ||
*/ | ||
public function processShouldBeSkippedWhenCacheIsNotFresh(): void | ||
{ | ||
$this->configCache->method('isFresh') | ||
->willReturn(false); | ||
|
||
$this->configCache->expects($this->never()) | ||
->method('write'); | ||
|
||
$pass = new DumpXmlContainer($this->configCache); | ||
$pass->process(new ContainerBuilder(new Parameters(['app.devmode' => true]))); | ||
} | ||
|
||
/** | ||
* @test | ||
* | ||
* @covers \Lcobucci\DependencyInjection\Compiler\DumpXmlContainer::__construct | ||
* @covers \Lcobucci\DependencyInjection\Compiler\DumpXmlContainer::process | ||
*/ | ||
public function processShouldDumpTheContainerUsingTheXmlDumper(): void | ||
{ | ||
$assertXmlHeader = new StringContains( | ||
'<container xmlns="http://symfony.com/schema/dic/services" ' | ||
. 'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ' | ||
. 'xsi:schemaLocation="http://symfony.com/schema/dic/services ' | ||
. 'http://symfony.com/schema/dic/services/services-1.0.xsd">' | ||
); | ||
|
||
$this->configCache->method('isFresh') | ||
->willReturn(true); | ||
|
||
$this->configCache->expects($this->once()) | ||
->method('write') | ||
->with($assertXmlHeader, []); | ||
|
||
$pass = new DumpXmlContainer($this->configCache); | ||
$pass->process(new ContainerBuilder(new Parameters(['app.devmode' => true]))); | ||
} | ||
} |