-
Notifications
You must be signed in to change notification settings - Fork 266
PHPLIB-1184: Improve CallbackIterator #1126
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 4 commits
8e003c0
66ca4a4
3bac402
56a33c1
f3d1ddc
4818124
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
<?php | ||
|
||
namespace MongoDB\Tests\Model; | ||
|
||
use ArrayIterator; | ||
use MongoDB\Model\CallbackIterator; | ||
use MongoDB\Tests\TestCase; | ||
|
||
use function array_keys; | ||
use function iterator_to_array; | ||
use function strrev; | ||
|
||
class CallbackIteratorTest extends TestCase | ||
{ | ||
public function testArrayIteration(): void | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd propose rewriting all of these tests to use the following matrix:
Using a data provider for iterables and expected arrays would probably be most efficient. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @alcaeus I can make the changes if it frees you up. |
||
{ | ||
$expectedKey = 0; | ||
|
||
$original = [1, 2, 3]; | ||
|
||
$callbackIterator = new CallbackIterator( | ||
new ArrayIterator($original), | ||
function ($value, $key) use (&$expectedKey) { | ||
$this->assertSame($expectedKey, $key); | ||
$expectedKey++; | ||
|
||
return $value * 2; | ||
} | ||
); | ||
|
||
$this->assertSame([2, 4, 6], iterator_to_array($callbackIterator)); | ||
} | ||
|
||
public function testHashIteration(): void | ||
{ | ||
$expectedKey = 0; | ||
|
||
$original = ['a' => 1, 'b' => 2, 'c' => 3]; | ||
$expectedKeys = array_keys($original); | ||
|
||
$callbackIterator = new CallbackIterator( | ||
new ArrayIterator($original), | ||
function ($value, $key) use (&$expectedKey, $expectedKeys) { | ||
$this->assertSame($expectedKeys[$expectedKey], $key); | ||
$expectedKey++; | ||
|
||
return $value * 2; | ||
} | ||
); | ||
|
||
$this->assertSame(['a' => 2, 'b' => 4, 'c' => 6], iterator_to_array($callbackIterator)); | ||
} | ||
|
||
public function testWithCallable(): void | ||
{ | ||
$original = ['foo', 'bar', 'baz']; | ||
|
||
$callbackIterator = new CallbackIterator( | ||
new ArrayIterator($original), | ||
GromNaN marked this conversation as resolved.
Show resolved
Hide resolved
|
||
[self::class, 'reverseValue'] | ||
); | ||
|
||
$this->assertSame(['oof', 'rab', 'zab'], iterator_to_array($callbackIterator)); | ||
} | ||
|
||
public static function reverseValue($value, $key) | ||
{ | ||
return strrev($value); | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.