Yaffs site version 1.1
[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($this->directory, $resource->getResource(), '->getResource() returns the path to the resource');
58     }
59
60     public function testGetPattern()
61     {
62         $resource = new DirectoryResource('foo', 'bar');
63         $this->assertEquals('bar', $resource->getPattern());
64     }
65
66     public function testIsFresh()
67     {
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');
71
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');
74     }
75
76     public function testIsFreshUpdateFile()
77     {
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');
81     }
82
83     public function testIsFreshNewFile()
84     {
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');
88     }
89
90     public function testIsFreshNewFileWithDifferentPattern()
91     {
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');
95     }
96
97     public function testIsFreshDeleteFile()
98     {
99         $resource = new DirectoryResource($this->directory);
100         $time = time();
101         sleep(1);
102         unlink($this->directory.'/tmp.xml');
103         $this->assertFalse($resource->isFresh($time), '->isFresh() returns false if an existing file is removed');
104     }
105
106     public function testIsFreshDeleteDirectory()
107     {
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');
111     }
112
113     public function testIsFreshCreateFileInSubdirectory()
114     {
115         $subdirectory = $this->directory.'/subdirectory';
116         mkdir($subdirectory);
117
118         $resource = new DirectoryResource($this->directory);
119         $this->assertTrue($resource->isFresh(time() + 10), '->isFresh() returns true if an unmodified subdirectory exists');
120
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');
123     }
124
125     public function testIsFreshModifySubdirectory()
126     {
127         $resource = new DirectoryResource($this->directory);
128
129         $subdirectory = $this->directory.'/subdirectory';
130         mkdir($subdirectory);
131         touch($subdirectory, time() + 20);
132
133         $this->assertFalse($resource->isFresh(time() + 10), '->isFresh() returns false if a subdirectory is modified (e.g. a file gets deleted)');
134     }
135
136     public function testFilterRegexListNoMatch()
137     {
138         $resource = new DirectoryResource($this->directory, '/\.(foo|xml)$/');
139
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');
142     }
143
144     public function testFilterRegexListMatch()
145     {
146         $resource = new DirectoryResource($this->directory, '/\.(foo|xml)$/');
147
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 ');
150     }
151
152     public function testSerializeUnserialize()
153     {
154         $resource = new DirectoryResource($this->directory, '/\.(foo|xml)$/');
155
156         $unserialized = unserialize(serialize($resource));
157
158         $this->assertSame($this->directory, $resource->getResource());
159         $this->assertSame('/\.(foo|xml)$/', $resource->getPattern());
160     }
161
162     public function testResourcesWithDifferentPatternsAreDifferent()
163     {
164         $resourceA = new DirectoryResource($this->directory, '/.xml$/');
165         $resourceB = new DirectoryResource($this->directory, '/.yaml$/');
166
167         $this->assertEquals(2, count(array_unique(array($resourceA, $resourceB))));
168     }
169 }