Merge branch 'configuration-fixes'

This commit is contained in:
Tim Nagel 2014-05-23 23:21:29 +10:00
commit 458b53240b
2 changed files with 211 additions and 250 deletions

View file

@ -60,16 +60,6 @@ class Configuration implements ConfigurationInterface
return $treeBuilder;
}
/**
* Generates the configuration tree.
*
* @return \Symfony\Component\DependencyInjection\Configuration\NodeInterface
*/
public function getConfigTree()
{
return $this->getConfigTreeBuilder()->buildTree();
}
/**
* Adds the configuration for the "clients" key
*/
@ -83,28 +73,17 @@ class Configuration implements ConfigurationInterface
->prototype('array')
->performNoDeepMerging()
->beforeNormalization()
->ifTrue(function($v) { return isset($v['host']) && isset($v['port']); })
->ifTrue(function($v) { return (isset($v['host']) && isset($v['port'])) || isset($v['url']); })
->then(function($v) {
return array(
'servers' => array(
array(
'host' => $v['host'],
'port' => $v['port'],
'host' => isset($v['host']) ? $v['host'] : null,
'port' => isset($v['port']) ? $v['port'] : null,
'url' => isset($v['url']) ? $v['url'] : null,
'logger' => isset($v['logger']) ? $v['logger'] : null,
'headers' => isset($v['headers']) ? $v['headers'] : null,
)
)
);
})
->end()
->beforeNormalization()
->ifTrue(function($v) { return isset($v['url']); })
->then(function($v) {
return array(
'servers' => array(
array(
'url' => $v['url'],
'logger' => isset($v['logger']) ? $v['logger'] : null
'timeout' => isset($v['timeout']) ? $v['timeout'] : null,
)
)
);
@ -170,65 +149,8 @@ class Configuration implements ConfigurationInterface
->children()
->scalarNode('index_analyzer')->end()
->scalarNode('search_analyzer')->end()
->arrayNode('persistence')
->validate()
->ifTrue(function($v) { return isset($v['driver']) && 'propel' === $v['driver'] && isset($v['listener']); })
->thenInvalid('Propel doesn\'t support listeners')
->ifTrue(function($v) { return isset($v['driver']) && 'propel' === $v['driver'] && isset($v['repository']); })
->thenInvalid('Propel doesn\'t support the "repository" parameter')
->end()
->children()
->scalarNode('driver')
->validate()
->ifNotInArray($this->supportedDrivers)
->thenInvalid('The driver %s is not supported. Please choose one of '.json_encode($this->supportedDrivers))
->end()
->end()
->scalarNode('identifier')->defaultValue('id')->end()
->arrayNode('provider')
->children()
->scalarNode('batch_size')->defaultValue(100)->end()
->scalarNode('clear_object_manager')->defaultTrue()->end()
->booleanNode('debug_logging')
->defaultValue($this->debug)
->treatNullLike($this->debug)
->end()
->scalarNode('query_builder_method')->defaultValue('createQueryBuilder')->end()
->scalarNode('service')->end()
->end()
->end()
->arrayNode('listener')
->children()
->scalarNode('insert')->defaultTrue()->end()
->scalarNode('update')->defaultTrue()->end()
->scalarNode('delete')->defaultTrue()->end()
->scalarNode('persist')->defaultValue('postFlush')->end()
->scalarNode('service')->end()
->variableNode('is_indexable_callback')->defaultNull()->end()
->end()
->end()
->arrayNode('finder')
->children()
->scalarNode('service')->end()
->end()
->end()
->arrayNode('elastica_to_model_transformer')
->addDefaultsIfNotSet()
->children()
->scalarNode('hydrate')->defaultTrue()->end()
->scalarNode('ignore_missing')->defaultFalse()->end()
->scalarNode('query_builder_method')->defaultValue('createQueryBuilder')->end()
->scalarNode('service')->end()
->end()
->end()
->arrayNode('model_to_elastica_transformer')
->addDefaultsIfNotSet()
->children()
->scalarNode('service')->end()
->end()
->end()
->end()
->end()
->append($this->getPersistenceNode())
->append($this->getSerializerNode())
->end()
->end()
->variableNode('settings')->defaultValue(array())->end()
@ -253,81 +175,10 @@ class Configuration implements ConfigurationInterface
->prototype('array')
->treatNullLike(array())
->children()
->arrayNode('serializer')
->addDefaultsIfNotSet()
->children()
->arrayNode('groups')
->treatNullLike(array())
->prototype('scalar')->end()
->end()
->scalarNode('version')->end()
->end()
->end()
->scalarNode('index_analyzer')->end()
->scalarNode('search_analyzer')->end()
->arrayNode('persistence')
->validate()
->ifTrue(function($v) { return isset($v['driver']) && 'propel' === $v['driver'] && isset($v['listener']); })
->thenInvalid('Propel doesn\'t support listeners')
->ifTrue(function($v) { return isset($v['driver']) && 'propel' === $v['driver'] && isset($v['repository']); })
->thenInvalid('Propel doesn\'t support the "repository" parameter')
->end()
->children()
->scalarNode('driver')
->validate()
->ifNotInArray($this->supportedDrivers)
->thenInvalid('The driver %s is not supported. Please choose one of '.json_encode($this->supportedDrivers))
->end()
->end()
->scalarNode('model')->end()
->scalarNode('repository')->end()
->scalarNode('identifier')->defaultValue('id')->end()
->arrayNode('provider')
->children()
->scalarNode('query_builder_method')->defaultValue('createQueryBuilder')->end()
->scalarNode('batch_size')->defaultValue(100)->end()
->scalarNode('clear_object_manager')->defaultTrue()->end()
->scalarNode('service')->end()
->end()
->end()
->arrayNode('listener')
->children()
->scalarNode('insert')->defaultTrue()->end()
->scalarNode('update')->defaultTrue()->end()
->scalarNode('delete')->defaultTrue()->end()
->scalarNode('flush')->defaultTrue()->end()
->booleanNode('immediate')->defaultFalse()->end()
->scalarNode('logger')
->defaultFalse()
->treatNullLike('fos_elastica.logger')
->treatTrueLike('fos_elastica.logger')
->end()
->scalarNode('service')->end()
->variableNode('is_indexable_callback')->defaultNull()->end()
->end()
->end()
->arrayNode('finder')
->children()
->scalarNode('service')->end()
->end()
->end()
->arrayNode('elastica_to_model_transformer')
->addDefaultsIfNotSet()
->children()
->scalarNode('hydrate')->defaultTrue()->end()
->scalarNode('ignore_missing')->defaultFalse()->end()
->scalarNode('query_builder_method')->defaultValue('createQueryBuilder')->end()
->scalarNode('service')->end()
->end()
->end()
->arrayNode('model_to_elastica_transformer')
->addDefaultsIfNotSet()
->children()
->scalarNode('service')->end()
->end()
->end()
->end()
->end()
->append($this->getPersistenceNode())
->append($this->getSerializerNode())
->end()
->append($this->getIdNode())
->append($this->getMappingsNode())
@ -763,4 +614,98 @@ class Configuration implements ConfigurationInterface
return $node;
}
/**
* @return ArrayNodeDefinition|\Symfony\Component\Config\Definition\Builder\NodeDefinition
*/
protected function getPersistenceNode()
{
$builder = new TreeBuilder();
$node = $builder->root('persistence');
$node
->validate()
->ifTrue(function($v) { return isset($v['driver']) && 'propel' === $v['driver'] && isset($v['listener']); })
->thenInvalid('Propel doesn\'t support listeners')
->ifTrue(function($v) { return isset($v['driver']) && 'propel' === $v['driver'] && isset($v['repository']); })
->thenInvalid('Propel doesn\'t support the "repository" parameter')
->end()
->children()
->scalarNode('driver')
->validate()
->ifNotInArray($this->supportedDrivers)
->thenInvalid('The driver %s is not supported. Please choose one of '.json_encode($this->supportedDrivers))
->end()
->end()
->scalarNode('model')->end()
->scalarNode('repository')->end()
->scalarNode('identifier')->defaultValue('id')->end()
->arrayNode('provider')
->children()
->scalarNode('query_builder_method')->defaultValue('createQueryBuilder')->end()
->scalarNode('batch_size')->defaultValue(100)->end()
->scalarNode('clear_object_manager')->defaultTrue()->end()
->scalarNode('service')->end()
->end()
->end()
->arrayNode('listener')
->children()
->scalarNode('insert')->defaultTrue()->end()
->scalarNode('update')->defaultTrue()->end()
->scalarNode('delete')->defaultTrue()->end()
->booleanNode('immediate')->defaultFalse()->end()
->scalarNode('logger')
->defaultFalse()
->treatNullLike('fos_elastica.logger')
->treatTrueLike('fos_elastica.logger')
->end()
->scalarNode('service')->end()
->variableNode('is_indexable_callback')->defaultNull()->end()
->end()
->end()
->arrayNode('finder')
->children()
->scalarNode('service')->end()
->end()
->end()
->arrayNode('elastica_to_model_transformer')
->addDefaultsIfNotSet()
->children()
->scalarNode('hydrate')->defaultTrue()->end()
->scalarNode('ignore_missing')->defaultFalse()->end()
->scalarNode('query_builder_method')->defaultValue('createQueryBuilder')->end()
->scalarNode('service')->end()
->end()
->end()
->arrayNode('model_to_elastica_transformer')
->addDefaultsIfNotSet()
->children()
->scalarNode('service')->end()
->end()
->end()
->end();
return $node;
}
/**
* @return ArrayNodeDefinition|\Symfony\Component\Config\Definition\Builder\NodeDefinition
*/
protected function getSerializerNode()
{
$builder = new TreeBuilder();
$node = $builder->root('serializer');
$node
->addDefaultsIfNotSet()
->children()
->arrayNode('groups')
->treatNullLike(array())
->prototype('scalar')->end()
->end()
->scalarNode('version')->end()
->end();
return $node;
}
}

