Skip to content

Commit af42e76

Browse files
committed
Move pedantic method declaration test to its own file
Related to: 495e46d
1 parent 97ab718 commit af42e76

File tree

2 files changed

+63
-32
lines changed

2 files changed

+63
-32
lines changed

tests/CollectionTest.php

Lines changed: 0 additions & 32 deletions
This file was deleted.

tests/PedantryTest.php

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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+
$methods = array_map(function(ReflectionMethod $method) { return $method->getName(); }, $methods);
45+
$sortedMethods = array_map(function(ReflectionMethod $method) { return $method->getName(); }, $sortedMethods);
46+
47+
$this->assertEquals($sortedMethods, $methods);
48+
}
49+
50+
public function provideProjectClassNames()
51+
{
52+
$classNames = array();
53+
$srcDir = realpath(__DIR__ . '/../src/');
54+
55+
$files = new RegexIterator(new RecursiveIteratorIterator(new RecursiveDirectoryIterator($srcDir)), '/\.php$/i');
56+
57+
foreach ($files as $file) {
58+
$classNames[][] = 'MongoDB\\' . str_replace(DIRECTORY_SEPARATOR, '\\', substr($file->getRealPath(), strlen($srcDir) + 1, -4));
59+
}
60+
61+
return $classNames;
62+
}
63+
}

0 commit comments

Comments
 (0)