PageRenderTime 293ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

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

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