FrameworkExtensionTest.php 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.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\FrameworkBundle\Tests\DependencyInjection;
  11. use Symfony\Bundle\FrameworkBundle\DependencyInjection\FrameworkExtension;
  12. use Symfony\Bundle\FrameworkBundle\Tests\TestCase;
  13. use Symfony\Component\DependencyInjection\ContainerBuilder;
  14. use Symfony\Component\DependencyInjection\DefinitionDecorator;
  15. use Symfony\Component\DependencyInjection\Loader\ClosureLoader;
  16. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
  17. use Symfony\Component\DependencyInjection\Reference;
  18. abstract class FrameworkExtensionTest extends TestCase
  19. {
  20. private static $containerCache = array();
  21. abstract protected function loadFromFile(ContainerBuilder $container, $file);
  22. public function testCsrfProtection()
  23. {
  24. $container = $this->createContainerFromFile('full');
  25. $def = $container->getDefinition('form.type_extension.csrf');
  26. $this->assertTrue($container->getParameter('form.type_extension.csrf.enabled'));
  27. $this->assertEquals('%form.type_extension.csrf.enabled%', $def->getArgument(1));
  28. $this->assertEquals('_csrf', $container->getParameter('form.type_extension.csrf.field_name'));
  29. $this->assertEquals('%form.type_extension.csrf.field_name%', $def->getArgument(2));
  30. }
  31. public function testPropertyAccessWithDefaultValue()
  32. {
  33. $container = $this->createContainerFromFile('full');
  34. $def = $container->getDefinition('property_accessor');
  35. $this->assertFalse($def->getArgument(0));
  36. $this->assertFalse($def->getArgument(1));
  37. }
  38. public function testPropertyAccessWithOverriddenValues()
  39. {
  40. $container = $this->createContainerFromFile('property_accessor');
  41. $def = $container->getDefinition('property_accessor');
  42. $this->assertTrue($def->getArgument(0));
  43. $this->assertTrue($def->getArgument(1));
  44. }
  45. /**
  46. * @expectedException \LogicException
  47. * @expectedExceptionMessage CSRF protection needs sessions to be enabled.
  48. */
  49. public function testCsrfProtectionNeedsSessionToBeEnabled()
  50. {
  51. $this->createContainerFromFile('csrf_needs_session');
  52. }
  53. public function testCsrfProtectionForFormsEnablesCsrfProtectionAutomatically()
  54. {
  55. $container = $this->createContainerFromFile('csrf');
  56. $this->assertTrue($container->hasDefinition('security.csrf.token_manager'));
  57. }
  58. /** @group legacy */
  59. public function testSecureRandomIsAvailableIfCsrfIsDisabled()
  60. {
  61. $container = $this->createContainerFromFile('csrf_disabled');
  62. $this->assertTrue($container->hasDefinition('security.secure_random'));
  63. }
  64. public function testProxies()
  65. {
  66. $container = $this->createContainerFromFile('full');
  67. $this->assertEquals(array('127.0.0.1', '10.0.0.1'), $container->getParameter('kernel.trusted_proxies'));
  68. }
  69. public function testHttpMethodOverride()
  70. {
  71. $container = $this->createContainerFromFile('full');
  72. $this->assertFalse($container->getParameter('kernel.http_method_override'));
  73. }
  74. public function testEsi()
  75. {
  76. $container = $this->createContainerFromFile('full');
  77. $this->assertTrue($container->hasDefinition('esi'), '->registerEsiConfiguration() loads esi.xml');
  78. $this->assertTrue($container->hasDefinition('fragment.renderer.esi'));
  79. }
  80. public function testEsiInactive()
  81. {
  82. $container = $this->createContainerFromFile('default_config');
  83. $this->assertFalse($container->hasDefinition('fragment.renderer.esi'));
  84. $this->assertFalse($container->hasDefinition('esi'));
  85. }
  86. public function testSsi()
  87. {
  88. $container = $this->createContainerFromFile('full');
  89. $this->assertTrue($container->hasDefinition('ssi'), '->registerSsiConfiguration() loads ssi.xml');
  90. $this->assertTrue($container->hasDefinition('fragment.renderer.ssi'));
  91. }
  92. public function testSsiInactive()
  93. {
  94. $container = $this->createContainerFromFile('default_config');
  95. $this->assertFalse($container->hasDefinition('fragment.renderer.ssi'));
  96. $this->assertFalse($container->hasDefinition('ssi'));
  97. }
  98. public function testEnabledProfiler()
  99. {
  100. $container = $this->createContainerFromFile('profiler');
  101. $this->assertTrue($container->hasDefinition('profiler'), '->registerProfilerConfiguration() loads profiling.xml');
  102. $this->assertTrue($container->hasDefinition('data_collector.config'), '->registerProfilerConfiguration() loads collectors.xml');
  103. }
  104. public function testDisabledProfiler()
  105. {
  106. $container = $this->createContainerFromFile('full');
  107. $this->assertFalse($container->hasDefinition('profiler'), '->registerProfilerConfiguration() does not load profiling.xml');
  108. $this->assertFalse($container->hasDefinition('data_collector.config'), '->registerProfilerConfiguration() does not load collectors.xml');
  109. }
  110. public function testRouter()
  111. {
  112. $container = $this->createContainerFromFile('full');
  113. $this->assertTrue($container->has('router'), '->registerRouterConfiguration() loads routing.xml');
  114. $arguments = $container->findDefinition('router')->getArguments();
  115. $this->assertEquals($container->getParameter('kernel.root_dir').'/config/routing.xml', $container->getParameter('router.resource'), '->registerRouterConfiguration() sets routing resource');
  116. $this->assertEquals('%router.resource%', $arguments[1], '->registerRouterConfiguration() sets routing resource');
  117. $this->assertEquals('xml', $arguments[2]['resource_type'], '->registerRouterConfiguration() sets routing resource type');
  118. }
  119. /**
  120. * @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException
  121. */
  122. public function testRouterRequiresResourceOption()
  123. {
  124. $container = $this->createContainer();
  125. $loader = new FrameworkExtension();
  126. $loader->load(array(array('router' => true)), $container);
  127. }
  128. public function testSession()
  129. {
  130. $container = $this->createContainerFromFile('full');
  131. $this->assertTrue($container->hasDefinition('session'), '->registerSessionConfiguration() loads session.xml');
  132. $this->assertEquals('fr', $container->getParameter('kernel.default_locale'));
  133. $this->assertEquals('session.storage.native', (string) $container->getAlias('session.storage'));
  134. $this->assertEquals('session.handler.native_file', (string) $container->getAlias('session.handler'));
  135. $options = $container->getParameter('session.storage.options');
  136. $this->assertEquals('_SYMFONY', $options['name']);
  137. $this->assertEquals(86400, $options['cookie_lifetime']);
  138. $this->assertEquals('/', $options['cookie_path']);
  139. $this->assertEquals('example.com', $options['cookie_domain']);
  140. $this->assertTrue($options['cookie_secure']);
  141. $this->assertFalse($options['cookie_httponly']);
  142. $this->assertTrue($options['use_cookies']);
  143. $this->assertEquals(108, $options['gc_divisor']);
  144. $this->assertEquals(1, $options['gc_probability']);
  145. $this->assertEquals(90000, $options['gc_maxlifetime']);
  146. $this->assertEquals('/path/to/sessions', $container->getParameter('session.save_path'));
  147. }
  148. public function testNullSessionHandler()
  149. {
  150. $container = $this->createContainerFromFile('session');
  151. $this->assertTrue($container->hasDefinition('session'), '->registerSessionConfiguration() loads session.xml');
  152. $this->assertNull($container->getDefinition('session.storage.native')->getArgument(1));
  153. $this->assertNull($container->getDefinition('session.storage.php_bridge')->getArgument(0));
  154. }
  155. public function testRequest()
  156. {
  157. $container = $this->createContainerFromFile('full');
  158. $this->assertTrue($container->hasDefinition('request.add_request_formats_listener'), '->registerRequestConfiguration() loads request.xml');
  159. $listenerDef = $container->getDefinition('request.add_request_formats_listener');
  160. $this->assertEquals(array('csv' => array('text/csv', 'text/plain'), 'pdf' => array('application/pdf')), $listenerDef->getArgument(0));
  161. }
  162. public function testEmptyRequestFormats()
  163. {
  164. $container = $this->createContainerFromFile('request');
  165. $this->assertFalse($container->hasDefinition('request.add_request_formats_listener'), '->registerRequestConfiguration() does not load request.xml when no request formats are defined');
  166. }
  167. public function testTemplating()
  168. {
  169. $container = $this->createContainerFromFile('full');
  170. $this->assertTrue($container->hasDefinition('templating.name_parser'), '->registerTemplatingConfiguration() loads templating.xml');
  171. $this->assertEquals('templating.engine.delegating', (string) $container->getAlias('templating'), '->registerTemplatingConfiguration() configures delegating loader if multiple engines are provided');
  172. $this->assertEquals($container->getDefinition('templating.loader.chain'), $container->getDefinition('templating.loader.wrapped'), '->registerTemplatingConfiguration() configures loader chain if multiple loaders are provided');
  173. $this->assertEquals($container->getDefinition('templating.loader'), $container->getDefinition('templating.loader.cache'), '->registerTemplatingConfiguration() configures the loader to use cache');
  174. $this->assertEquals('%templating.loader.cache.path%', $container->getDefinition('templating.loader.cache')->getArgument(1));
  175. $this->assertEquals('/path/to/cache', $container->getParameter('templating.loader.cache.path'));
  176. $this->assertEquals(array('php', 'twig'), $container->getParameter('templating.engines'), '->registerTemplatingConfiguration() sets a templating.engines parameter');
  177. $this->assertEquals(array('FrameworkBundle:Form', 'theme1', 'theme2'), $container->getParameter('templating.helper.form.resources'), '->registerTemplatingConfiguration() registers the theme and adds the base theme');
  178. $this->assertEquals('global_hinclude_template', $container->getParameter('fragment.renderer.hinclude.global_template'), '->registerTemplatingConfiguration() registers the global hinclude.js template');
  179. }
  180. /**
  181. * @group legacy
  182. */
  183. public function testLegacyTemplatingAssets()
  184. {
  185. $this->checkAssetsPackages($this->createContainerFromFile('legacy_templating_assets'), true);
  186. }
  187. public function testAssets()
  188. {
  189. $this->checkAssetsPackages($this->createContainerFromFile('assets'));
  190. }
  191. public function testTranslator()
  192. {
  193. $container = $this->createContainerFromFile('full');
  194. $this->assertTrue($container->hasDefinition('translator.default'), '->registerTranslatorConfiguration() loads translation.xml');
  195. $this->assertEquals('translator.default', (string) $container->getAlias('translator'), '->registerTranslatorConfiguration() redefines translator service from identity to real translator');
  196. $options = $container->getDefinition('translator.default')->getArgument(3);
  197. $files = array_map('realpath', $options['resource_files']['en']);
  198. $ref = new \ReflectionClass('Symfony\Component\Validator\Validation');
  199. $this->assertContains(
  200. strtr(\dirname($ref->getFileName()).'/Resources/translations/validators.en.xlf', '/', \DIRECTORY_SEPARATOR),
  201. $files,
  202. '->registerTranslatorConfiguration() finds Validator translation resources'
  203. );
  204. $ref = new \ReflectionClass('Symfony\Component\Form\Form');
  205. $this->assertContains(
  206. strtr(\dirname($ref->getFileName()).'/Resources/translations/validators.en.xlf', '/', \DIRECTORY_SEPARATOR),
  207. $files,
  208. '->registerTranslatorConfiguration() finds Form translation resources'
  209. );
  210. $ref = new \ReflectionClass('Symfony\Component\Security\Core\Security');
  211. $this->assertContains(
  212. strtr(\dirname($ref->getFileName()).'/Resources/translations/security.en.xlf', '/', \DIRECTORY_SEPARATOR),
  213. $files,
  214. '->registerTranslatorConfiguration() finds Security translation resources'
  215. );
  216. $this->assertContains(
  217. strtr(__DIR__.'/Fixtures/translations/test_paths.en.yml', '/', \DIRECTORY_SEPARATOR),
  218. $files,
  219. '->registerTranslatorConfiguration() finds translation resources in custom paths'
  220. );
  221. $calls = $container->getDefinition('translator.default')->getMethodCalls();
  222. $this->assertEquals(array('fr'), $calls[1][1][0]);
  223. }
  224. public function testTranslatorMultipleFallbacks()
  225. {
  226. $container = $this->createContainerFromFile('translator_fallbacks');
  227. $calls = $container->getDefinition('translator.default')->getMethodCalls();
  228. $this->assertEquals(array('en', 'fr'), $calls[1][1][0]);
  229. }
  230. /**
  231. * @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException
  232. */
  233. public function testTemplatingRequiresAtLeastOneEngine()
  234. {
  235. $container = $this->createContainer();
  236. $loader = new FrameworkExtension();
  237. $loader->load(array(array('templating' => null)), $container);
  238. }
  239. public function testValidation()
  240. {
  241. $container = $this->createContainerFromFile('full');
  242. $ref = new \ReflectionClass('Symfony\Component\Form\Form');
  243. $xmlMappings = array(\dirname($ref->getFileName()).'/Resources/config/validation.xml');
  244. $calls = $container->getDefinition('validator.builder')->getMethodCalls();
  245. $this->assertCount(6, $calls);
  246. $this->assertSame('setConstraintValidatorFactory', $calls[0][0]);
  247. $this->assertEquals(array(new Reference('validator.validator_factory')), $calls[0][1]);
  248. $this->assertSame('setTranslator', $calls[1][0]);
  249. $this->assertEquals(array(new Reference('translator')), $calls[1][1]);
  250. $this->assertSame('setTranslationDomain', $calls[2][0]);
  251. $this->assertSame(array('%validator.translation_domain%'), $calls[2][1]);
  252. $this->assertSame('addXmlMappings', $calls[3][0]);
  253. $this->assertSame(array($xmlMappings), $calls[3][1]);
  254. $this->assertSame('addMethodMapping', $calls[4][0]);
  255. $this->assertSame(array('loadValidatorMetadata'), $calls[4][1]);
  256. $this->assertSame('setMetadataCache', $calls[5][0]);
  257. $this->assertEquals(array(new Reference('validator.mapping.cache.doctrine.apc')), $calls[5][1]);
  258. }
  259. /**
  260. * @group legacy
  261. * @requires extension apc
  262. */
  263. public function testLegacyFullyConfiguredValidationService()
  264. {
  265. $container = $this->createContainerFromFile('full');
  266. $this->assertInstanceOf('Symfony\Component\Validator\Validator\ValidatorInterface', $container->get('validator'));
  267. }
  268. public function testValidationService()
  269. {
  270. $container = $this->createContainerFromFile('validation_annotations');
  271. $this->assertInstanceOf('Symfony\Component\Validator\Validator\ValidatorInterface', $container->get('validator'));
  272. }
  273. public function testAnnotations()
  274. {
  275. $container = $this->createContainerFromFile('full');
  276. $this->assertEquals($container->getParameter('kernel.cache_dir').'/annotations', $container->getDefinition('annotations.filesystem_cache')->getArgument(0));
  277. $this->assertSame('annotations.cached_reader', (string) $container->getAlias('annotation_reader'));
  278. $this->assertSame('annotations.filesystem_cache', (string) $container->getDefinition('annotations.cached_reader')->getArgument(1));
  279. }
  280. public function testFileLinkFormat()
  281. {
  282. if (ini_get('xdebug.file_link_format') || get_cfg_var('xdebug.file_link_format')) {
  283. $this->markTestSkipped('A custom file_link_format is defined.');
  284. }
  285. $container = $this->createContainerFromFile('full');
  286. $this->assertEquals('file%link%format', $container->getParameter('templating.helper.code.file_link_format'));
  287. }
  288. public function testValidationAnnotations()
  289. {
  290. $container = $this->createContainerFromFile('validation_annotations');
  291. $calls = $container->getDefinition('validator.builder')->getMethodCalls();
  292. $this->assertCount(6, $calls);
  293. $this->assertSame('enableAnnotationMapping', $calls[4][0]);
  294. $this->assertEquals(array(new Reference('annotation_reader')), $calls[4][1]);
  295. $this->assertSame('addMethodMapping', $calls[5][0]);
  296. $this->assertSame(array('loadValidatorMetadata'), $calls[5][1]);
  297. // no cache this time
  298. }
  299. public function testValidationPaths()
  300. {
  301. require_once __DIR__.'/Fixtures/TestBundle/TestBundle.php';
  302. $container = $this->createContainerFromFile('validation_annotations', array(
  303. 'kernel.bundles' => array('TestBundle' => 'Symfony\\Bundle\\FrameworkBundle\\Tests\\TestBundle'),
  304. 'kernel.bundles_metadata' => array('TestBundle' => array('namespace' => 'Symfony\\Bundle\\FrameworkBundle\\Tests', 'parent' => null, 'path' => __DIR__.'/Fixtures/TestBundle')),
  305. ));
  306. $calls = $container->getDefinition('validator.builder')->getMethodCalls();
  307. $this->assertCount(7, $calls);
  308. $this->assertSame('addXmlMappings', $calls[3][0]);
  309. $this->assertSame('addYamlMappings', $calls[4][0]);
  310. $this->assertSame('enableAnnotationMapping', $calls[5][0]);
  311. $this->assertSame('addMethodMapping', $calls[6][0]);
  312. $this->assertSame(array('loadValidatorMetadata'), $calls[6][1]);
  313. $xmlMappings = $calls[3][1][0];
  314. $this->assertCount(2, $xmlMappings);
  315. try {
  316. // Testing symfony/symfony
  317. $this->assertStringEndsWith('Component'.\DIRECTORY_SEPARATOR.'Form/Resources/config/validation.xml', $xmlMappings[0]);
  318. } catch (\Exception $e) {
  319. // Testing symfony/framework-bundle with deps=high
  320. $this->assertStringEndsWith('symfony'.\DIRECTORY_SEPARATOR.'form/Resources/config/validation.xml', $xmlMappings[0]);
  321. }
  322. $this->assertStringEndsWith('TestBundle/Resources/config/validation.xml', $xmlMappings[1]);
  323. $yamlMappings = $calls[4][1][0];
  324. $this->assertCount(1, $yamlMappings);
  325. $this->assertStringEndsWith('TestBundle/Resources/config/validation.yml', $yamlMappings[0]);
  326. }
  327. public function testValidationPathsUsingCustomBundlePath()
  328. {
  329. require_once __DIR__.'/Fixtures/CustomPathBundle/src/CustomPathBundle.php';
  330. $container = $this->createContainerFromFile('validation_annotations', array(
  331. 'kernel.bundles' => array('CustomPathBundle' => 'Symfony\\Bundle\\FrameworkBundle\\Tests\\CustomPathBundle'),
  332. 'kernel.bundles_metadata' => array('TestBundle' => array('namespace' => 'Symfony\\Bundle\\FrameworkBundle\\Tests', 'parent' => null, 'path' => __DIR__.'/Fixtures/CustomPathBundle')),
  333. ));
  334. $calls = $container->getDefinition('validator.builder')->getMethodCalls();
  335. $xmlMappings = $calls[3][1][0];
  336. $this->assertCount(2, $xmlMappings);
  337. try {
  338. // Testing symfony/symfony
  339. $this->assertStringEndsWith('Component'.\DIRECTORY_SEPARATOR.'Form/Resources/config/validation.xml', $xmlMappings[0]);
  340. } catch (\Exception $e) {
  341. // Testing symfony/framework-bundle with deps=high
  342. $this->assertStringEndsWith('symfony'.\DIRECTORY_SEPARATOR.'form/Resources/config/validation.xml', $xmlMappings[0]);
  343. }
  344. $this->assertStringEndsWith('CustomPathBundle/Resources/config/validation.xml', $xmlMappings[1]);
  345. $yamlMappings = $calls[4][1][0];
  346. $this->assertCount(1, $yamlMappings);
  347. $this->assertStringEndsWith('CustomPathBundle/Resources/config/validation.yml', $yamlMappings[0]);
  348. }
  349. public function testValidationNoStaticMethod()
  350. {
  351. $container = $this->createContainerFromFile('validation_no_static_method');
  352. $calls = $container->getDefinition('validator.builder')->getMethodCalls();
  353. $this->assertCount(4, $calls);
  354. $this->assertSame('addXmlMappings', $calls[3][0]);
  355. // no cache, no annotations, no static methods
  356. }
  357. public function testValidationTranslationDomain()
  358. {
  359. $container = $this->createContainerFromFile('validation_translation_domain');
  360. $this->assertSame('messages', $container->getParameter('validator.translation_domain'));
  361. }
  362. public function testValidationStrictEmail()
  363. {
  364. $container = $this->createContainerFromFile('validation_strict_email');
  365. $this->assertTrue($container->getDefinition('validator.email')->getArgument(0));
  366. }
  367. public function testFormsCanBeEnabledWithoutCsrfProtection()
  368. {
  369. $container = $this->createContainerFromFile('form_no_csrf');
  370. $this->assertFalse($container->getParameter('form.type_extension.csrf.enabled'));
  371. }
  372. /**
  373. * @group legacy
  374. */
  375. public function testLegacyFormCsrfFieldNameCanBeSetUnderCsrfSettings()
  376. {
  377. $container = $this->createContainerFromFile('form_csrf_sets_field_name');
  378. $this->assertTrue($container->getParameter('form.type_extension.csrf.enabled'));
  379. $this->assertEquals('_custom', $container->getParameter('form.type_extension.csrf.field_name'));
  380. }
  381. /**
  382. * @group legacy
  383. */
  384. public function testLegacyFormCsrfFieldNameUnderFormSettingsTakesPrecedence()
  385. {
  386. $container = $this->createContainerFromFile('form_csrf_under_form_sets_field_name');
  387. $this->assertTrue($container->getParameter('form.type_extension.csrf.enabled'));
  388. $this->assertEquals('_custom_form', $container->getParameter('form.type_extension.csrf.field_name'));
  389. }
  390. public function testStopwatchEnabledWithDebugModeEnabled()
  391. {
  392. $container = $this->createContainerFromFile('default_config', array(
  393. 'kernel.container_class' => 'foo',
  394. 'kernel.debug' => true,
  395. ));
  396. $this->assertTrue($container->has('debug.stopwatch'));
  397. }
  398. public function testStopwatchEnabledWithDebugModeDisabled()
  399. {
  400. $container = $this->createContainerFromFile('default_config', array(
  401. 'kernel.container_class' => 'foo',
  402. ));
  403. $this->assertTrue($container->has('debug.stopwatch'));
  404. }
  405. public function testSerializerDisabled()
  406. {
  407. $container = $this->createContainerFromFile('default_config');
  408. $this->assertFalse($container->has('serializer'));
  409. }
  410. public function testSerializerEnabled()
  411. {
  412. $container = $this->createContainerFromFile('full');
  413. $this->assertTrue($container->has('serializer'));
  414. $argument = $container->getDefinition('serializer.mapping.chain_loader')->getArgument(0);
  415. $this->assertCount(1, $argument);
  416. $this->assertEquals('Symfony\Component\Serializer\Mapping\Loader\AnnotationLoader', $argument[0]->getClass());
  417. $this->assertEquals(new Reference('serializer.mapping.cache.apc'), $container->getDefinition('serializer.mapping.class_metadata_factory')->getArgument(1));
  418. $this->assertEquals(new Reference('serializer.name_converter.camel_case_to_snake_case'), $container->getDefinition('serializer.normalizer.object')->getArgument(1));
  419. }
  420. public function testObjectNormalizerRegistered()
  421. {
  422. $container = $this->createContainerFromFile('full');
  423. $definition = $container->getDefinition('serializer.normalizer.object');
  424. $tag = $definition->getTag('serializer.normalizer');
  425. $this->assertEquals('Symfony\Component\Serializer\Normalizer\ObjectNormalizer', $definition->getClass());
  426. $this->assertEquals(-1000, $tag[0]['priority']);
  427. }
  428. public function testAssetHelperWhenAssetsAreEnabled()
  429. {
  430. $container = $this->createContainerFromFile('full');
  431. $packages = $container->getDefinition('templating.helper.assets')->getArgument(0);
  432. $this->assertSame('assets.packages', (string) $packages);
  433. }
  434. public function testAssetHelperWhenTemplatesAreEnabledAndAssetsAreDisabled()
  435. {
  436. $container = $this->createContainerFromFile('assets_disabled');
  437. $packages = $container->getDefinition('templating.helper.assets')->getArgument(0);
  438. $this->assertSame('assets.packages', (string) $packages);
  439. }
  440. public function testSerializerServiceIsRegisteredWhenEnabled()
  441. {
  442. $container = $this->createContainerFromFile('serializer_enabled');
  443. $this->assertTrue($container->hasDefinition('serializer'));
  444. }
  445. public function testSerializerServiceIsNotRegisteredWhenDisabled()
  446. {
  447. $container = $this->createContainerFromFile('serializer_disabled');
  448. $this->assertFalse($container->hasDefinition('serializer'));
  449. }
  450. public function testPropertyInfoDisabled()
  451. {
  452. $container = $this->createContainerFromFile('default_config');
  453. $this->assertFalse($container->has('property_info'));
  454. }
  455. public function testPropertyInfoEnabled()
  456. {
  457. $container = $this->createContainerFromFile('property_info');
  458. $this->assertTrue($container->has('property_info'));
  459. }
  460. protected function createContainer(array $data = array())
  461. {
  462. return new ContainerBuilder(new ParameterBag(array_merge(array(
  463. 'kernel.bundles' => array('FrameworkBundle' => 'Symfony\\Bundle\\FrameworkBundle\\FrameworkBundle'),
  464. 'kernel.bundles_metadata' => array('FrameworkBundle' => array('namespace' => 'Symfony\\Bundle\\FrameworkBundle', 'path' => __DIR__.'/../..', 'parent' => null)),
  465. 'kernel.cache_dir' => __DIR__,
  466. 'kernel.debug' => false,
  467. 'kernel.environment' => 'test',
  468. 'kernel.name' => 'kernel',
  469. 'kernel.root_dir' => __DIR__,
  470. 'kernel.container_class' => 'testContainer',
  471. ), $data)));
  472. }
  473. protected function createContainerFromFile($file, $data = array())
  474. {
  475. $cacheKey = md5(\get_class($this).$file.serialize($data));
  476. if (isset(self::$containerCache[$cacheKey])) {
  477. return self::$containerCache[$cacheKey];
  478. }
  479. $container = $this->createContainer($data);
  480. $container->registerExtension(new FrameworkExtension());
  481. $this->loadFromFile($container, $file);
  482. $container->getCompilerPassConfig()->setOptimizationPasses(array());
  483. $container->getCompilerPassConfig()->setRemovingPasses(array());
  484. $container->compile();
  485. return self::$containerCache[$cacheKey] = $container;
  486. }
  487. protected function createContainerFromClosure($closure, $data = array())
  488. {
  489. $container = $this->createContainer($data);
  490. $container->registerExtension(new FrameworkExtension());
  491. $loader = new ClosureLoader($container);
  492. $loader->load($closure);
  493. $container->getCompilerPassConfig()->setOptimizationPasses(array());
  494. $container->getCompilerPassConfig()->setRemovingPasses(array());
  495. $container->compile();
  496. return $container;
  497. }
  498. private function checkAssetsPackages(ContainerBuilder $container, $legacy = false)
  499. {
  500. $packages = $container->getDefinition('assets.packages');
  501. // default package
  502. $defaultPackage = $container->getDefinition($packages->getArgument(0));
  503. $this->assertUrlPackage($container, $defaultPackage, array('http://cdn.example.com'), 'SomeVersionScheme', '%%s?version=%%s');
  504. // packages
  505. $packages = $packages->getArgument(1);
  506. $this->assertCount($legacy ? 4 : 5, $packages);
  507. if (!$legacy) {
  508. $package = $container->getDefinition($packages['images_path']);
  509. $this->assertPathPackage($container, $package, '/foo', 'SomeVersionScheme', '%%s?version=%%s');
  510. }
  511. $package = $container->getDefinition($packages['images']);
  512. $this->assertUrlPackage($container, $package, array('http://images1.example.com', 'http://images2.example.com'), '1.0.0', $legacy ? '%%s?%%s' : '%%s?version=%%s');
  513. $package = $container->getDefinition($packages['foo']);
  514. $this->assertPathPackage($container, $package, '', '1.0.0', '%%s-%%s');
  515. $package = $container->getDefinition($packages['bar']);
  516. $this->assertUrlPackage($container, $package, array('https://bar2.example.com'), $legacy ? null : 'SomeVersionScheme', $legacy ? '%%s?%%s' : '%%s?version=%%s');
  517. $this->assertEquals($legacy ? 'assets.empty_version_strategy' : 'assets._version__default', (string) $container->getDefinition('assets._package_bar')->getArgument(1));
  518. $this->assertEquals('assets.empty_version_strategy', (string) $container->getDefinition('assets._package_bar_null_version')->getArgument(1));
  519. }
  520. private function assertPathPackage(ContainerBuilder $container, DefinitionDecorator $package, $basePath, $version, $format)
  521. {
  522. $this->assertEquals('assets.path_package', $package->getParent());
  523. $this->assertEquals($basePath, $package->getArgument(0));
  524. $this->assertVersionStrategy($container, $package->getArgument(1), $version, $format);
  525. }
  526. private function assertUrlPackage(ContainerBuilder $container, DefinitionDecorator $package, $baseUrls, $version, $format)
  527. {
  528. $this->assertEquals('assets.url_package', $package->getParent());
  529. $this->assertEquals($baseUrls, $package->getArgument(0));
  530. $this->assertVersionStrategy($container, $package->getArgument(1), $version, $format);
  531. }
  532. private function assertVersionStrategy(ContainerBuilder $container, Reference $reference, $version, $format)
  533. {
  534. $versionStrategy = $container->getDefinition((string) $reference);
  535. if (null === $version) {
  536. $this->assertEquals('assets.empty_version_strategy', (string) $reference);
  537. } else {
  538. $this->assertEquals('assets.static_version_strategy', $versionStrategy->getParent());
  539. $this->assertEquals($version, $versionStrategy->getArgument(0));
  540. $this->assertEquals($format, $versionStrategy->getArgument(1));
  541. }
  542. }
  543. }