Skip to content

Commit

Permalink
Merge pull request #11 from lcobucci/dump-xml-container
Browse files Browse the repository at this point in the history
Provide compiler pass for XML dumping
  • Loading branch information
lcobucci authored Jan 11, 2018
2 parents ace3cec + 874813f commit b89022f
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 0 deletions.
32 changes: 32 additions & 0 deletions src/Compiler/DumpXmlContainer.php
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());
}
}
89 changes: 89 additions & 0 deletions test/Compiler/DumpXmlContainerTest.php
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])));
}
}

0 comments on commit b89022f

Please sign in to comment.