public function testHandleResult_JsonRequest() { $PHPDS = PHPDSlib::instance(); $_SERVER["HTTP_X_REQUESTED_WITH"] = 'XMLHttpRequest'; $_SERVER["HTTP_X_REQUESTED_TYPE"] = 'json'; $data = array('test' => true); $this->assertTrue(PU_isAJAX()); $this->assertEquals('{"test":true}', PU_isJSON($data)); // set up the template with fake data and call the method under test $template = $PHPDS->PHPDS_template(); $core = $PHPDS->PHPDS_core(); $this->assertType('PHPDS_template', $template); $this->assertType('PHPDS_core', $core); $core->themeFile = 'testTemplate'; $core->data = 'testController'; // TODO: decide what is the correct behavior // test a controller result of false: error /*$result = $this->object->handleResult(false); $this->assertFalse($result); $this->assertEquals('testTemplate', $core->themeFile); $this->assertEquals('testController', $template->controller);*/ // test a controller result of null: standard handling $result = $this->object->handleResult(null); $this->assertTrue($result); $this->assertEquals('', $core->themeFile); $this->assertEquals('null', $core->data); // test a controller result of true: don't do anything $result = $this->object->handleResult(true); $this->assertTrue($result); $this->assertEquals('', $core->themeFile); $this->assertEquals('true', $core->data); // test a controller result of some data $result = $this->object->handleResult($data); $this->assertTrue($result); $this->assertEquals('', $core->themeFile); $this->assertEquals('{"test":true}', $core->data); // TODO: should this throw an exception? // test an invalid controller result /*$this->setExpectedException('PHPDS_exception'); $result = $this->object->handleResult(array('test' => true));*/ }
/** * Deal with the controller's output * * @version 1.0 * @since 3.0.5 * @author greg <*****@*****.**> * * @param mixed $raw_data * @return mixed */ public function handleResult($raw_data) { $template = $this->template; $core = $this->core; $core->themeFile = ''; $core->data = ''; $encoded_data = PU_isJSON($raw_data); if (false !== $encoded_data) { $core->data = $encoded_data; return true; } else { if (false === $raw_data) { // we consider it's an error return false; } elseif (is_null($raw_data)) { // deal with it the usual way (normal template) return true; } elseif (true === $raw_data) { // controller handled output return true; } elseif (is_string($raw_data)) { // bare data, using empty template $core->data = $raw_data; return true; } else { throw new PHPDS_exception(sprintf(___('The return value of controller %d is invalid.'), $this->configuration['m'])); } } }
public function testIsJson() { $data = array('test' => true); $_SERVER["HTTP_X_REQUESTED_WITH"] = ''; $_SERVER["HTTP_X_REQUESTED_TYPE"] = 'json'; $this->assertFalse(PU_isAJAX()); $this->assertEquals('', PU_isJSON($data)); $_SERVER["HTTP_X_REQUESTED_WITH"] = 'XMLHttpRequest'; $_SERVER["HTTP_X_REQUESTED_TYPE"] = 'json'; $this->assertTrue(PU_isAJAX()); $this->assertEquals('{"test":true}', PU_isJSON($data)); }