Skip to content

Commit

Permalink
Merge branch '4.4' into 5.0
Browse files Browse the repository at this point in the history
* 4.4:
  updated VERSION for 3.4.40
  update CONTRIBUTORS for 3.4.40
  updated CHANGELOG for 3.4.40
  [WebProfilerBundle] changed label of peak memory usage in the time & memory panels (MB into MiB)
  add tests for the ConstraintViolationBuilder class
  Improve dirname usage
  [PhpUnitBridge] Use COMPOSER_BINARY env var if available
  Allow invalidateTags calls to be traced by data collector
  [YAML] escape DEL(\x7f)
  fix compatibility with phpunit 9
  [Cache] skip APCu in chains when the backend is disabled
  [Mailer] Add a comment to avoid more wrong PRs on this piece of code
  [Form] apply automatically step=1 for datetime-local input
  Fixing a bug where class_alias would cause incorrect items in debug:autowiring
  [DependencyInjection][ServiceSubscriber] Support late aliases
  • Loading branch information
nicolas-grekas committed Apr 28, 2020
2 parents 3543cfd + b829f1a commit fe94566
Showing 1 changed file with 106 additions and 0 deletions.
106 changes: 106 additions & 0 deletions Tests/Violation/ConstraintViolationBuilderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Validator\Tests\Violation;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Translation\IdentityTranslator;
use Symfony\Component\Validator\Constraints\Valid;
use Symfony\Component\Validator\ConstraintViolation;
use Symfony\Component\Validator\ConstraintViolationList;
use Symfony\Component\Validator\Test\ForwardCompatTestTrait;
use Symfony\Component\Validator\Violation\ConstraintViolationBuilder;

class ConstraintViolationBuilderTest extends TestCase
{
use ForwardCompatTestTrait;

private $root;
private $violations;
private $messageTemplate = '%value% is invalid';
private $builder;

private function doSetUp()
{
$this->root = [
'data' => [
'foo' => 'bar',
'baz' => 'foobar',
],
];
$this->violations = new ConstraintViolationList();
$this->builder = new ConstraintViolationBuilder($this->violations, new Valid(), $this->messageTemplate, [], $this->root, 'data', 'foo', new IdentityTranslator());
}

public function testAddViolation()
{
$this->builder->addViolation();

$this->assertViolationEquals(new ConstraintViolation($this->messageTemplate, $this->messageTemplate, [], $this->root, 'data', 'foo', null, null, new Valid()));
}

public function testAppendPropertyPath()
{
$this->builder
->atPath('foo')
->addViolation();

$this->assertViolationEquals(new ConstraintViolation($this->messageTemplate, $this->messageTemplate, [], $this->root, 'data.foo', 'foo', null, null, new Valid()));
}

public function testAppendMultiplePropertyPaths()
{
$this->builder
->atPath('foo')
->atPath('bar')
->addViolation();

$this->assertViolationEquals(new ConstraintViolation($this->messageTemplate, $this->messageTemplate, [], $this->root, 'data.foo.bar', 'foo', null, null, new Valid()));
}

public function testCodeCanBeSet()
{
$this->builder
->setCode(5)
->addViolation();

$this->assertViolationEquals(new ConstraintViolation($this->messageTemplate, $this->messageTemplate, [], $this->root, 'data', 'foo', null, 5, new Valid()));
}

public function testCauseCanBeSet()
{
$cause = new \LogicException();

$this->builder
->setCause($cause)
->addViolation();

$this->assertViolationEquals(new ConstraintViolation($this->messageTemplate, $this->messageTemplate, [], $this->root, 'data', 'foo', null, null, new Valid(), $cause));
}

private function assertViolationEquals(ConstraintViolation $expectedViolation)
{
$this->assertCount(1, $this->violations);

$violation = $this->violations->get(0);

$this->assertSame($expectedViolation->getMessage(), $violation->getMessage());
$this->assertSame($expectedViolation->getMessageTemplate(), $violation->getMessageTemplate());
$this->assertSame($expectedViolation->getParameters(), $violation->getParameters());
$this->assertSame($expectedViolation->getPlural(), $violation->getPlural());
$this->assertSame($expectedViolation->getRoot(), $violation->getRoot());
$this->assertSame($expectedViolation->getPropertyPath(), $violation->getPropertyPath());
$this->assertSame($expectedViolation->getInvalidValue(), $violation->getInvalidValue());
$this->assertSame($expectedViolation->getCode(), $violation->getCode());
$this->assertEquals($expectedViolation->getConstraint(), $violation->getConstraint());
$this->assertSame($expectedViolation->getCause(), $violation->getCause());
}
}

0 comments on commit fe94566

Please sign in to comment.