|
| 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