Skip to content

Commit b219e0a

Browse files
committed
merged branch jakzal/2.3-config-tests (PR #8178)
This PR was merged into the 2.3 branch. Discussion ---------- [Config] Added few tests | Q | A | ------------- | --- | Bug fix? | no | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | - Commits ------- 804b182 [Config] Added tests for the FileResource and DirectoryResource. c5dda79 [Config] Fixed @Covers annotation which ignored some of the methods from the code coverage. bf769e0 [Config] Added tests for the ConfigCache.
2 parents e1f530e + 804b182 commit b219e0a

File tree

4 files changed

+164
-39
lines changed

4 files changed

+164
-39
lines changed
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
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;
13+
14+
use Symfony\Component\Config\ConfigCache;
15+
use Symfony\Component\Config\Resource\FileResource;
16+
17+
class ConfigTest extends \PHPUnit_Framework_TestCase
18+
{
19+
private $resourceFile = null;
20+
21+
private $cacheFile = null;
22+
23+
private $metaFile = null;
24+
25+
public function setUp()
26+
{
27+
$this->resourceFile = tempnam(sys_get_temp_dir(), '_resource');
28+
$this->cacheFile = tempnam(sys_get_temp_dir(), 'config_');
29+
$this->metaFile = $this->cacheFile.'.meta';
30+
31+
$this->makeCacheFresh();
32+
$this->generateMetaFile();
33+
}
34+
35+
public function tearDown()
36+
{
37+
$files = array($this->cacheFile, $this->metaFile, $this->resourceFile);
38+
39+
foreach ($files as $file) {
40+
if (file_exists($file)) {
41+
unlink($file);
42+
}
43+
}
44+
}
45+
46+
public function testToString()
47+
{
48+
$cache = new ConfigCache($this->cacheFile, true);
49+
50+
$this->assertSame($this->cacheFile, (string) $cache);
51+
}
52+
53+
public function testCacheIsNotFreshIfFileDoesNotExist()
54+
{
55+
unlink($this->cacheFile);
56+
57+
$cache = new ConfigCache($this->cacheFile, false);
58+
59+
$this->assertFalse($cache->isFresh());
60+
}
61+
62+
public function testCacheIsAlwaysFreshIfFileExistsWithDebugDisabled()
63+
{
64+
$this->makeCacheStale();
65+
66+
$cache = new ConfigCache($this->cacheFile, false);
67+
68+
$this->assertTrue($cache->isFresh());
69+
}
70+
71+
public function testCacheIsNotFreshWithoutMetaFile()
72+
{
73+
unlink($this->metaFile);
74+
75+
$cache = new ConfigCache($this->cacheFile, true);
76+
77+
$this->assertFalse($cache->isFresh());
78+
}
79+
80+
public function testCacheIsFreshIfResourceIsFresh()
81+
{
82+
$cache = new ConfigCache($this->cacheFile, true);
83+
84+
$this->assertTrue($cache->isFresh());
85+
}
86+
87+
public function testCacheIsNotFreshIfOneOfTheResourcesIsNotFresh()
88+
{
89+
$this->makeCacheStale();
90+
91+
$cache = new ConfigCache($this->cacheFile, true);
92+
93+
$this->assertFalse($cache->isFresh());
94+
}
95+
96+
public function testWriteDumpsFile()
97+
{
98+
unlink($this->cacheFile);
99+
unlink($this->metaFile);
100+
101+
$cache = new ConfigCache($this->cacheFile, false);
102+
$cache->write('FOOBAR');
103+
104+
$this->assertFileExists($this->cacheFile, 'Cache file is created');
105+
$this->assertSame('FOOBAR', file_get_contents($this->cacheFile));
106+
$this->assertFileNotExists($this->metaFile, 'Meta file is not created');
107+
}
108+
109+
public function testWriteDumpsMetaFileWithDebugEnabled()
110+
{
111+
unlink($this->cacheFile);
112+
unlink($this->metaFile);
113+
114+
$metadata = array(new FileResource($this->resourceFile));
115+
116+
$cache = new ConfigCache($this->cacheFile, true);
117+
$cache->write('FOOBAR', $metadata);
118+
119+
$this->assertFileExists($this->cacheFile, 'Cache file is created');
120+
$this->assertFileExists($this->metaFile, 'Meta file is created');
121+
$this->assertSame(serialize($metadata), file_get_contents($this->metaFile));
122+
}
123+
124+
private function makeCacheFresh()
125+
{
126+
touch($this->resourceFile, filemtime($this->cacheFile) - 3600);
127+
}
128+
129+
private function makeCacheStale()
130+
{
131+
touch($this->cacheFile, time() - 3600);
132+
}
133+
134+
private function generateMetaFile()
135+
{
136+
file_put_contents($this->metaFile, serialize(array(new FileResource($this->resourceFile))));
137+
}
138+
}

src/Symfony/Component/Config/Tests/Loader/FileLoaderTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
class FileLoaderTest extends \PHPUnit_Framework_TestCase
1919
{
2020
/**
21-
* @covers Symfony\Component\Config\Loader\FileLoader::import
21+
* @covers Symfony\Component\Config\Loader\FileLoader
2222
*/
2323
public function testImport()
2424
{

src/Symfony/Component/Config/Tests/Resource/DirectoryResourceTest.php

Lines changed: 12 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,11 @@ protected function removeDirectory($directory)
5050
rmdir($directory);
5151
}
5252

53-
/**
54-
* @covers Symfony\Component\Config\Resource\DirectoryResource::getResource
55-
*/
5653
public function testGetResource()
5754
{
5855
$resource = new DirectoryResource($this->directory);
59-
$this->assertEquals($this->directory, $resource->getResource(), '->getResource() returns the path to the resource');
56+
$this->assertSame($this->directory, $resource->getResource(), '->getResource() returns the path to the resource');
57+
$this->assertSame($this->directory, (string) $resource, '->__toString() returns the path to the resource');
6058
}
6159

6260
public function testGetPattern()
@@ -65,9 +63,6 @@ public function testGetPattern()
6563
$this->assertEquals('bar', $resource->getPattern());
6664
}
6765

68-
/**
69-
* @covers Symfony\Component\Config\Resource\DirectoryResource::isFresh
70-
*/
7166
public function testIsFresh()
7267
{
7368
$resource = new DirectoryResource($this->directory);
@@ -78,49 +73,34 @@ public function testIsFresh()
7873
$this->assertFalse($resource->isFresh(time()), '->isFresh() returns false if the resource does not exist');
7974
}
8075

81-
/**
82-
* @covers Symfony\Component\Config\Resource\DirectoryResource::isFresh
83-
*/
8476
public function testIsFreshUpdateFile()
8577
{
8678
$resource = new DirectoryResource($this->directory);
8779
touch($this->directory.'/tmp.xml', time() + 20);
8880
$this->assertFalse($resource->isFresh(time() + 10), '->isFresh() returns false if an existing file is modified');
8981
}
9082

91-
/**
92-
* @covers Symfony\Component\Config\Resource\DirectoryResource::isFresh
93-
*/
9483
public function testIsFreshNewFile()
9584
{
9685
$resource = new DirectoryResource($this->directory);
9786
touch($this->directory.'/new.xml', time() + 20);
9887
$this->assertFalse($resource->isFresh(time() + 10), '->isFresh() returns false if a new file is added');
9988
}
10089

101-
/**
102-
* @covers Symfony\Component\Config\Resource\DirectoryResource::isFresh
103-
*/
10490
public function testIsFreshDeleteFile()
10591
{
10692
$resource = new DirectoryResource($this->directory);
10793
unlink($this->directory.'/tmp.xml');
10894
$this->assertFalse($resource->isFresh(time()), '->isFresh() returns false if an existing file is removed');
10995
}
11096

111-
/**
112-
* @covers Symfony\Component\Config\Resource\DirectoryResource::isFresh
113-
*/
11497
public function testIsFreshDeleteDirectory()
11598
{
11699
$resource = new DirectoryResource($this->directory);
117100
$this->removeDirectory($this->directory);
118101
$this->assertFalse($resource->isFresh(time()), '->isFresh() returns false if the whole resource is removed');
119102
}
120103

121-
/**
122-
* @covers Symfony\Component\Config\Resource\DirectoryResource::isFresh
123-
*/
124104
public function testIsFreshCreateFileInSubdirectory()
125105
{
126106
$subdirectory = $this->directory.'/subdirectory';
@@ -133,9 +113,6 @@ public function testIsFreshCreateFileInSubdirectory()
133113
$this->assertFalse($resource->isFresh(time() + 10), '->isFresh() returns false if a new file in a subdirectory is added');
134114
}
135115

136-
/**
137-
* @covers Symfony\Component\Config\Resource\DirectoryResource::isFresh
138-
*/
139116
public function testIsFreshModifySubdirectory()
140117
{
141118
$resource = new DirectoryResource($this->directory);
@@ -147,9 +124,6 @@ public function testIsFreshModifySubdirectory()
147124
$this->assertFalse($resource->isFresh(time() + 10), '->isFresh() returns false if a subdirectory is modified (e.g. a file gets deleted)');
148125
}
149126

150-
/**
151-
* @covers Symfony\Component\Config\Resource\DirectoryResource::isFresh
152-
*/
153127
public function testFilterRegexListNoMatch()
154128
{
155129
$resource = new DirectoryResource($this->directory, '/\.(foo|xml)$/');
@@ -158,14 +132,21 @@ public function testFilterRegexListNoMatch()
158132
$this->assertTrue($resource->isFresh(time() + 10), '->isFresh() returns true if a new file not matching the filter regex is created');
159133
}
160134

161-
/**
162-
* @covers Symfony\Component\Config\Resource\DirectoryResource::isFresh
163-
*/
164135
public function testFilterRegexListMatch()
165136
{
166137
$resource = new DirectoryResource($this->directory, '/\.(foo|xml)$/');
167138

168139
touch($this->directory.'/new.xml', time() + 20);
169140
$this->assertFalse($resource->isFresh(time() + 10), '->isFresh() returns false if an new file matching the filter regex is created ');
170141
}
142+
143+
public function testSerializeUnserialize()
144+
{
145+
$resource = new DirectoryResource($this->directory, '/\.(foo|xml)$/');
146+
147+
$unserialized = unserialize(serialize($resource));
148+
149+
$this->assertSame($this->directory, $resource->getResource());
150+
$this->assertSame('/\.(foo|xml)$/', $resource->getPattern());
151+
}
171152
}

