title: Unit testing custom validation constraint in Symfony 2.1 but without accessing the container?
body: <p>How can i unit test <code>ContainsItalianVatinValidator</code> custom validator, but w*<em>ithout accessing the container</em>* and the <code>validator</code> service (and thus, create a stub object)?</p>
  
  
  
  <pre class="lang-php prettyprint-override"><code>class ContainsItalianVatinValidator extends ConstraintValidator
  {
      /**
       * @param mixed $value
       * @param \Symfony\Component\Validator\Constraint $constraint
       */
      public function validate($value, Constraint $constraint)
      {    
          if (!preg_match('/^[0-9]{11}$/', $value, $matches)) {
              $this-&gt;context-&gt;addViolation($constraint-&gt;message, array(
                  '%string%' =&gt; $value
              ));
          }
  
          // Compute and check control code
          // ...
      }
  }
  </code></pre>
  
  <p>In my test case i know i should access the <a href="http://api.symfony.com/2.1/Symfony/Component/Validator/ConstraintViolationList.html" rel="noreferrer"><code>ConstraintViolationList</code></a>, but i don't know how to do it from the validator itself:</p>
  
  <pre class="lang-php prettyprint-override"><code>class ContainsItalianVatinValidatorTest extends \PHPUnit_Framework_TestCase
  {
      public function testEmptyItalianVatin()
      {
          $emptyVatin = '';
          $validator  = new ContainsItalianVatinValidator();
          $constraint = new ContainsItalianVatinConstraint();
  
          // Do the validation
          $validator-&gt;validate($emptyVatin, $constraint);
  
          // How can a get a violation list and call -&gt;count()?
          $violations = /* ... */;
  
          // Assert
          $this-&gt;assertGreaterThan(0, $violations-&gt;count());
      }
  }
  </code></pre>