Skip to content

Instantly share code, notes, and snippets.

@jmikola
Created March 18, 2015 21:47
Show Gist options
  • Save jmikola/71e0afb6f153ad2d777d to your computer and use it in GitHub Desktop.
Save jmikola/71e0afb6f153ad2d777d to your computer and use it in GitHub Desktop.
Pedantic tests that have nothing to do with functional correctness
<?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));
}
);
$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;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment