ResultSetDecoratorTest.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://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. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @since 3.0.0
  13. * @license http://www.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. */
  22. class ResultSetDecoratorTest extends TestCase {
  23. /**
  24. * Tests the decorator can wrap a simple iterator
  25. *
  26. * @return void
  27. */
  28. public function testDecorateSimpleIterator() {
  29. $data = new \ArrayIterator([1, 2, 3]);
  30. $decorator = new ResultSetDecorator($data);
  31. $this->assertEquals([1, 2, 3], iterator_to_array($decorator));
  32. }
  33. /**
  34. * Tests it toArray() method
  35. *
  36. * @return void
  37. */
  38. public function testToArray() {
  39. $data = new \ArrayIterator([1, 2, 3]);
  40. $decorator = new ResultSetDecorator($data);
  41. $this->assertEquals([1, 2, 3], $decorator->toArray());
  42. }
  43. /**
  44. * Tests json encoding method
  45. *
  46. * @return void
  47. */
  48. public function testToJson() {
  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. * @return void
  57. */
  58. public function testSerialization() {
  59. $data = new \ArrayIterator([1, 2, 3]);
  60. $decorator = new ResultSetDecorator($data);
  61. $serialized = serialize($decorator);
  62. $this->assertEquals([1, 2, 3], unserialize($serialized)->toArray());
  63. }
  64. /**
  65. * Test the first() method which is part of the ResultSet duck type.
  66. *
  67. * @return void
  68. */
  69. public function testFirst() {
  70. $data = new \ArrayIterator([1, 2, 3]);
  71. $decorator = new ResultSetDecorator($data);
  72. $this->assertEquals(1, $decorator->first());
  73. $this->assertEquals(1, $decorator->first());
  74. }
  75. /**
  76. * Test the count() method which is part of the ResultSet duck type.
  77. *
  78. * @return void
  79. */
  80. public function testCount() {
  81. $data = new \ArrayIterator([1, 2, 3]);
  82. $decorator = new ResultSetDecorator($data);
  83. $this->assertEquals(3, $decorator->count());
  84. $this->assertCount(3, $decorator);
  85. }
  86. }