JsonableBehaviorTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. <?php
  2. namespace Tools\Test\TestCase\Model\Behavior;
  3. use Cake\ORM\TableRegistry;
  4. use stdClass;
  5. use Tools\TestSuite\TestCase;
  6. class JsonableBehaviorTest extends TestCase {
  7. /**
  8. * @var array
  9. */
  10. public $fixtures = [
  11. 'plugin.Tools.JsonableComments'
  12. ];
  13. /**
  14. * @var \Tools\Model\Table\Table
  15. */
  16. public $Comments;
  17. /**
  18. * @return void
  19. */
  20. public function setUp() {
  21. parent::setUp();
  22. $this->Comments = TableRegistry::getTableLocator()->get('JsonableComments');
  23. $this->Comments->addBehavior('Tools.Jsonable', ['fields' => ['details']]);
  24. }
  25. /**
  26. * JsonableBehaviorTest::testBasic()
  27. *
  28. * @return void
  29. */
  30. public function testBasic() {
  31. // accuracy >= 5
  32. $data = [
  33. 'comment' => 'blabla',
  34. 'url' => 'www.dereuromark.de',
  35. 'title' => 'some Name',
  36. 'details' => ['x' => 'y'],
  37. ];
  38. $entity = $this->Comments->newEntity($data);
  39. $res = $this->Comments->save($entity);
  40. $this->assertTrue((bool)$res);
  41. $this->assertSame('{"x":"y"}', $res['details']);
  42. }
  43. /**
  44. * Find list should still work
  45. *
  46. * @return void
  47. */
  48. public function testList() {
  49. $data = [
  50. 'comment' => 'blabla',
  51. 'url' => 'www.dereuromark.de',
  52. 'title' => 'some Name',
  53. 'details' => ['x' => 'y'],
  54. ];
  55. $entity = $this->Comments->newEntity($data);
  56. $res = $this->Comments->save($entity);
  57. $this->assertTrue((bool)$res);
  58. $result = $this->Comments->find('list');
  59. $expected = [1 => 'some Name'];
  60. $this->assertSame($expected, $result->toArray());
  61. }
  62. /**
  63. * @return void
  64. */
  65. public function testFieldsWithList() {
  66. //echo $this->_header(__FUNCTION__);
  67. $this->Comments->removeBehavior('Jsonable');
  68. $this->Comments->addBehavior('Tools.Jsonable', ['fields' => ['details'], 'input' => 'list']);
  69. $data = [
  70. 'comment' => 'blabla',
  71. 'url' => 'www.dereuromark.de',
  72. 'title' => 'some Name',
  73. 'details' => 'z|y|x',
  74. ];
  75. $entity = $this->Comments->newEntity($data);
  76. $res = $this->Comments->save($entity);
  77. $this->assertTrue((bool)$res);
  78. $this->assertSame('["z","y","x"]', $res['details']);
  79. // with sort and unique
  80. $data = [
  81. 'comment' => 'blabla',
  82. 'url' => 'www.dereuromark.de',
  83. 'title' => 'some Name',
  84. 'details' => 'z|x|y|x',
  85. ];
  86. $this->Comments->removeBehavior('Jsonable');
  87. $this->Comments->addBehavior('Tools.Jsonable', ['fields' => ['details'], 'input' => 'list', 'sort' => true]);
  88. $entity = $this->Comments->newEntity($data);
  89. $res = $this->Comments->save($entity);
  90. $this->assertTrue((bool)$res);
  91. $this->assertSame('["x","y","z"]', $res['details']);
  92. }
  93. /**
  94. * @return void
  95. */
  96. public function testFieldsWithParam() {
  97. $this->Comments->removeBehavior('Jsonable');
  98. $this->Comments->addBehavior('Tools.Jsonable', ['fields' => ['details'], 'input' => 'param']);
  99. $data = [
  100. 'comment' => 'blabla',
  101. 'url' => 'www.dereuromark.de',
  102. 'title' => 'some Name',
  103. 'details' => 'z:vz|y:yz|x:xz',
  104. ];
  105. $entity = $this->Comments->newEntity($data);
  106. $res = $this->Comments->save($entity);
  107. $this->assertTrue((bool)$res);
  108. $this->assertSame('{"z":"vz","y":"yz","x":"xz"}', $res['details']);
  109. }
  110. /**
  111. * @return void
  112. */
  113. public function testFieldsOnFind() {
  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. * @return void
  165. */
  166. public function testEncodeParams() {
  167. // Test encode depth = 1
  168. $this->Comments->removeBehavior('Jsonable');
  169. $this->Comments->addBehavior('Tools.Jsonable', ['fields' => ['details'], 'encodeParams' => ['depth' => 1]]);
  170. $data = [
  171. 'comment' => 'blabla',
  172. 'url' => 'www.dereuromark.de',
  173. 'title' => 'param',
  174. 'details' => ['x' => ['y' => 'z']],
  175. ];
  176. $entity = $this->Comments->newEntity($data);
  177. $this->Comments->save($entity);
  178. $res = $this->Comments->find('all', ['conditions' => ['title' => 'param']])->first();
  179. $expected = ['x' => ['y' => 'z']];
  180. $this->assertEquals($expected, $res['details']);
  181. $this->Comments->truncate();
  182. // Test encode depth = 2
  183. $this->Comments->removeBehavior('Jsonable');
  184. $this->Comments->addBehavior('Tools.Jsonable', ['fields' => ['details'], 'encodeParams' => ['depth' => 2], 'decodeParams' => ['assoc' => false]]);
  185. $data = [
  186. 'comment' => 'blabla',
  187. 'url' => 'www.dereuromark.de',
  188. 'title' => 'param',
  189. 'details' => ['x' => ['y' => 'z']],
  190. ];
  191. $entity = $this->Comments->newEntity($data);
  192. $this->Comments->save($entity);
  193. $res = $this->Comments->find('all', ['conditions' => ['title' => 'param']])->first();
  194. $obj = new stdClass();
  195. $obj->x = new stdClass();
  196. $obj->x->y = 'z';
  197. $expected = $obj;
  198. $this->assertEquals($expected, $res['details']);
  199. }
  200. /**
  201. * @return void
  202. */
  203. public function testEncodeParamsAssocFalse() {
  204. // Test encode depth = 1
  205. $this->Comments->removeBehavior('Jsonable');
  206. $this->Comments->addBehavior('Tools.Jsonable', ['fields' => ['details'], 'encodeParams' => ['depth' => 1], 'decodeParams' => ['assoc' => false]]);
  207. $data = [
  208. 'comment' => 'blabla',
  209. 'url' => 'www.dereuromark.de',
  210. 'title' => 'param',
  211. 'details' => ['y' => 'yy'],
  212. ];
  213. $entity = $this->Comments->newEntity($data);
  214. $this->Comments->save($entity);
  215. $res = $this->Comments->find('all', ['conditions' => ['title' => 'param']])->first();
  216. $obj = new stdClass();
  217. $obj->y = 'yy';
  218. $expected = $obj;
  219. $this->assertEquals($expected, $res['details']);
  220. $this->Comments->truncate();
  221. $this->Comments->removeBehavior('Jsonable');
  222. $this->Comments->addBehavior('Tools.Jsonable', ['fields' => ['details'], 'encodeParams' => ['depth' => 1], 'decodeParams' => ['assoc' => false]]);
  223. $data = [
  224. 'comment' => 'blabla',
  225. 'url' => 'www.dereuromark.de',
  226. 'title' => 'param',
  227. 'details' => ['y' => ['yy' => 'yyy']],
  228. ];
  229. $entity = $this->Comments->newEntity($data);
  230. $this->Comments->save($entity);
  231. $res = $this->Comments->find('all', ['conditions' => ['title' => 'param']])->first();
  232. $expected = new stdClass();
  233. $expected->y = new stdClass();
  234. $expected->y->yy = 'yyy';
  235. $this->assertEquals($expected, $res['details']);
  236. }
  237. /**
  238. * @return void
  239. */
  240. public function testDecodeParams() {
  241. $this->Comments->removeBehavior('Jsonable');
  242. $this->Comments->addBehavior('Tools.Jsonable', ['output' => 'array', 'fields' => ['details'], 'decodeParams' => ['assoc' => false]]);
  243. $data = [
  244. 'comment' => 'blabla',
  245. 'url' => 'www.dereuromark.de',
  246. 'title' => 'param',
  247. 'details' => ['x' => ['y' => 'z']],
  248. ];
  249. $entity = $this->Comments->newEntity($data);
  250. $this->Comments->save($entity);
  251. // Test decode with default params
  252. $res = $this->Comments->find('all', ['conditions' => ['title' => 'param']])->first();
  253. $obj = new stdClass();
  254. $obj->x = new stdClass();
  255. $obj->x->y = 'z';
  256. $expected = $obj;
  257. $this->assertEquals($expected, $res['details']);
  258. // Test decode with assoc = true
  259. $this->Comments->removeBehavior('Jsonable');
  260. $this->Comments->addBehavior('Tools.Jsonable', ['fields' => ['details'], 'decodeParams' => ['assoc' => true]]);
  261. $res = $this->Comments->find('all', ['conditions' => ['title' => 'param']])->first();
  262. $expected = ['x' => ['y' => 'z']];
  263. $this->assertEquals($expected, $res['details']);
  264. // Test decode with assoc = true and depth = 2
  265. $this->Comments->removeBehavior('Jsonable');
  266. $this->Comments->addBehavior('Tools.Jsonable', ['fields' => ['details'], 'decodeParams' => ['assoc' => true, 'depth' => 2]]);
  267. $res = $this->Comments->find('all', ['conditions' => ['title' => 'param']])->first();
  268. $expected = [];
  269. $this->assertEquals($expected, $res['details']);
  270. // Test decode with assoc = true and depth = 3
  271. $this->Comments->removeBehavior('Jsonable');
  272. $this->Comments->addBehavior('Tools.Jsonable', ['fields' => ['details'], 'decodeParams' => ['assoc' => true, 'depth' => 3]]);
  273. $res = $this->Comments->find('all', ['conditions' => ['title' => 'param']])->first();
  274. $expected = ['x' => ['y' => 'z']];
  275. $this->assertEquals($expected, $res['details']);
  276. }
  277. /**
  278. * @return void
  279. */
  280. public function testEncodeWithComplexContent() {
  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. $this->Comments->removeBehavior('Jsonable');
  312. $this->Comments->addBehavior('Tools.Jsonable', [
  313. 'output' => 'array',
  314. 'fields' => ['details'],
  315. 'encodeParams' => [
  316. 'options' => 0
  317. ]
  318. ]);
  319. $data = [
  320. 'comment' => 'blabla',
  321. 'url' => 'www.dereuromark.de',
  322. 'title' => 'param',
  323. 'details' => [
  324. 'foo' => 'bar',
  325. 'nan' => NAN,
  326. 'inf' => INF,
  327. ],
  328. ];
  329. $entity = $this->Comments->newEntity($data);
  330. $result = $this->Comments->save($entity);
  331. $this->assertTrue((bool)$result);
  332. $res = $this->Comments->get($entity->id);
  333. $expected = [
  334. ];
  335. $this->assertSame($expected, $res->details);
  336. }
  337. }