PageRenderTime 63ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 1ms

/vendor/symfony/symfony/src/Symfony/Component/Validator/Tests/Constraints/CollectionValidatorTest.php

https://github.com/nattaphat/hgis
PHP | 412 lines | 316 code | 84 blank | 12 comment | 0 complexity | 190df6e73adb59cbba96032a4da0bd01 MD5 | raw file
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Validator\Tests\Constraints;
  11. use Symfony\Component\Validator\ExecutionContext;
  12. use Symfony\Component\Validator\Constraints\Range;
  13. use Symfony\Component\Validator\Constraints\NotNull;
  14. use Symfony\Component\Validator\Constraints\Collection\Required;
  15. use Symfony\Component\Validator\Constraints\Collection\Optional;
  16. use Symfony\Component\Validator\Constraints\Collection;
  17. use Symfony\Component\Validator\Constraints\CollectionValidator;
  18. abstract class CollectionValidatorTest extends \PHPUnit_Framework_TestCase
  19. {
  20. protected $context;
  21. protected $validator;
  22. protected function setUp()
  23. {
  24. $this->context = $this->getMock('Symfony\Component\Validator\ExecutionContext', array(), array(), '', false);
  25. $this->validator = new CollectionValidator();
  26. $this->validator->initialize($this->context);
  27. $this->context->expects($this->any())
  28. ->method('getGroup')
  29. ->will($this->returnValue('MyGroup'));
  30. }
  31. protected function tearDown()
  32. {
  33. $this->context = null;
  34. $this->validator = null;
  35. }
  36. abstract protected function prepareTestData(array $contents);
  37. public function testNullIsValid()
  38. {
  39. $this->context->expects($this->never())
  40. ->method('addViolationAt');
  41. $this->validator->validate(null, new Collection(array('fields' => array(
  42. 'foo' => new Range(array('min' => 4)),
  43. ))));
  44. }
  45. public function testFieldsAsDefaultOption()
  46. {
  47. $data = $this->prepareTestData(array('foo' => 'foobar'));
  48. $this->context->expects($this->never())
  49. ->method('addViolationAt');
  50. $this->validator->validate($data, new Collection(array(
  51. 'foo' => new Range(array('min' => 4)),
  52. )));
  53. }
  54. /**
  55. * @expectedException \Symfony\Component\Validator\Exception\UnexpectedTypeException
  56. */
  57. public function testThrowsExceptionIfNotTraversable()
  58. {
  59. $this->validator->validate('foobar', new Collection(array('fields' => array(
  60. 'foo' => new Range(array('min' => 4)),
  61. ))));
  62. }
  63. public function testWalkSingleConstraint()
  64. {
  65. $constraint = new Range(array('min' => 4));
  66. $array = array(
  67. 'foo' => 3,
  68. 'bar' => 5,
  69. );
  70. $i = 1;
  71. foreach ($array as $key => $value) {
  72. $this->context->expects($this->at($i++))
  73. ->method('validateValue')
  74. ->with($value, $constraint, '['.$key.']', 'MyGroup');
  75. }
  76. $data = $this->prepareTestData($array);
  77. $this->context->expects($this->never())
  78. ->method('addViolationAt');
  79. $this->validator->validate($data, new Collection(array(
  80. 'fields' => array(
  81. 'foo' => $constraint,
  82. 'bar' => $constraint,
  83. ),
  84. )));
  85. }
  86. public function testWalkMultipleConstraints()
  87. {
  88. $constraints = array(
  89. new Range(array('min' => 4)),
  90. new NotNull(),
  91. );
  92. $array = array(
  93. 'foo' => 3,
  94. 'bar' => 5,
  95. );
  96. $i = 1;
  97. foreach ($array as $key => $value) {
  98. foreach ($constraints as $constraint) {
  99. $this->context->expects($this->at($i++))
  100. ->method('validateValue')
  101. ->with($value, $constraint, '['.$key.']', 'MyGroup');
  102. }
  103. }
  104. $data = $this->prepareTestData($array);
  105. $this->context->expects($this->never())
  106. ->method('addViolationAt');
  107. $this->validator->validate($data, new Collection(array(
  108. 'fields' => array(
  109. 'foo' => $constraints,
  110. 'bar' => $constraints,
  111. )
  112. )));
  113. }
  114. public function testExtraFieldsDisallowed()
  115. {
  116. $data = $this->prepareTestData(array(
  117. 'foo' => 5,
  118. 'baz' => 6,
  119. ));
  120. $this->context->expects($this->once())
  121. ->method('addViolationAt')
  122. ->with('[baz]', 'myMessage', array(
  123. '{{ field }}' => 'baz'
  124. ));
  125. $this->validator->validate($data, new Collection(array(
  126. 'fields' => array(
  127. 'foo' => new Range(array('min' => 4)),
  128. ),
  129. 'extraFieldsMessage' => 'myMessage',
  130. )));
  131. }
  132. // bug fix
  133. public function testNullNotConsideredExtraField()
  134. {
  135. $data = $this->prepareTestData(array(
  136. 'foo' => null,
  137. ));
  138. $constraint = new Collection(array(
  139. 'fields' => array(
  140. 'foo' => new Range(array('min' => 4)),
  141. ),
  142. ));
  143. $this->context->expects($this->never())
  144. ->method('addViolationAt');
  145. $this->validator->validate($data, $constraint);
  146. }
  147. public function testExtraFieldsAllowed()
  148. {
  149. $data = $this->prepareTestData(array(
  150. 'foo' => 5,
  151. 'bar' => 6,
  152. ));
  153. $constraint = new Collection(array(
  154. 'fields' => array(
  155. 'foo' => new Range(array('min' => 4)),
  156. ),
  157. 'allowExtraFields' => true,
  158. ));
  159. $this->context->expects($this->never())
  160. ->method('addViolationAt');
  161. $this->validator->validate($data, $constraint);
  162. }
  163. public function testMissingFieldsDisallowed()
  164. {
  165. $data = $this->prepareTestData(array());
  166. $constraint = new Collection(array(
  167. 'fields' => array(
  168. 'foo' => new Range(array('min' => 4)),
  169. ),
  170. 'missingFieldsMessage' => 'myMessage',
  171. ));
  172. $this->context->expects($this->once())
  173. ->method('addViolationAt')
  174. ->with('[foo]', 'myMessage', array(
  175. '{{ field }}' => 'foo',
  176. ));
  177. $this->validator->validate($data, $constraint);
  178. }
  179. public function testMissingFieldsAllowed()
  180. {
  181. $data = $this->prepareTestData(array());
  182. $constraint = new Collection(array(
  183. 'fields' => array(
  184. 'foo' => new Range(array('min' => 4)),
  185. ),
  186. 'allowMissingFields' => true,
  187. ));
  188. $this->context->expects($this->never())
  189. ->method('addViolationAt');
  190. $this->validator->validate($data, $constraint);
  191. }
  192. public function testOptionalFieldPresent()
  193. {
  194. $data = $this->prepareTestData(array(
  195. 'foo' => null,
  196. ));
  197. $this->context->expects($this->never())
  198. ->method('addViolationAt');
  199. $this->validator->validate($data, new Collection(array(
  200. 'foo' => new Optional(),
  201. )));
  202. }
  203. public function testOptionalFieldNotPresent()
  204. {
  205. $data = $this->prepareTestData(array());
  206. $this->context->expects($this->never())
  207. ->method('addViolationAt');
  208. $this->validator->validate($data, new Collection(array(
  209. 'foo' => new Optional(),
  210. )));
  211. }
  212. public function testOptionalFieldSingleConstraint()
  213. {
  214. $array = array(
  215. 'foo' => 5,
  216. );
  217. $constraint = new Range(array('min' => 4));
  218. $this->context->expects($this->once())
  219. ->method('validateValue')
  220. ->with($array['foo'], $constraint, '[foo]', 'MyGroup');
  221. $this->context->expects($this->never())
  222. ->method('addViolationAt');
  223. $data = $this->prepareTestData($array);
  224. $this->validator->validate($data, new Collection(array(
  225. 'foo' => new Optional($constraint),
  226. )));
  227. }
  228. public function testOptionalFieldMultipleConstraints()
  229. {
  230. $array = array(
  231. 'foo' => 5,
  232. );
  233. $constraints = array(
  234. new NotNull(),
  235. new Range(array('min' => 4)),
  236. );
  237. $i = 1;
  238. foreach ($constraints as $constraint) {
  239. $this->context->expects($this->at($i++))
  240. ->method('validateValue')
  241. ->with($array['foo'], $constraint, '[foo]', 'MyGroup');
  242. }
  243. $this->context->expects($this->never())
  244. ->method('addViolationAt');
  245. $data = $this->prepareTestData($array);
  246. $this->validator->validate($data, new Collection(array(
  247. 'foo' => new Optional($constraints),
  248. )));
  249. }
  250. public function testRequiredFieldPresent()
  251. {
  252. $data = $this->prepareTestData(array(
  253. 'foo' => null,
  254. ));
  255. $this->context->expects($this->never())
  256. ->method('addViolationAt');
  257. $this->validator->validate($data, new Collection(array(
  258. 'foo' => new Required(),
  259. )));
  260. }
  261. public function testRequiredFieldNotPresent()
  262. {
  263. $data = $this->prepareTestData(array());
  264. $this->context->expects($this->once())
  265. ->method('addViolationAt')
  266. ->with('[foo]', 'myMessage', array(
  267. '{{ field }}' => 'foo',
  268. ));
  269. $this->validator->validate($data, new Collection(array(
  270. 'fields' => array(
  271. 'foo' => new Required(),
  272. ),
  273. 'missingFieldsMessage' => 'myMessage',
  274. )));
  275. }
  276. public function testRequiredFieldSingleConstraint()
  277. {
  278. $array = array(
  279. 'foo' => 5,
  280. );
  281. $constraint = new Range(array('min' => 4));
  282. $this->context->expects($this->once())
  283. ->method('validateValue')
  284. ->with($array['foo'], $constraint, '[foo]', 'MyGroup');
  285. $this->context->expects($this->never())
  286. ->method('addViolationAt');
  287. $data = $this->prepareTestData($array);
  288. $this->validator->validate($data, new Collection(array(
  289. 'foo' => new Required($constraint),
  290. )));
  291. }
  292. public function testRequiredFieldMultipleConstraints()
  293. {
  294. $array = array(
  295. 'foo' => 5,
  296. );
  297. $constraints = array(
  298. new NotNull(),
  299. new Range(array('min' => 4)),
  300. );
  301. $i = 1;
  302. foreach ($constraints as $constraint) {
  303. $this->context->expects($this->at($i++))
  304. ->method('validateValue')
  305. ->with($array['foo'], $constraint, '[foo]', 'MyGroup');
  306. }
  307. $this->context->expects($this->never())
  308. ->method('addViolationAt');
  309. $data = $this->prepareTestData($array);
  310. $this->validator->validate($array, new Collection(array(
  311. 'foo' => new Required($constraints),
  312. )));
  313. }
  314. public function testObjectShouldBeLeftUnchanged()
  315. {
  316. $value = new \ArrayObject(array(
  317. 'foo' => 3
  318. ));
  319. $this->validator->validate($value, new Collection(array(
  320. 'fields' => array(
  321. 'foo' => new Range(array('min' => 2)),
  322. )
  323. )));
  324. $this->assertEquals(array(
  325. 'foo' => 3
  326. ), (array) $value);
  327. }
  328. }