ConfigurationTest.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Bundle\DoctrineMongoDBBundle\Tests\DependencyInjection;
  11. use Symfony\Component\DependencyInjection\ContainerBuilder;
  12. use Symfony\Bundle\DoctrineMongoDBBundle\DependencyInjection\Configuration;
  13. use Symfony\Component\Config\Definition\Processor;
  14. class ConfigurationTest extends \PHPUnit_Framework_TestCase
  15. {
  16. /**
  17. * @dataProvider optionProvider
  18. * @param array $configs The source array of configuration arrays
  19. * @param array $correctValues A key-value pair of end values to check
  20. */
  21. public function testMergeOptions(array $configs, array $correctValues)
  22. {
  23. $processor = new Processor();
  24. $configuration = new Configuration();
  25. $options = $processor->process($configuration->getConfigTree(), $configs);
  26. foreach ($correctValues as $key => $correctVal)
  27. {
  28. $this->assertEquals($correctVal, $options[$key]);
  29. }
  30. }
  31. public function optionProvider()
  32. {
  33. $cases = array();
  34. // single config, testing normal option setting
  35. $cases[] = array(
  36. array(
  37. array('default_document_manager' => 'foo'),
  38. ),
  39. array('default_document_manager' => 'foo')
  40. );
  41. // single config, testing normal option setting with dashes
  42. $cases[] = array(
  43. array(
  44. array('default-document-manager' => 'bar'),
  45. ),
  46. array('default_document_manager' => 'bar')
  47. );
  48. // testing the normal override merging - the later config array wins
  49. $cases[] = array(
  50. array(
  51. array('default_document_manager' => 'foo'),
  52. array('default_document_manager' => 'baz'),
  53. ),
  54. array('default_document_manager' => 'baz')
  55. );
  56. // the "options" array is totally replaced
  57. $cases[] = array(
  58. array(
  59. array('options' => array('timeout' => 2000)),
  60. array('options' => array('username' => 'foo')),
  61. ),
  62. array('options' => array('username' => 'foo')),
  63. );
  64. // mappings are merged non-recursively.
  65. $cases[] = array(
  66. array(
  67. array('mappings' => array('foomap' => array('type' => 'val1'), 'barmap' => array('dir' => 'val2'))),
  68. array('mappings' => array('barmap' => array('prefix' => 'val3'))),
  69. ),
  70. array('mappings' => array('foomap' => array('type' => 'val1'), 'barmap' => array('prefix' => 'val3'))),
  71. );
  72. // connections are merged non-recursively.
  73. $cases[] = array(
  74. array(
  75. array('connections' => array('foocon' => array('server' => 'val1'), 'barcon' => array('options' => array('username' => 'val2')))),
  76. array('connections' => array('barcon' => array('server' => 'val3'))),
  77. ),
  78. array('connections' => array(
  79. 'foocon' => array('server' => 'val1', 'options' => array()),
  80. 'barcon' => array('server' => 'val3', 'options' => array())
  81. )),
  82. );
  83. // managers are merged non-recursively.
  84. $cases[] = array(
  85. array(
  86. array('document_managers' => array('foodm' => array('database' => 'val1'), 'bardm' => array('default_database' => 'val2'))),
  87. array('document_managers' => array('bardm' => array('database' => 'val3'))),
  88. ),
  89. array('document_managers' => array(
  90. 'foodm' => array('database' => 'val1', 'mappings' => array()),
  91. 'bardm' => array('database' => 'val3', 'mappings' => array()),
  92. )),
  93. );
  94. return $cases;
  95. }
  96. /**
  97. * @dataProvider getNormalizationTests
  98. */
  99. public function testNormalizeOptions(array $config, $targetKey, array $normalized)
  100. {
  101. $processor = new Processor();
  102. $configuration = new Configuration();
  103. $options = $processor->process($configuration->getConfigTree(), array($config));
  104. $this->assertSame($normalized, $options[$targetKey]);
  105. }
  106. public function getNormalizationTests()
  107. {
  108. return array(
  109. // connection versus connections (id is the identifier)
  110. array(
  111. array('connection' => array(
  112. array('server' => 'mongodb://abc', 'id' => 'foo'),
  113. array('server' => 'mongodb://def', 'id' => 'bar'),
  114. )),
  115. 'connections',
  116. array(
  117. 'foo' => array('server' => 'mongodb://abc', 'options' => array()),
  118. 'bar' => array('server' => 'mongodb://def', 'options' => array()),
  119. ),
  120. ),
  121. // document_manager versus document_managers (id is the identifier)
  122. array(
  123. array('document_manager' => array(
  124. array('connection' => 'conn1', 'id' => 'foo'),
  125. array('connection' => 'conn2', 'id' => 'bar'),
  126. )),
  127. 'document_managers',
  128. array(
  129. 'foo' => array('connection' => 'conn1', 'mappings' => array()),
  130. 'bar' => array('connection' => 'conn2', 'mappings' => array()),
  131. ),
  132. ),
  133. // mapping versus mappings (name is the identifier)
  134. array(
  135. array('mapping' => array(
  136. array('type' => 'yml', 'name' => 'foo'),
  137. array('type' => 'xml', 'name' => 'bar'),
  138. )),
  139. 'mappings',
  140. array(
  141. 'foo' => array('type' => 'yml'),
  142. 'bar' => array('type' => 'xml'),
  143. ),
  144. ),
  145. // mapping configuration that's beneath a specific document manager
  146. array(
  147. array('document_manager' => array(
  148. array('id' => 'foo', 'connection' => 'conn1', 'mapping' => array(
  149. 'type' => 'xml', 'name' => 'foo-mapping'
  150. )),
  151. )),
  152. 'document_managers',
  153. array(
  154. 'foo' => array('connection' => 'conn1', 'mappings' => array(
  155. 'foo-mapping' => array('type' => 'xml'),
  156. )),
  157. ),
  158. ),
  159. );
  160. }
  161. }