ResultSetDecoratorTest.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. /**
  25. * Tests the decorator can wrap a simple iterator
  26. *
  27. * @return void
  28. */
  29. public function testDecorateSimpleIterator()
  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. * @return void
  39. */
  40. public function testToArray()
  41. {
  42. $data = new \ArrayIterator([1, 2, 3]);
  43. $decorator = new ResultSetDecorator($data);
  44. $this->assertEquals([1, 2, 3], $decorator->toArray());
  45. }
  46. /**
  47. * Tests json encoding method
  48. *
  49. * @return void
  50. */
  51. public function testToJson()
  52. {
  53. $data = new \ArrayIterator([1, 2, 3]);
  54. $decorator = new ResultSetDecorator($data);
  55. $this->assertEquals(json_encode([1, 2, 3]), json_encode($decorator));
  56. }
  57. /**
  58. * Tests serializing and unserializing the decorator
  59. *
  60. * @return void
  61. */
  62. public function testSerialization()
  63. {
  64. $data = new \ArrayIterator([1, 2, 3]);
  65. $decorator = new ResultSetDecorator($data);
  66. $serialized = serialize($decorator);
  67. $this->assertEquals([1, 2, 3], unserialize($serialized)->toArray());
  68. }
  69. /**
  70. * Test the first() method which is part of the ResultSet duck type.
  71. *
  72. * @return void
  73. */
  74. public function testFirst()
  75. {
  76. $data = new \ArrayIterator([1, 2, 3]);
  77. $decorator = new ResultSetDecorator($data);
  78. $this->assertEquals(1, $decorator->first());
  79. $this->assertEquals(1, $decorator->first());
  80. }
  81. /**
  82. * Test the count() method which is part of the ResultSet duck type.
  83. *
  84. * @return void
  85. */
  86. public function testCount()
  87. {
  88. $data = new \ArrayIterator([1, 2, 3]);
  89. $decorator = new ResultSetDecorator($data);
  90. $this->assertEquals(3, $decorator->count());
  91. $this->assertCount(3, $decorator);
  92. }
  93. }