JsonableBehaviorTest.php 10 KB

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