JsonableBehaviorTest.php 11 KB

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