EnvParametersResourceTest.php 2.7 KB

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