Commit af42e76b authored by Jeremy Mikola's avatar Jeremy Mikola

Move pedantic method declaration test to its own file

Related to: 495e46d4
parent 97ab718e
<?php
namespace MongoDB\Tests;
use ReflectionClass;
use ReflectionMethod;
class CollectionTest extends TestCase
{
public function testMethodOrder()
{
$class = new ReflectionClass('MongoDB\Collection');
$filters = array(
'public' => ReflectionMethod::IS_PUBLIC,
'protected' => ReflectionMethod::IS_PROTECTED,
'private' => ReflectionMethod::IS_PRIVATE,
);
foreach ($filters as $visibility => $filter) {
$methods = array_map(
function(ReflectionMethod $method) { return $method->getName(); },
$class->getMethods($filter)
);
$sortedMethods = $methods;
sort($sortedMethods);
$this->assertEquals($methods, $sortedMethods, sprintf('%s methods are declared alphabetically', ucfirst($visibility)));
}
}
}
<?php
namespace MongoDB\Tests;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;
use ReflectionClass;
use ReflectionMethod;
use RegexIterator;
/**
* Pedantic tests that have nothing to do with functional correctness.
*/
class PedantryTest extends \PHPUnit_Framework_TestCase
{
/**
* @dataProvider provideProjectClassNames
*/
public function testMethodsAreOrderedAlphabeticallyByVisibility($className)
{
$class = new ReflectionClass($className);
$methods = $class->getMethods();
$getSortValue = function(ReflectionMethod $method) {
if ($method->getModifiers() & ReflectionMethod::IS_PRIVATE) {
return '2' . $method->getName();
}
if ($method->getModifiers() & ReflectionMethod::IS_PROTECTED) {
return '1' . $method->getName();
}
if ($method->getModifiers() & ReflectionMethod::IS_PUBLIC) {
return '0' . $method->getName();
}
};
$sortedMethods = $methods;
usort(
$sortedMethods,
function(ReflectionMethod $a, ReflectionMethod $b) use ($getSortValue) {
return strcasecmp($getSortValue($a), $getSortValue($b));
}
);
$methods = array_map(function(ReflectionMethod $method) { return $method->getName(); }, $methods);
$sortedMethods = array_map(function(ReflectionMethod $method) { return $method->getName(); }, $sortedMethods);
$this->assertEquals($sortedMethods, $methods);
}
public function provideProjectClassNames()
{
$classNames = array();
$srcDir = realpath(__DIR__ . '/../src/');
$files = new RegexIterator(new RecursiveIteratorIterator(new RecursiveDirectoryIterator($srcDir)), '/\.php$/i');
foreach ($files as $file) {
$classNames[][] = 'MongoDB\\' . str_replace(DIRECTORY_SEPARATOR, '\\', substr($file->getRealPath(), strlen($srcDir) + 1, -4));
}
return $classNames;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment