Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8526215
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T08:12:34+00:00 2026-06-11T08:12:34+00:00

How can i unit test ContainsItalianVatinValidator custom validator, but w* ithout accessing the container

  • 0

How can i unit test ContainsItalianVatinValidator custom validator, but w*ithout accessing the container* and the validator service (and thus, create a stub object)?

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->context->addViolation($constraint->message, array(
                '%string%' => $value
            ));
        }

        // Compute and check control code
        // ...
    }
}

In my test case i know i should access the ConstraintViolationList, but i don’t know how to do it from the validator itself:

class ContainsItalianVatinValidatorTest extends \PHPUnit_Framework_TestCase
{
    public function testEmptyItalianVatin()
    {
        $emptyVatin = '';
        $validator  = new ContainsItalianVatinValidator();
        $constraint = new ContainsItalianVatinConstraint();

        // Do the validation
        $validator->validate($emptyVatin, $constraint);

        // How can a get a violation list and call ->count()?
        $violations = /* ... */;

        // Assert
        $this->assertGreaterThan(0, $violations->count());
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T08:12:36+00:00Added an answer on June 11, 2026 at 8:12 am

    When you take a look at the parent class of the validator Symfony\Component\Validator\ConstraintValidator you see that there is a method called initialize which takes an instance of Symfony\Component\Validator\ExecutionContext as argument.

    After you created the validator you can call the initialize method and pass a mock context to the validator. You don’t have to test if the addViolation method works correctly, you only have to test if it is called and if it is called with the correct parameters. You can do that with the mock functionality of PHPUnit.

    ...
    $validator  = new ContainsItalianVatinValidator();
    $context = $this->getMockBuilder('Symfony\Component\Validator\ExecutionContext')-> disableOriginalConstructor()->getMock();
    
    $context->expects($this->once())
        ->method('addViolation')
        ->with($this->equalTo('[message]'), $this->equalTo(array('%string%', '')));
    
    $validator->initialize($context);
    
    $validator->validate($emptyVatin, $constraint);
    ...
    

    In this code you have to replace [message] with the message stored in $constraint->message.

    Actually, this question is more related to PHPUnit than to Symfony. You may find the Test Doubles chapter of the PHPUnit documentation interesting.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I unit test a method which uses a session object inside of
How can I unit test my NSURLConnection delegate? I made a ConnectionDelegate class which
As topic suggested, how can I unit test a function that does not return
Can I use FlexUnit to unit test a File Upload and delete? Are those
How can I write a unit test to test the ActualWidth property in a
Can you share the way you setup your unit test projects within your .net
Can someone tell me why this unit test that checks for exceptions fails? Obviously
I can typically test a regular Test::Unit method using the following commandline syntax for
I've been trying to set my application up so that I can unit test
What is the difference between unit tests and functional tests? Can a unit test

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.