Skip to content

Instantly share code, notes, and snippets.

@Rayne
Last active October 12, 2015 04:08
Show Gist options
  • Save Rayne/ec2bb92a2c65133187c8 to your computer and use it in GitHub Desktop.
Save Rayne/ec2bb92a2c65133187c8 to your computer and use it in GitHub Desktop.
<?php
use Symfony\Component\ClassLoader\UniversalClassLoader;
use Symfony\Component\Finder\Finder;
require_once __DIR__ . '/com.symfony/Symfony/Component/ClassLoader/UniversalClassLoader.php';
$loader = new UniversalClassLoader();
$loader->register();
$loader->useIncludePath(true);
$loader->registerNamespaces(array(
'Symfony' => __DIR__.'/com.symfony',
));
class Test extends \PHPUnit_Framework_TestCase {
const TEST_DIR = "/tmp/finder-test";
public static function provider() {
$wd = self::TEST_DIR . "/" . \date(\DATE_ATOM);
$fileName = "$wd/HELLO WHITESPACE!";
\is_dir(self::TEST_DIR) || \mkdir(self::TEST_DIR, 0700);
\is_dir($wd) || \mkdir($wd, 0700);
\is_file($fileName) || \touch($fileName);
return array(
array($wd, $fileName),
);
}
/**
* @param str $wd
* @param str $expectedPathname
* @dataProvider provider
*/
public function testWithoutSorting ($wd, $expectedPathname) {
$finder = new Finder();
$finder
->in($wd);
foreach ($finder as $finderFile) {
$this->assertEquals($expectedPathname, $finderFile->getPathname());
}
}
/**
* @param str $wd
* @param str $expectedPathname
* @dataProvider provider
*
* FIXME Wrong usage or bug when sorting files with whitespaces with built in features.
*/
public function testWithBuiltInSorting ($wd, $expectedPathname) {
$finder = new Finder();
$finder
->in($wd)
->sortByType();
foreach ($finder as $finderFile) {
$this->assertEquals($expectedPathname, $finderFile->getPathname());
}
}
/**
* @param str $wd
* @param str $expectedPathname
* @dataProvider provider
*/
public function testWithCustomSorting ($wd, $expectedPathname) {
$finder = new Finder();
$finder
->in($wd)
->sort(function (\SplFileInfo $x, \SplFileInfo $y) { return -1; });
foreach ($finder as $finderFile) {
$this->assertEquals($expectedPathname, $finderFile->getPathname());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment