Skip to content

Commit

Permalink
Use anonymous class for testing with IteratorAggregate (#40)
Browse files Browse the repository at this point in the history
  • Loading branch information
chitoku-k committed Mar 16, 2023
1 parent b07f9b2 commit 3efcb7c
Showing 1 changed file with 20 additions and 21 deletions.
41 changes: 20 additions & 21 deletions tests/TestCase/PaginationResultTest.php
Expand Up @@ -5,21 +5,19 @@
namespace Lampager\Cake\Test\TestCase;

use ArrayIterator;
use Cake\Datasource\ConnectionManager;
use Cake\I18n\FrozenTime;
use Cake\ORM\Entity;
use Generator;
use IteratorAggregate;
use Lampager\Cake\PaginationResult;
use PHPUnit\Framework\MockObject\MockObject;
use Traversable;

class PaginationResultTest extends TestCase
{
public function setUp(): void
{
parent::setUp();

set_error_handler(
static function ($errno, $errstr, $errfile, $errline) {
throw new \ErrorException($errstr, 0, $errno, $errfile, $errline);
Expand Down Expand Up @@ -375,23 +373,6 @@ public function arrayProvider(): Generator

public function iteratorAggregateProvider(): Generator
{
/** @var IteratorAggregate&MockObject $iteratorAggregate */
$iteratorAggregate = $this->getMockForAbstractClass(IteratorAggregate::class);
$iteratorAggregate->method('getIterator')->willReturn(new ArrayIterator([
new Entity([
'id' => 1,
'modified' => new FrozenTime('2017-01-01 10:00:00'),
]),
new Entity([
'id' => 3,
'modified' => new FrozenTime('2017-01-01 10:00:00'),
]),
new Entity([
'id' => 5,
'modified' => new FrozenTime('2017-01-01 10:00:00'),
]),
]));

yield 'IteratorAggregate iteration' => [
[
new Entity([
Expand All @@ -407,7 +388,25 @@ public function iteratorAggregateProvider(): Generator
'modified' => new FrozenTime('2017-01-01 10:00:00'),
]),
],
$iteratorAggregate,
new class implements IteratorAggregate {
public function getIterator(): Traversable
{
return new ArrayIterator([
new Entity([
'id' => 1,
'modified' => new FrozenTime('2017-01-01 10:00:00'),
]),
new Entity([
'id' => 3,
'modified' => new FrozenTime('2017-01-01 10:00:00'),
]),
new Entity([
'id' => 5,
'modified' => new FrozenTime('2017-01-01 10:00:00'),
]),
]);
}
},
[
'hasPrevious' => null,
'previousCursor' => null,
Expand Down

0 comments on commit 3efcb7c

Please sign in to comment.