PageRenderTime 45ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/RestAPI/vendor/symfony/config/Tests/Resource/DirectoryResourceTest.php

https://gitlab.com/martinstti/silex-microframework-rest
PHP | 166 lines | 127 code | 31 blank | 8 comment | 5 complexity | b4110981de149d05ba64f6e0d0dab4bb MD5 | raw file
  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\Config\Tests\Resource;
  11. use Symfony\Component\Config\Resource\DirectoryResource;
  12. class DirectoryResourceTest extends \PHPUnit_Framework_TestCase
  13. {
  14. protected $directory;
  15. protected function setUp()
  16. {
  17. $this->directory = sys_get_temp_dir().DIRECTORY_SEPARATOR.'symfonyDirectoryIterator';
  18. if (!file_exists($this->directory)) {
  19. mkdir($this->directory);
  20. }
  21. touch($this->directory.'/tmp.xml');
  22. }
  23. protected function tearDown()
  24. {
  25. if (!is_dir($this->directory)) {
  26. return;
  27. }
  28. $this->removeDirectory($this->directory);
  29. }
  30. protected function removeDirectory($directory)
  31. {
  32. $iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($directory), \RecursiveIteratorIterator::CHILD_FIRST);
  33. foreach ($iterator as $path) {
  34. if (preg_match('#[/\\\\]\.\.?$#', $path->__toString())) {
  35. continue;
  36. }
  37. if ($path->isDir()) {
  38. rmdir($path->__toString());
  39. } else {
  40. unlink($path->__toString());
  41. }
  42. }
  43. rmdir($directory);
  44. }
  45. public function testGetResource()
  46. {
  47. $resource = new DirectoryResource($this->directory);
  48. $this->assertSame($this->directory, $resource->getResource(), '->getResource() returns the path to the resource');
  49. }
  50. public function testGetPattern()
  51. {
  52. $resource = new DirectoryResource('foo', 'bar');
  53. $this->assertEquals('bar', $resource->getPattern());
  54. }
  55. public function testIsFresh()
  56. {
  57. $resource = new DirectoryResource($this->directory);
  58. $this->assertTrue($resource->isFresh(time() + 10), '->isFresh() returns true if the resource has not changed');
  59. $this->assertFalse($resource->isFresh(time() - 86400), '->isFresh() returns false if the resource has been updated');
  60. $resource = new DirectoryResource('/____foo/foobar'.mt_rand(1, 999999));
  61. $this->assertFalse($resource->isFresh(time()), '->isFresh() returns false if the resource does not exist');
  62. }
  63. public function testIsFreshUpdateFile()
  64. {
  65. $resource = new DirectoryResource($this->directory);
  66. touch($this->directory.'/tmp.xml', time() + 20);
  67. $this->assertFalse($resource->isFresh(time() + 10), '->isFresh() returns false if an existing file is modified');
  68. }
  69. public function testIsFreshNewFile()
  70. {
  71. $resource = new DirectoryResource($this->directory);
  72. touch($this->directory.'/new.xml', time() + 20);
  73. $this->assertFalse($resource->isFresh(time() + 10), '->isFresh() returns false if a new file is added');
  74. }
  75. public function testIsFreshNewFileWithDifferentPattern()
  76. {
  77. $resource = new DirectoryResource($this->directory, '/.xml$/');
  78. touch($this->directory.'/new.yaml', time() + 20);
  79. $this->assertTrue($resource->isFresh(time() + 10), '->isFresh() returns true if a new file with a non-matching pattern is added');
  80. }
  81. public function testIsFreshDeleteFile()
  82. {
  83. $resource = new DirectoryResource($this->directory);
  84. unlink($this->directory.'/tmp.xml');
  85. $this->assertFalse($resource->isFresh(time()), '->isFresh() returns false if an existing file is removed');
  86. }
  87. public function testIsFreshDeleteDirectory()
  88. {
  89. $resource = new DirectoryResource($this->directory);
  90. $this->removeDirectory($this->directory);
  91. $this->assertFalse($resource->isFresh(time()), '->isFresh() returns false if the whole resource is removed');
  92. }
  93. public function testIsFreshCreateFileInSubdirectory()
  94. {
  95. $subdirectory = $this->directory.'/subdirectory';
  96. mkdir($subdirectory);
  97. $resource = new DirectoryResource($this->directory);
  98. $this->assertTrue($resource->isFresh(time() + 10), '->isFresh() returns true if an unmodified subdirectory exists');
  99. touch($subdirectory.'/newfile.xml', time() + 20);
  100. $this->assertFalse($resource->isFresh(time() + 10), '->isFresh() returns false if a new file in a subdirectory is added');
  101. }
  102. public function testIsFreshModifySubdirectory()
  103. {
  104. $resource = new DirectoryResource($this->directory);
  105. $subdirectory = $this->directory.'/subdirectory';
  106. mkdir($subdirectory);
  107. touch($subdirectory, time() + 20);
  108. $this->assertFalse($resource->isFresh(time() + 10), '->isFresh() returns false if a subdirectory is modified (e.g. a file gets deleted)');
  109. }
  110. public function testFilterRegexListNoMatch()
  111. {
  112. $resource = new DirectoryResource($this->directory, '/\.(foo|xml)$/');
  113. touch($this->directory.'/new.bar', time() + 20);
  114. $this->assertTrue($resource->isFresh(time() + 10), '->isFresh() returns true if a new file not matching the filter regex is created');
  115. }
  116. public function testFilterRegexListMatch()
  117. {
  118. $resource = new DirectoryResource($this->directory, '/\.(foo|xml)$/');
  119. touch($this->directory.'/new.xml', time() + 20);
  120. $this->assertFalse($resource->isFresh(time() + 10), '->isFresh() returns false if an new file matching the filter regex is created ');
  121. }
  122. public function testSerializeUnserialize()
  123. {
  124. $resource = new DirectoryResource($this->directory, '/\.(foo|xml)$/');
  125. $unserialized = unserialize(serialize($resource));
  126. $this->assertSame($this->directory, $resource->getResource());
  127. $this->assertSame('/\.(foo|xml)$/', $resource->getPattern());
  128. }
  129. public function testResourcesWithDifferentPatternsAreDifferent()
  130. {
  131. $resourceA = new DirectoryResource($this->directory, '/.xml$/');
  132. $resourceB = new DirectoryResource($this->directory, '/.yaml$/');
  133. $this->assertEquals(2, count(array_unique(array($resourceA, $resourceB))));
  134. }
  135. }