EnvParametersResourceTest.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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\HttpKernel\Tests\Config;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\HttpKernel\Config\EnvParametersResource;
  13. class EnvParametersResourceTest extends TestCase
  14. {
  15. protected $prefix = '__DUMMY_';
  16. protected $initialEnv;
  17. protected $resource;
  18. protected function setUp()
  19. {
  20. $this->initialEnv = array(
  21. $this->prefix.'1' => 'foo',
  22. $this->prefix.'2' => 'bar',
  23. );
  24. foreach ($this->initialEnv as $key => $value) {
  25. $_SERVER[$key] = $value;
  26. }
  27. $this->resource = new EnvParametersResource($this->prefix);
  28. }
  29. protected function tearDown()
  30. {
  31. foreach ($_SERVER as $key => $value) {
  32. if (0 === strpos($key, $this->prefix)) {
  33. unset($_SERVER[$key]);
  34. }
  35. }
  36. }
  37. public function testGetResource()
  38. {
  39. $this->assertSame(
  40. array('prefix' => $this->prefix, 'variables' => $this->initialEnv),
  41. $this->resource->getResource(),
  42. '->getResource() returns the resource'
  43. );
  44. }
  45. public function testToString()
  46. {
  47. $this->assertSame(
  48. serialize(array('prefix' => $this->prefix, 'variables' => $this->initialEnv)),
  49. (string) $this->resource
  50. );
  51. }
  52. public function testIsFreshNotChanged()
  53. {
  54. $this->assertTrue(
  55. $this->resource->isFresh(time()),
  56. '->isFresh() returns true if the variables have not changed'
  57. );
  58. }
  59. public function testIsFreshValueChanged()
  60. {
  61. reset($this->initialEnv);
  62. $_SERVER[key($this->initialEnv)] = 'baz';
  63. $this->assertFalse(
  64. $this->resource->isFresh(time()),
  65. '->isFresh() returns false if a variable has been changed'
  66. );
  67. }
  68. public function testIsFreshValueRemoved()
  69. {
  70. reset($this->initialEnv);
  71. unset($_SERVER[key($this->initialEnv)]);
  72. $this->assertFalse(
  73. $this->resource->isFresh(time()),
  74. '->isFresh() returns false if a variable has been removed'
  75. );
  76. }
  77. public function testIsFreshValueAdded()
  78. {
  79. $_SERVER[$this->prefix.'3'] = 'foo';
  80. $this->assertFalse(
  81. $this->resource->isFresh(time()),
  82. '->isFresh() returns false if a variable has been added'
  83. );
  84. }
  85. public function testSerializeUnserialize()
  86. {
  87. $this->assertEquals($this->resource, unserialize(serialize($this->resource)));
  88. }
  89. }