c37c3e2fc7a3b76690513b4a47545ee956d97957
[yaffs-website] / vendor / symfony / config / Tests / Resource / DirectoryResourceTest.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Symfony\Component\Config\Tests\Resource;
13
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\Config\Resource\DirectoryResource;
16
17 class DirectoryResourceTest extends TestCase
18 {
19     protected $directory;
20
21     protected function setUp()
22     {
23         $this->directory = sys_get_temp_dir().DIRECTORY_SEPARATOR.'symfonyDirectoryIterator';
24         if (!file_exists($this->directory)) {
25             mkdir($this->directory);
26         }
27         touch($this->directory.'/tmp.xml');
28     }
29
30     protected function tearDown()
31     {
32         if (!is_dir($this->directory)) {
33             return;
34         }
35         $this->removeDirectory($this->directory);
36     }
37
38     protected function removeDirectory($directory)
39     {
40         $iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($directory), \RecursiveIteratorIterator::CHILD_FIRST);
41         foreach ($iterator as $path) {
42             if (preg_match('#[/\\\\]\.\.?$#', $path->__toString())) {
43                 continue;
44             }
45             if ($path->isDir()) {
46                 rmdir($path->__toString());
47             } else {
48                 unlink($path->__toString());
49             }
50         }
51         rmdir($directory);
52     }
53
54     public function testGetResource()
55     {
56         $resource = new DirectoryResource($this->directory);
57         $this->assertSame(realpath($this->directory), $resource->getResource(), '->getResource() returns the path to the resource');
58     }
59
60     public function testGetPattern()
61     {
62         $resource = new DirectoryResource($this->directory, 'bar');
63         $this->assertEquals('bar', $resource->getPattern());
64     }
65
66     /**
67      * @expectedException \InvalidArgumentException
68      * @expectedExceptionMessageRegExp /The directory ".*" does not exist./
69      */
70     public function testResourceDoesNotExist()
71     {
72         $resource = new DirectoryResource('/____foo/foobar'.mt_rand(1, 999999));
73     }
74
75     public function testIsFresh()
76     {
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');
80     }
81
82     public function testIsFreshForDeletedResources()
83     {
84         $resource = new DirectoryResource($this->directory);
85         $this->removeDirectory($this->directory);
86
87         $this->assertFalse($resource->isFresh(time()), '->isFresh() returns false if the resource does not exist');
88     }
89
90     public function testIsFreshUpdateFile()
91     {
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');
95     }
96
97     public function testIsFreshNewFile()
98     {
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');
102     }
103
104     public function testIsFreshNewFileWithDifferentPattern()
105     {
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');
109     }
110
111     public function testIsFreshDeleteFile()
112     {
113         $resource = new DirectoryResource($this->directory);
114         $time = time();
115         sleep(1);
116         unlink($this->directory.'/tmp.xml');
117         $this->assertFalse($resource->isFresh($time), '->isFresh() returns false if an existing file is removed');
118     }
119
120     public function testIsFreshDeleteDirectory()
121     {
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');
125     }
126
127     public function testIsFreshCreateFileInSubdirectory()
128     {
129         $subdirectory = $this->directory.'/subdirectory';
130         mkdir($subdirectory);
131
132         $resource = new DirectoryResource($this->directory);
133         $this->assertTrue($resource->isFresh(time() + 10), '->isFresh() returns true if an unmodified subdirectory exists');
134
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');
137     }
138
139     public function testIsFreshModifySubdirectory()
140     {
141         $resource = new DirectoryResource($this->directory);
142
143         $subdirectory = $this->directory.'/subdirectory';
144         mkdir($subdirectory);
145         touch($subdirectory, time() + 20);
146
147         $this->assertFalse($resource->isFresh(time() + 10), '->isFresh() returns false if a subdirectory is modified (e.g. a file gets deleted)');
148     }
149
150     public function testFilterRegexListNoMatch()
151     {
152         $resource = new DirectoryResource($this->directory, '/\.(foo|xml)$/');
153
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');
156     }
157
158     public function testFilterRegexListMatch()
159     {
160         $resource = new DirectoryResource($this->directory, '/\.(foo|xml)$/');
161
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 ');
164     }
165
166     public function testSerializeUnserialize()
167     {
168         $resource = new DirectoryResource($this->directory, '/\.(foo|xml)$/');
169
170         $unserialized = unserialize(serialize($resource));
171
172         $this->assertSame(realpath($this->directory), $resource->getResource());
173         $this->assertSame('/\.(foo|xml)$/', $resource->getPattern());
174     }
175
176     public function testResourcesWithDifferentPatternsAreDifferent()
177     {
178         $resourceA = new DirectoryResource($this->directory, '/.xml$/');
179         $resourceB = new DirectoryResource($this->directory, '/.yaml$/');
180
181         $this->assertCount(2, array_unique(array($resourceA, $resourceB)));
182     }
183 }