Skip to content

Instantly share code, notes, and snippets.

@srosato
Last active September 19, 2016 16:04
Show Gist options
  • Save srosato/ea01256974c10409c79ee15f799f6db6 to your computer and use it in GitHub Desktop.
Save srosato/ea01256974c10409c79ee15f799f6db6 to your computer and use it in GitHub Desktop.
Form Validation Testing
<?php
namespace Tests\Component\Validation;
use Mockery as m;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\Validator\ValidatorInterface;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Context\ExecutionContextInterface;
use Tests\Codeception\TestCase\ComponentTest;
abstract class AbstractValidationTest extends ComponentTest
{
/**
* @var ValidatorInterface
*/
protected $validator;
/**
* @var Constraint
*/
protected $constraints;
/**
* {@inheritdoc}
*/
public function setUp()
{
$this->validator = $this->getContainer()->get('validator');
$this->constraints = $this->createConstraints();
}
/**
* @test
* @dataProvider getInvalidData
*/
public function itShouldHaveErrorsOnInvalidData($value)
{
$this->validateData($value, false);
}
/**
* @test
* @dataProvider getValidData
*/
public function itShouldNotHaveErrorsOnValidData($value)
{
$this->validateData($value);
}
protected function validateData($value, $shouldBeValid = true)
{
$errorsCountMatcher = $shouldBeValid ? is(equalTo(0)) : is(atLeast(1));
$message = sprintf("value %s failed on %s validation", $value, $shouldBeValid ? 'positive': 'negative');
$this->verifyThat(
$message,
$this->validator->validate($value, $this->constraints)->count(), $errorsCountMatcher);
}
abstract public function getValidData(): array;
abstract public function getInvalidData(): array;
/**
* @return Constraint[]
*/
abstract protected function createConstraints(): array;
}
<?php
namespace Tests\Codeception\TestCase;
use Codeception\Module\Symfony2;
use Codeception\TestCase\Test;
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
use PSS\SymfonyMockerContainer\DependencyInjection\MockerContainer;
use Symfony\Bundle\SwiftmailerBundle\DataCollector\MessageDataCollector;
use Symfony\Component\HttpKernel\Profiler\Profile;
use Symfony\Component\HttpKernel\Profiler\Profiler;
use Tests\DataConfiguration;
use Tests\ComponentTester;
use Symfony\Component\BrowserKit\Client;
use Symfony\Component\Routing\Router;
use Tests\Utils\Hamcrest;
/**
* @author Steven Rosato <steven.rosato@majisti.com>
*/
abstract class ComponentTest extends Test
{
use Hamcrest;
use MockeryPHPUnitIntegration;
/**
* @var ComponentTester
*/
protected $tester;
public function getRouter(): Router
{
return $this->getContainer()->get('router');
}
public function getSymfonyClient(): Client
{
return $this->getSymfony2()->client;
}
protected function getProfiler(): Profiler
{
$profiler = $this->getSymfony2()->grabService('profiler');
return $profiler;
}
protected function getProfile(): Profile
{
$this->initiateRequest();
$profiler = $this->getProfiler();
$response = $this->getSymfonyClient()->getResponse();
if (null === $response) {
$this->fail("You must perform a request before using this method.");
}
return $profiler->loadProfileFromResponse($response);
}
/**
* @return MessageDataCollector
*/
protected function getCollector()
{
return $this->getProfile()->getCollector('swiftmailer');
}
public function getSymfony2(): Symfony2
{
return $this->getModule(DataConfiguration::HELPER_SYMFONY2);
}
public function getContainer(): MockerContainer
{
return $this->getSymfony2()->container;
}
}
<?php
namespace AppBundle\Form\Constraint;
use Symfony\Component\Validator\Constraints\Email as BaseEmail;
/**
* @Annotation
*/
class EmailConstraint extends BaseEmail
{
public $message = 'constraint.email.format';
public function validatedBy()
{
return get_class($this).'Validator';
}
}
<?php
namespace Tests\Component\Validation;
use Mockery as m;
use AppBundle\Form\Constraint;
/**
* @group validation.email
*/
class EmailValidationTest extends AbstractValidationTest
{
/**
* {@inheritdoc}
*/
protected function createConstraints(): array
{
return [new Email()];
}
public function getValidData(): array
{
return [
['foo@bar.com'],
['foo1@bar.com'],
['foo.bar@baz.com'],
['foo-.bar@baz.com'],
['foo_.bar@baz.com'],
["fo'o_.bar@baz.com"],
['fo.o@bar.co.m'],
];
}
public function getInvalidData(): array
{
return [
['foo'],
['fOo1@bar.com'],
['foo@bAr.com,'],
['foo@bAr.com,'],
['foo..bar@baz.com'],
['.foo@bar.com'],
['foo.@bar.com'],
];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment