Skip to content

Commit

Permalink
test cache warmer extension
Browse files Browse the repository at this point in the history
  • Loading branch information
goetas committed Nov 19, 2017
1 parent b635851 commit cb975a4
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 6 deletions.
12 changes: 10 additions & 2 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,16 @@ private function addMetadataSection(NodeBuilder $builder)
->arrayNode('warmup')
->addDefaultsIfNotSet()
->children()
->arrayNode('directories')
->prototype('scalar')->end()
->arrayNode('paths')
->addDefaultsIfNotSet()
->children()
->arrayNode('included')
->prototype('scalar')->end()
->end()
->arrayNode('excluded')
->prototype('scalar')->end()
->end()
->end()
->end()
->end()
->end()
Expand Down
5 changes: 3 additions & 2 deletions DependencyInjection/JMSSerializerExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,11 @@ public function loadInternal(array $config, ContainerBuilder $container)
->replaceArgument(2, $config['metadata']['debug']);

// warmup
if ($config['metadata']['warmup']['directories']) {
if ($config['metadata']['warmup']['paths']['included']) {
$container
->getDefinition('jms_serializer.cache.cache_warmer')
->replaceArgument(0, $config['metadata']['warmup']['directories']);
->replaceArgument(0, $config['metadata']['warmup']['paths']['included'])
->replaceArgument(2, $config['metadata']['warmup']['paths']['excluded']);
} else {
$container->removeDefinition('jms_serializer.cache.cache_warmer');
}
Expand Down
3 changes: 2 additions & 1 deletion Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -288,8 +288,9 @@


<service id="jms_serializer.cache.cache_warmer" class="%jms_serializer.cache.cache_warmer.class%" public="false">
<argument/>
<argument/> <!-- included dirs -->
<argument type="service" id="jms_serializer.metadata_factory"/>
<argument/> <!-- excluded dirs -->
<tag name="kernel.cache_warmer" />
</service>

Expand Down
4 changes: 3 additions & 1 deletion Resources/doc/configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,9 @@ values:
path: "@MyBarBundle/Resources/config/serializer"
warmup:
# list of directories to scan searching for php classes to use when warming up the cache
directories: []
paths:
included: []
excluded: []
expression_evaluator:
id: jms_serializer.expression_evaluator # auto detected
Expand Down
31 changes: 31 additions & 0 deletions Tests/DependencyInjection/JMSSerializerExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,37 @@ public function testLoadExistentMetadataDir()
$this->assertEquals(['foo_ns' => __DIR__], $directories);
}

public function testWarmUpWithDirs()
{
$container = $this->getContainerForConfig([[
'metadata' => [
'warmup' => [
'paths' => [
'included' => ['a'],
'excluded' => ['b']
]
]
]
]], function ($container){
$container->getDefinition('jms_serializer.cache.cache_warmer')->setPublic(true);
});

$this->assertTrue($container->hasDefinition('jms_serializer.cache.cache_warmer'));

$def = $container->getDefinition('jms_serializer.cache.cache_warmer');

$this->assertEquals(['a'], $def->getArgument(0));
$this->assertEquals(['b'], $def->getArgument(2));
}

public function testWarmUpWithDirsWithNoPaths()
{
$this->getContainerForConfig([[]], function ($container) {
$this->assertFalse($container->hasDefinition('jms_serializer.cache.cache_warmer'));
});
}


/**
* @expectedException \JMS\Serializer\Exception\RuntimeException
* @expectedExceptionMessage The metadata directory "foo_dir" does not exist for the namespace "foo_ns"
Expand Down

0 comments on commit cb975a4

Please sign in to comment.