View file

@ -11,141 +11,157 @@ use Symfony\Component\Config\Definition\Processor;
class ConfigurationTest extends \PHPUnit_Framework_TestCase
{
/**
* @var Configuration
* @var Processor
*/
private $configuration;
private $processor;
public function setUp()
{
$this->configuration = new Configuration(array(), false);
$this->processor = new Processor();
}
public function testEmptyConfigContainsFormatMappingOptionNode()
private function getConfigs(array $configArray)
{
$tree = $this->configuration->getConfigTree();
$children = $tree->getChildren();
$children = $children['indexes']->getPrototype()->getChildren();
$typeNodes = $children['types']->getPrototype()->getChildren();
$mappings = $typeNodes['mappings']->getPrototype()->getChildren();
$configuration = new Configuration($configArray, true);
$this->assertArrayHasKey('format', $mappings);
$this->assertInstanceOf('Symfony\Component\Config\Definition\ScalarNode', $mappings['format']);
$this->assertNull($mappings['format']->getDefaultValue());
return $this->processor->processConfiguration($configuration, array($configArray));
}
public function testDynamicTemplateNodes()
public function testUnconfiguredConfiguration()
{
$tree = $this->configuration->getConfigTree();
$children = $tree->getChildren();
$children = $children['indexes']->getPrototype()->getChildren();
$typeNodes = $children['types']->getPrototype()->getChildren();
$dynamicTemplates = $typeNodes['dynamic_templates']->getPrototype()->getChildren();
$configuration = $this->getConfigs(array());
$this->assertArrayHasKey('match', $dynamicTemplates);
$this->assertInstanceOf('Symfony\Component\Config\Definition\ScalarNode', $dynamicTemplates['match']);
$this->assertNull($dynamicTemplates['match']->getDefaultValue());
$this->assertArrayHasKey('match_mapping_type', $dynamicTemplates);
$this->assertInstanceOf('Symfony\Component\Config\Definition\ScalarNode', $dynamicTemplates['match_mapping_type']);
$this->assertNull($dynamicTemplates['match_mapping_type']->getDefaultValue());
$this->assertArrayHasKey('unmatch', $dynamicTemplates);
$this->assertInstanceOf('Symfony\Component\Config\Definition\ScalarNode', $dynamicTemplates['unmatch']);
$this->assertNull($dynamicTemplates['unmatch']->getDefaultValue());
$this->assertArrayHasKey('path_match', $dynamicTemplates);
$this->assertInstanceOf('Symfony\Component\Config\Definition\ScalarNode', $dynamicTemplates['path_match']);
$this->assertNull($dynamicTemplates['path_match']->getDefaultValue());
$this->assertArrayHasKey('path_unmatch', $dynamicTemplates);
$this->assertInstanceOf('Symfony\Component\Config\Definition\ScalarNode', $dynamicTemplates['path_unmatch']);
$this->assertNull($dynamicTemplates['path_unmatch']->getDefaultValue());
$this->assertArrayHasKey('match_pattern', $dynamicTemplates);
$this->assertInstanceOf('Symfony\Component\Config\Definition\ScalarNode', $dynamicTemplates['match_pattern']);
$this->assertNull($dynamicTemplates['match_pattern']->getDefaultValue());
$this->assertArrayHasKey('mapping', $dynamicTemplates);
$this->assertInstanceOf('Symfony\Component\Config\Definition\ArrayNode', $dynamicTemplates['mapping']);
$this->assertSame(array(
'clients' => array(),
'indexes' => array(),
'default_manager' => 'orm'
), $configuration);
}
public function testDynamicTemplateMappingNodes()
public function testClientConfiguration()
{
$tree = $this->configuration->getConfigTree();
$children = $tree->getChildren();
$children = $children['indexes']->getPrototype()->getChildren();
$typeNodes = $children['types']->getPrototype()->getChildren();
$dynamicTemplates = $typeNodes['dynamic_templates']->getPrototype()->getChildren();
$mapping = $dynamicTemplates['mapping']->getChildren();
$configuration = $this->getConfigs(array(
'clients' => array(
'default' => array(
'url' => 'http://localhost:9200',
),
'clustered' => array(
'servers' => array(
array(
'url' => 'http://es1:9200',
'headers' => array(
'Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ=='
)
),
array(
'url' => 'http://es2:9200',
'headers' => array(
'Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ=='
)
),
)
)
)
));
$this->assertArrayHasKey('type', $mapping);
$this->assertInstanceOf('Symfony\Component\Config\Definition\ScalarNode', $mapping['type']);
$this->assertSame('string', $mapping['type']->getDefaultValue());
$this->assertCount(2, $configuration['clients']);
$this->assertCount(1, $configuration['clients']['default']['servers']);
$this->assertCount(0, $configuration['clients']['default']['servers'][0]['headers']);
$this->assertArrayHasKey('index', $mapping);
$this->assertInstanceOf('Symfony\Component\Config\Definition\ScalarNode', $mapping['index']);
$this->assertNull($mapping['index']->getDefaultValue());
$this->assertCount(2, $configuration['clients']['clustered']['servers']);
$this->assertEquals('http://es2:9200/', $configuration['clients']['clustered']['servers'][1]['url']);
$this->assertCount(1, $configuration['clients']['clustered']['servers'][1]['headers']);
$this->assertEquals('Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==', $configuration['clients']['clustered']['servers'][0]['headers'][0]);
}
public function testLogging()
{
$configuration = $this->getConfigs(array(
'clients' => array(
'logging_enabled' => array(
'url' => 'http://localhost:9200',
'logger' => true,
),
'logging_disabled' => array(
'url' => 'http://localhost:9200',
'logger' => false,
),
'logging_not_mentioned' => array(
'url' => 'http://localhost:9200',
),
'logging_custom' => array(
'url' => 'http://localhost:9200',
'logger' => 'custom.service'
),
)
));
$this->assertCount(4, $configuration['clients']);
$this->assertEquals('fos_elastica.logger', $configuration['clients']['logging_enabled']['servers'][0]['logger']);
$this->assertFalse($configuration['clients']['logging_disabled']['servers'][0]['logger']);
$this->assertEquals('fos_elastica.logger', $configuration['clients']['logging_not_mentioned']['servers'][0]['logger']);
$this->assertEquals('custom.service', $configuration['clients']['logging_custom']['servers'][0]['logger']);
}
public function testSlashIsAddedAtTheEndOfServerUrl()
{
$config = array(
'clients' => array(
'default' => array(
'url' => 'http://www.github.com',
),
'default' => array('url' => 'http://www.github.com'),
),
);
$processor = new Processor();
$configuration = $processor->processConfiguration($this->configuration, array($config));
);
$configuration = $this->getConfigs($config);
$this->assertEquals('http://www.github.com/', $configuration['clients']['default']['servers'][0]['url']);
}
public function testEmptyFieldsIndexIsUnset()
public function testTypeConfig()
{
$config = array(
$configuration = $this->getConfigs(array(
'clients' => array(
'default' => array('url' => 'http://localhost:9200'),
),
'indexes' => array(
'test' => array(
'type_prototype' => array(
'index_analyzer' => 'custom_analyzer',
'persistence' => array(
'identifier' => 'ID',
),
'serializer' => array(
'groups' => array('Search'),
'version' => 1
)
),
'types' => array(
'test' => array(
'mappings' => array(
'title' => array(
'type' => 'string',
'fields' => array(
'autocomplete' => null
)
),
'content' => null,
'title' => array(),
'published' => array('type' => 'datetime'),
'body' => null,
),
'persistence' => array(
'listener' => array(
'logger' => true,
)
)
),
'test2' => array(
'mappings' => array(
'title' => null,
'children' => array(
'type' => 'nested',
'properties' => array(
'title' => array(
'type' => 'string',
'fields' => array(
'autocomplete' => null
)
),
'content' => null
)
)
)
)
)
)
)
);
));
$processor = new Processor();
$configuration = $processor->processConfiguration(new Configuration(array($config), false), array($config));
$this->assertArrayNotHasKey('fields', $configuration['indexes']['test']['types']['test']['mappings']['content']);
$this->assertArrayHasKey('fields', $configuration['indexes']['test']['types']['test']['mappings']['title']);
$this->assertArrayNotHasKey('fields', $configuration['indexes']['test']['types']['test']['mappings']['children']['properties']['content']);
$this->assertArrayHasKey('fields', $configuration['indexes']['test']['types']['test']['mappings']['children']['properties']['title']);
$this->assertEquals('string', $configuration['indexes']['test']['types']['test']['mappings']['title']['type']);
$this->assertTrue($configuration['indexes']['test']['types']['test']['mappings']['title']['include_in_all']);
}
public function testEmptyPropertiesIndexIsUnset()