/tests/ZendTest/InputFilter/CollectionInputFilterTest.php

https://github.com/cgmartin/zf2 · PHP · 438 lines · 360 code · 67 blank · 11 comment · 11 complexity · 5d1afb4b93428e4154f147408e70b595 MD5 · raw file

  1. <?php
  2. /**
  3. * Zend Framework (http://framework.zend.com/)
  4. *
  5. * @link http://github.com/zendframework/zf2 for the canonical source repository
  6. * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
  7. * @license http://framework.zend.com/license/new-bsd New BSD License
  8. */
  9. namespace ZendTest\InputFilter;
  10. use PHPUnit_Framework_TestCase as TestCase;
  11. use Zend\InputFilter\BaseInputFilter;
  12. use Zend\InputFilter\CollectionInputFilter;
  13. use Zend\InputFilter\Input;
  14. use Zend\Validator;
  15. class CollectionInputFilterTest extends TestCase
  16. {
  17. /**
  18. * @var \Zend\InputFilter\CollectionInputFilter
  19. */
  20. protected $filter;
  21. public function setUp()
  22. {
  23. $this->filter = new CollectionInputFilter();
  24. }
  25. public function getBaseInputFilter()
  26. {
  27. $filter = new BaseInputFilter();
  28. $foo = new Input();
  29. $foo->getFilterChain()->attachByName('stringtrim')
  30. ->attachByName('alpha');
  31. $foo->getValidatorChain()->attach(new Validator\StringLength(3, 6));
  32. $bar = new Input();
  33. $bar->getFilterChain()->attachByName('stringtrim');
  34. $bar->getValidatorChain()->attach(new Validator\Digits());
  35. $baz = new Input();
  36. $baz->setRequired(false);
  37. $baz->getFilterChain()->attachByName('stringtrim');
  38. $baz->getValidatorChain()->attach(new Validator\StringLength(1, 6));
  39. $filter->add($foo, 'foo')
  40. ->add($bar, 'bar')
  41. ->add($baz, 'baz')
  42. ->add($this->getChildInputFilter(), 'nest');
  43. return $filter;
  44. }
  45. public function getChildInputFilter()
  46. {
  47. $filter = new BaseInputFilter();
  48. $foo = new Input();
  49. $foo->getFilterChain()->attachByName('stringtrim')
  50. ->attachByName('alpha');
  51. $foo->getValidatorChain()->attach(new Validator\StringLength(3, 6));
  52. $bar = new Input();
  53. $bar->getFilterChain()->attachByName('stringtrim');
  54. $bar->getValidatorChain()->attach(new Validator\Digits());
  55. $baz = new Input();
  56. $baz->setRequired(false);
  57. $baz->getFilterChain()->attachByName('stringtrim');
  58. $baz->getValidatorChain()->attach(new Validator\StringLength(1, 6));
  59. $filter->add($foo, 'foo')
  60. ->add($bar, 'bar')
  61. ->add($baz, 'baz');
  62. return $filter;
  63. }
  64. public function getValidCollectionData()
  65. {
  66. return array(
  67. array(
  68. 'foo' => ' bazbat ',
  69. 'bar' => '12345',
  70. 'baz' => '',
  71. 'nest' => array(
  72. 'foo' => ' bazbat ',
  73. 'bar' => '12345',
  74. 'baz' => '',
  75. ),
  76. ),
  77. array(
  78. 'foo' => ' batbaz ',
  79. 'bar' => '54321',
  80. 'baz' => '',
  81. 'nest' => array(
  82. 'foo' => ' batbaz ',
  83. 'bar' => '54321',
  84. 'baz' => '',
  85. ),
  86. )
  87. );
  88. }
  89. public function testSetInputFilter()
  90. {
  91. $this->filter->setInputFilter(new BaseInputFilter());
  92. $this->assertInstanceOf('Zend\InputFilter\BaseInputFilter', $this->filter->getInputFilter());
  93. }
  94. public function testInputFilterInputsAppliedToCollection()
  95. {
  96. if (!extension_loaded('intl')) {
  97. $this->markTestSkipped('ext/intl not enabled');
  98. }
  99. $this->filter->setInputFilter($this->getBaseInputFilter());
  100. $this->assertCount(4, $this->filter->getInputs());
  101. }
  102. public function testGetDefaultInputFilter()
  103. {
  104. $this->assertInstanceOf('Zend\InputFilter\BaseInputFilter', $this->filter->getInputFilter());
  105. }
  106. public function testSetCount()
  107. {
  108. $this->filter->setCount(5);
  109. $this->assertEquals(5, $this->filter->getCount());
  110. }
  111. public function testSetCountBelowZero()
  112. {
  113. $this->filter->setCount(-1);
  114. $this->assertEquals(0, $this->filter->getCount());
  115. }
  116. public function testGetCountUsesCountOfCollectionDataWhenNotSet()
  117. {
  118. $collectionData = array(
  119. array('foo' => 'bar'),
  120. array('foo' => 'baz')
  121. );
  122. $this->filter->setData($collectionData);
  123. $this->assertEquals(2, $this->filter->getCount());
  124. }
  125. public function testGetCountUsesSpecifiedCount()
  126. {
  127. $collectionData = array(
  128. array('foo' => 'bar'),
  129. array('foo' => 'baz')
  130. );
  131. $this->filter->setCount(3);
  132. $this->filter->setData($collectionData);
  133. $this->assertEquals(3, $this->filter->getCount());
  134. }
  135. public function testCanValidateValidData()
  136. {
  137. if (!extension_loaded('intl')) {
  138. $this->markTestSkipped('ext/intl not enabled');
  139. }
  140. $this->filter->setInputFilter($this->getBaseInputFilter());
  141. $this->filter->setData($this->getValidCollectionData());
  142. $this->assertTrue($this->filter->isValid());
  143. }
  144. public function testCanValidateValidDataWithNonConsecutiveKeys()
  145. {
  146. if (!extension_loaded('intl')) {
  147. $this->markTestSkipped('ext/intl not enabled');
  148. }
  149. $collectionData = $this->getValidCollectionData();
  150. $collectionData[2] = $collectionData[0];
  151. unset($collectionData[0]);
  152. $this->filter->setInputFilter($this->getBaseInputFilter());
  153. $this->filter->setData($collectionData);
  154. $this->assertTrue($this->filter->isValid());
  155. }
  156. public function testInvalidDataReturnsFalse()
  157. {
  158. if (!extension_loaded('intl')) {
  159. $this->markTestSkipped('ext/intl not enabled');
  160. }
  161. $invalidCollectionData = array(
  162. array(
  163. 'foo' => ' bazbatlong ',
  164. 'bar' => '12345',
  165. 'baz' => '',
  166. ),
  167. array(
  168. 'foo' => ' bazbat ',
  169. 'bar' => '12345',
  170. 'baz' => '',
  171. )
  172. );
  173. $this->filter->setInputFilter($this->getBaseInputFilter());
  174. $this->filter->setData($invalidCollectionData);
  175. $this->assertFalse($this->filter->isValid());
  176. }
  177. public function testDataLessThanCountIsInvalid()
  178. {
  179. if (!extension_loaded('intl')) {
  180. $this->markTestSkipped('ext/intl not enabled');
  181. }
  182. $invalidCollectionData = array(
  183. array(
  184. 'foo' => ' bazbat ',
  185. 'bar' => '12345',
  186. 'baz' => '',
  187. 'nest' => array(
  188. 'foo' => ' bazbat ',
  189. 'bar' => '12345',
  190. 'baz' => '',
  191. ),
  192. ),
  193. );
  194. $this->filter->setCount(2);
  195. $this->filter->setInputFilter($this->getBaseInputFilter());
  196. $this->filter->setData($invalidCollectionData);
  197. $this->assertFalse($this->filter->isValid());
  198. }
  199. public function testGetValues()
  200. {
  201. if (!extension_loaded('intl')) {
  202. $this->markTestSkipped('ext/intl not enabled');
  203. }
  204. $expectedData = array(
  205. array(
  206. 'foo' => 'bazbat',
  207. 'bar' => '12345',
  208. 'baz' => '',
  209. 'nest' => array(
  210. 'foo' => 'bazbat',
  211. 'bar' => '12345',
  212. 'baz' => '',
  213. ),
  214. ),
  215. array(
  216. 'foo' => 'batbaz',
  217. 'bar' => '54321',
  218. 'baz' => '',
  219. 'nest' => array(
  220. 'foo' => 'batbaz',
  221. 'bar' => '54321',
  222. 'baz' => '',
  223. ),
  224. )
  225. );
  226. $this->filter->setInputFilter($this->getBaseInputFilter());
  227. $this->filter->setData($this->getValidCollectionData());
  228. $this->assertTrue($this->filter->isValid());
  229. $this->assertEquals($expectedData, $this->filter->getValues());
  230. $this->assertCount(2, $this->filter->getValidInput());
  231. foreach ($this->filter->getValidInput() as $validInputs) {
  232. $this->assertCount(4, $validInputs);
  233. }
  234. }
  235. public function testGetRawValues()
  236. {
  237. if (!extension_loaded('intl')) {
  238. $this->markTestSkipped('ext/intl not enabled');
  239. }
  240. $expectedData = array(
  241. array(
  242. 'foo' => ' bazbat ',
  243. 'bar' => '12345',
  244. 'baz' => '',
  245. 'nest' => array(
  246. 'foo' => ' bazbat ',
  247. 'bar' => '12345',
  248. 'baz' => '',
  249. ),
  250. ),
  251. array(
  252. 'foo' => ' batbaz ',
  253. 'bar' => '54321',
  254. 'baz' => '',
  255. 'nest' => array(
  256. 'foo' => ' batbaz ',
  257. 'bar' => '54321',
  258. 'baz' => '',
  259. ),
  260. )
  261. );
  262. $this->filter->setInputFilter($this->getBaseInputFilter());
  263. $this->filter->setData($this->getValidCollectionData());
  264. $this->assertTrue($this->filter->isValid());
  265. $this->assertEquals($expectedData, $this->filter->getRawValues());
  266. }
  267. public function testGetMessagesForInvalidInputs()
  268. {
  269. if (!extension_loaded('intl')) {
  270. $this->markTestSkipped('ext/intl not enabled');
  271. }
  272. $invalidCollectionData = array(
  273. array(
  274. 'foo' => ' bazbattoolong ',
  275. 'bar' => '12345',
  276. 'baz' => '',
  277. 'nest' => array(
  278. 'foo' => ' bazbat ',
  279. 'bar' => '12345',
  280. 'baz' => '',
  281. ),
  282. ),
  283. array(
  284. 'foo' => ' bazbat ',
  285. 'bar' => 'notstring',
  286. 'baz' => '',
  287. 'nest' => array(
  288. 'foo' => ' bazbat ',
  289. 'bar' => '12345',
  290. 'baz' => '',
  291. ),
  292. ),
  293. );
  294. $this->filter->setInputFilter($this->getBaseInputFilter());
  295. $this->filter->setData($invalidCollectionData);
  296. $this->assertFalse($this->filter->isValid());
  297. $this->assertCount(2, $this->filter->getInvalidInput());
  298. foreach ($this->filter->getInvalidInput() as $invalidInputs) {
  299. $this->assertCount(1, $invalidInputs);
  300. }
  301. $messages = $this->filter->getMessages();
  302. $this->assertCount(2, $messages);
  303. $this->assertArrayHasKey('foo', $messages[0]);
  304. $this->assertArrayHasKey('bar', $messages[1]);
  305. }
  306. public function testSetValidationGroupUsingFormStyle()
  307. {
  308. if (!extension_loaded('intl')) {
  309. $this->markTestSkipped('ext/intl not enabled');
  310. }
  311. // forms set an array of identical validation groups for each set of data
  312. $formValidationGroup = array(
  313. array(
  314. 'foo',
  315. 'bar',
  316. ),
  317. array(
  318. 'foo',
  319. 'bar',
  320. ),
  321. array(
  322. 'foo',
  323. 'bar',
  324. )
  325. );
  326. $data = array(
  327. array(
  328. 'foo' => ' bazbat ',
  329. 'bar' => '12345'
  330. ),
  331. array(
  332. 'foo' => ' batbaz ',
  333. 'bar' => '54321'
  334. ),
  335. array(
  336. 'foo' => ' batbaz ',
  337. 'bar' => '54321'
  338. )
  339. );
  340. $this->filter->setInputFilter($this->getBaseInputFilter());
  341. $this->filter->setData($data);
  342. $this->filter->setValidationGroup($formValidationGroup);
  343. $this->assertTrue($this->filter->isValid());
  344. }
  345. public function testEmptyCollectionIsValidByDefault()
  346. {
  347. if (!extension_loaded('intl')) {
  348. $this->markTestSkipped('ext/intl not enabled');
  349. }
  350. $data = array();
  351. $this->filter->setInputFilter($this->getBaseInputFilter());
  352. $this->filter->setData($data);
  353. $this->assertTrue($this->filter->isValid());
  354. }
  355. public function testEmptyCollectionIsNotValidIfRequired()
  356. {
  357. if (!extension_loaded('intl')) {
  358. $this->markTestSkipped('ext/intl not enabled');
  359. }
  360. $data = array();
  361. $this->filter->setInputFilter($this->getBaseInputFilter());
  362. $this->filter->setData($data);
  363. $this->filter->setIsRequired(true);
  364. $this->assertFalse($this->filter->isValid());
  365. }
  366. public function testSetRequired()
  367. {
  368. $this->filter->setIsRequired(true);
  369. $this->assertEquals(true,$this->filter->getIsRequired());
  370. }
  371. }