Browse Source

New default value for AjaxAction->data, new tests

master
Anton Terekhov 12 years ago
parent
commit
2aafb3a394
  1. 2
      app/AjaxAction.php
  2. 15
      tests/app/Action_TestCase.php
  3. 46
      tests/app/AjaxActionTest.php

2
app/AjaxAction.php

@ -18,7 +18,7 @@ abstract class AjaxAction extends Action
* Data to output * Data to output
* @var mixed * @var mixed
*/ */
public $data = 1;
public $data = false;
/** /**
* Use json_encode * Use json_encode

15
tests/app/Action_TestCase.php

@ -17,6 +17,7 @@ require_once dirname(__FILE__) . '/../../classes/Env.class.php';
require_once dirname(__FILE__) . '/../../exception/ErrorHandler.php'; require_once dirname(__FILE__) . '/../../exception/ErrorHandler.php';
require_once dirname(__FILE__) . '/../../app/FrontController.php'; require_once dirname(__FILE__) . '/../../app/FrontController.php';
require_once dirname(__FILE__) . '/../../app/Action.php'; require_once dirname(__FILE__) . '/../../app/Action.php';
require_once dirname(__FILE__) . '/../../view/iView.php';
class Action_TestCase extends PHPUnit_Framework_TestCase class Action_TestCase extends PHPUnit_Framework_TestCase
{ {
@ -42,7 +43,7 @@ class Action_TestCase extends PHPUnit_Framework_TestCase
} }
} }
class SomeView
class SomeView implements iView
{ {
private $result = array(); private $result = array();
public function fetch($template) public function fetch($template)
@ -53,7 +54,17 @@ class SomeView
public function assignObject() {} public function assignObject() {}
public function assign($name, $value) {
public function assign($name, $value = null) {
$this->result[$name] = $value; $this->result[$name] = $value;
} }
public function prepend($name, $value)
{
}
public function append($name, $value)
{
}
} }

46
tests/app/AjaxActionTest.php

@ -5,7 +5,7 @@
* @link http://netmonsters.ru * @link http://netmonsters.ru
* @package Majestic * @package Majestic
* @subpackage UnitTests * @subpackage UnitTests
* @since 2011-11-1
* @since 2011-11-01
* *
* Unit tests for AjaxAction class * Unit tests for AjaxAction class
*/ */
@ -21,10 +21,9 @@ class AjaxActionTest extends Action_TestCase
*/ */
public function testConstruct() public function testConstruct()
{ {
Config::set('DEBUG', false); Config::set('DEBUG', false);
Env::setParams(array('ajax' => 'AjaxTemplate', 'param2' => 'value2')); Env::setParams(array('ajax' => 'AjaxTemplate', 'param2' => 'value2'));
$action = $this->getMockForAbstractClass('AjaxAction' );
$action = $this->getMockForAbstractClass('AjaxAction');
$this->assertAttributeEquals('ajax', 'template', $action); $this->assertAttributeEquals('ajax', 'template', $action);
} }
@ -33,11 +32,10 @@ class AjaxActionTest extends Action_TestCase
*/ */
public function testFetchWithEncode() public function testFetchWithEncode()
{ {
Config::set('DEBUG', false); Config::set('DEBUG', false);
$controller = FrontController::getInstance(); $controller = FrontController::getInstance();
$controller->setView('SomeView'); $controller->setView('SomeView');
$action = $this->getMockForAbstractClass('AjaxAction' );
$action = $this->getMockForAbstractClass('AjaxAction');
$action->data = array('var' => 'val'); $action->data = array('var' => 'val');
$result = $action->fetch(); $result = $action->fetch();
$this->assertSame('/actions//ajax', $result['template']); $this->assertSame('/actions//ajax', $result['template']);
@ -49,15 +47,47 @@ class AjaxActionTest extends Action_TestCase
*/ */
public function testFetchNoEncode() public function testFetchNoEncode()
{ {
Config::set('DEBUG', false); Config::set('DEBUG', false);
Env::setParams(array('json_encode' => false)); Env::setParams(array('json_encode' => false));
$controller = FrontController::getInstance(); $controller = FrontController::getInstance();
$controller->setView('SomeView'); $controller->setView('SomeView');
$action = $this->getMockForAbstractClass('AjaxAction' );
$action = $this->getMockForAbstractClass('AjaxAction');
$action->data = array('var' => 'val'); $action->data = array('var' => 'val');
$result = $action->fetch(); $result = $action->fetch();
$this->assertSame('/actions//ajax', $result['template']); $this->assertSame('/actions//ajax', $result['template']);
$this->assertSame( $action->data, $result['data']);
$this->assertSame('Array', (string) $result['data']);
$action->data = 'stringvalue';
$result = $action->fetch();
$this->assertSame('/actions//ajax', $result['template']);
$this->assertSame('stringvalue', (string) $result['data']);
}
/**
* @runInSeparateProcess
*/
public function testFetchWithEncodeDefault()
{
Config::set('DEBUG', false);
$controller = FrontController::getInstance();
$controller->setView('SomeView');
$action = $this->getMockForAbstractClass('AjaxAction');
$result = $action->fetch();
$this->assertSame('/actions//ajax', $result['template']);
$this->assertSame('false', (string) $result['data']);
}
/**
* @runInSeparateProcess
*/
public function testFetchNoEncodeDefault()
{
Config::set('DEBUG', false);
Env::setParams(array('json_encode' => false));
$controller = FrontController::getInstance();
$controller->setView('SomeView');
$action = $this->getMockForAbstractClass('AjaxAction');
$result = $action->fetch();
$this->assertSame('/actions//ajax', $result['template']);
$this->assertSame('', (string) $result['data']);
} }
} }
Loading…
Cancel
Save