JsonableBehaviorTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. <?php
  2. namespace Tools\Test\TestCase\Model\Behavior;
  3. use PDOException;
  4. use Shim\TestSuite\TestCase;
  5. use stdClass;
  6. class JsonableBehaviorTest extends TestCase {
  7. /**
  8. * @var array
  9. */
  10. protected array $fixtures = [
  11. 'plugin.Tools.JsonableComments',
  12. ];
  13. /**
  14. * @var \Tools\Model\Table\Table
  15. */
  16. protected $Comments;
  17. /**
  18. * @return void
  19. */
  20. public function setUp(): void {
  21. parent::setUp();
  22. $this->Comments = $this->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. $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. $this->Comments->removeBehavior('Jsonable');
  281. $this->Comments->addBehavior('Tools.Jsonable', [
  282. 'output' => 'array',
  283. 'fields' => ['details'],
  284. ]);
  285. $data = [
  286. 'comment' => 'blabla',
  287. 'url' => 'www.dereuromark.de',
  288. 'title' => 'param',
  289. 'details' => [
  290. 'foo' => 'bar',
  291. 'nan' => NAN,
  292. 'inf' => INF,
  293. ],
  294. ];
  295. $entity = $this->Comments->newEntity($data);
  296. $result = $this->Comments->save($entity);
  297. $this->assertTrue((bool)$result);
  298. $res = $this->Comments->get($entity->id);
  299. $expected = [
  300. 'foo' => 'bar',
  301. 'nan' => 0,
  302. 'inf' => 0,
  303. ];
  304. $this->assertSame($expected, $res->details);
  305. }
  306. /**
  307. * @return void
  308. */
  309. public function testEncodeWithNoParamsComplexContent() {
  310. $this->Comments->removeBehavior('Jsonable');
  311. $this->Comments->addBehavior('Tools.Jsonable', [
  312. 'output' => 'array',
  313. 'fields' => ['details'],
  314. 'encodeParams' => [
  315. 'options' => 0,
  316. ],
  317. ]);
  318. $data = [
  319. 'comment' => 'blabla',
  320. 'url' => 'www.dereuromark.de',
  321. 'title' => 'param',
  322. 'details' => [
  323. 'foo' => 'bar',
  324. 'nan' => NAN,
  325. 'inf' => INF,
  326. ],
  327. ];
  328. $entity = $this->Comments->newEntity($data);
  329. $this->expectException(PDOException::class);
  330. $this->Comments->save($entity);
  331. }
  332. /**
  333. * @return void
  334. */
  335. public function testEncodeWithNoParamsComplexContentNullable() {
  336. $this->Comments->removeBehavior('Jsonable');
  337. $this->Comments->addBehavior('Tools.Jsonable', [
  338. 'output' => 'array',
  339. 'fields' => ['details_nullable', 'details'],
  340. 'encodeParams' => [
  341. 'options' => 0,
  342. ],
  343. ]);
  344. $data = [
  345. 'comment' => 'blabla',
  346. 'url' => 'www.dereuromark.de',
  347. 'title' => 'param',
  348. 'details' => [
  349. ],
  350. 'details_nullable' => [
  351. 'foo' => 'bar',
  352. 'nan' => NAN,
  353. 'inf' => INF,
  354. ],
  355. ];
  356. $entity = $this->Comments->newEntity($data);
  357. $result = $this->Comments->save($entity);
  358. $this->assertTrue((bool)$result);
  359. $res = $this->Comments->get($entity->id);
  360. $this->assertSame([], $res->details);
  361. $this->assertNull($res->details_nullable);
  362. }
  363. }