ContainerParametersResourceCheckerTest.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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\Component\DependencyInjection\Tests\Config;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\Config\ResourceCheckerInterface;
  13. use Symfony\Component\DependencyInjection\Config\ContainerParametersResource;
  14. use Symfony\Component\DependencyInjection\Config\ContainerParametersResourceChecker;
  15. use Symfony\Component\DependencyInjection\ContainerInterface;
  16. class ContainerParametersResourceCheckerTest extends TestCase
  17. {
  18. /** @var ContainerParametersResource */
  19. private $resource;
  20. /** @var ResourceCheckerInterface */
  21. private $resourceChecker;
  22. /** @var ContainerInterface */
  23. private $container;
  24. protected function setUp()
  25. {
  26. $this->resource = new ContainerParametersResource(array('locales' => array('fr', 'en'), 'default_locale' => 'fr'));
  27. $this->container = $this->getMockBuilder(ContainerInterface::class)->getMock();
  28. $this->resourceChecker = new ContainerParametersResourceChecker($this->container);
  29. }
  30. public function testSupports()
  31. {
  32. $this->assertTrue($this->resourceChecker->supports($this->resource));
  33. }
  34. /**
  35. * @dataProvider isFreshProvider
  36. */
  37. public function testIsFresh(callable $mockContainer, $expected)
  38. {
  39. $mockContainer($this->container);
  40. $this->assertSame($expected, $this->resourceChecker->isFresh($this->resource, time()));
  41. }
  42. public function isFreshProvider()
  43. {
  44. yield 'not fresh on missing parameter' => array(function (\PHPUnit_Framework_MockObject_MockObject $container) {
  45. $container->method('hasParameter')->with('locales')->willReturn(false);
  46. }, false);
  47. yield 'not fresh on different value' => array(function (\PHPUnit_Framework_MockObject_MockObject $container) {
  48. $container->method('getParameter')->with('locales')->willReturn(array('nl', 'es'));
  49. }, false);
  50. yield 'fresh on every identical parameters' => array(function (\PHPUnit_Framework_MockObject_MockObject $container) {
  51. $container->expects($this->exactly(2))->method('hasParameter')->willReturn(true);
  52. $container->expects($this->exactly(2))->method('getParameter')
  53. ->withConsecutive(
  54. array($this->equalTo('locales')),
  55. array($this->equalTo('default_locale'))
  56. )
  57. ->will($this->returnValueMap(array(
  58. array('locales', array('fr', 'en')),
  59. array('default_locale', 'fr'),
  60. )))
  61. ;
  62. }, true);
  63. }
  64. }