Skip to content

Commit

Permalink
Test undefined properties on \Lampager\Cake\PaginationResult (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
chitoku-k authored Jul 29, 2020
1 parent fbff8a4 commit 4b6aada
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 deletions.
12 changes: 7 additions & 5 deletions src/PaginationResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@
* This class intentionally does not extend \Lampager\PaginationResult
* but has the same signature because \Cake\Datasource\ResultSetInterface
* already implements \Iterator which conflicts with \IteratorAggregate.
*
* @property-read mixed $records
* @property-read null|bool $hasPrevious
* @property-read null|mixed $previousCursor
* @property-read null|bool $hasNext
* @property-read null|mixed $nextCursor
*/
class PaginationResult implements ResultSetInterface
{
Expand Down Expand Up @@ -145,8 +151,6 @@ protected function getIterator(): Iterator

/**
* @param string $name The name of the parameter to fetch
*
* @return mixed
*/
public function __get(string $name)
{
Expand All @@ -159,10 +163,8 @@ public function __get(string $name)
'Undefined property via __get(): ' . $name . ' in ' . $trace[0]['file'] . ' on line ' . $trace[0]['line'],
E_USER_NOTICE
);
}

return null;
}

/**
* Returns an array that can be used to describe the internal state of this
* object.
Expand Down
21 changes: 19 additions & 2 deletions tests/TestCase/PaginationResultTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Generator;
use IteratorAggregate;
use Lampager\Cake\PaginationResult;
use PHPUnit\Framework\Error\Error;
use PHPUnit\Framework\MockObject\MockObject;
use Traversable;

Expand Down Expand Up @@ -192,9 +193,9 @@ public function testDebugInfo(array $entities, $records, array $meta): void
}

/**
* @param Entity[] $entities
* @param Entity[] $entities
* @param Entity[]|Traversable<Entity> $records
* @param mixed[] $meta
* @param mixed[] $meta
* @dataProvider arrayProvider
* @dataProvider iteratorAggregateProvider
*/
Expand All @@ -208,6 +209,22 @@ public function testPublicProperties(array $entities, $records, array $meta): vo
$this->assertEquals($meta['nextCursor'], $paginationResult->nextCursor);
}

/**
* @param Entity[] $entities
* @param Entity[]|Traversable<Entity> $records
* @param mixed[] $meta
* @dataProvider arrayProvider
* @dataProvider iteratorAggregateProvider
*/
public function testUndefinedProperties(array $entities, $records, array $meta): void
{
$this->expectException(Error::class);
$this->expectExceptionMessageMatches('/^Undefined property via __get\(\): undefinedProperty/');

$paginationResult = new PaginationResult($records, $meta);
$paginationResult->undefinedProperty;
}

public function arrayProvider(): Generator
{
yield 'Array iteration' => [
Expand Down

0 comments on commit 4b6aada

Please sign in to comment.