PageRenderTime 45ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/Zend/Form/Decorator/FormErrorsTest.php

https://github.com/Exercise/zf2
PHP | 296 lines | 235 code | 22 blank | 39 comment | 15 complexity | 254e8d8dfd9b933ae04d9aeef82be2dc 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-2010 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. namespace ZendTest\Form\Decorator;
  23. use Zend\Form\Decorator\FormErrors as FormErrorsDecorator,
  24. Zend\Form\Form,
  25. Zend\Form\SubForm,
  26. Zend\View\View;
  27. /**
  28. * Test class for Zend_Form_Decorator_FormErrors
  29. *
  30. * @category Zend
  31. * @package Zend_Form
  32. * @subpackage UnitTests
  33. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  34. * @license http://framework.zend.com/license/new-bsd New BSD License
  35. * @group Zend_Form
  36. */
  37. class FormErrorsTest extends \PHPUnit_Framework_TestCase
  38. {
  39. /**
  40. * Sets up the fixture, for example, open a network connection.
  41. * This method is called before a test is executed.
  42. *
  43. * @return void
  44. */
  45. public function setUp()
  46. {
  47. $this->decorator = new FormErrorsDecorator();
  48. }
  49. public function getView()
  50. {
  51. $view = new View();
  52. return $view;
  53. }
  54. public function setupForm()
  55. {
  56. $form1 = new SubForm;
  57. $form1->addElement('text', 'foo', array(
  58. 'label' => 'Sub Foo: ',
  59. 'required' => true,
  60. 'validators' => array(
  61. 'NotEmpty',
  62. 'Alpha',
  63. ),
  64. ))
  65. ->addElement('text', 'bar', array(
  66. 'label' => 'Sub Bar: ',
  67. 'required' => true,
  68. 'validators' => array(
  69. 'Alpha',
  70. 'Alnum',
  71. ),
  72. ));
  73. $form2 = new Form;
  74. $form2->addElement('text', 'foo', array(
  75. 'label' => 'Master Foo: ',
  76. 'required' => true,
  77. 'validators' => array(
  78. 'NotEmpty',
  79. 'Alpha',
  80. ),
  81. ))
  82. ->addElement('text', 'bar', array(
  83. 'required' => true,
  84. 'validators' => array(
  85. 'Alpha',
  86. 'Alnum',
  87. ),
  88. ))
  89. ->addSubForm($form1, 'sub');
  90. $form2->isValid(array(
  91. 'foo' => '',
  92. 'bar' => 'foo 2 u 2',
  93. 'sub' => array(
  94. 'foo' => '',
  95. 'bar' => 'foo 2 u 2',
  96. ),
  97. ));
  98. $form2->setView($this->getView());
  99. $this->decorator->setElement($form2);
  100. $this->form = $form2;
  101. return $form2;
  102. }
  103. public function testRenderReturnsInitialContentIfNoViewPresentInForm()
  104. {
  105. $form = new Form();
  106. $this->decorator->setElement($form);
  107. $content = 'test content';
  108. $this->assertSame($content, $this->decorator->render($content));
  109. }
  110. public function testRenderRendersAllErrorMessages()
  111. {
  112. $this->setupForm();
  113. $content = 'test content';
  114. $test = $this->decorator->render($content);
  115. $this->assertContains($content, $test);
  116. foreach ($this->form->getMessages() as $name => $messages) {
  117. foreach ($messages as $key => $message) {
  118. if (is_string($message)) {
  119. $this->assertContains($message, $test, var_export($messages, 1));
  120. } else {
  121. foreach ($message as $m) {
  122. $this->assertContains($m, $test, var_export($messages, 1));
  123. }
  124. }
  125. }
  126. }
  127. }
  128. public function testRenderAppendsMessagesToContentByDefault()
  129. {
  130. $this->setupForm();
  131. $content = 'test content';
  132. $test = $this->decorator->render($content);
  133. $this->assertRegexp('#' . $content . '.*?<ul#s', $test, $test);
  134. }
  135. public function testRenderPrependsMessagesToContentWhenRequested()
  136. {
  137. $this->decorator->setOptions(array('placement' => 'PREPEND'));
  138. $this->setupForm();
  139. $content = 'test content';
  140. $test = $this->decorator->render($content);
  141. $this->assertRegexp('#</ul>.*?' . $content . '#s', $test);
  142. }
  143. public function testRenderSeparatesContentAndErrorsWithPhpEolByDefault()
  144. {
  145. $this->setupForm();
  146. $content = 'test content';
  147. $test = $this->decorator->render($content);
  148. $this->assertContains($content . PHP_EOL . '<ul', $test);
  149. }
  150. public function testRenderSeparatesContentAndErrorsWithCustomSeparatorWhenRequested()
  151. {
  152. $this->decorator->setOptions(array('separator' => '<br />'));
  153. $this->setupForm();
  154. $content = 'test content';
  155. $test = $this->decorator->render($content);
  156. $this->assertContains($content . $this->decorator->getSeparator() . '<ul', $test, $test);
  157. }
  158. public function testIgnoreSubFormsFlagShouldBeFalseByDefault()
  159. {
  160. $this->assertFalse($this->decorator->ignoreSubForms());
  161. }
  162. public function testLabelsShouldBeUsed()
  163. {
  164. $this->setupForm();
  165. $markup = $this->decorator->render('');
  166. $this->assertContains('>Sub Foo: </b>', $markup, $markup);
  167. $this->assertContains('>Sub Bar: </b>', $markup, $markup);
  168. $this->assertContains('>Master Foo: </b>', $markup);
  169. $this->assertNotContains('>Master Bar: </b>', $markup);
  170. $this->assertContains('>bar</b>', $markup);
  171. }
  172. public function testMarkupOptionsMayBePassedViaSetOptions()
  173. {
  174. $options = array(
  175. 'ignoreSubForms' => true,
  176. 'markupElementLabelEnd' => '</i>',
  177. 'markupElementLabelStart' => '<i>',
  178. 'markupListEnd' => '</dl>',
  179. 'markupListItemEnd' => '</dd>',
  180. 'markupListItemStart' => '<dd>',
  181. 'markupListStart' => '<dl class="form-errors">',
  182. );
  183. $this->decorator->setOptions($options);
  184. foreach ($options as $key => $value) {
  185. if ($key == 'ignoreSubForms') {
  186. $this->assertTrue($this->decorator->ignoreSubForms());
  187. } else {
  188. $method = 'get' . ucfirst($key);
  189. $this->assertEquals($value, $this->decorator->$method());
  190. }
  191. }
  192. }
  193. public function testMarkupOptionsShouldBeUsedWhenRendering()
  194. {
  195. $options = array(
  196. 'ignoreSubForms' => true,
  197. 'markupElementLabelEnd' => '</i>',
  198. 'markupElementLabelStart' => '<i>',
  199. 'markupListEnd' => '</div>',
  200. 'markupListItemEnd' => '</p>',
  201. 'markupListItemStart' => '<p>',
  202. 'markupListStart' => '<div class="form-errors">',
  203. );
  204. $this->setupForm();
  205. $this->decorator->setOptions($options);
  206. $markup = $this->decorator->render('');
  207. foreach ($options as $key => $value) {
  208. if ($key == 'ignoreSubForms') {
  209. $this->assertNotContains('Sub ', $markup);
  210. } else {
  211. $this->assertContains($value, $markup);
  212. }
  213. }
  214. }
  215. public function testRenderIsArrayForm()
  216. {
  217. $this->setupForm();
  218. $this->form->setName('foo')
  219. ->setIsArray(true);
  220. $content = 'test content';
  221. $test = $this->decorator->render($content);
  222. $this->assertContains($content, $test);
  223. foreach ($this->form->getMessages() as $name => $messages) {
  224. while (($message = current($messages))) {
  225. if (is_string($message)) {
  226. $this->assertContains($message, $test, var_export($messages, 1));
  227. }
  228. if (false === next($messages) && is_array(prev($messages))) {
  229. $messages = current($messages);
  230. }
  231. }
  232. }
  233. }
  234. public function testCustomFormErrors()
  235. {
  236. $this->setupForm();
  237. $this->form->addDecorator($this->decorator)
  238. ->addError('form-badness');
  239. $html = $this->form->render();
  240. $this->assertContains('form-badness', $html);
  241. $this->decorator->setOnlyCustomFormErrors(true);
  242. $html = $this->form->render();
  243. $this->assertNotRegexp('/form-errors.*?Master Foo/', $html);
  244. $this->decorator->setShowCustomFormErrors(false);
  245. $html = $this->form->render();
  246. $this->assertNotContains('form-badness', $html);
  247. }
  248. /**
  249. * @dataProvider markupOptionMethodsProvider
  250. */
  251. public function testMarkupOptionsMayBeMutated($property)
  252. {
  253. $setter = 'set' . $property;
  254. $getter = 'get' . $property;
  255. $this->decorator->$setter('foo');
  256. if ($property == 'IgnoreSubForms') {
  257. $this->assertTrue($this->decorator->ignoreSubForms());
  258. } else {
  259. $this->assertEquals('foo', $this->decorator->$getter());
  260. }
  261. }
  262. public function markupOptionMethodsProvider()
  263. {
  264. return array(
  265. array('IgnoreSubForms'),
  266. array('MarkupElementLabelEnd'),
  267. array('MarkupElementLabelStart'),
  268. array('MarkupListEnd'),
  269. array('MarkupListItemEnd'),
  270. array('MarkupListItemStart'),
  271. array('MarkupListStart'),
  272. );
  273. }
  274. }