Skip to content

Instantly share code, notes, and snippets.

@blazarecki
Last active February 4, 2016 08:47
Show Gist options
  • Save blazarecki/5dcb754be22dae4f0e53 to your computer and use it in GitHub Desktop.
Save blazarecki/5dcb754be22dae4f0e53 to your computer and use it in GitHub Desktop.
<?php
namespace AppBundle\Tests;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\Validator\Constraints\Collection;
use Symfony\Component\Validator\Constraints\Length;
use Symfony\Component\Validator\Constraints\NotBlank;
class TestCollection
{
/**
* @Collection(
* fields={
* "foo" = {
* @NotBlank(groups={"foo"}),
* @Length(min="2", groups={"bar"}),
* @Length(min="4", groups={"baz"})
* }
* }
* )
*/
public $config;
}
class TestSimple
{
/**
* @NotBlank(groups={"foo"})
* @Length(min="2", groups={"bar"})
* @Length(min="4", groups={"baz"})
*/
public $name;
}
class FailTest extends KernelTestCase
{
private $validator;
protected function setUp()
{
self::bootKernel();
$this->validator = static::$kernel->getContainer()->get('validator');
}
public function testOnlyFirstGroupIsUse()
{
$test = new TestCollection();
$test->config = ['foo' => 'a'];
/*
* foo => valid
* bar => invalid
* baz => invalid
*/
$violations = $this->validator->validate($test, null, ['foo', 'bar', 'baz']);
$this->assertCount(2, $violations); // Failed asserting that actual size 0 matches expected size 2.
$violations = $this->validator->validate($test, null, ['baz', 'bar', 'foo']);
$this->assertCount(2, $violations); // Failed asserting that actual size 1 matches expected size 2.
}
public function testGroupWithSimpleValidationWork()
{
$test = new TestSimple();
$test->name = 'a';
/*
* foo => valid
* bar => invalid
* baz => invalid
*/
$violations = $this->validator->validate($test, null, ['foo', 'bar', 'baz']);
$this->assertCount(2, $violations); // OK
$violations = $this->validator->validate($test, null, ['baz', 'bar', 'foo']);
$this->assertCount(2, $violations); // OK
}
}
<?php
namespace AppBundle\Tests;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\Validator\Constraints\Collection;
use Symfony\Component\Validator\Constraints\Length;
use Symfony\Component\Validator\Constraints\NotBlank;
class TestCollection
{
/**
* @Collection(
* fields={
* "foo" = {
* @NotBlank(groups={"foo"})
* }
* }
* )
* @Collection(
* fields={
* "foo" = {
* @Length(min="2", groups={"bar"})
* }
* }
* )
* @Collection(
* fields={
* "foo" = {
* @Length(min="4", groups={"baz"})
* }
* }
* )
*/
public $config;
}
class TestSimple
{
/**
* @NotBlank(groups={"foo"})
* @Length(min="2", groups={"bar"})
* @Length(min="4", groups={"baz"})
*/
public $name;
}
class FailTest extends KernelTestCase
{
private $validator;
protected function setUp()
{
self::bootKernel();
$this->validator = static::$kernel->getContainer()->get('validator');
}
public function testOnlyFirstGroupIsUse()
{
$test = new TestCollection();
$test->config = ['foo' => 'a'];
/*
* foo => valid
* bar => invalid
* baz => invalid
*/
$violations = $this->validator->validate($test, null, ['foo', 'bar', 'baz']);
$this->assertCount(2, $violations); // OK
$violations = $this->validator->validate($test, null, ['baz', 'bar', 'foo']);
$this->assertCount(2, $violations); // OK
}
public function testGroupWithSimpleValidationWork()
{
$test = new TestSimple();
$test->name = 'a';
/*
* foo => valid
* bar => invalid
* baz => invalid
*/
$violations = $this->validator->validate($test, null, ['foo', 'bar', 'baz']);
$this->assertCount(2, $violations); // OK
$violations = $this->validator->validate($test, null, ['baz', 'bar', 'foo']);
$this->assertCount(2, $violations); // OK
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment