Skip to content

Commit 6fba5e7

Browse files
committed
Move pedantic method declaration test to its own file
Related to: 495e46d
1 parent 465b046 commit 6fba5e7

File tree

2 files changed

+60
-23
lines changed

2 files changed

+60
-23
lines changed

tests/CollectionTest.php

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -44,28 +44,5 @@ function testInsertAndRetrieve() {
4444
}
4545
$this->assertEquals(0, $n);
4646
}
47-
48-
public function testMethodOrder()
49-
{
50-
$class = new ReflectionClass('MongoDB\Collection');
51-
52-
$filters = array(
53-
'public' => ReflectionMethod::IS_PUBLIC,
54-
'protected' => ReflectionMethod::IS_PROTECTED,
55-
'private' => ReflectionMethod::IS_PRIVATE,
56-
);
57-
58-
foreach ($filters as $visibility => $filter) {
59-
$methods = array_map(
60-
function(ReflectionMethod $method) { return $method->getName(); },
61-
$class->getMethods($filter)
62-
);
63-
64-
$sortedMethods = $methods;
65-
sort($sortedMethods);
66-
67-
$this->assertEquals($methods, $sortedMethods, sprintf('%s methods are declared alphabetically', ucfirst($visibility)));
68-
}
69-
}
7047
}
7148

tests/PedantryTest.php

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
namespace MongoDB\Tests;
4+
5+
use RecursiveDirectoryIterator;
6+
use RecursiveIteratorIterator;
7+
use ReflectionClass;
8+
use ReflectionMethod;
9+
use RegexIterator;
10+
11+
/**
12+
* Pedantic tests that have nothing to do with functional correctness.
13+
*/
14+
class PedantryTest extends \PHPUnit_Framework_TestCase
15+
{
16+
/**
17+
* @dataProvider provideProjectClassNames
18+
*/
19+
public function testMethodsAreOrderedAlphabeticallyByVisibility($className)
20+
{
21+
$class = new ReflectionClass($className);
22+
$methods = $class->getMethods();
23+
24+
$getSortValue = function(ReflectionMethod $method) {
25+
if ($method->getModifiers() & ReflectionMethod::IS_PRIVATE) {
26+
return '2' . $method->getName();
27+
}
28+
if ($method->getModifiers() & ReflectionMethod::IS_PROTECTED) {
29+
return '1' . $method->getName();
30+
}
31+
if ($method->getModifiers() & ReflectionMethod::IS_PUBLIC) {
32+
return '0' . $method->getName();
33+
}
34+
};
35+
36+
$sortedMethods = $methods;
37+
usort(
38+
$sortedMethods,
39+
function(ReflectionMethod $a, ReflectionMethod $b) use ($getSortValue) {
40+
return strcasecmp($getSortValue($a), $getSortValue($b));
41+
}
42+
);
43+
44+
$this->assertEquals($sortedMethods, $methods);
45+
}
46+
47+
public function provideProjectClassNames()
48+
{
49+
$classNames = array();
50+
$srcDir = realpath(__DIR__ . '/../src/');
51+
52+
$files = new RegexIterator(new RecursiveIteratorIterator(new RecursiveDirectoryIterator($srcDir)), '/\.php$/i');
53+
54+
foreach ($files as $file) {
55+
$classNames[][] = 'MongoDB\\' . str_replace(DIRECTORY_SEPARATOR, '\\', substr($file->getRealPath(), strlen($srcDir) + 1, -4));
56+
}
57+
58+
return $classNames;
59+
}
60+
}

0 commit comments

Comments
 (0)