ResultSetDecoratorTest.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 3.0.0
  14. * @license https://opensource.org/licenses/mit-license.php MIT License
  15. */
  16. namespace Cake\Test\TestCase\Datasource;
  17. use ArrayIterator;
  18. use Cake\Core\Configure;
  19. use Cake\Datasource\ResultSetDecorator;
  20. use Cake\TestSuite\TestCase;
  21. /**
  22. * Tests ResultSetDecorator class
  23. */
  24. class ResultSetDecoratorTest extends TestCase
  25. {
  26. /**
  27. * Tests the decorator can wrap a simple iterator
  28. */
  29. public function testDecorateSimpleIterator(): void
  30. {
  31. $data = new ArrayIterator([1, 2, 3]);
  32. $decorator = new ResultSetDecorator($data);
  33. $this->assertEquals([1, 2, 3], iterator_to_array($decorator));
  34. }
  35. /**
  36. * Tests it toArray() method
  37. */
  38. public function testToArray(): void
  39. {
  40. $data = new ArrayIterator([1, 2, 3]);
  41. $decorator = new ResultSetDecorator($data);
  42. $this->assertEquals([1, 2, 3], $decorator->toArray());
  43. }
  44. /**
  45. * Tests JSON encoding method
  46. */
  47. public function testToJson(): void
  48. {
  49. $data = new ArrayIterator([1, 2, 3]);
  50. $decorator = new ResultSetDecorator($data);
  51. $this->assertEquals(json_encode([1, 2, 3]), json_encode($decorator));
  52. }
  53. /**
  54. * Tests serializing and unserializing the decorator
  55. */
  56. public function testSerialization(): void
  57. {
  58. $data = new ArrayIterator([1, 2, 3]);
  59. $decorator = new ResultSetDecorator($data);
  60. $serialized = serialize($decorator);
  61. $this->assertEquals([1, 2, 3], unserialize($serialized)->toArray());
  62. }
  63. /**
  64. * Test the first() method which is part of the ResultSet duck type.
  65. */
  66. public function testFirst(): void
  67. {
  68. $data = new ArrayIterator([1, 2, 3]);
  69. $decorator = new ResultSetDecorator($data);
  70. $this->assertSame(1, $decorator->first());
  71. $this->assertSame(1, $decorator->first());
  72. }
  73. /**
  74. * Test the count() method which is part of the ResultSet duck type.
  75. */
  76. public function testCount(): void
  77. {
  78. $data = new ArrayIterator([1, 2, 3]);
  79. $decorator = new ResultSetDecorator($data);
  80. $this->assertSame(3, $decorator->count());
  81. $this->assertCount(3, $decorator);
  82. }
  83. /**
  84. * Test the __debugInfo() method which is used by DebugKit
  85. */
  86. public function testDebugInfo(): void
  87. {
  88. Configure::write('App.ResultSetDebugLimit', 2);
  89. $data = new ArrayIterator([1, 2, 3]);
  90. $decorator = new ResultSetDecorator($data);
  91. $this->assertEquals([
  92. 'count' => 3,
  93. 'items' => [1, 2],
  94. ], $decorator->__debugInfo());
  95. }
  96. }