AutoCompleteTest.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Controller
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. // Call Zend_Controller_Action_Helper_AutoCompleteTest::main() if this source file is executed directly.
  23. if (!defined("PHPUnit_MAIN_METHOD")) {
  24. require_once dirname(__FILE__) . '/../../../../TestHelper.php';
  25. define("PHPUnit_MAIN_METHOD", "Zend_Controller_Action_Helper_AutoCompleteTest::main");
  26. }
  27. require_once "PHPUnit/Framework/TestCase.php";
  28. require_once "PHPUnit/Framework/TestSuite.php";
  29. require_once 'Zend/Controller/Action/Helper/AutoCompleteDojo.php';
  30. require_once 'Zend/Controller/Action/Helper/AutoCompleteScriptaculous.php';
  31. require_once 'Zend/Controller/Action.php';
  32. require_once 'Zend/Controller/Action/HelperBroker.php';
  33. require_once 'Zend/Controller/Front.php';
  34. require_once 'Zend/Controller/Request/Http.php';
  35. require_once 'Zend/Controller/Response/Cli.php';
  36. require_once 'Zend/Layout.php';
  37. /**
  38. * Test class for Zend_Controller_Action_Helper_AutoComplete.
  39. *
  40. * @category Zend
  41. * @package Zend_Controller
  42. * @subpackage UnitTests
  43. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  44. * @license http://framework.zend.com/license/new-bsd New BSD License
  45. * @group Zend_Controller
  46. * @group Zend_Controller_Action
  47. * @group Zend_Controller_Action_Helper
  48. */
  49. class Zend_Controller_Action_Helper_AutoCompleteTest extends PHPUnit_Framework_TestCase
  50. {
  51. /**
  52. * Runs the test methods of this class.
  53. *
  54. * @access public
  55. * @static
  56. */
  57. public static function main()
  58. {
  59. require_once "PHPUnit/TextUI/TestRunner.php";
  60. $suite = new PHPUnit_Framework_TestSuite("Zend_Controller_Action_Helper_AutoCompleteTest");
  61. $result = PHPUnit_TextUI_TestRunner::run($suite);
  62. }
  63. /**
  64. * Sets up the fixture, for example, open a network connection.
  65. * This method is called before a test is executed.
  66. *
  67. * @return void
  68. */
  69. public function setUp()
  70. {
  71. Zend_Controller_Action_Helper_AutoCompleteTest_LayoutOverride::resetMvcInstance();
  72. Zend_Controller_Action_HelperBroker::resetHelpers();
  73. Zend_Controller_Action_HelperBroker::setPluginLoader(null);
  74. $this->request = new Zend_Controller_Request_Http();
  75. $this->response = new Zend_Controller_Response_Cli();
  76. $this->front = Zend_Controller_Front::getInstance();
  77. $this->front->resetInstance();
  78. $this->front->setRequest($this->request)->setResponse($this->response);
  79. $this->viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');
  80. $this->layout = Zend_Layout::startMvc();
  81. }
  82. /**
  83. * Tears down the fixture, for example, close a network connection.
  84. * This method is called after a test is executed.
  85. *
  86. * @return void
  87. */
  88. public function tearDown()
  89. {
  90. }
  91. public function testConcreteImplementationsDeriveFromAutoCompleteBaseClass()
  92. {
  93. $dojo = new Zend_Controller_Action_Helper_AutoCompleteDojo();
  94. $this->assertTrue($dojo instanceof Zend_Controller_Action_Helper_AutoComplete_Abstract);
  95. $scriptaculous = new Zend_Controller_Action_Helper_AutoCompleteScriptaculous();
  96. $this->assertTrue($scriptaculous instanceof Zend_Controller_Action_Helper_AutoComplete_Abstract);
  97. }
  98. public function testEncodeJsonProxiesToJsonActionHelper()
  99. {
  100. $dojo = new Zend_Controller_Action_Helper_AutoCompleteDojo();
  101. $data = array('foo', 'bar', 'baz');
  102. $encoded = $dojo->prepareAutoCompletion($data);
  103. $decoded = Zend_Json::decode($encoded);
  104. $test = array();
  105. foreach ($decoded['items'] as $item) {
  106. $test[] = $item['name'];
  107. }
  108. $this->assertSame($data, $test);
  109. $this->assertFalse($this->layout->isEnabled());
  110. $headers = $this->response->getHeaders();
  111. $found = false;
  112. foreach ($headers as $header) {
  113. if ('Content-Type' == $header['name']) {
  114. if ('application/json' == $header['value']) {
  115. $found = true;
  116. }
  117. break;
  118. }
  119. }
  120. $this->assertTrue($found, "JSON content-type header not found");
  121. }
  122. public function testDojoHelperEncodesToJson()
  123. {
  124. $dojo = new Zend_Controller_Action_Helper_AutoCompleteDojo();
  125. $data = array('foo', 'bar', 'baz');
  126. $encoded = $dojo->direct($data, false);
  127. $decoded = Zend_Json::decode($encoded);
  128. $this->assertContains('items', array_keys($decoded));
  129. $this->assertContains('identifier', array_keys($decoded));
  130. $this->assertEquals('name', $decoded['identifier']);
  131. $test = array();
  132. foreach ($decoded['items'] as $item) {
  133. $test[] = $item['label'];
  134. }
  135. $this->assertEquals($data, $test);
  136. }
  137. public function testDojoHelperSendsResponseByDefault()
  138. {
  139. $dojo = new Zend_Controller_Action_Helper_AutoCompleteDojo();
  140. $dojo->suppressExit = true;
  141. $data = array('foo', 'bar', 'baz');
  142. $encoded = $dojo->direct($data);
  143. $decoded = Zend_Json::decode($encoded);
  144. $test = array();
  145. foreach ($decoded['items'] as $item) {
  146. $test[] = $item['name'];
  147. }
  148. $this->assertSame($data, $test);
  149. $body = $this->response->getBody();
  150. $this->assertSame($encoded, $body);
  151. }
  152. public function testDojoHelperDisablesLayoutsAndViewRendererByDefault()
  153. {
  154. $dojo = new Zend_Controller_Action_Helper_AutoCompleteDojo();
  155. $dojo->suppressExit = true;
  156. $data = array('foo', 'bar', 'baz');
  157. $encoded = $dojo->direct($data);
  158. $this->assertFalse($this->layout->isEnabled());
  159. $this->assertTrue($this->viewRenderer->getNoRender());
  160. }
  161. public function testDojoHelperCanEnableLayoutsAndViewRenderer()
  162. {
  163. $dojo = new Zend_Controller_Action_Helper_AutoCompleteDojo();
  164. $dojo->suppressExit = true;
  165. $data = array('foo', 'bar', 'baz');
  166. $encoded = $dojo->direct($data, false, true);
  167. $this->assertTrue($this->layout->isEnabled());
  168. $this->assertFalse($this->viewRenderer->getNoRender());
  169. }
  170. public function testScriptaculousHelperThrowsExceptionOnInvalidDataFormat()
  171. {
  172. $scriptaculous = new Zend_Controller_Action_Helper_AutoCompleteScriptaculous();
  173. $data = new stdClass;
  174. $data->foo = 'bar';
  175. $data->bar = 'baz';
  176. try {
  177. $encoded = $scriptaculous->encodeJson($data);
  178. $this->fail('Objects should be considered invalid');
  179. } catch (Zend_Controller_Action_Exception $e) {
  180. $this->assertContains('Invalid data', $e->getMessage());
  181. }
  182. }
  183. public function testScriptaculousHelperCreatesHtmlMarkup()
  184. {
  185. $scriptaculous = new Zend_Controller_Action_Helper_AutoCompleteScriptaculous();
  186. $scriptaculous->suppressExit = true;
  187. $data = array('foo', 'bar', 'baz');
  188. $formatted = $scriptaculous->direct($data);
  189. $this->assertContains('<ul>', $formatted);
  190. foreach ($data as $value) {
  191. $this->assertContains('<li>' . $value . '</li>', $formatted);
  192. }
  193. $this->assertContains('</ul>', $formatted);
  194. }
  195. public function testScriptaculousHelperSendsResponseByDefault()
  196. {
  197. $scriptaculous = new Zend_Controller_Action_Helper_AutoCompleteScriptaculous();
  198. $scriptaculous->suppressExit = true;
  199. $data = array('foo', 'bar', 'baz');
  200. $encoded = $scriptaculous->direct($data);
  201. $body = $this->response->getBody();
  202. $this->assertSame($encoded, $body);
  203. }
  204. public function testScriptaculousHelperDisablesLayoutsAndViewRendererByDefault()
  205. {
  206. $scriptaculous = new Zend_Controller_Action_Helper_AutoCompleteScriptaculous();
  207. $scriptaculous->suppressExit = true;
  208. $data = array('foo', 'bar', 'baz');
  209. $encoded = $scriptaculous->direct($data);
  210. $this->assertFalse($this->layout->isEnabled());
  211. $this->assertTrue($this->viewRenderer->getNoRender());
  212. }
  213. public function testScriptaculousHelperCanEnableLayoutsAndViewRenderer()
  214. {
  215. $scriptaculous = new Zend_Controller_Action_Helper_AutoCompleteScriptaculous();
  216. $scriptaculous->suppressExit = true;
  217. $data = array('foo', 'bar', 'baz');
  218. $encoded = $scriptaculous->direct($data, false, true);
  219. $this->assertTrue($this->layout->isEnabled());
  220. $this->assertFalse($this->viewRenderer->getNoRender());
  221. }
  222. }
  223. class Zend_Controller_Action_Helper_AutoCompleteTest_LayoutOverride extends Zend_Layout
  224. {
  225. public static function resetMvcInstance()
  226. {
  227. self::$_mvcInstance = null;
  228. }
  229. }
  230. // Call Zend_Controller_Action_Helper_AutoCompleteTest::main() if this source file is executed directly.
  231. if (PHPUnit_MAIN_METHOD == "Zend_Controller_Action_Helper_AutoCompleteTest::main") {
  232. Zend_Controller_Action_Helper_AutoCompleteTest::main();
  233. }