request = new Zend_Controller_Request_Http(); $this->response = new Zend_Controller_Response_Cli(); $this->front = Zend_Controller_Front::getInstance(); $this->front->resetInstance(); $this->front->setRequest($this->request)->setResponse($this->response); $this->viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer'); $this->layout = Zend_Layout::startMvc(); } /** * Tears down the fixture, for example, close a network connection. * This method is called after a test is executed. * * @return void */ public function tearDown() { } public function testConcreteImplementationsDeriveFromAutoCompleteBaseClass() { $dojo = new Zend_Controller_Action_Helper_AutoCompleteDojo(); $this->assertTrue($dojo instanceof Zend_Controller_Action_Helper_AutoComplete_Abstract); $scriptaculous = new Zend_Controller_Action_Helper_AutoCompleteScriptaculous(); $this->assertTrue($scriptaculous instanceof Zend_Controller_Action_Helper_AutoComplete_Abstract); } public function testEncodeJsonProxiesToJsonActionHelper() { $dojo = new Zend_Controller_Action_Helper_AutoCompleteDojo(); $data = array('foo', 'bar', 'baz'); $encoded = $dojo->prepareAutoCompletion($data); $decoded = Zend_Json::decode($encoded); $test = array(); foreach ($decoded['items'] as $item) { $test[] = $item['name']; } $this->assertSame($data, $test); $this->assertFalse($this->layout->isEnabled()); $headers = $this->response->getHeaders(); $found = false; foreach ($headers as $header) { if ('Content-Type' == $header['name']) { if ('application/json' == $header['value']) { $found = true; } break; } } $this->assertTrue($found, "JSON content-type header not found"); } public function testDojoHelperEncodesToJson() { $dojo = new Zend_Controller_Action_Helper_AutoCompleteDojo(); $data = array('foo', 'bar', 'baz'); $encoded = $dojo->direct($data, false); $decoded = Zend_Json::decode($encoded); $this->assertContains('items', array_keys($decoded)); $this->assertContains('identifier', array_keys($decoded)); $this->assertEquals('name', $decoded['identifier']); $test = array(); foreach ($decoded['items'] as $item) { $test[] = $item['label']; } $this->assertEquals($data, $test); } public function testDojoHelperSendsResponseByDefault() { $dojo = new Zend_Controller_Action_Helper_AutoCompleteDojo(); $dojo->suppressExit = true; $data = array('foo', 'bar', 'baz'); $encoded = $dojo->direct($data); $decoded = Zend_Json::decode($encoded); $test = array(); foreach ($decoded['items'] as $item) { $test[] = $item['name']; } $this->assertSame($data, $test); $body = $this->response->getBody(); $this->assertSame($encoded, $body); } public function testDojoHelperDisablesLayoutsAndViewRendererByDefault() { $dojo = new Zend_Controller_Action_Helper_AutoCompleteDojo(); $dojo->suppressExit = true; $data = array('foo', 'bar', 'baz'); $encoded = $dojo->direct($data); $this->assertFalse($this->layout->isEnabled()); $this->assertTrue($this->viewRenderer->getNoRender()); } public function testDojoHelperCanEnableLayoutsAndViewRenderer() { $dojo = new Zend_Controller_Action_Helper_AutoCompleteDojo(); $dojo->suppressExit = true; $data = array('foo', 'bar', 'baz'); $encoded = $dojo->direct($data, false, true); $this->assertTrue($this->layout->isEnabled()); $this->assertFalse($this->viewRenderer->getNoRender()); } public function testScriptaculousHelperThrowsExceptionOnInvalidDataFormat() { $scriptaculous = new Zend_Controller_Action_Helper_AutoCompleteScriptaculous(); $data = new stdClass; $data->foo = 'bar'; $data->bar = 'baz'; try { $encoded = $scriptaculous->encodeJson($data); $this->fail('Objects should be considered invalid'); } catch (Zend_Controller_Action_Exception $e) { $this->assertContains('Invalid data', $e->getMessage()); } } public function testScriptaculousHelperCreatesHtmlMarkup() { $scriptaculous = new Zend_Controller_Action_Helper_AutoCompleteScriptaculous(); $scriptaculous->suppressExit = true; $data = array('foo', 'bar', 'baz'); $formatted = $scriptaculous->direct($data); $this->assertContains('