mirror of
https://codeberg.org/icewind/streams.git
synced 2026-06-03 08:34:09 +02:00
Add IteratorDirectory to create directory handles from iterators and arrays
This commit is contained in:
parent
b876cef7f2
commit
214a31bc98
3 changed files with 258 additions and 0 deletions
130
tests/IteratorDirectory.php
Normal file
130
tests/IteratorDirectory.php
Normal file
|
|
@ -0,0 +1,130 @@
|
|||
<?php
|
||||
/**
|
||||
* Copyright (c) 2014 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 IteratorDirectory extends \PHPUnit_Framework_TestCase {
|
||||
|
||||
/**
|
||||
* @param \Iterator | array $source
|
||||
* @return resource
|
||||
*/
|
||||
protected function wrapSource($source) {
|
||||
return \Icewind\Streams\IteratorDirectory::wrap($source);
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \BadMethodCallException
|
||||
*/
|
||||
public function testNoContext() {
|
||||
stream_wrapper_register('iterator', '\Icewind\Streams\IteratorDirectory');
|
||||
$context = stream_context_create(array());
|
||||
try {
|
||||
opendir('iterator://', $context);
|
||||
stream_wrapper_unregister('iterator');
|
||||
} catch (\Exception $e) {
|
||||
stream_wrapper_unregister('iterator');
|
||||
throw $e;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \BadMethodCallException
|
||||
*/
|
||||
public function testInvalidSource() {
|
||||
stream_wrapper_register('iterator', '\Icewind\Streams\IteratorDirectory');
|
||||
$context = stream_context_create(array(
|
||||
'dir' => array(
|
||||
'array' => 2
|
||||
)
|
||||
));
|
||||
try {
|
||||
opendir('iterator://', $context);
|
||||
stream_wrapper_unregister('iterator');
|
||||
} catch (\Exception $e) {
|
||||
stream_wrapper_unregister('iterator');
|
||||
throw $e;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \BadMethodCallException
|
||||
*/
|
||||
public function testWrapInvalidSource() {
|
||||
$this->wrapSource(2);
|
||||
}
|
||||
|
||||
public function fileListProvider() {
|
||||
$longList = array_fill(0, 500, 'foo');
|
||||
return array(
|
||||
array(
|
||||
array(
|
||||
'foo',
|
||||
'bar',
|
||||
'qwerty'
|
||||
)
|
||||
),
|
||||
array(
|
||||
array(
|
||||
'with spaces',
|
||||
'under_scores',
|
||||
'日本語',
|
||||
'character %$_',
|
||||
'.',
|
||||
'0',
|
||||
'double "quotes"',
|
||||
"single 'quotes'"
|
||||
)
|
||||
),
|
||||
array(
|
||||
array(
|
||||
'single item'
|
||||
)
|
||||
),
|
||||
array(
|
||||
$longList
|
||||
),
|
||||
array(
|
||||
array()
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
protected function basicTest($fileList, $dh) {
|
||||
$result = array();
|
||||
|
||||
while (($file = readdir($dh)) !== false) {
|
||||
$result[] = $file;
|
||||
}
|
||||
|
||||
$this->assertEquals($fileList, $result);
|
||||
|
||||
rewinddir($dh);
|
||||
if (count($fileList)) {
|
||||
$this->assertEquals($fileList[0], readdir($dh));
|
||||
} else {
|
||||
$this->assertFalse(readdir($dh));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider fileListProvider
|
||||
*/
|
||||
public function testBasicIterator($fileList) {
|
||||
$iterator = new \ArrayIterator($fileList);
|
||||
$dh = $this->wrapSource($iterator);
|
||||
$this->basicTest($fileList, $dh);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider fileListProvider
|
||||
*/
|
||||
public function testBasicArray($fileList) {
|
||||
$dh = $this->wrapSource($fileList);
|
||||
$this->basicTest($fileList, $dh);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue