Add directory filter

This commit is contained in:
Robin Appelman 2015-10-16 15:47:50 +02:00
commit f47297b76a
4 changed files with 112 additions and 16 deletions

44
tests/DirectoryFilter.php Normal file
View file

@ -0,0 +1,44 @@
<?php
/**
* Copyright (c) 2015 Robin Appelman <icewind@owncloud.com>
* This file is licensed under the Licensed under the MIT license:
* http://opensource.org/licenses/MIT
*/
namespace Icewind\Streams\Tests;
class DirectoryFilter extends \PHPUnit_Framework_TestCase {
public function testFilterAcceptAll() {
$this->filter(['a', 'b', 'c'],
function () {
return true;
},
['a', 'b', 'c']);
}
public function testFilterRejectAll() {
$this->filter(['a', 'b', 'c'],
function () {
return false;
},
[]);
}
public function testFilterRejectLong() {
$this->filter(['a', 'bb', 'c'],
function ($file) {
return strlen($file) < 2;
},
['a', 'c']);
}
private function filter(array $files, callable $filter, array $expected) {
$source = \Icewind\Streams\IteratorDirectory::wrap($files);
$filtered = \Icewind\Streams\DirectoryFilter::wrap($source, $filter);
$result = [];
while (($file = readdir($filtered)) !== false) {
$result[] = $file;
}
$this->assertEquals($expected, $result);
}
}