Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a59e5e4

Browse files
committed
[Finder] Unified tests
1 parent 9c46043 commit a59e5e4

9 files changed

+246
-93
lines changed

src/Symfony/Component/Finder/Tests/FinderTest.php

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,6 @@
1717

1818
class FinderTest extends Iterator\RealIteratorTestCase
1919
{
20-
protected static $tmpDir;
21-
22-
public static function setUpBeforeClass()
23-
{
24-
parent::setUpBeforeClass();
25-
26-
self::$tmpDir = realpath(sys_get_temp_dir().'/symfony2_finder');
27-
}
2820

2921
public function testCreate()
3022
{
@@ -503,26 +495,6 @@ public function testCountWithoutIn()
503495
count($finder);
504496
}
505497

506-
protected function toAbsolute($files)
507-
{
508-
$f = array();
509-
foreach ($files as $file) {
510-
$f[] = self::$tmpDir . DIRECTORY_SEPARATOR . str_replace('/', DIRECTORY_SEPARATOR, $file);
511-
}
512-
513-
return $f;
514-
}
515-
516-
protected function toAbsoluteFixtures($files)
517-
{
518-
$f = array();
519-
foreach ($files as $file) {
520-
$f[] = __DIR__.DIRECTORY_SEPARATOR.'Fixtures'.DIRECTORY_SEPARATOR.$file;
521-
}
522-
523-
return $f;
524-
}
525-
526498
/**
527499
* @dataProvider getContainsTestData
528500
* @group grep

src/Symfony/Component/Finder/Tests/Iterator/DateRangeFilterIteratorTest.php

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,45 @@ public function testAccept($size, $expected)
3030

3131
public function getAcceptData()
3232
{
33+
$since20YearsAgo = array(
34+
'.git',
35+
'test.py',
36+
'foo',
37+
'foo/bar.tmp',
38+
'test.php',
39+
'toto',
40+
'.bar',
41+
'.foo',
42+
'.foo/.bar',
43+
'foo bar',
44+
'.foo/bar',
45+
);
46+
47+
$since2MonthsAgo = array(
48+
'.git',
49+
'test.py',
50+
'foo',
51+
'toto',
52+
'.bar',
53+
'.foo',
54+
'.foo/.bar',
55+
'foo bar',
56+
'.foo/bar',
57+
);
58+
59+
$untilLastMonth = array(
60+
'.git',
61+
'foo',
62+
'foo/bar.tmp',
63+
'test.php',
64+
'toto',
65+
'.foo',
66+
);
67+
3368
return array(
34-
array(array(new DateComparator('since 20 years ago')), array(sys_get_temp_dir().'/symfony2_finder/.git', sys_get_temp_dir().'/symfony2_finder/test.py', sys_get_temp_dir().'/symfony2_finder/foo', sys_get_temp_dir().'/symfony2_finder/foo/bar.tmp', sys_get_temp_dir().'/symfony2_finder/test.php', sys_get_temp_dir().'/symfony2_finder/toto', sys_get_temp_dir().'/symfony2_finder/.bar', sys_get_temp_dir().'/symfony2_finder/.foo', sys_get_temp_dir().'/symfony2_finder/.foo/.bar', sys_get_temp_dir().'/symfony2_finder/foo bar', sys_get_temp_dir().'/symfony2_finder/.foo/bar')),
35-
array(array(new DateComparator('since 2 months ago')), array(sys_get_temp_dir().'/symfony2_finder/.git', sys_get_temp_dir().'/symfony2_finder/test.py', sys_get_temp_dir().'/symfony2_finder/foo', sys_get_temp_dir().'/symfony2_finder/toto', sys_get_temp_dir().'/symfony2_finder/.bar', sys_get_temp_dir().'/symfony2_finder/.foo', sys_get_temp_dir().'/symfony2_finder/.foo/.bar', sys_get_temp_dir().'/symfony2_finder/foo bar', sys_get_temp_dir().'/symfony2_finder/.foo/bar')),
36-
array(array(new DateComparator('until last month')), array(sys_get_temp_dir().'/symfony2_finder/.git', sys_get_temp_dir().'/symfony2_finder/foo', sys_get_temp_dir().'/symfony2_finder/foo/bar.tmp', sys_get_temp_dir().'/symfony2_finder/test.php', sys_get_temp_dir().'/symfony2_finder/toto', sys_get_temp_dir().'/symfony2_finder/.foo')),
69+
array(array(new DateComparator('since 20 years ago')), $this->toAbsolute($since20YearsAgo)),
70+
array(array(new DateComparator('since 2 months ago')), $this->toAbsolute($since2MonthsAgo)),
71+
array(array(new DateComparator('until last month')), $this->toAbsolute($untilLastMonth)),
3772
);
3873
}
3974
}

src/Symfony/Component/Finder/Tests/Iterator/DepthRangeFilterIteratorTest.php

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class DepthRangeFilterIteratorTest extends RealIteratorTestCase
2020
*/
2121
public function testAccept($minDepth, $maxDepth, $expected)
2222
{
23-
$inner = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($this->getAbsolutePath(''), \FilesystemIterator::SKIP_DOTS), \RecursiveIteratorIterator::SELF_FIRST);
23+
$inner = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($this->toAbsolute(), \FilesystemIterator::SKIP_DOTS), \RecursiveIteratorIterator::SELF_FIRST);
2424

2525
$iterator = new DepthRangeFilterIterator($inner, $minDepth, $maxDepth);
2626

@@ -32,17 +32,50 @@ public function testAccept($minDepth, $maxDepth, $expected)
3232

3333
public function getAcceptData()
3434
{
35+
$lessThan1 = array(
36+
'.git',
37+
'test.py',
38+
'foo',
39+
'test.php',
40+
'toto',
41+
'.foo',
42+
'.bar',
43+
'foo bar',
44+
);
45+
46+
$lessThanOrEqualTo1 = array(
47+
'.git',
48+
'test.py',
49+
'foo',
50+
'foo/bar.tmp',
51+
'test.php',
52+
'toto',
53+
'.foo',
54+
'.foo/.bar',
55+
'.bar',
56+
'foo bar',
57+
'.foo/bar',
58+
);
59+
60+
$graterThanOrEqualTo1 = array(
61+
'foo/bar.tmp',
62+
'.foo/.bar',
63+
'.foo/bar',
64+
);
65+
66+
$equalTo1 = array(
67+
'foo/bar.tmp',
68+
'.foo/.bar',
69+
'.foo/bar',
70+
);
71+
3572
return array(
36-
array(0, 0, array($this->getAbsolutePath('/.git'), $this->getAbsolutePath('/test.py'), $this->getAbsolutePath('/foo'), $this->getAbsolutePath('/test.php'), $this->getAbsolutePath('/toto'), $this->getAbsolutePath('/.foo'), $this->getAbsolutePath('/.bar'), $this->getAbsolutePath('/foo bar'))),
37-
array(0, 1, array($this->getAbsolutePath('/.git'), $this->getAbsolutePath('/test.py'), $this->getAbsolutePath('/foo'), $this->getAbsolutePath('/foo/bar.tmp'), $this->getAbsolutePath('/test.php'), $this->getAbsolutePath('/toto'), $this->getAbsolutePath('/.foo'), $this->getAbsolutePath('/.foo/.bar'), $this->getAbsolutePath('/.bar'), $this->getAbsolutePath('/foo bar'), $this->getAbsolutePath('/.foo/bar'))),
73+
array(0, 0, $this->toAbsolute($lessThan1)),
74+
array(0, 1, $this->toAbsolute($lessThanOrEqualTo1)),
3875
array(2, INF, array()),
39-
array(1, INF, array($this->getAbsolutePath('/foo/bar.tmp'), $this->getAbsolutePath('/.foo/.bar'), $this->getAbsolutePath('/.foo/bar'))),
40-
array(1, 1, array($this->getAbsolutePath('/foo/bar.tmp'), $this->getAbsolutePath('/.foo/.bar'), $this->getAbsolutePath('/.foo/bar'))),
76+
array(1, INF, $this->toAbsolute($graterThanOrEqualTo1)),
77+
array(1, 1, $this->toAbsolute($equalTo1)),
4178
);
4279
}
4380

44-
protected function getAbsolutePath($path)
45-
{
46-
return sys_get_temp_dir().'/symfony2_finder'.str_replace('/', DIRECTORY_SEPARATOR, $path);
47-
}
4881
}

src/Symfony/Component/Finder/Tests/Iterator/ExcludeDirectoryFilterIteratorTest.php

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class ExcludeDirectoryFilterIteratorTest extends RealIteratorTestCase
2121
*/
2222
public function testAccept($directories, $expected)
2323
{
24-
$inner = new \RecursiveIteratorIterator(new RecursiveDirectoryIterator(sys_get_temp_dir().'/symfony2_finder', \FilesystemIterator::SKIP_DOTS), \RecursiveIteratorIterator::SELF_FIRST);
24+
$inner = new \RecursiveIteratorIterator(new RecursiveDirectoryIterator($this->toAbsolute(), \FilesystemIterator::SKIP_DOTS), \RecursiveIteratorIterator::SELF_FIRST);
2525

2626
$iterator = new ExcludeDirectoryFilterIterator($inner, $directories);
2727

@@ -30,33 +30,36 @@ public function testAccept($directories, $expected)
3030

3131
public function getAcceptData()
3232
{
33-
$tmpDir = sys_get_temp_dir().'/symfony2_finder';
33+
$foo = array(
34+
'.bar',
35+
'.foo',
36+
'.foo/.bar',
37+
'.foo/bar',
38+
'.git',
39+
'test.py',
40+
'test.php',
41+
'toto',
42+
'foo bar'
43+
);
44+
45+
$fo = array(
46+
'.bar',
47+
'.foo',
48+
'.foo/.bar',
49+
'.foo/bar',
50+
'.git',
51+
'test.py',
52+
'foo',
53+
'foo/bar.tmp',
54+
'test.php',
55+
'toto',
56+
'foo bar'
57+
);
3458

3559
return array(
36-
array(array('foo'), array(
37-
$tmpDir.DIRECTORY_SEPARATOR.'.bar',
38-
$tmpDir.DIRECTORY_SEPARATOR.'.foo',
39-
$tmpDir.DIRECTORY_SEPARATOR.'.foo'.DIRECTORY_SEPARATOR.'.bar',
40-
$tmpDir.DIRECTORY_SEPARATOR.'.foo'.DIRECTORY_SEPARATOR.'bar',
41-
$tmpDir.DIRECTORY_SEPARATOR.'.git',
42-
$tmpDir.DIRECTORY_SEPARATOR.'test.py',
43-
$tmpDir.DIRECTORY_SEPARATOR.'test.php',
44-
$tmpDir.DIRECTORY_SEPARATOR.'toto',
45-
$tmpDir.DIRECTORY_SEPARATOR.'foo bar',
46-
)),
47-
array(array('fo'), array(
48-
$tmpDir.DIRECTORY_SEPARATOR.'.bar',
49-
$tmpDir.DIRECTORY_SEPARATOR.'.foo',
50-
$tmpDir.DIRECTORY_SEPARATOR.'.foo'.DIRECTORY_SEPARATOR.'.bar',
51-
$tmpDir.DIRECTORY_SEPARATOR.'.foo'.DIRECTORY_SEPARATOR.'bar',
52-
$tmpDir.DIRECTORY_SEPARATOR.'.git',
53-
$tmpDir.DIRECTORY_SEPARATOR.'test.py',
54-
$tmpDir.DIRECTORY_SEPARATOR.'foo',
55-
$tmpDir.DIRECTORY_SEPARATOR.'foo'.DIRECTORY_SEPARATOR.'bar.tmp',
56-
$tmpDir.DIRECTORY_SEPARATOR.'test.php',
57-
$tmpDir.DIRECTORY_SEPARATOR.'toto',
58-
$tmpDir.DIRECTORY_SEPARATOR.'foo bar',
59-
)),
60+
array(array('foo'), $this->toAbsolute($foo)),
61+
array(array('fo'), $this->toAbsolute($fo)),
6062
);
6163
}
64+
6265
}

src/Symfony/Component/Finder/Tests/Iterator/FileTypeFilterIteratorTest.php

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,26 @@ public function testAccept($mode, $expected)
2929

3030
public function getAcceptData()
3131
{
32+
$onlyFiles = array(
33+
'test.py',
34+
'foo/bar.tmp',
35+
'test.php',
36+
'.bar',
37+
'.foo/.bar',
38+
'.foo/bar',
39+
'foo bar',
40+
);
41+
42+
$onlyDirectories = array(
43+
'.git',
44+
'foo',
45+
'toto',
46+
'.foo',
47+
);
48+
3249
return array(
33-
array(FileTypeFilterIterator::ONLY_FILES, array(sys_get_temp_dir().'/symfony2_finder/test.py', sys_get_temp_dir().'/symfony2_finder/foo/bar.tmp', sys_get_temp_dir().'/symfony2_finder/test.php', sys_get_temp_dir().'/symfony2_finder/.bar', sys_get_temp_dir().'/symfony2_finder/.foo/.bar', sys_get_temp_dir().'/symfony2_finder/foo bar', sys_get_temp_dir().'/symfony2_finder/.foo/bar')),
34-
array(FileTypeFilterIterator::ONLY_DIRECTORIES, array(sys_get_temp_dir().'/symfony2_finder/.git', sys_get_temp_dir().'/symfony2_finder/foo', sys_get_temp_dir().'/symfony2_finder/toto', sys_get_temp_dir().'/symfony2_finder/.foo')),
50+
array(FileTypeFilterIterator::ONLY_FILES, $this->toAbsolute($onlyFiles)),
51+
array(FileTypeFilterIterator::ONLY_DIRECTORIES, $this->toAbsolute($onlyDirectories)),
3552
);
3653
}
3754
}

