JsonableBehaviorTest.php 11 KB

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