Skip to content

Commit 55b35a2

Browse files
committed
minor #12028 [Finder] [Iterator] Make the tests less fragile (AlphaStream)
This PR was submitted for the master branch but it was merged into the 2.3 branch instead (closes #12028). Discussion ---------- [Finder] [Iterator] Make the tests less fragile Modified the Iterator tests so that they assert only on what's actually being tested. In particular, the tests were checking the order of the files [that were created virtually simultaneously] after sorting them by date. This change is a part of the effort to make Symfony successfully pass tests on HHVM | Q | A | ------------- | --- | Bug fix? | [no] | New feature? | [no] | BC breaks? | [no] | Deprecations? | [no] | Tests pass? | [yes] | Fixed tickets | [none] | License | MIT | Doc PR | [none] Commits ------- 10f9135 [Finder] [Iterator] Make the tests less fragile
2 parents 1653ca5 + 10f9135 commit 55b35a2

File tree

3 files changed

+78
-35
lines changed

3 files changed

+78
-35
lines changed

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,31 @@ protected function assertOrderedIterator($expected, \Traversable $iterator)
3434
$this->assertEquals($expected, array_values($values));
3535
}
3636

37+
/**
38+
* Same as assertOrderedIterator, but checks the order of groups of
39+
* array elements.
40+
*
41+
* @param array $expected - an array of arrays. For any two subarrays
42+
* $a and $b such that $a goes before $b in $expected, the method
43+
* asserts that any element of $a goes before any element of $b
44+
* in the sequence generated by $iterator
45+
* @param \Traversable $iterator
46+
*/
47+
protected function assertOrderedIteratorForGroups($expected, \Traversable $iterator)
48+
{
49+
$values = array_values(array_map(function (\SplFileInfo $fileinfo) { return $fileinfo->getPathname(); }, iterator_to_array($iterator)));
50+
51+
foreach ($expected as $subarray) {
52+
$temp = array();
53+
while (count($values) && count($temp) < count($subarray)) {
54+
array_push($temp, array_shift($values));
55+
}
56+
sort($temp);
57+
sort($subarray);
58+
$this->assertEquals($subarray, $temp);
59+
}
60+
}
61+
3762
/**
3863
* Same as IteratorTestCase::assertIterator with foreach usage
3964
*

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,11 @@ protected static function toAbsolute($files = null)
8080
if (is_array($files)) {
8181
$f = array();
8282
foreach ($files as $file) {
83-
$f[] = self::$tmpDir.DIRECTORY_SEPARATOR.str_replace('/', DIRECTORY_SEPARATOR, $file);
83+
if (is_array($file)) {
84+
$f[] = self::toAbsolute($file);
85+
} else {
86+
$f[] = self::$tmpDir.DIRECTORY_SEPARATOR.str_replace('/', DIRECTORY_SEPARATOR, $file);
87+
}
8488
}
8589

8690
return $f;

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

Lines changed: 48 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,13 @@ public function testAccept($mode, $expected)
5454

5555
$iterator = new SortableIterator($inner, $mode);
5656

57-
$this->assertOrderedIterator($expected, $iterator);
57+
if ($mode === SortableIterator::SORT_BY_ACCESSED_TIME
58+
|| $mode === SortableIterator::SORT_BY_CHANGED_TIME
59+
|| $mode === SortableIterator::SORT_BY_MODIFIED_TIME) {
60+
$this->assertOrderedIteratorForGroups($expected, $iterator);
61+
} else {
62+
$this->assertOrderedIterator($expected, $iterator);
63+
}
5864
}
5965

6066
public function getAcceptData()
@@ -102,45 +108,53 @@ public function getAcceptData()
102108
);
103109

104110
$sortByAccessedTime = array(
105-
'foo/bar.tmp',
106-
'test.php',
107-
'toto',
108-
'foo bar',
109-
'foo',
110-
'test.py',
111-
'.foo',
112-
'.foo/.bar',
113-
'.foo/bar',
114-
'.git',
115-
'.bar',
111+
// For these two files the access time was set to 2005-10-15
112+
array('foo/bar.tmp', 'test.php'),
113+
// These files were created more or less at the same time
114+
array(
115+
'.git',
116+
'.foo',
117+
'.foo/.bar',
118+
'.foo/bar',
119+
'test.py',
120+
'foo',
121+
'toto',
122+
'foo bar',
123+
),
124+
// This file was accessed after sleeping for 1 sec
125+
array('.bar'),
116126
);
117127

118128
$sortByChangedTime = array(
119-
'foo',
120-
'foo/bar.tmp',
121-
'toto',
122-
'.git',
123-
'.bar',
124-
'.foo',
125-
'foo bar',
126-
'.foo/.bar',
127-
'.foo/bar',
128-
'test.php',
129-
'test.py',
129+
array(
130+
'.git',
131+
'.foo',
132+
'.foo/.bar',
133+
'.foo/bar',
134+
'.bar',
135+
'foo',
136+
'foo/bar.tmp',
137+
'toto',
138+
'foo bar',
139+
),
140+
array('test.php'),
141+
array('test.py'),
130142
);
131143

132144
$sortByModifiedTime = array(
133-
'foo/bar.tmp',
134-
'foo',
135-
'toto',
136-
'.git',
137-
'.bar',
138-
'.foo',
139-
'foo bar',
140-
'.foo/.bar',
141-
'.foo/bar',
142-
'test.php',
143-
'test.py',
145+
array(
146+
'.git',
147+
'.foo',
148+
'.foo/.bar',
149+
'.foo/bar',
150+
'.bar',
151+
'foo',
152+
'foo/bar.tmp',
153+
'toto',
154+
'foo bar',
155+
),
156+
array('test.php'),
157+
array('test.py'),
144158
);
145159

146160
return array(

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy