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(realpath($this->directory), $resource->getResource(), '->getResource() returns the path to the resource');
60 public function testGetPattern()
62 $resource = new DirectoryResource($this->directory, 'bar');
63 $this->assertEquals('bar', $resource->getPattern());
67 * @expectedException \InvalidArgumentException
68 * @expectedExceptionMessageRegExp /The directory ".*" does not exist./
70 public function testResourceDoesNotExist()
72 $resource = new DirectoryResource('/____foo/foobar'.mt_rand(1, 999999));
75 public function testIsFresh()
77 $resource = new DirectoryResource($this->directory);
78 $this->assertTrue($resource->isFresh(time() + 10), '->isFresh() returns true if the resource has not changed');
79 $this->assertFalse($resource->isFresh(time() - 86400), '->isFresh() returns false if the resource has been updated');
82 public function testIsFreshForDeletedResources()
84 $resource = new DirectoryResource($this->directory);
85 $this->removeDirectory($this->directory);
87 $this->assertFalse($resource->isFresh(time()), '->isFresh() returns false if the resource does not exist');
90 public function testIsFreshUpdateFile()
92 $resource = new DirectoryResource($this->directory);
93 touch($this->directory.'/tmp.xml', time() + 20);
94 $this->assertFalse($resource->isFresh(time() + 10), '->isFresh() returns false if an existing file is modified');
97 public function testIsFreshNewFile()
99 $resource = new DirectoryResource($this->directory);
100 touch($this->directory.'/new.xml', time() + 20);
101 $this->assertFalse($resource->isFresh(time() + 10), '->isFresh() returns false if a new file is added');
104 public function testIsFreshNewFileWithDifferentPattern()
106 $resource = new DirectoryResource($this->directory, '/.xml$/');
107 touch($this->directory.'/new.yaml', time() + 20);
108 $this->assertTrue($resource->isFresh(time() + 10), '->isFresh() returns true if a new file with a non-matching pattern is added');
111 public function testIsFreshDeleteFile()
113 $resource = new DirectoryResource($this->directory);
116 unlink($this->directory.'/tmp.xml');
117 $this->assertFalse($resource->isFresh($time), '->isFresh() returns false if an existing file is removed');
120 public function testIsFreshDeleteDirectory()
122 $resource = new DirectoryResource($this->directory);
123 $this->removeDirectory($this->directory);
124 $this->assertFalse($resource->isFresh(time()), '->isFresh() returns false if the whole resource is removed');
127 public function testIsFreshCreateFileInSubdirectory()
129 $subdirectory = $this->directory.'/subdirectory';
130 mkdir($subdirectory);
132 $resource = new DirectoryResource($this->directory);
133 $this->assertTrue($resource->isFresh(time() + 10), '->isFresh() returns true if an unmodified subdirectory exists');
135 touch($subdirectory.'/newfile.xml', time() + 20);
136 $this->assertFalse($resource->isFresh(time() + 10), '->isFresh() returns false if a new file in a subdirectory is added');
139 public function testIsFreshModifySubdirectory()
141 $resource = new DirectoryResource($this->directory);
143 $subdirectory = $this->directory.'/subdirectory';
144 mkdir($subdirectory);
145 touch($subdirectory, time() + 20);
147 $this->assertFalse($resource->isFresh(time() + 10), '->isFresh() returns false if a subdirectory is modified (e.g. a file gets deleted)');
150 public function testFilterRegexListNoMatch()
152 $resource = new DirectoryResource($this->directory, '/\.(foo|xml)$/');
154 touch($this->directory.'/new.bar', time() + 20);
155 $this->assertTrue($resource->isFresh(time() + 10), '->isFresh() returns true if a new file not matching the filter regex is created');
158 public function testFilterRegexListMatch()
160 $resource = new DirectoryResource($this->directory, '/\.(foo|xml)$/');
162 touch($this->directory.'/new.xml', time() + 20);
163 $this->assertFalse($resource->isFresh(time() + 10), '->isFresh() returns false if an new file matching the filter regex is created ');
166 public function testSerializeUnserialize()
168 $resource = new DirectoryResource($this->directory, '/\.(foo|xml)$/');
170 $unserialized = unserialize(serialize($resource));
172 $this->assertSame(realpath($this->directory), $resource->getResource());
173 $this->assertSame('/\.(foo|xml)$/', $resource->getPattern());
176 public function testResourcesWithDifferentPatternsAreDifferent()
178 $resourceA = new DirectoryResource($this->directory, '/.xml$/');
179 $resourceB = new DirectoryResource($this->directory, '/.yaml$/');
181 $this->assertCount(2, array_unique(array($resourceA, $resourceB)));