streams/tests/DirectoryFilterTest.php
Andy Scherzinger d95aa6fa7e
chore: Add SPDX header
Signed-off-by: Andy Scherzinger <info@andy-scherzinger.de>
2024-06-02 20:52:50 +02:00

51 lines
1,021 B
PHP

<?php
/**
* SPDX-FileCopyrightText: 2015 Robin Appelman <icewind@owncloud.com>
* SPDX-License-Identifier: MIT
*/
namespace Icewind\Streams\Tests;
use PHPUnit\Framework\TestCase;
class DirectoryFilterTest extends 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);
}
}