PageRenderTime 141ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://gitlab.com/Isaki/le331.fr
PHP | 441 lines | 305 code | 70 blank | 66 comment | 3 complexity | 7fb593221a4b7be4298d3e4f6ecb2ac1 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\Constraint;
  12. use Symfony\Component\Validator\Constraints\NotNull;
  13. use Symfony\Component\Validator\ConstraintValidatorInterface;
  14. use Symfony\Component\Validator\ConstraintViolation;
  15. use Symfony\Component\Validator\Context\ExecutionContext;
  16. use Symfony\Component\Validator\Context\ExecutionContextInterface;
  17. use Symfony\Component\Validator\Context\LegacyExecutionContext;
  18. use Symfony\Component\Validator\ExecutionContextInterface as LegacyExecutionContextInterface;
  19. use Symfony\Component\Validator\Mapping\ClassMetadata;
  20. use Symfony\Component\Validator\Mapping\PropertyMetadata;
  21. use Symfony\Component\Validator\Validation;
  22. /**
  23. * @since 2.5.3
  24. *
  25. * @author Bernhard Schussek <bschussek@gmail.com>
  26. */
  27. abstract class AbstractConstraintValidatorTest extends \PHPUnit_Framework_TestCase
  28. {
  29. /**
  30. * @var ExecutionContextInterface
  31. */
  32. protected $context;
  33. /**
  34. * @var ConstraintValidatorInterface
  35. */
  36. protected $validator;
  37. protected $group;
  38. protected $metadata;
  39. protected $object;
  40. protected $value;
  41. protected $root;
  42. protected $propertyPath;
  43. protected $constraint;
  44. protected $defaultTimezone;
  45. protected function setUp()
  46. {
  47. $this->group = 'MyGroup';
  48. $this->metadata = null;
  49. $this->object = null;
  50. $this->value = 'InvalidValue';
  51. $this->root = 'root';
  52. $this->propertyPath = 'property.path';
  53. // Initialize the context with some constraint so that we can
  54. // successfully build a violation.
  55. $this->constraint = new NotNull();
  56. $this->context = $this->createContext();
  57. $this->validator = $this->createValidator();
  58. $this->validator->initialize($this->context);
  59. \Locale::setDefault('en');
  60. $this->setDefaultTimezone('UTC');
  61. }
  62. protected function tearDown()
  63. {
  64. $this->restoreDefaultTimezone();
  65. }
  66. protected function setDefaultTimezone($defaultTimezone)
  67. {
  68. // Make sure this method can not be called twice before calling
  69. // also restoreDefaultTimezone()
  70. if (null === $this->defaultTimezone) {
  71. $this->defaultTimezone = date_default_timezone_get();
  72. date_default_timezone_set($defaultTimezone);
  73. }
  74. }
  75. protected function restoreDefaultTimezone()
  76. {
  77. if (null !== $this->defaultTimezone) {
  78. date_default_timezone_set($this->defaultTimezone);
  79. $this->defaultTimezone = null;
  80. }
  81. }
  82. protected function createContext()
  83. {
  84. $translator = $this->getMock('Symfony\Component\Translation\TranslatorInterface');
  85. $validator = $this->getMock('Symfony\Component\Validator\Validator\ValidatorInterface');
  86. $contextualValidator = $this->getMock('Symfony\Component\Validator\Validator\ContextualValidatorInterface');
  87. switch ($this->getApiVersion()) {
  88. case Validation::API_VERSION_2_5:
  89. $context = new ExecutionContext(
  90. $validator,
  91. $this->root,
  92. $translator
  93. );
  94. break;
  95. case Validation::API_VERSION_2_4:
  96. case Validation::API_VERSION_2_5_BC:
  97. $context = new LegacyExecutionContext(
  98. $validator,
  99. $this->root,
  100. $this->getMock('Symfony\Component\Validator\MetadataFactoryInterface'),
  101. $translator
  102. );
  103. break;
  104. default:
  105. throw new \RuntimeException('Invalid API version');
  106. }
  107. $context->setGroup($this->group);
  108. $context->setNode($this->value, $this->object, $this->metadata, $this->propertyPath);
  109. $context->setConstraint($this->constraint);
  110. $validator->expects($this->any())
  111. ->method('inContext')
  112. ->with($context)
  113. ->will($this->returnValue($contextualValidator));
  114. return $context;
  115. }
  116. /**
  117. * @param mixed $message
  118. * @param array $parameters
  119. * @param string $propertyPath
  120. * @param string $invalidValue
  121. * @param null $plural
  122. * @param null $code
  123. *
  124. * @return ConstraintViolation
  125. *
  126. * @deprecated to be removed in Symfony 3.0. Use {@link buildViolation()} instead.
  127. */
  128. protected function createViolation($message, array $parameters = array(), $propertyPath = 'property.path', $invalidValue = 'InvalidValue', $plural = null, $code = null)
  129. {
  130. return new ConstraintViolation(
  131. null,
  132. $message,
  133. $parameters,
  134. $this->root,
  135. $propertyPath,
  136. $invalidValue,
  137. $plural,
  138. $code,
  139. $this->constraint
  140. );
  141. }
  142. protected function setGroup($group)
  143. {
  144. $this->group = $group;
  145. $this->context->setGroup($group);
  146. }
  147. protected function setObject($object)
  148. {
  149. $this->object = $object;
  150. $this->metadata = is_object($object)
  151. ? new ClassMetadata(get_class($object))
  152. : null;
  153. $this->context->setNode($this->value, $this->object, $this->metadata, $this->propertyPath);
  154. }
  155. protected function setProperty($object, $property)
  156. {
  157. $this->object = $object;
  158. $this->metadata = is_object($object)
  159. ? new PropertyMetadata(get_class($object), $property)
  160. : null;
  161. $this->context->setNode($this->value, $this->object, $this->metadata, $this->propertyPath);
  162. }
  163. protected function setValue($value)
  164. {
  165. $this->value = $value;
  166. $this->context->setNode($this->value, $this->object, $this->metadata, $this->propertyPath);
  167. }
  168. protected function setRoot($root)
  169. {
  170. $this->root = $root;
  171. $this->context = $this->createContext();
  172. $this->validator->initialize($this->context);
  173. }
  174. protected function setPropertyPath($propertyPath)
  175. {
  176. $this->propertyPath = $propertyPath;
  177. $this->context->setNode($this->value, $this->object, $this->metadata, $this->propertyPath);
  178. }
  179. protected function expectNoValidate()
  180. {
  181. $validator = $this->context->getValidator()->inContext($this->context);
  182. $validator->expects($this->never())
  183. ->method('atPath');
  184. $validator->expects($this->never())
  185. ->method('validate');
  186. }
  187. protected function expectValidateAt($i, $propertyPath, $value, $group)
  188. {
  189. $validator = $this->context->getValidator()->inContext($this->context);
  190. $validator->expects($this->at(2 * $i))
  191. ->method('atPath')
  192. ->with($propertyPath)
  193. ->will($this->returnValue($validator));
  194. $validator->expects($this->at(2 * $i + 1))
  195. ->method('validate')
  196. ->with($value, $this->logicalOr(null, array()), $group);
  197. }
  198. protected function expectValidateValueAt($i, $propertyPath, $value, $constraints, $group = null)
  199. {
  200. $contextualValidator = $this->context->getValidator()->inContext($this->context);
  201. $contextualValidator->expects($this->at(2 * $i))
  202. ->method('atPath')
  203. ->with($propertyPath)
  204. ->will($this->returnValue($contextualValidator));
  205. $contextualValidator->expects($this->at(2 * $i + 1))
  206. ->method('validate')
  207. ->with($value, $constraints, $group);
  208. }
  209. protected function assertNoViolation()
  210. {
  211. $this->assertSame(0, $violationsCount = count($this->context->getViolations()), sprintf('0 violation expected. Got %u.', $violationsCount));
  212. }
  213. /**
  214. * @param mixed $message
  215. * @param array $parameters
  216. * @param string $propertyPath
  217. * @param string $invalidValue
  218. * @param null $plural
  219. * @param null $code
  220. *
  221. * @deprecated To be removed in Symfony 3.0. Use
  222. * {@link buildViolation()} instead.
  223. */
  224. protected function assertViolation($message, array $parameters = array(), $propertyPath = 'property.path', $invalidValue = 'InvalidValue', $plural = null, $code = null)
  225. {
  226. @trigger_error('The '.__METHOD__.' method is deprecated since version 2.3 and will be removed in 3.0. Use the buildViolation() method instead.', E_USER_DEPRECATED);
  227. $this->buildViolation($message)
  228. ->setParameters($parameters)
  229. ->atPath($propertyPath)
  230. ->setInvalidValue($invalidValue)
  231. ->setCode($code)
  232. ->setPlural($plural)
  233. ->assertRaised();
  234. }
  235. /**
  236. * @param array $expected
  237. *
  238. * @deprecated To be removed in Symfony 3.0. Use
  239. * {@link buildViolation()} instead.
  240. */
  241. protected function assertViolations(array $expected)
  242. {
  243. @trigger_error('The '.__METHOD__.' method is deprecated since version 2.3 and will be removed in 3.0. Use the buildViolation() method instead.', E_USER_DEPRECATED);
  244. $violations = $this->context->getViolations();
  245. $this->assertCount(count($expected), $violations);
  246. $i = 0;
  247. foreach ($expected as $violation) {
  248. $this->assertEquals($violation, $violations[$i++]);
  249. }
  250. }
  251. /**
  252. * @param $message
  253. *
  254. * @return ConstraintViolationAssertion
  255. */
  256. protected function buildViolation($message)
  257. {
  258. return new ConstraintViolationAssertion($this->context, $message, $this->constraint);
  259. }
  260. protected function getApiVersion()
  261. {
  262. return Validation::API_VERSION_2_5;
  263. }
  264. abstract protected function createValidator();
  265. }
  266. /**
  267. * @internal
  268. */
  269. class ConstraintViolationAssertion
  270. {
  271. /**
  272. * @var LegacyExecutionContextInterface
  273. */
  274. private $context;
  275. /**
  276. * @var ConstraintViolationAssertion[]
  277. */
  278. private $assertions;
  279. private $message;
  280. private $parameters = array();
  281. private $invalidValue = 'InvalidValue';
  282. private $propertyPath = 'property.path';
  283. private $translationDomain;
  284. private $plural;
  285. private $code;
  286. private $constraint;
  287. private $cause;
  288. public function __construct(LegacyExecutionContextInterface $context, $message, Constraint $constraint = null, array $assertions = array())
  289. {
  290. $this->context = $context;
  291. $this->message = $message;
  292. $this->constraint = $constraint;
  293. $this->assertions = $assertions;
  294. }
  295. public function atPath($path)
  296. {
  297. $this->propertyPath = $path;
  298. return $this;
  299. }
  300. public function setParameter($key, $value)
  301. {
  302. $this->parameters[$key] = $value;
  303. return $this;
  304. }
  305. public function setParameters(array $parameters)
  306. {
  307. $this->parameters = $parameters;
  308. return $this;
  309. }
  310. public function setTranslationDomain($translationDomain)
  311. {
  312. $this->translationDomain = $translationDomain;
  313. return $this;
  314. }
  315. public function setInvalidValue($invalidValue)
  316. {
  317. $this->invalidValue = $invalidValue;
  318. return $this;
  319. }
  320. public function setPlural($number)
  321. {
  322. $this->plural = $number;
  323. return $this;
  324. }
  325. public function setCode($code)
  326. {
  327. $this->code = $code;
  328. return $this;
  329. }
  330. public function setCause($cause)
  331. {
  332. $this->cause = $cause;
  333. return $this;
  334. }
  335. public function buildNextViolation($message)
  336. {
  337. $assertions = $this->assertions;
  338. $assertions[] = $this;
  339. return new self($this->context, $message, $this->constraint, $assertions);
  340. }
  341. public function assertRaised()
  342. {
  343. $expected = array();
  344. foreach ($this->assertions as $assertion) {
  345. $expected[] = $assertion->getViolation();
  346. }
  347. $expected[] = $this->getViolation();
  348. $violations = iterator_to_array($this->context->getViolations());
  349. \PHPUnit_Framework_Assert::assertSame($expectedCount = count($expected), $violationsCount = count($violations), sprintf('%u violation(s) expected. Got %u.', $expectedCount, $violationsCount));
  350. reset($violations);
  351. foreach ($expected as $violation) {
  352. \PHPUnit_Framework_Assert::assertEquals($violation, current($violations));
  353. next($violations);
  354. }
  355. }
  356. private function getViolation()
  357. {
  358. return new ConstraintViolation(
  359. null,
  360. $this->message,
  361. $this->parameters,
  362. $this->context->getRoot(),
  363. $this->propertyPath,
  364. $this->invalidValue,
  365. $this->plural,
  366. $this->code,
  367. $this->constraint,
  368. $this->cause
  369. );
  370. }
  371. }