JsonableBehaviorTest.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. <?php
  2. App::uses('JsonableBehavior', 'Tools.Model/Behavior');
  3. App::uses('AppModel', 'Model');
  4. App::uses('MyCakeTestCase', 'Tools.TestSuite');
  5. class JsonableBehaviorTest extends MyCakeTestCase {
  6. public $fixtures = [
  7. 'plugin.tools.jsonable_comment'
  8. ];
  9. public $Comment;
  10. public function setUp() {
  11. parent::setUp();
  12. $this->Comment = ClassRegistry::init('JsonableComment');
  13. $this->Comment->Behaviors->load('Tools.Jsonable', []);
  14. }
  15. /** INPUT **/
  16. public function testBasic() {
  17. //echo $this->_header(__FUNCTION__);
  18. // accuracy >= 5
  19. $data = [
  20. 'comment' => 'blabla',
  21. 'url' => 'www.dereuromark.de',
  22. 'title' => 'some Name',
  23. 'details' => ['x' => 'y'],
  24. ];
  25. $res = $this->Comment->save($data);
  26. $this->assertTrue((bool)$res);
  27. $this->assertSame('{"x":"y"}', $res['JsonableComment']['details']);
  28. }
  29. public function testFieldsWithList() {
  30. //echo $this->_header(__FUNCTION__);
  31. $this->Comment->Behaviors->unload('Jsonable');
  32. $this->Comment->Behaviors->load('Tools.Jsonable', ['fields' => ['details'], 'input' => 'list']);
  33. $data = [
  34. 'comment' => 'blabla',
  35. 'url' => 'www.dereuromark.de',
  36. 'title' => 'some Name',
  37. 'details' => 'z|y|x',
  38. ];
  39. $res = $this->Comment->save($data);
  40. $this->assertTrue((bool)$res);
  41. $this->assertSame('["z","y","x"]', $res['JsonableComment']['details']);
  42. // with sort and unique
  43. $data = [
  44. 'comment' => 'blabla',
  45. 'url' => 'www.dereuromark.de',
  46. 'title' => 'some Name',
  47. 'details' => 'z|x|y|x',
  48. ];
  49. $this->Comment->Behaviors->unload('Jsonable');
  50. $this->Comment->Behaviors->load('Tools.Jsonable', ['fields' => ['details'], 'input' => 'list', 'sort' => true]);
  51. $res = $this->Comment->save($data);
  52. $this->assertTrue((bool)$res);
  53. $this->assertSame('["x","y","z"]', $res['JsonableComment']['details']);
  54. }
  55. public function testFieldsWithParam() {
  56. //echo $this->_header(__FUNCTION__);
  57. $this->Comment->Behaviors->unload('Jsonable');
  58. $this->Comment->Behaviors->load('Tools.Jsonable', ['fields' => ['details'], 'input' => 'param']);
  59. $data = [
  60. 'comment' => 'blabla',
  61. 'url' => 'www.dereuromark.de',
  62. 'title' => 'some Name',
  63. 'details' => 'z:vz|y:yz|x:xz',
  64. ];
  65. $res = $this->Comment->save($data);
  66. $this->assertTrue((bool)$res);
  67. $this->assertSame('{"z":"vz","y":"yz","x":"xz"}', $res['JsonableComment']['details']);
  68. }
  69. /** OUTPUT **/
  70. public function testFieldsOnFind() {
  71. //echo $this->_header(__FUNCTION__);
  72. // array
  73. $this->Comment->Behaviors->unload('Jsonable');
  74. $this->Comment->Behaviors->load('Tools.Jsonable', ['fields' => ['details']]);
  75. $data = [
  76. 'comment' => 'blabla',
  77. 'url' => 'www.dereuromark.de',
  78. 'title' => 'param',
  79. 'details' => ['x' => 'y'],
  80. ];
  81. $res = $this->Comment->save($data);
  82. $this->assertTrue((bool)$res);
  83. $res = $this->Comment->find('first', ['conditions' => ['title' => 'param']]);
  84. $this->assertEquals(['x' => 'y'], $res['JsonableComment']['details']);
  85. // param
  86. $this->Comment->Behaviors->unload('Jsonable');
  87. $res = $this->Comment->find('first', ['conditions' => ['title' => 'param']]);
  88. $this->assertEquals('{"x":"y"}', $res['JsonableComment']['details']);
  89. $this->Comment->Behaviors->load('Tools.Jsonable', ['output' => 'param', 'fields' => ['details']]);
  90. $res = $this->Comment->find('first', ['conditions' => ['title' => 'param']]);
  91. $this->assertEquals('x:y', $res['JsonableComment']['details']);
  92. // list
  93. $this->Comment->Behaviors->unload('Jsonable');
  94. $this->Comment->Behaviors->load('Tools.Jsonable', ['output' => 'list', 'fields' => ['details']]);
  95. $data = [
  96. 'comment' => 'blabla',
  97. 'url' => 'www.dereuromark.de',
  98. 'title' => 'list',
  99. 'details' => ['z', 'y', 'x'],
  100. ];
  101. $this->Comment->create();
  102. $res = $this->Comment->save($data);
  103. $this->assertTrue((bool)$res);
  104. $this->Comment->Behaviors->unload('Jsonable');
  105. $res = $this->Comment->find('first', ['conditions' => ['title' => 'list']]);
  106. $this->assertEquals('["z","y","x"]', $res['JsonableComment']['details']);
  107. $this->Comment->Behaviors->load('Tools.Jsonable', ['output' => 'list', 'fields' => ['details']]);
  108. $res = $this->Comment->find('first', ['conditions' => ['title' => 'list']]);
  109. $this->assertEquals('z|y|x', $res['JsonableComment']['details']);
  110. // custom separator
  111. $this->Comment->Behaviors->unload('Jsonable');
  112. $this->Comment->Behaviors->load('Tools.Jsonable', ['output' => 'list', 'separator' => ', ', 'fields' => ['details']]);
  113. // find first
  114. $res = $this->Comment->find('first', ['conditions' => ['title' => 'list']]);
  115. $this->assertEquals('z, y, x', $res['JsonableComment']['details']);
  116. // find all
  117. $res = $this->Comment->find('all', ['order' => ['title' => 'ASC']]);
  118. $this->assertEquals('z, y, x', $res[0]['JsonableComment']['details']);
  119. }
  120. public function testEncodeParams() {
  121. // $depth param added in 5.5.0
  122. $this->skipIf(!version_compare(PHP_VERSION, '5.5.0', '>='));
  123. // Test encode depth = 1
  124. $this->Comment->Behaviors->unload('Jsonable');
  125. $this->Comment->Behaviors->load('Tools.Jsonable', ['fields' => ['details'], 'encodeParams' => ['depth' => 1]]);
  126. $data = [
  127. 'comment' => 'blabla',
  128. 'url' => 'www.dereuromark.de',
  129. 'title' => 'param',
  130. 'details' => ['x' => ['y' => 'z']],
  131. ];
  132. $this->Comment->save($data);
  133. $res = $this->Comment->find('first', ['conditions' => ['title' => 'param']]);
  134. $expected = [];
  135. $this->assertEquals($expected, $res['JsonableComment']['details']);
  136. // Test encode depth = 2
  137. $this->Comment->Behaviors->unload('Jsonable');
  138. $this->Comment->Behaviors->load('Tools.Jsonable', ['fields' => ['details'], 'encodeParams' => ['depth' => 2]]);
  139. $data = [
  140. 'comment' => 'blabla',
  141. 'url' => 'www.dereuromark.de',
  142. 'title' => 'param',
  143. 'details' => ['x' => ['y' => 'z']],
  144. ];
  145. $this->Comment->save($data);
  146. $res = $this->Comment->find('first', ['conditions' => ['title' => 'param']]);
  147. $obj = new stdClass();
  148. $obj->y = 'z';
  149. $expected = ['x' => $obj];
  150. $this->assertEquals($expected, $res['JsonableComment']['details']);
  151. }
  152. public function testDecodeParams() {
  153. $this->Comment->Behaviors->load('Tools.Jsonable', ['fields' => ['details']]);
  154. $data = [
  155. 'comment' => 'blabla',
  156. 'url' => 'www.dereuromark.de',
  157. 'title' => 'param',
  158. 'details' => ['x' => ['y' => 'z']],
  159. ];
  160. $this->Comment->save($data);
  161. // Test decode with default params
  162. $res = $this->Comment->find('first', ['conditions' => ['title' => 'param']]);
  163. $obj = new stdClass();
  164. $obj->y = 'z';
  165. $expected = ['x' => $obj];
  166. $this->assertEquals($expected, $res['JsonableComment']['details']);
  167. // Test decode with assoc = true
  168. $this->Comment->Behaviors->unload('Jsonable');
  169. $this->Comment->Behaviors->load('Tools.Jsonable', ['fields' => ['details'], 'decodeParams' => ['assoc' => true]]);
  170. $res = $this->Comment->find('first', ['conditions' => ['title' => 'param']]);
  171. $expected = ['x' => ['y' => 'z']];
  172. $this->assertEquals($expected, $res['JsonableComment']['details']);
  173. // Test decode with assoc = true and depth = 2
  174. $this->Comment->Behaviors->unload('Jsonable');
  175. $this->Comment->Behaviors->load('Tools.Jsonable', ['fields' => ['details'], 'decodeParams' => ['assoc' => true, 'depth' => 2]]);
  176. $res = $this->Comment->find('first', ['conditions' => ['title' => 'param']]);
  177. $expected = [];
  178. $this->assertEquals($expected, $res['JsonableComment']['details']);
  179. // Test decode with assoc = true and depth = 3
  180. $this->Comment->Behaviors->unload('Jsonable');
  181. $this->Comment->Behaviors->load('Tools.Jsonable', ['fields' => ['details'], 'decodeParams' => ['assoc' => true, 'depth' => 3]]);
  182. $res = $this->Comment->find('first', ['conditions' => ['title' => 'param']]);
  183. $expected = ['x' => ['y' => 'z']];
  184. $this->assertEquals($expected, $res['JsonableComment']['details']);
  185. }
  186. }