AjaxViewTest.php 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. }