JsonableBehaviorTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. <?php
  2. namespace Tools\Test\TestCase\Model\Behavior;
  3. use Cake\Core\Configure;
  4. use Cake\ORM\TableRegistry;
  5. use Tools\Model\Behavior\JsonableBehavior;
  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. * JsonableBehaviorTest::testFieldsWithList()
  63. *
  64. * @return void
  65. */
  66. public function testFieldsWithList() {
  67. //echo $this->_header(__FUNCTION__);
  68. $this->Comments->removeBehavior('Jsonable');
  69. $this->Comments->addBehavior('Tools.Jsonable', ['fields' => ['details'], 'input' => 'list']);
  70. $data = [
  71. 'comment' => 'blabla',
  72. 'url' => 'www.dereuromark.de',
  73. 'title' => 'some Name',
  74. 'details' => 'z|y|x',
  75. ];
  76. $entity = $this->Comments->newEntity($data);
  77. $res = $this->Comments->save($entity);
  78. $this->assertTrue((bool)$res);
  79. $this->assertSame('["z","y","x"]', $res['details']);
  80. // with sort and unique
  81. $data = [
  82. 'comment' => 'blabla',
  83. 'url' => 'www.dereuromark.de',
  84. 'title' => 'some Name',
  85. 'details' => 'z|x|y|x',
  86. ];
  87. $this->Comments->removeBehavior('Jsonable');
  88. $this->Comments->addBehavior('Tools.Jsonable', ['fields' => ['details'], 'input' => 'list', 'sort' => true]);
  89. $entity = $this->Comments->newEntity($data);
  90. $res = $this->Comments->save($entity);
  91. $this->assertTrue((bool)$res);
  92. $this->assertSame('["x","y","z"]', $res['details']);
  93. }
  94. /**
  95. * JsonableBehaviorTest::testFieldsWithParam()
  96. *
  97. * @return void
  98. */
  99. public function testFieldsWithParam() {
  100. $this->Comments->removeBehavior('Jsonable');
  101. $this->Comments->addBehavior('Tools.Jsonable', ['fields' => ['details'], 'input' => 'param']);
  102. $data = [
  103. 'comment' => 'blabla',
  104. 'url' => 'www.dereuromark.de',
  105. 'title' => 'some Name',
  106. 'details' => 'z:vz|y:yz|x:xz',
  107. ];
  108. $entity = $this->Comments->newEntity($data);
  109. $res = $this->Comments->save($entity);
  110. $this->assertTrue((bool)$res);
  111. $this->assertSame('{"z":"vz","y":"yz","x":"xz"}', $res['details']);
  112. }
  113. /**
  114. * JsonableBehaviorTest::testFieldsOnFind()
  115. *
  116. * @return void
  117. */
  118. public function testFieldsOnFind() {
  119. //echo $this->_header(__FUNCTION__);
  120. // array
  121. $this->Comments->removeBehavior('Jsonable');
  122. $this->Comments->addBehavior('Tools.Jsonable', ['fields' => ['details']]);
  123. $data = [
  124. 'comment' => 'blabla',
  125. 'url' => 'www.dereuromark.de',
  126. 'title' => 'param',
  127. 'details' => ['x' => 'y'],
  128. ];
  129. $entity = $this->Comments->newEntity($data);
  130. $res = $this->Comments->save($entity);
  131. $this->assertTrue((bool)$res);
  132. $res = $this->Comments->find('all', ['conditions' => ['title' => 'param']])->first();
  133. $this->assertEquals(['x' => 'y'], $res['details']);
  134. // param
  135. $this->Comments->removeBehavior('Jsonable');
  136. $res = $this->Comments->find('all', ['conditions' => ['title' => 'param']])->first();
  137. $this->assertEquals('{"x":"y"}', $res['details']);
  138. $this->Comments->addBehavior('Tools.Jsonable', ['output' => 'param', 'fields' => ['details']]);
  139. $res = $this->Comments->find('all', ['conditions' => ['title' => 'param']])->first();
  140. $this->assertEquals('x:y', $res['details']);
  141. // list
  142. $this->Comments->removeBehavior('Jsonable');
  143. $this->Comments->addBehavior('Tools.Jsonable', ['output' => 'list', 'fields' => ['details']]);
  144. $data = [
  145. 'comment' => 'blabla',
  146. 'url' => 'www.dereuromark.de',
  147. 'title' => 'list',
  148. 'details' => ['z', 'y', 'x'],
  149. ];
  150. $entity = $this->Comments->newEntity($data);
  151. $res = $this->Comments->save($entity);
  152. $this->assertTrue((bool)$res);
  153. $this->Comments->removeBehavior('Jsonable');
  154. $res = $this->Comments->find('all', ['conditions' => ['title' => 'list']])->first();
  155. $this->assertEquals('["z","y","x"]', $res['details']);
  156. $this->Comments->addBehavior('Tools.Jsonable', ['output' => 'list', 'fields' => ['details']]);
  157. $res = $this->Comments->find('all', ['conditions' => ['title' => 'list']])->first();
  158. $this->assertEquals('z|y|x', $res['details']);
  159. // custom separator
  160. $this->Comments->removeBehavior('Jsonable');
  161. $this->Comments->addBehavior('Tools.Jsonable', ['output' => 'list', 'separator' => ', ', 'fields' => ['details']]);
  162. // find first
  163. $res = $this->Comments->find('all', ['conditions' => ['title' => 'list']])->first();
  164. $this->assertEquals('z, y, x', $res['details']);
  165. // find all
  166. $res = $this->Comments->find('all', ['order' => ['title' => 'ASC']])->toArray();
  167. $this->assertEquals('z, y, x', $res[0]['details']);
  168. }
  169. /**
  170. * JsonableBehaviorTest::testEncodeParams()
  171. *
  172. * @return void
  173. */
  174. public function testEncodeParams() {
  175. // $depth param added in 5.5.0
  176. $this->skipIf(!version_compare(PHP_VERSION, '5.5.0', '>='));
  177. // Test encode depth = 1
  178. $this->Comments->removeBehavior('Jsonable');
  179. $this->Comments->addBehavior('Tools.Jsonable', ['fields' => ['details'], 'encodeParams' => ['depth' => 1]]);
  180. $data = [
  181. 'comment' => 'blabla',
  182. 'url' => 'www.dereuromark.de',
  183. 'title' => 'param',
  184. 'details' => ['x' => ['y' => 'z']],
  185. ];
  186. $entity = $this->Comments->newEntity($data);
  187. $this->Comments->save($entity);
  188. $res = $this->Comments->find('all', ['conditions' => ['title' => 'param']])->first();
  189. $expected = [];
  190. $this->assertEquals($expected, $res['details']);
  191. $this->Comments->truncate();
  192. // Test encode depth = 2
  193. $this->Comments->removeBehavior('Jsonable');
  194. $this->Comments->addBehavior('Tools.Jsonable', ['fields' => ['details'], 'encodeParams' => ['depth' => 2], 'decodeParams' => ['assoc' => false]]);
  195. $data = [
  196. 'comment' => 'blabla',
  197. 'url' => 'www.dereuromark.de',
  198. 'title' => 'param',
  199. 'details' => ['x' => ['y' => 'z']],
  200. ];
  201. $entity = $this->Comments->newEntity($data);
  202. $this->Comments->save($entity);
  203. $res = $this->Comments->find('all', ['conditions' => ['title' => 'param']])->first();
  204. $obj = new \stdClass();
  205. $obj->x = new \stdClass();
  206. $obj->x->y = 'z';
  207. $expected = $obj;
  208. $this->assertEquals($expected, $res['details']);
  209. }
  210. public function testEncodeParamsAssocFalse() {
  211. // $depth param added in 5.5.0
  212. $this->skipIf(!version_compare(PHP_VERSION, '5.5.0', '>='));
  213. // Test encode depth = 1
  214. $this->Comments->removeBehavior('Jsonable');
  215. $this->Comments->addBehavior('Tools.Jsonable', ['fields' => ['details'], 'encodeParams' => ['depth' => 1], 'decodeParams' => ['assoc' => false]]);
  216. $data = [
  217. 'comment' => 'blabla',
  218. 'url' => 'www.dereuromark.de',
  219. 'title' => 'param',
  220. 'details' => ['y' => 'yy'],
  221. ];
  222. $entity = $this->Comments->newEntity($data);
  223. $this->Comments->save($entity);
  224. $res = $this->Comments->find('all', ['conditions' => ['title' => 'param']])->first();
  225. $obj = new \stdClass();
  226. $obj->y = 'yy';
  227. $expected = $obj;
  228. $this->assertEquals($expected, $res['details']);
  229. $this->Comments->truncate();
  230. $this->Comments->removeBehavior('Jsonable');
  231. $this->Comments->addBehavior('Tools.Jsonable', ['fields' => ['details'], 'encodeParams' => ['depth' => 1], 'decodeParams' => ['assoc' => false]]);
  232. $data = [
  233. 'comment' => 'blabla',
  234. 'url' => 'www.dereuromark.de',
  235. 'title' => 'param',
  236. 'details' => ['y' => ['yy' => 'yyy']],
  237. ];
  238. $entity = $this->Comments->newEntity($data);
  239. $this->Comments->save($entity);
  240. $res = $this->Comments->find('all', ['conditions' => ['title' => 'param']])->first();
  241. $expected = null;
  242. $this->assertEquals($expected, $res['details']);
  243. }
  244. /**
  245. * JsonableBehaviorTest::testDecodeParams()
  246. *
  247. * @return void
  248. */
  249. public function testDecodeParams() {
  250. $this->Comments->removeBehavior('Jsonable');
  251. $this->Comments->addBehavior('Tools.Jsonable', ['output' => 'array', 'fields' => ['details'], 'decodeParams' => ['assoc' => false]]);
  252. $data = [
  253. 'comment' => 'blabla',
  254. 'url' => 'www.dereuromark.de',
  255. 'title' => 'param',
  256. 'details' => ['x' => ['y' => 'z']],
  257. ];
  258. $entity = $this->Comments->newEntity($data);
  259. $this->Comments->save($entity);
  260. // Test decode with default params
  261. $res = $this->Comments->find('all', ['conditions' => ['title' => 'param']])->first();
  262. $obj = new \stdClass();
  263. $obj->x = new \stdClass();
  264. $obj->x->y = 'z';
  265. $expected = $obj;
  266. $this->assertEquals($expected, $res['details']);
  267. // Test decode with assoc = true
  268. $this->Comments->removeBehavior('Jsonable');
  269. $this->Comments->addBehavior('Tools.Jsonable', ['fields' => ['details'], 'decodeParams' => ['assoc' => true]]);
  270. $res = $this->Comments->find('all', ['conditions' => ['title' => 'param']])->first();
  271. $expected = ['x' => ['y' => 'z']];
  272. $this->assertEquals($expected, $res['details']);
  273. // Test decode with assoc = true and depth = 2
  274. $this->Comments->removeBehavior('Jsonable');
  275. $this->Comments->addBehavior('Tools.Jsonable', ['fields' => ['details'], 'decodeParams' => ['assoc' => true, 'depth' => 2]]);
  276. $res = $this->Comments->find('all', ['conditions' => ['title' => 'param']])->first();
  277. $expected = [];
  278. $this->assertEquals($expected, $res['details']);
  279. // Test decode with assoc = true and depth = 3
  280. $this->Comments->removeBehavior('Jsonable');
  281. $this->Comments->addBehavior('Tools.Jsonable', ['fields' => ['details'], 'decodeParams' => ['assoc' => true, 'depth' => 3]]);
  282. $res = $this->Comments->find('all', ['conditions' => ['title' => 'param']])->first();
  283. $expected = ['x' => ['y' => 'z']];
  284. $this->assertEquals($expected, $res['details']);
  285. }
  286. }