Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #729 from WordPress/feature/648-split-test-class-u…
…tility-filterediteratortest
- Loading branch information
Showing
5 changed files
with
282 additions
and
217 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
<?php | ||
|
||
namespace WpOrg\Requests\Tests\Utility\FilteredIterator; | ||
|
||
use ReflectionObject; | ||
use stdClass; | ||
use WpOrg\Requests\Exception\InvalidArgument; | ||
use WpOrg\Requests\Tests\TestCase; | ||
use WpOrg\Requests\Tests\TypeProviderHelper; | ||
use WpOrg\Requests\Utility\FilteredIterator; | ||
|
||
/** | ||
* @covers \WpOrg\Requests\Utility\FilteredIterator::__construct | ||
*/ | ||
final class ConstructorTest extends TestCase { | ||
|
||
/** | ||
* Tests that valid $data is accepted by the constructor. | ||
* | ||
* @dataProvider dataValidData | ||
* | ||
* @param mixed $input Valid input. | ||
* | ||
* @return void | ||
*/ | ||
public function testValidData($input) { | ||
$this->assertInstanceOf(FilteredIterator::class, new FilteredIterator($input, 'ltrim')); | ||
} | ||
|
||
/** | ||
* Data Provider. | ||
* | ||
* @return array | ||
*/ | ||
public function dataValidData() { | ||
return TypeProviderHelper::getSelection(TypeProviderHelper::GROUP_ITERABLE); | ||
} | ||
|
||
/** | ||
* Tests receiving an exception when an invalid input type is passed as `$data` to the constructor. | ||
* | ||
* @dataProvider dataInvalidData | ||
* | ||
* @param mixed $input Invalid input. | ||
* | ||
* @return void | ||
*/ | ||
public function testInvalidData($input) { | ||
$this->expectException(InvalidArgument::class); | ||
$this->expectExceptionMessage('Argument #1 ($data) must be of type iterable'); | ||
|
||
new FilteredIterator($input, 'ltrim'); | ||
} | ||
|
||
/** | ||
* Data Provider. | ||
* | ||
* @return array | ||
*/ | ||
public function dataInvalidData() { | ||
return TypeProviderHelper::getAllExcept(TypeProviderHelper::GROUP_ITERABLE); | ||
} | ||
|
||
/** | ||
* Tests that valid $callback is accepted by the constructor. | ||
* | ||
* @dataProvider dataValidCallback | ||
* | ||
* @param mixed $input Valid input. | ||
* | ||
* @return void | ||
*/ | ||
public function testValidCallback($input) { | ||
$obj = new FilteredIterator([], $input); | ||
|
||
$reflection = new ReflectionObject($obj); | ||
$property = $reflection->getProperty('callback'); | ||
$property->setAccessible(true); | ||
$callback_value = $property->getValue($obj); | ||
$property->setAccessible(false); | ||
|
||
$this->assertSame($input, $callback_value, 'Callback property has not been set'); | ||
} | ||
|
||
/** | ||
* Data Provider. | ||
* | ||
* @return array | ||
*/ | ||
public function dataValidCallback() { | ||
return [ | ||
'existing PHP native function' => ['strtolower'], | ||
'dummy callback method' => [[$this, 'dummyCallback']], | ||
]; | ||
} | ||
|
||
/** | ||
* Verify that invalid callbacks are not accepted by the constructor. | ||
* | ||
* @dataProvider dataInvalidCallback | ||
* | ||
* @param mixed $input Invalid callback. | ||
* | ||
* @return void | ||
*/ | ||
public function testInvalidCallback($input) { | ||
$obj = new FilteredIterator([], $input); | ||
|
||
$reflection = new ReflectionObject($obj); | ||
$property = $reflection->getProperty('callback'); | ||
$property->setAccessible(true); | ||
$callback_value = $property->getValue($obj); | ||
$property->setAccessible(false); | ||
|
||
$this->assertNull($callback_value, 'Callback property has been set to invalid callback'); | ||
} | ||
|
||
/** | ||
* Data Provider. | ||
* | ||
* @return array | ||
*/ | ||
public function dataInvalidCallback() { | ||
return [ | ||
'null' => [null], | ||
'non-existent function' => ['functionname'], | ||
'plain object' => [new stdClass(), 'method'], | ||
]; | ||
} | ||
|
||
/** | ||
* Dummy callback method. | ||
* | ||
* @return void | ||
*/ | ||
public function dummyCallback() {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
<?php | ||
|
||
namespace WpOrg\Requests\Tests\Utility\FilteredIterator; | ||
|
||
use WpOrg\Requests\Tests\TestCase; | ||
use WpOrg\Requests\Utility\FilteredIterator; | ||
|
||
/** | ||
* @covers \WpOrg\Requests\Utility\FilteredIterator::current | ||
*/ | ||
final class CurrentTest extends TestCase { | ||
|
||
/** | ||
* Tests that when there is a valid callback, the current value is always filtered. | ||
* | ||
* @dataProvider dataCallbackIsAppliedIfValid | ||
* | ||
* @param iterable $data The array or object to be iterated on. | ||
* @param callable $callback Callback to be called on each value. | ||
* @param array $expected The values expected to be seen during iteration. | ||
* | ||
* @return void | ||
*/ | ||
public function testCallbackIsAppliedIfValid($data, $callback, $expected) { | ||
$iterator = new FilteredIterator($data, $callback); | ||
|
||
$actual = []; | ||
foreach ($iterator as $key => $value) { | ||
$actual[$key] = $value; | ||
} | ||
|
||
$this->assertSame($expected, $actual); | ||
} | ||
|
||
/** | ||
* Data Provider. | ||
* | ||
* @return array | ||
*/ | ||
public function dataCallbackIsAppliedIfValid() { | ||
$original = [ | ||
'key1' => 'lowercase', | ||
'key2' => 'UPPER CASE', | ||
'key3' => 'Sentence case', | ||
'key4' => 'Title Case', | ||
]; | ||
|
||
return [ | ||
'invalid callback: null' => [ | ||
'data' => $original, | ||
'callback' => null, | ||
'expected' => $original, | ||
], | ||
'invalid callback: function does not exist' => [ | ||
'data' => $original, | ||
'callback' => 'i_am_not_callable', | ||
'expected' => $original, | ||
], | ||
'valid callback' => [ | ||
'data' => $original, | ||
'callback' => 'strtolower', | ||
'expected' => [ | ||
'key1' => 'lowercase', | ||
'key2' => 'upper case', | ||
'key3' => 'sentence case', | ||
'key4' => 'title case', | ||
], | ||
], | ||
]; | ||
} | ||
} |
Oops, something went wrong.