From 4b6aadaf33493e427677ca191c990e47aa790bb9 Mon Sep 17 00:00:00 2001 From: Chitoku Date: Wed, 29 Jul 2020 23:11:58 +0900 Subject: [PATCH] Test undefined properties on \Lampager\Cake\PaginationResult (#23) --- src/PaginationResult.php | 12 +++++++----- tests/TestCase/PaginationResultTest.php | 21 +++++++++++++++++++-- 2 files changed, 26 insertions(+), 7 deletions(-) diff --git a/src/PaginationResult.php b/src/PaginationResult.php index cdeaff0..4118b43 100644 --- a/src/PaginationResult.php +++ b/src/PaginationResult.php @@ -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 { @@ -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) { @@ -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. diff --git a/tests/TestCase/PaginationResultTest.php b/tests/TestCase/PaginationResultTest.php index 4c2c6d4..9968d20 100644 --- a/tests/TestCase/PaginationResultTest.php +++ b/tests/TestCase/PaginationResultTest.php @@ -10,6 +10,7 @@ use Generator; use IteratorAggregate; use Lampager\Cake\PaginationResult; +use PHPUnit\Framework\Error\Error; use PHPUnit\Framework\MockObject\MockObject; use Traversable; @@ -192,9 +193,9 @@ public function testDebugInfo(array $entities, $records, array $meta): void } /** - * @param Entity[] $entities + * @param Entity[] $entities * @param Entity[]|Traversable $records - * @param mixed[] $meta + * @param mixed[] $meta * @dataProvider arrayProvider * @dataProvider iteratorAggregateProvider */ @@ -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 $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' => [