jsonable.test.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. App::import('Behavior', 'Tools.Jsonable');
  3. App::import('Model', 'App');
  4. App::import('Vendor', 'MyCakeTestCase');
  5. class TestModel extends AppModel {
  6. var $name = 'TestModel';
  7. var $alias = 'TestModel';
  8. var $useTable = false;
  9. var $displayField = 'title';
  10. function find($type = null, $options = array(), $customData = null) {
  11. $data = array(
  12. 'comment' => 'blabla',
  13. 'url' => 'www.dereuromark.de',
  14. 'name' => 'some Name',
  15. 'details' => '{"x":"y"}',
  16. );
  17. if ($customData !== null) {
  18. $data = $customData;
  19. }
  20. $results = array($this->alias=>$data);
  21. $results = $this->__filterResults($results);
  22. return $results;
  23. }
  24. }
  25. class JsonableTestCase extends MyCakeTestCase {
  26. /*
  27. var $fixtures = array(
  28. 'core.comment'
  29. );
  30. */
  31. var $Comment;
  32. function startTest() {
  33. //$this->Comment =& ClassRegistry::init('Comment');
  34. $this->Comment = new TestModel();
  35. $this->Comment->Behaviors->attach('Jsonable', array());
  36. }
  37. /** INPUT **/
  38. function testBasic() {
  39. echo $this->_header(__FUNCTION__);
  40. // accuracy >= 5
  41. $data = array(
  42. 'comment' => 'blabla',
  43. 'url' => 'www.dereuromark.de',
  44. 'name' => 'some Name',
  45. 'details' => array('x'=>'y'),
  46. );
  47. $res = $this->Comment->save($data);
  48. $this->assertTrue($res);
  49. $res = $this->Comment->data;
  50. echo returns($res);
  51. $this->assertIdentical($res['TestModel']['details'], '{"x":"y"}');
  52. }
  53. function testFieldsWithList() {
  54. echo $this->_header(__FUNCTION__);
  55. $this->Comment->Behaviors->detach('Jsonable');
  56. $this->Comment->Behaviors->attach('Jsonable', array('fields'=>array('details'), 'input'=>'list'));
  57. $data = array(
  58. 'comment' => 'blabla',
  59. 'url' => 'www.dereuromark.de',
  60. 'name' => 'some Name',
  61. 'details' => 'z|y|x',
  62. );
  63. $res = $this->Comment->save($data);
  64. $this->assertTrue($res);
  65. $res = $this->Comment->data;
  66. echo returns($res);
  67. $this->assertIdentical($res['TestModel']['details'], '["z","y","x"]');
  68. # with sort and unique
  69. $data = array(
  70. 'comment' => 'blabla',
  71. 'url' => 'www.dereuromark.de',
  72. 'name' => 'some Name',
  73. 'details' => 'z|x|y|x',
  74. );
  75. $this->Comment->Behaviors->detach('Jsonable');
  76. $this->Comment->Behaviors->attach('Jsonable', array('fields'=>array('details'), 'input'=>'list', 'sort'=>true));
  77. $res = $this->Comment->save($data);
  78. $this->assertTrue($res);
  79. $res = $this->Comment->data;
  80. echo returns($res);
  81. $this->assertIdentical($res['TestModel']['details'], '["x","y","z"]');
  82. }
  83. function testFieldsWithParam() {
  84. echo $this->_header(__FUNCTION__);
  85. $this->Comment->Behaviors->detach('Jsonable');
  86. $this->Comment->Behaviors->attach('Jsonable', array('fields'=>array('details'), 'input'=>'param'));
  87. $data = array(
  88. 'comment' => 'blabla',
  89. 'url' => 'www.dereuromark.de',
  90. 'name' => 'some Name',
  91. 'details' => 'z:vz|y:yz|x:xz',
  92. );
  93. $res = $this->Comment->save($data);
  94. $this->assertTrue($res);
  95. $res = $this->Comment->data;
  96. echo returns($res);
  97. $this->assertIdentical($res['TestModel']['details'], '{"z":"vz","y":"yz","x":"xz"}');
  98. }
  99. /** OUTPUT **/
  100. function _testFieldsOnFind() {
  101. echo $this->_header(__FUNCTION__);
  102. $this->Comment->Behaviors->detach('Jsonable');
  103. $this->Comment->Behaviors->attach('Jsonable', array('fields'=>array('details')));
  104. $res = $this->Comment->find('first', array());
  105. $this->assertEqual($res['TestModel']['details'], array('x'=>'y'));
  106. pr($res);
  107. $this->Comment->Behaviors->detach('Jsonable');
  108. $this->Comment->Behaviors->attach('Jsonable', array('output'=>'param', 'fields'=>array('details')));
  109. $res = $this->Comment->find('first', array());
  110. $this->assertEqual($res['TestModel']['details'], array('{"x":"y"}'));
  111. pr($res);
  112. $this->Comment->Behaviors->detach('Jsonable');
  113. $this->Comment->Behaviors->attach('Jsonable', array('output'=>'list', 'fields'=>array('details')));
  114. $data = array(
  115. 'comment' => 'blabla',
  116. 'url' => 'www.dereuromark.de',
  117. 'name' => 'some Name',
  118. 'details' => '["z","y","x"]',
  119. );
  120. $res = $this->Comment->find('first', array(), $data);
  121. $this->assertEqual($res['TestModel']['details'], array('{"x":"y"}'));
  122. pr($res);
  123. }
  124. }