AjaxViewTest.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. /**
  3. * PHP 5
  4. *
  5. * Licensed under The MIT License
  6. * For full copyright and license information, please see the LICENSE.txt
  7. * Redistributions of files must retain the above copyright notice
  8. *
  9. * @author Mark Scherer
  10. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  11. */
  12. App::uses('Controller', 'Controller');
  13. App::uses('CakeRequest', 'Network');
  14. App::uses('CakeResponse', 'Network');
  15. App::uses('AjaxView', 'Tools.View');
  16. /**
  17. * AjaxViewTest
  18. *
  19. */
  20. class AjaxViewTest extends CakeTestCase {
  21. public $Ajax;
  22. /**
  23. * AjaxViewTest::setUp()
  24. *
  25. * @return void
  26. */
  27. public function setUp() {
  28. parent::setUp();
  29. $this->Ajax = new AjaxView();
  30. App::build(array(
  31. 'View' => array(CakePlugin::path('Tools') . 'Test' . DS . 'test_app' . DS . 'View' . DS)
  32. ), App::RESET);
  33. }
  34. /**
  35. * AjaxViewTest::testSerialize()
  36. *
  37. * @return void
  38. */
  39. public function testSerialize() {
  40. $Request = new CakeRequest();
  41. $Response = new CakeResponse();
  42. $Controller = new Controller($Request, $Response);
  43. $items = array(
  44. array('title' => 'Title One', 'link' => 'http://example.org/one', 'author' => 'one@example.org', 'description' => 'Content one'),
  45. array('title' => 'Title Two', 'link' => 'http://example.org/two', 'author' => 'two@example.org', 'description' => 'Content two'),
  46. );
  47. $Controller->set(array('items' => $items, '_serialize' => array('items')));
  48. $View = new AjaxView($Controller);
  49. $result = $View->render(false);
  50. $this->assertSame('application/json', $Response->type());
  51. $expected = array('error' => null, 'content' => null, 'items' => $items);
  52. $expected = json_encode($expected);
  53. $this->assertTextEquals($expected, $result);
  54. }
  55. /**
  56. * AjaxViewTest::testSerialize()
  57. *
  58. * @return void
  59. */
  60. public function testRenderWithSerialize() {
  61. $Request = new CakeRequest();
  62. $Response = new CakeResponse();
  63. $Controller = new Controller($Request, $Response);
  64. $items = array(
  65. array('title' => 'Title One', 'link' => 'http://example.org/one', 'author' => 'one@example.org', 'description' => 'Content one'),
  66. array('title' => 'Title Two', 'link' => 'http://example.org/two', 'author' => 'two@example.org', 'description' => 'Content two'),
  67. );
  68. $Controller->set(array('items' => $items, '_serialize' => 'items'));
  69. $View = new AjaxView($Controller);
  70. $View->viewPath = 'Items';
  71. $result = $View->render('index');
  72. $this->assertSame('application/json', $Response->type());
  73. $expected = array('error' => null, 'content' => 'My Index Test ctp', 'items' => $items);
  74. $expected = json_encode($expected);
  75. $this->assertTextEquals($expected, $result);
  76. }
  77. /**
  78. * AjaxViewTest::testError()
  79. *
  80. * @return void
  81. */
  82. public function testError() {
  83. $Request = new CakeRequest();
  84. $Response = new CakeResponse();
  85. $Controller = new Controller($Request, $Response);
  86. $items = array(
  87. array('title' => 'Title One', 'link' => 'http://example.org/one', 'author' => 'one@example.org', 'description' => 'Content one'),
  88. array('title' => 'Title Two', 'link' => 'http://example.org/two', 'author' => 'two@example.org', 'description' => 'Content two'),
  89. );
  90. $Controller->set(array('error' => 'Some message', 'items' => $items, '_serialize' => array('error', 'items')));
  91. $View = new AjaxView($Controller);
  92. $View->viewPath = 'Items';
  93. $result = $View->render('index');
  94. $this->assertSame('application/json', $Response->type());
  95. $expected = array('error' => 'Some message', 'content' => null, 'items' => $items);
  96. $expected = json_encode($expected);
  97. $this->assertTextEquals($expected, $result);
  98. }
  99. }