ResultSetDecoratorTest.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  11. * @link https://cakephp.org CakePHP(tm) Project
  12. * @since 3.0.0
  13. * @license https://opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\Datasource;
  16. use Cake\Datasource\ResultSetDecorator;
  17. use Cake\TestSuite\TestCase;
  18. /**
  19. * Tests ResultSetDecorator class
  20. */
  21. class ResultSetDecoratorTest extends TestCase
  22. {
  23. /**
  24. * Tests the decorator can wrap a simple iterator
  25. *
  26. * @return void
  27. */
  28. public function testDecorateSimpleIterator()
  29. {
  30. $data = new \ArrayIterator([1, 2, 3]);
  31. $decorator = new ResultSetDecorator($data);
  32. $this->assertEquals([1, 2, 3], iterator_to_array($decorator));
  33. }
  34. /**
  35. * Tests it toArray() method
  36. *
  37. * @return void
  38. */
  39. public function testToArray()
  40. {
  41. $data = new \ArrayIterator([1, 2, 3]);
  42. $decorator = new ResultSetDecorator($data);
  43. $this->assertEquals([1, 2, 3], $decorator->toArray());
  44. }
  45. /**
  46. * Tests json encoding method
  47. *
  48. * @return void
  49. */
  50. public function testToJson()
  51. {
  52. $data = new \ArrayIterator([1, 2, 3]);
  53. $decorator = new ResultSetDecorator($data);
  54. $this->assertEquals(json_encode([1, 2, 3]), json_encode($decorator));
  55. }
  56. /**
  57. * Tests serializing and unserializing the decorator
  58. *
  59. * @return void
  60. */
  61. public function testSerialization()
  62. {
  63. $data = new \ArrayIterator([1, 2, 3]);
  64. $decorator = new ResultSetDecorator($data);
  65. $serialized = serialize($decorator);
  66. $this->assertEquals([1, 2, 3], unserialize($serialized)->toArray());
  67. }
  68. /**
  69. * Test the first() method which is part of the ResultSet duck type.
  70. *
  71. * @return void
  72. */
  73. public function testFirst()
  74. {
  75. $data = new \ArrayIterator([1, 2, 3]);
  76. $decorator = new ResultSetDecorator($data);
  77. $this->assertEquals(1, $decorator->first());
  78. $this->assertEquals(1, $decorator->first());
  79. }
  80. /**
  81. * Test the count() method which is part of the ResultSet duck type.
  82. *
  83. * @return void
  84. */
  85. public function testCount()
  86. {
  87. $data = new \ArrayIterator([1, 2, 3]);
  88. $decorator = new ResultSetDecorator($data);
  89. $this->assertEquals(3, $decorator->count());
  90. $this->assertCount(3, $decorator);
  91. }
  92. }