JsonableBehaviorTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. <?php
  2. namespace Tools\Test\TestCase\Model\Behavior;
  3. use Cake\ORM\TableRegistry;
  4. use Tools\TestSuite\TestCase;
  5. use Cake\Core\Configure;
  6. use Tools\Model\Behavior\JsonableBehavior;
  7. class JsonableBehaviorTest extends TestCase {
  8. public $fixtures = [
  9. 'plugin.tools.jsonable_comments'
  10. ];
  11. public $Comments;
  12. public function setUp() {
  13. parent::setUp();
  14. Configure::write('App.namespace', 'TestApp');
  15. $this->Comments = TableRegistry::get('JsonableComments');
  16. $this->Comments->addBehavior('Tools.Jsonable', ['fields' => ['details']]);
  17. }
  18. /**
  19. * JsonableBehaviorTest::testBasic()
  20. *
  21. * @return void
  22. */
  23. public function testBasic() {
  24. // accuracy >= 5
  25. $data = [
  26. 'comment' => 'blabla',
  27. 'url' => 'www.dereuromark.de',
  28. 'title' => 'some Name',
  29. 'details' => ['x' => 'y'],
  30. ];
  31. $entity = $this->Comments->newEntity($data);
  32. $res = $this->Comments->save($entity);
  33. $this->assertTrue((bool)$res);
  34. $this->assertSame('{"x":"y"}', $res['details']);
  35. }
  36. /**
  37. * Find list should still work
  38. *
  39. * @return void
  40. */
  41. public function testList() {
  42. $data = [
  43. 'comment' => 'blabla',
  44. 'url' => 'www.dereuromark.de',
  45. 'title' => 'some Name',
  46. 'details' => ['x' => 'y'],
  47. ];
  48. $entity = $this->Comments->newEntity($data);
  49. $res = $this->Comments->save($entity);
  50. $this->assertTrue((bool)$res);
  51. $result = $this->Comments->find('list');
  52. $expected = [1 => 'some Name'];
  53. $this->assertSame($expected, $result->toArray());
  54. }
  55. /**
  56. * JsonableBehaviorTest::testFieldsWithList()
  57. *
  58. * @return void
  59. */
  60. public function testFieldsWithList() {
  61. //echo $this->_header(__FUNCTION__);
  62. $this->Comments->removeBehavior('Jsonable');
  63. $this->Comments->addBehavior('Tools.Jsonable', ['fields' => ['details'], 'input' => 'list']);
  64. $data = [
  65. 'comment' => 'blabla',
  66. 'url' => 'www.dereuromark.de',
  67. 'title' => 'some Name',
  68. 'details' => 'z|y|x',
  69. ];
  70. $entity = $this->Comments->newEntity($data);
  71. $res = $this->Comments->save($entity);
  72. $this->assertTrue((bool)$res);
  73. $this->assertSame('["z","y","x"]', $res['details']);
  74. // with sort and unique
  75. $data = [
  76. 'comment' => 'blabla',
  77. 'url' => 'www.dereuromark.de',
  78. 'title' => 'some Name',
  79. 'details' => 'z|x|y|x',
  80. ];
  81. $this->Comments->removeBehavior('Jsonable');
  82. $this->Comments->addBehavior('Tools.Jsonable', ['fields' => ['details'], 'input' => 'list', 'sort' => true]);
  83. $entity = $this->Comments->newEntity($data);
  84. $res = $this->Comments->save($entity);
  85. $this->assertTrue((bool)$res);
  86. $this->assertSame('["x","y","z"]', $res['details']);
  87. }
  88. /**
  89. * JsonableBehaviorTest::testFieldsWithParam()
  90. *
  91. * @return void
  92. */
  93. public function testFieldsWithParam() {
  94. $this->Comments->removeBehavior('Jsonable');
  95. $this->Comments->addBehavior('Tools.Jsonable', ['fields' => ['details'], 'input' => 'param']);
  96. $data = [
  97. 'comment' => 'blabla',
  98. 'url' => 'www.dereuromark.de',
  99. 'title' => 'some Name',
  100. 'details' => 'z:vz|y:yz|x:xz',
  101. ];
  102. $entity = $this->Comments->newEntity($data);
  103. $res = $this->Comments->save($entity);
  104. $this->assertTrue((bool)$res);
  105. $this->assertSame('{"z":"vz","y":"yz","x":"xz"}', $res['details']);
  106. }
  107. /**
  108. * JsonableBehaviorTest::testFieldsOnFind()
  109. *
  110. * @return void
  111. */
  112. public function testFieldsOnFind() {
  113. //echo $this->_header(__FUNCTION__);
  114. // array
  115. $this->Comments->removeBehavior('Jsonable');
  116. $this->Comments->addBehavior('Tools.Jsonable', ['fields' => ['details']]);
  117. $data = [
  118. 'comment' => 'blabla',
  119. 'url' => 'www.dereuromark.de',
  120. 'title' => 'param',
  121. 'details' => ['x' => 'y'],
  122. ];
  123. $entity = $this->Comments->newEntity($data);
  124. $res = $this->Comments->save($entity);
  125. $this->assertTrue((bool)$res);
  126. $res = $this->Comments->find('all', ['conditions' => ['title' => 'param']])->first();
  127. $this->assertEquals(['x' => 'y'], $res['details']);
  128. // param
  129. $this->Comments->removeBehavior('Jsonable');
  130. $res = $this->Comments->find('all', ['conditions' => ['title' => 'param']])->first();
  131. $this->assertEquals('{"x":"y"}', $res['details']);
  132. $this->Comments->addBehavior('Tools.Jsonable', ['output' => 'param', 'fields' => ['details']]);
  133. $res = $this->Comments->find('all', ['conditions' => ['title' => 'param']])->first();
  134. $this->assertEquals('x:y', $res['details']);
  135. // list
  136. $this->Comments->removeBehavior('Jsonable');
  137. $this->Comments->addBehavior('Tools.Jsonable', ['output' => 'list', 'fields' => ['details']]);
  138. $data = [
  139. 'comment' => 'blabla',
  140. 'url' => 'www.dereuromark.de',
  141. 'title' => 'list',
  142. 'details' => ['z', 'y', 'x'],
  143. ];
  144. $entity = $this->Comments->newEntity($data);
  145. $res = $this->Comments->save($entity);
  146. $this->assertTrue((bool)$res);
  147. $this->Comments->removeBehavior('Jsonable');
  148. $res = $this->Comments->find('all', ['conditions' => ['title' => 'list']])->first();
  149. $this->assertEquals('["z","y","x"]', $res['details']);
  150. $this->Comments->addBehavior('Tools.Jsonable', ['output' => 'list', 'fields' => ['details']]);
  151. $res = $this->Comments->find('all', ['conditions' => ['title' => 'list']])->first();
  152. $this->assertEquals('z|y|x', $res['details']);
  153. // custom separator
  154. $this->Comments->removeBehavior('Jsonable');
  155. $this->Comments->addBehavior('Tools.Jsonable', ['output' => 'list', 'separator' => ', ', 'fields' => ['details']]);
  156. // find first
  157. $res = $this->Comments->find('all', ['conditions' => ['title' => 'list']])->first();
  158. $this->assertEquals('z, y, x', $res['details']);
  159. // find all
  160. $res = $this->Comments->find('all', ['order' => ['title' => 'ASC']])->toArray();
  161. $this->assertEquals('z, y, x', $res[0]['details']);
  162. }
  163. /**
  164. * JsonableBehaviorTest::testEncodeParams()
  165. *
  166. * @return void
  167. */
  168. public function testEncodeParams() {
  169. // $depth param added in 5.5.0
  170. $this->skipIf(!version_compare(PHP_VERSION, '5.5.0', '>='));
  171. // Test encode depth = 1
  172. $this->Comments->removeBehavior('Jsonable');
  173. $this->Comments->addBehavior('Tools.Jsonable', ['fields' => ['details'], 'encodeParams' => ['depth' => 1]]);
  174. $data = [
  175. 'comment' => 'blabla',
  176. 'url' => 'www.dereuromark.de',
  177. 'title' => 'param',
  178. 'details' => ['x' => ['y' => 'z']],
  179. ];
  180. $entity = $this->Comments->newEntity($data);
  181. $this->Comments->save($entity);
  182. $res = $this->Comments->find('all', ['conditions' => ['title' => 'param']])->first();
  183. $expected = [];
  184. $this->assertEquals($expected, $res['details']);
  185. $this->Comments->truncate();
  186. // Test encode depth = 2
  187. $this->Comments->removeBehavior('Jsonable');
  188. $this->Comments->addBehavior('Tools.Jsonable', ['fields' => ['details'], 'encodeParams' => ['depth' => 2], 'decodeParams' => ['assoc' => false]]);
  189. $data = [
  190. 'comment' => 'blabla',
  191. 'url' => 'www.dereuromark.de',
  192. 'title' => 'param',
  193. 'details' => ['x' => ['y' => 'z']],
  194. ];
  195. $entity = $this->Comments->newEntity($data);
  196. $this->Comments->save($entity);
  197. $res = $this->Comments->find('all', ['conditions' => ['title' => 'param']])->first();
  198. $obj = new \stdClass();
  199. $obj->x = new \stdClass();
  200. $obj->x->y = 'z';
  201. $expected = $obj;
  202. $this->assertEquals($expected, $res['details']);
  203. }
  204. public function testEncodeParamsAssocFalse() {
  205. // $depth param added in 5.5.0
  206. $this->skipIf(!version_compare(PHP_VERSION, '5.5.0', '>='));
  207. // Test encode depth = 1
  208. $this->Comments->removeBehavior('Jsonable');
  209. $this->Comments->addBehavior('Tools.Jsonable', ['fields' => ['details'], 'encodeParams' => ['depth' => 1], 'decodeParams' => ['assoc' => false]]);
  210. $data = [
  211. 'comment' => 'blabla',
  212. 'url' => 'www.dereuromark.de',
  213. 'title' => 'param',
  214. 'details' => ['y' => 'yy'],
  215. ];
  216. $entity = $this->Comments->newEntity($data);
  217. $this->Comments->save($entity);
  218. $res = $this->Comments->find('all', ['conditions' => ['title' => 'param']])->first();
  219. $obj = new \stdClass();
  220. $obj->y = 'yy';
  221. $expected = $obj;
  222. $this->assertEquals($expected, $res['details']);
  223. $this->Comments->truncate();
  224. $this->Comments->removeBehavior('Jsonable');
  225. $this->Comments->addBehavior('Tools.Jsonable', ['fields' => ['details'], 'encodeParams' => ['depth' => 1], 'decodeParams' => ['assoc' => false]]);
  226. $data = [
  227. 'comment' => 'blabla',
  228. 'url' => 'www.dereuromark.de',
  229. 'title' => 'param',
  230. 'details' => ['y' => ['yy' => 'yyy']],
  231. ];
  232. $entity = $this->Comments->newEntity($data);
  233. $this->Comments->save($entity);
  234. $res = $this->Comments->find('all', ['conditions' => ['title' => 'param']])->first();
  235. $expected = null;
  236. $this->assertEquals($expected, $res['details']);
  237. }
  238. /**
  239. * JsonableBehaviorTest::testDecodeParams()
  240. *
  241. * @return void
  242. */
  243. public function testDecodeParams() {
  244. $this->Comments->removeBehavior('Jsonable');
  245. $this->Comments->addBehavior('Tools.Jsonable', ['output' => 'array', 'fields' => ['details'], 'decodeParams' => ['assoc'=> false]]);
  246. $data = [
  247. 'comment' => 'blabla',
  248. 'url' => 'www.dereuromark.de',
  249. 'title' => 'param',
  250. 'details' => ['x' => ['y' => 'z']],
  251. ];
  252. $entity = $this->Comments->newEntity($data);
  253. $this->Comments->save($entity);
  254. // Test decode with default params
  255. $res = $this->Comments->find('all', ['conditions' => ['title' => 'param']])->first();
  256. $obj = new \stdClass();
  257. $obj->x = new \stdClass();
  258. $obj->x->y = 'z';
  259. $expected = $obj;
  260. $this->assertEquals($expected, $res['details']);
  261. // Test decode with assoc = true
  262. $this->Comments->removeBehavior('Jsonable');
  263. $this->Comments->addBehavior('Tools.Jsonable', ['fields' => ['details'], 'decodeParams' => ['assoc' => true]]);
  264. $res = $this->Comments->find('all', ['conditions' => ['title' => 'param']])->first();
  265. $expected = ['x' => ['y' => 'z']];
  266. $this->assertEquals($expected, $res['details']);
  267. // Test decode with assoc = true and depth = 2
  268. $this->Comments->removeBehavior('Jsonable');
  269. $this->Comments->addBehavior('Tools.Jsonable', ['fields' => ['details'], 'decodeParams' => ['assoc' => true, 'depth' => 2]]);
  270. $res = $this->Comments->find('all', ['conditions' => ['title' => 'param']])->first();
  271. $expected = [];
  272. $this->assertEquals($expected, $res['details']);
  273. // Test decode with assoc = true and depth = 3
  274. $this->Comments->removeBehavior('Jsonable');
  275. $this->Comments->addBehavior('Tools.Jsonable', ['fields' => ['details'], 'decodeParams' => ['assoc' => true, 'depth' => 3]]);
  276. $res = $this->Comments->find('all', ['conditions' => ['title' => 'param']])->first();
  277. $expected = ['x' => ['y' => 'z']];
  278. $this->assertEquals($expected, $res['details']);
  279. }
  280. }