4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Config\Tests\Resource;
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\Config\Resource\DirectoryResource;
17 class DirectoryResourceTest extends TestCase
21 protected function setUp()
23 $this->directory = sys_get_temp_dir().DIRECTORY_SEPARATOR.'symfonyDirectoryIterator';
24 if (!file_exists($this->directory)) {
25 mkdir($this->directory);
27 touch($this->directory.'/tmp.xml');
30 protected function tearDown()
32 if (!is_dir($this->directory)) {
35 $this->removeDirectory($this->directory);
38 protected function removeDirectory($directory)
40 $iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($directory), \RecursiveIteratorIterator::CHILD_FIRST);
41 foreach ($iterator as $path) {
42 if (preg_match('#[/\\\\]\.\.?$#', $path->__toString())) {
46 rmdir($path->__toString());
48 unlink($path->__toString());
54 public function testGetResource()
56 $resource = new DirectoryResource($this->directory);
57 $this->assertSame($this->directory, $resource->getResource(), '->getResource() returns the path to the resource');
60 public function testGetPattern()
62 $resource = new DirectoryResource('foo', 'bar');
63 $this->assertEquals('bar', $resource->getPattern());
66 public function testIsFresh()
68 $resource = new DirectoryResource($this->directory);
69 $this->assertTrue($resource->isFresh(time() + 10), '->isFresh() returns true if the resource has not changed');
70 $this->assertFalse($resource->isFresh(time() - 86400), '->isFresh() returns false if the resource has been updated');
72 $resource = new DirectoryResource('/____foo/foobar'.mt_rand(1, 999999));
73 $this->assertFalse($resource->isFresh(time()), '->isFresh() returns false if the resource does not exist');
76 public function testIsFreshUpdateFile()
78 $resource = new DirectoryResource($this->directory);
79 touch($this->directory.'/tmp.xml', time() + 20);
80 $this->assertFalse($resource->isFresh(time() + 10), '->isFresh() returns false if an existing file is modified');
83 public function testIsFreshNewFile()
85 $resource = new DirectoryResource($this->directory);
86 touch($this->directory.'/new.xml', time() + 20);
87 $this->assertFalse($resource->isFresh(time() + 10), '->isFresh() returns false if a new file is added');
90 public function testIsFreshNewFileWithDifferentPattern()
92 $resource = new DirectoryResource($this->directory, '/.xml$/');
93 touch($this->directory.'/new.yaml', time() + 20);
94 $this->assertTrue($resource->isFresh(time() + 10), '->isFresh() returns true if a new file with a non-matching pattern is added');
97 public function testIsFreshDeleteFile()
99 $resource = new DirectoryResource($this->directory);
102 unlink($this->directory.'/tmp.xml');
103 $this->assertFalse($resource->isFresh($time), '->isFresh() returns false if an existing file is removed');
106 public function testIsFreshDeleteDirectory()
108 $resource = new DirectoryResource($this->directory);
109 $this->removeDirectory($this->directory);
110 $this->assertFalse($resource->isFresh(time()), '->isFresh() returns false if the whole resource is removed');
113 public function testIsFreshCreateFileInSubdirectory()
115 $subdirectory = $this->directory.'/subdirectory';
116 mkdir($subdirectory);
118 $resource = new DirectoryResource($this->directory);
119 $this->assertTrue($resource->isFresh(time() + 10), '->isFresh() returns true if an unmodified subdirectory exists');
121 touch($subdirectory.'/newfile.xml', time() + 20);
122 $this->assertFalse($resource->isFresh(time() + 10), '->isFresh() returns false if a new file in a subdirectory is added');
125 public function testIsFreshModifySubdirectory()
127 $resource = new DirectoryResource($this->directory);
129 $subdirectory = $this->directory.'/subdirectory';
130 mkdir($subdirectory);
131 touch($subdirectory, time() + 20);
133 $this->assertFalse($resource->isFresh(time() + 10), '->isFresh() returns false if a subdirectory is modified (e.g. a file gets deleted)');
136 public function testFilterRegexListNoMatch()
138 $resource = new DirectoryResource($this->directory, '/\.(foo|xml)$/');
140 touch($this->directory.'/new.bar', time() + 20);
141 $this->assertTrue($resource->isFresh(time() + 10), '->isFresh() returns true if a new file not matching the filter regex is created');
144 public function testFilterRegexListMatch()
146 $resource = new DirectoryResource($this->directory, '/\.(foo|xml)$/');
148 touch($this->directory.'/new.xml', time() + 20);
149 $this->assertFalse($resource->isFresh(time() + 10), '->isFresh() returns false if an new file matching the filter regex is created ');
152 public function testSerializeUnserialize()
154 $resource = new DirectoryResource($this->directory, '/\.(foo|xml)$/');
156 $unserialized = unserialize(serialize($resource));
158 $this->assertSame($this->directory, $resource->getResource());
159 $this->assertSame('/\.(foo|xml)$/', $resource->getPattern());
162 public function testResourcesWithDifferentPatternsAreDifferent()
164 $resourceA = new DirectoryResource($this->directory, '/.xml$/');
165 $resourceB = new DirectoryResource($this->directory, '/.yaml$/');
167 $this->assertEquals(2, count(array_unique(array($resourceA, $resourceB))));