src/Symfony/Component/Finder/Tests/Iterator/FilterIteratorTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class FilterIteratorTest extends RealIteratorTestCase
1818
{
1919
public function testFilterFilesystemIterators()
2020
{
21-
$i = new \FilesystemIterator(sys_get_temp_dir().'/symfony2_finder');
21+
$i = new \FilesystemIterator($this->toAbsolute());
2222

2323
// it is expected that there are test.py test.php in the tmpDir
2424
$i = $this->getMockForAbstractClass('Symfony\Component\Finder\Iterator\FilterIterator', array($i));

src/Symfony/Component/Finder/Tests/Iterator/RealIteratorTestCase.php

Lines changed: 62 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,54 +13,96 @@
1313

1414
abstract class RealIteratorTestCase extends IteratorTestCase
1515
{
16+
17+
protected static $tmpDir;
1618
protected static $files;
1719

1820
public static function setUpBeforeClass()
1921
{
20-
$tmpDir = sys_get_temp_dir().'/symfony2_finder';
22+
self::$tmpDir = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'symfony2_finder';
23+
2124
self::$files = array(
22-
$tmpDir.'/.git/',
23-
$tmpDir.'/.foo/',
24-
$tmpDir.'/.foo/.bar',
25-
$tmpDir.'/.foo/bar',
26-
$tmpDir.'/.bar',
27-
$tmpDir.'/test.py',
28-
$tmpDir.'/foo/',
29-
$tmpDir.'/foo/bar.tmp',
30-
$tmpDir.'/test.php',
31-
$tmpDir.'/toto/',
32-
$tmpDir.'/foo bar',
25+
'.git/',
26+
'.foo/',
27+
'.foo/.bar',
28+
'.foo/bar',
29+
'.bar',
30+
'test.py',
31+
'foo/',
32+
'foo/bar.tmp',
33+
'test.php',
34+
'toto/',
35+
'foo bar'
3336
);
3437

35-
if (is_dir($tmpDir)) {
38+
self::$files = self::toAbsolute(self::$files);
39+
40+
if (is_dir(self::$tmpDir)) {
3641
self::tearDownAfterClass();
3742
} else {
38-
mkdir($tmpDir);
43+
mkdir(self::$tmpDir);
3944
}
4045

4146
foreach (self::$files as $file) {
42-
if ('/' === $file[strlen($file) - 1]) {
47+
if (DIRECTORY_SEPARATOR === $file[strlen($file) - 1]) {
4348
mkdir($file);
4449
} else {
4550
touch($file);
4651
}
4752
}
4853

49-
file_put_contents($tmpDir.'/test.php', str_repeat(' ', 800));
50-
file_put_contents($tmpDir.'/test.py', str_repeat(' ', 2000));
54+
file_put_contents(self::toAbsolute('test.php'), str_repeat(' ', 800));
55+
file_put_contents(self::toAbsolute('test.py'), str_repeat(' ', 2000));
5156

52-
touch($tmpDir.'/foo/bar.tmp', strtotime('2005-10-15'));
53-
touch($tmpDir.'/test.php', strtotime('2005-10-15'));
57+
touch(self::toAbsolute('foo/bar.tmp'), strtotime('2005-10-15'));
58+
touch(self::toAbsolute('test.php'), strtotime('2005-10-15'));
5459
}
5560

5661
public static function tearDownAfterClass()
5762
{
5863
foreach (array_reverse(self::$files) as $file) {
59-
if ('/' === $file[strlen($file) - 1]) {
64+
if (DIRECTORY_SEPARATOR === $file[strlen($file) - 1]) {
6065
@rmdir($file);
6166
} else {
6267
@unlink($file);
6368
}
6469
}
6570
}
71+
72+
protected static function toAbsolute($files = null)
73+
{
74+
/*
75+
* Without the call to setUpBeforeClass() property can be null.
76+
*/
77+
if (!self::$tmpDir) {
78+
self::$tmpDir = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'symfony2_finder';
79+
}
80+
81+
if (is_array($files)) {
82+
$f = array();
83+
foreach ($files as $file) {
84+
$f[] = self::$tmpDir . DIRECTORY_SEPARATOR . str_replace('/', DIRECTORY_SEPARATOR, $file);
85+
}
86+
87+
return $f;
88+
}
89+
90+
if (is_string($files)) {
91+
92+
return self::$tmpDir . DIRECTORY_SEPARATOR . str_replace('/', DIRECTORY_SEPARATOR, $files);
93+
}
94+
95+
return self::$tmpDir;
96+
}
97+
98+
protected static function toAbsoluteFixtures($files)
99+
{
100+
$f = array();
101+
foreach ($files as $file) {
102+
$f[] = realpath(__DIR__.DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.'Fixtures'.DIRECTORY_SEPARATOR.$file);
103+
}
104+
105+
return $f;
106+
}
107+
66108
}

0 commit comments

Comments
 (0)