PaginatedResultSetTest.php 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  5. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  6. *
  7. * Licensed under The MIT License
  8. * For full copyright and license information, please see the LICENSE.txt
  9. * Redistributions of files must retain the above copyright notice
  10. *
  11. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  12. * @link https://cakephp.org CakePHP(tm) Project
  13. * @since 5.0.0
  14. * @license https://www.opensource.org/licenses/mit-license.php MIT License
  15. */
  16. namespace Cake\Test\TestCase\Datasource\Paging;
  17. use ArrayIterator;
  18. use Cake\Collection\Collection;
  19. use Cake\Datasource\Paging\PaginatedResultSet;
  20. use Cake\Datasource\ResultSetInterface;
  21. use Cake\ORM\ResultSet;
  22. use Cake\TestSuite\TestCase;
  23. use Mockery;
  24. use PHPUnit\Framework\Attributes\WithoutErrorHandler;
  25. use function Cake\Collection\collection;
  26. class PaginatedResultSetTest extends TestCase
  27. {
  28. public function testItems(): void
  29. {
  30. $resultSet = new class ([]) extends ResultSet {
  31. };
  32. $paginatedResults = new PaginatedResultSet(
  33. $resultSet,
  34. []
  35. );
  36. $this->assertInstanceOf(ResultSetInterface::class, $paginatedResults->items());
  37. }
  38. public function testToArray(): void
  39. {
  40. $paginatedResults = new PaginatedResultSet(new Collection([1, 2, 3]), []);
  41. $out = $paginatedResults->toArray();
  42. $this->assertSame([1, 2, 3], $out);
  43. }
  44. #[WithoutErrorHandler]
  45. public function testCall(): void
  46. {
  47. $resultSet = Mockery::mock(ResultSet::class);
  48. $resultSet->shouldReceive('extract')
  49. ->with('foo')
  50. ->once()
  51. ->andReturn(collection(['bar']));
  52. $paginatedResults = new PaginatedResultSet(
  53. $resultSet,
  54. []
  55. );
  56. $this->deprecated(function () use ($paginatedResults): void {
  57. $result = $paginatedResults->extract('foo')->toList();
  58. $this->assertEquals(['bar'], $result);
  59. });
  60. }
  61. public function testJsonEncode(): void
  62. {
  63. $paginatedResults = new PaginatedResultSet(
  64. new ArrayIterator([1 => 'a', 2 => 'b', 3 => 'c']),
  65. []
  66. );
  67. $this->assertEquals('{"1":"a","2":"b","3":"c"}', json_encode($paginatedResults));
  68. }
  69. public function testSerialization(): void
  70. {
  71. $paginatedResults = new PaginatedResultSet(
  72. new ArrayIterator([1 => 'a', 2 => 'b', 3 => 'c']),
  73. ['foo' => 'bar']
  74. );
  75. $serialized = serialize($paginatedResults);
  76. $unserialized = unserialize($serialized);
  77. $this->assertEquals($paginatedResults->pagingParams(), $unserialized->pagingParams());
  78. $this->assertEquals($paginatedResults->items(), $unserialized->items());
  79. }
  80. }