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