Skip to content

Commit

Permalink
Allows accessing public properties (#24)
Browse files Browse the repository at this point in the history
* Allows accessing public properties

This allows accessing public properties of the underlying `Lampager\PaginationResult` object (For example, `hasNext`, `nextCursor`, etc.)

* Add typehint

* Add test for public properties

* Test undefined properties on \Lampager\Cake\PaginationResult

Co-authored-by: Walther Lalk <[email protected]>
  • Loading branch information
chitoku-k and dakota authored Jul 29, 2020
1 parent d35ccca commit f135703
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/PaginationResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,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 @@ -142,6 +148,22 @@ protected function getIterator()
return $iterator;
}

/**
* @param string $name The name of the parameter to fetch
*/
public function __get($name)
{
if (property_exists($this->result, $name)) {
return $this->result->{$name};
}

$trace = debug_backtrace();
trigger_error(
'Undefined property via __get(): ' . $name . ' in ' . $trace[0]['file'] . ' on line ' . $trace[0]['line'],
E_USER_NOTICE
);
}

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

Expand Down Expand Up @@ -189,6 +190,39 @@ public function testDebugInfo(array $entities, $records, array $meta)
], $actual);
}

/**
* @param Entity[] $entities
* @param Entity[]|Traversable<Entity> $records
* @param mixed[] $meta
* @dataProvider arrayProvider
* @dataProvider iteratorAggregateProvider
*/
public function testPublicProperties(array $entities, $records, array $meta)
{
$paginationResult = new PaginationResult($records, $meta);

$this->assertEquals($meta['hasPrevious'], $paginationResult->hasPrevious);
$this->assertEquals($meta['previousCursor'], $paginationResult->previousCursor);
$this->assertEquals($meta['hasNext'], $paginationResult->hasNext);
$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)
{
$this->expectException(Error::class);
$this->expectExceptionMessageRegExp('/^Undefined property via __get\(\): undefinedProperty/');

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

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

0 comments on commit f135703

Please sign in to comment.