You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

92 lines
2.9 KiB

  1. <?php
  2. /*
  3. * @copyright NetMonsters <team@netmonsters.ru>
  4. * @link http://netmonsters.ru
  5. * @package Majestic
  6. * @subpackage UnitTests
  7. * @since 2011-11-01
  8. *
  9. * Unit tests for AjaxAction class
  10. */
  11. require_once dirname(__FILE__) . '/Action_TestCase.php';
  12. require_once dirname(__FILE__) . '/../../app/AjaxAction.php';
  13. class AjaxActionTest extends Action_TestCase
  14. {
  15. /**
  16. * @runInSeparateProcess
  17. */
  18. public function testConstruct()
  19. {
  20. Config::set('DEBUG', false);
  21. Env::setParams(array('ajax' => 'AjaxTemplate', 'param2' => 'value2'));
  22. $action = $this->getMockForAbstractClass('AjaxAction');
  23. $this->assertAttributeEquals('ajax', 'template', $action);
  24. }
  25. /**
  26. * @runInSeparateProcess
  27. */
  28. public function testFetchWithEncode()
  29. {
  30. Config::set('DEBUG', false);
  31. $controller = FrontController::getInstance();
  32. $controller->setView('SomeView');
  33. $action = $this->getMockForAbstractClass('AjaxAction');
  34. $action->data = array('var' => 'val');
  35. $result = $action->fetch();
  36. $this->assertSame('/actions//ajax', $result['template']);
  37. $this->assertSame('{"var":"val"}', $result['data']);
  38. }
  39. /**
  40. * @runInSeparateProcess
  41. */
  42. public function testFetchNoEncode()
  43. {
  44. Config::set('DEBUG', false);
  45. Env::setParams(array('json_encode' => false));
  46. $controller = FrontController::getInstance();
  47. $controller->setView('SomeView');
  48. $action = $this->getMockForAbstractClass('AjaxAction');
  49. $action->data = array('var' => 'val');
  50. $result = $action->fetch();
  51. $this->assertSame('/actions//ajax', $result['template']);
  52. $this->assertSame('Array', (string) $result['data']);
  53. $action->data = 'stringvalue';
  54. $result = $action->fetch();
  55. $this->assertSame('/actions//ajax', $result['template']);
  56. $this->assertSame('stringvalue', (string) $result['data']);
  57. }
  58. /**
  59. * @runInSeparateProcess
  60. */
  61. public function testFetchWithEncodeDefault()
  62. {
  63. Config::set('DEBUG', false);
  64. $controller = FrontController::getInstance();
  65. $controller->setView('SomeView');
  66. $action = $this->getMockForAbstractClass('AjaxAction');
  67. $result = $action->fetch();
  68. $this->assertSame('/actions//ajax', $result['template']);
  69. $this->assertSame('false', (string) $result['data']);
  70. }
  71. /**
  72. * @runInSeparateProcess
  73. */
  74. public function testFetchNoEncodeDefault()
  75. {
  76. Config::set('DEBUG', false);
  77. Env::setParams(array('json_encode' => false));
  78. $controller = FrontController::getInstance();
  79. $controller->setView('SomeView');
  80. $action = $this->getMockForAbstractClass('AjaxAction');
  81. $result = $action->fetch();
  82. $this->assertSame('/actions//ajax', $result['template']);
  83. $this->assertSame('', (string) $result['data']);
  84. }
  85. }