PageRenderTime 48ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/ZendFramework/tests/Zend/Form/Decorator/ViewScriptTest.php

https://bitbucket.org/Dal-Papa/is-340-publish-base
PHP | 245 lines | 159 code | 33 blank | 53 comment | 3 complexity | 5ec0287ab91aa27737a31ad7d6ccfd63 MD5 | raw file
  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_Form
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id: ViewScriptTest.php 24979 2012-06-19 19:15:52Z rob $
  21. */
  22. // Call Zend_Form_Decorator_ViewScriptTest::main() if this source file is executed directly.
  23. if (!defined("PHPUnit_MAIN_METHOD")) {
  24. define("PHPUnit_MAIN_METHOD", "Zend_Form_Decorator_ViewScriptTest::main");
  25. }
  26. require_once 'Zend/Form/Decorator/ViewScript.php';
  27. require_once 'Zend/Form/Element.php';
  28. require_once 'Zend/Form/Element/Text.php';
  29. require_once 'Zend/View.php';
  30. /**
  31. * Test class for Zend_Form_Decorator_ViewScript
  32. *
  33. * @category Zend
  34. * @package Zend_Form
  35. * @subpackage UnitTests
  36. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  37. * @license http://framework.zend.com/license/new-bsd New BSD License
  38. * @group Zend_Form
  39. */
  40. class Zend_Form_Decorator_ViewScriptTest extends PHPUnit_Framework_TestCase
  41. {
  42. /**
  43. * Runs the test methods of this class.
  44. *
  45. * @return void
  46. */
  47. public static function main()
  48. {
  49. $suite = new PHPUnit_Framework_TestSuite("Zend_Form_Decorator_ViewScriptTest");
  50. $result = PHPUnit_TextUI_TestRunner::run($suite);
  51. }
  52. /**
  53. * Sets up the fixture, for example, open a network connection.
  54. * This method is called before a test is executed.
  55. *
  56. * @return void
  57. */
  58. public function setUp()
  59. {
  60. $this->decorator = new Zend_Form_Decorator_ViewScript();
  61. }
  62. /**
  63. * Tears down the fixture, for example, close a network connection.
  64. * This method is called after a test is executed.
  65. *
  66. * @return void
  67. */
  68. public function tearDown()
  69. {
  70. }
  71. public function getView()
  72. {
  73. $view = new Zend_View();
  74. $view->addHelperPath(dirname(__FILE__) . '/../../../../library/Zend/View/Helper');
  75. $view->addScriptPath(dirname(__FILE__) . '/../_files/views/');
  76. return $view;
  77. }
  78. public function getElement()
  79. {
  80. $element = new Zend_Form_Element_Text('foo');
  81. $element->setView($this->getView());
  82. $this->decorator->setElement($element);
  83. return $element;
  84. }
  85. public function testRenderRaisesExceptionIfNoViewScriptRegistered()
  86. {
  87. $this->getElement();
  88. try {
  89. $this->decorator->render('');
  90. } catch (Zend_Form_Exception $e) {
  91. $this->assertContains('script', $e->getMessage());
  92. }
  93. }
  94. public function testViewScriptNullByDefault()
  95. {
  96. $this->assertNull($this->decorator->getViewScript());
  97. }
  98. public function testCanSetViewScript()
  99. {
  100. $this->testViewScriptNullByDefault();
  101. $this->decorator->setViewScript('decorator.phtml');
  102. $this->assertEquals('decorator.phtml', $this->decorator->getViewScript());
  103. }
  104. public function testCanSetViewScriptViaOption()
  105. {
  106. $this->testViewScriptNullByDefault();
  107. $this->decorator->setOption('viewScript', 'decorator.phtml');
  108. $this->assertEquals('decorator.phtml', $this->decorator->getViewScript());
  109. }
  110. public function testCanSetViewScriptViaElementAttribute()
  111. {
  112. $this->testViewScriptNullByDefault();
  113. $this->getElement()->setAttrib('viewScript', 'decorator.phtml');
  114. $this->assertEquals('decorator.phtml', $this->decorator->getViewScript());
  115. }
  116. public function testCanSetViewModule()
  117. {
  118. $this->testViewScriptNullByDefault();
  119. $this->decorator->setViewModule('fooModule');
  120. $this->assertEquals('fooModule', $this->decorator->getViewModule());
  121. }
  122. public function testCanSetViewModuleViaOption()
  123. {
  124. $this->testViewScriptNullByDefault();
  125. $this->decorator->setOption('viewModule', 'fooModule');
  126. $this->assertEquals('fooModule', $this->decorator->getViewModule());
  127. }
  128. public function testCanSetViewModuleViaElementAttribute()
  129. {
  130. $this->testViewScriptNullByDefault();
  131. $this->getElement()->setAttrib('viewModule', 'fooModule');
  132. $this->assertEquals('fooModule', $this->decorator->getViewModule());
  133. }
  134. public function testRenderingRendersViewScript()
  135. {
  136. $this->testCanSetViewScriptViaElementAttribute();
  137. $test = $this->decorator->render('');
  138. $this->assertContains('This is content from the view script', $test);
  139. }
  140. public function testRenderingRendersViewScriptWithModule()
  141. {
  142. $this->testCanSetViewScriptViaElementAttribute();
  143. $module = 'fooModule';
  144. // add module to front controller so partial view helper can verify it exists
  145. require_once 'Zend/Controller/Front.php';
  146. Zend_Controller_Front::getInstance()->addControllerDirectory('', $module);
  147. $this->getElement()->setAttrib('viewModule', $module);
  148. $test = $this->decorator->render('');
  149. $this->assertContains('This is content from the view script', $test);
  150. }
  151. public function testOptionsArePassedToPartialAsVariables()
  152. {
  153. $this->decorator->setOptions(array(
  154. 'foo' => 'Foo Value',
  155. 'bar' => 'Bar Value',
  156. 'baz' => 'Baz Value',
  157. 'bat' => 'Bat Value',
  158. 'viewScript' => 'decorator.phtml',
  159. ));
  160. $this->getElement();
  161. $test = $this->decorator->render('');
  162. foreach ($this->decorator->getOptions() as $key => $value) {
  163. $this->assertContains("$key: $value", $test);
  164. }
  165. }
  166. public function testCanReplaceContentBySpecifyingFalsePlacement()
  167. {
  168. $this->decorator->setViewScript('replacingDecorator.phtml')
  169. ->setOption('placement', false)
  170. ->setElement($this->getElement());
  171. $test = $this->decorator->render('content to decorate');
  172. $this->assertNotContains('content to decorate', $test, $test);
  173. $this->assertContains('This is content from the view script', $test);
  174. }
  175. public function testContentCanBeRenderedWithinViewScript()
  176. {
  177. $this->decorator->setViewScript('contentWrappingDecorator.phtml')
  178. ->setOption('placement', false)
  179. ->setElement($this->getElement());
  180. $test = $this->decorator->render('content to decorate');
  181. $this->assertContains('content to decorate', $test, $test);
  182. $this->assertContains('This text prefixes the content', $test);
  183. $this->assertContains('This text appends the content', $test);
  184. }
  185. public function testDecoratorCanControlPlacementFromWithinViewScript()
  186. {
  187. $this->decorator->setViewScript('decoratorCausesReplacement.phtml')
  188. ->setElement($this->getElement());
  189. $test = $this->decorator->render('content to decorate');
  190. $this->assertContains('content to decorate', $test, $test);
  191. $count = substr_count($test, 'content to decorate');
  192. $this->assertEquals(1, $count);
  193. $this->assertContains('This text prefixes the content', $test);
  194. $this->assertContains('This text appends the content', $test);
  195. }
  196. /**
  197. * @group ZF-6061
  198. */
  199. public function testRenderingWithoutHelperInAttribs()
  200. {
  201. $this->decorator->setViewScript('withouthelperinattribs.phtml')
  202. ->setElement($this->getElement());
  203. $expected = 'Foo:'
  204. . PHP_EOL
  205. . '<input type="text" name="foo" id="foo" value="">';
  206. $this->assertSame($expected, $this->decorator->render('Foo:'));
  207. }
  208. }
  209. // Call Zend_Form_Decorator_ViewScriptTest::main() if this source file is executed directly.
  210. if (PHPUnit_MAIN_METHOD == "Zend_Form_Decorator_ViewScriptTest::main") {
  211. Zend_Form_Decorator_ViewScriptTest::main();
  212. }