src/Symfony/Component/Config/Tests/Resource/FileResourceTest.php

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,16 @@ protected function tearDown()
3030
unlink($this->file);
3131
}
3232

33-
/**
34-
* @covers Symfony\Component\Config\Resource\FileResource::getResource
35-
*/
3633
public function testGetResource()
3734
{
38-
$this->assertEquals(realpath($this->file), $this->resource->getResource(), '->getResource() returns the path to the resource');
35+
$this->assertSame(realpath($this->file), $this->resource->getResource(), '->getResource() returns the path to the resource');
36+
}
37+
38+
public function testToString()
39+
{
40+
$this->assertSame(realpath($this->file), (string) $this->resource);
3941
}
4042

41-
/**
42-
* @covers Symfony\Component\Config\Resource\FileResource::isFresh
43-
*/
4443
public function testIsFresh()
4544
{
4645
$this->assertTrue($this->resource->isFresh(time() + 10), '->isFresh() returns true if the resource has not changed');
@@ -49,4 +48,11 @@ public function testIsFresh()
4948
$resource = new FileResource('/____foo/foobar'.rand(1, 999999));
5049
$this->assertFalse($resource->isFresh(time()), '->isFresh() returns false if the resource does not exist');
5150
}
51+
52+
public function testSerializeUnserialize()
53+
{
54+
$unserialized = unserialize(serialize($this->resource));
55+
56+
$this->assertSame($this->file, $this->resource->getResource());
57+
}
5258
}

0 commit comments

Comments
 (0)