Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4b14e09

Browse files
committed
update tests
1 parent fb9842a commit 4b14e09

File tree

1 file changed

+136
-0
lines changed

1 file changed

+136
-0
lines changed

src/Symfony/Component/Validator/Tests/Constraints/UniqueValidatorTest.php

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,14 @@
1515
use Symfony\Component\Validator\Constraints\UniqueValidator;
1616
use Symfony\Component\Validator\Test\ConstraintValidatorTestCase;
1717

18+
class CallableClass
19+
{
20+
public static function execute(\stdClass $object)
21+
{
22+
return [$object->name, $object->email];
23+
}
24+
}
25+
1826
class UniqueValidatorTest extends ConstraintValidatorTestCase
1927
{
2028
protected function createValidator()
@@ -84,4 +92,132 @@ public function getInvalidValues()
8492
yield 'not unique objects' => [[$object, $object]],
8593
];
8694
}
95+
96+
/**
97+
* @dataProvider getCallback
98+
*/
99+
public function testExpectsUniqueObjects($callback)
100+
{
101+
$object1 = new \stdClass();
102+
$object1->name = 'Foo';
103+
$object1->email = '[email protected]';
104+
105+
$object2 = new \stdClass();
106+
$object2->name = 'Foo';
107+
$object2->email = '[email protected]';
108+
109+
$object3 = new \stdClass();
110+
$object3->name = 'Bar';
111+
$object3->email = '[email protected]';
112+
113+
$value = [$object1, $object2, $object3];
114+
115+
$this->validator->validate($value, new Unique([
116+
'valueNormalizer' => $callback,
117+
]));
118+
119+
$this->assertNoViolation();
120+
}
121+
122+
/**
123+
* @dataProvider getCallback
124+
*/
125+
public function testExpectsNonUniqueObjects($callback)
126+
{
127+
$object1 = new \stdClass();
128+
$object1->name = 'Foo';
129+
$object1->email = '[email protected]';
130+
131+
$object2 = new \stdClass();
132+
$object2->name = 'Foo';
133+
$object2->email = '[email protected]';
134+
135+
$object3 = new \stdClass();
136+
$object3->name = 'Foo';
137+
$object3->email = '[email protected]';
138+
139+
$value = [$object1, $object2, $object3];
140+
141+
$this->validator->validate($value, new Unique([
142+
'message' => 'myMessage',
143+
'valueNormalizer' => $callback,
144+
]));
145+
146+
$this->buildViolation('myMessage')
147+
->setParameter('{{ value }}', 'array')
148+
->setCode(Unique::IS_NOT_UNIQUE)
149+
->assertRaised();
150+
}
151+
152+
public function getCallback()
153+
{
154+
return [
155+
yield 'static function' => [static function (\stdClass $object) {
156+
return [$object->name, $object->email];
157+
}],
158+
yield 'callable with string notation' => ['Symfony\Component\Validator\Tests\Constraints\CallableClass::execute'],
159+
yield 'callable with static notation' => [[CallableClass::class, 'execute']],
160+
yield 'callable with object' => [[new CallableClass(), 'execute']],
161+
];
162+
}
163+
164+
public function testExpectsInvalidNonStrictComparison()
165+
{
166+
$callback = static function ($item) {
167+
return (int) $item;
168+
};
169+
170+
$this->validator->validate([1, '1', 1.0, '1.0'], new Unique([
171+
'message' => 'myMessage',
172+
'valueNormalizer' => $callback,
173+
]));
174+
175+
$this->buildViolation('myMessage')
176+
->setParameter('{{ value }}', 'array')
177+
->setCode(Unique::IS_NOT_UNIQUE)
178+
->assertRaised();
179+
}
180+
181+
public function testExpectsValidNonStrictComparison()
182+
{
183+
$callback = static function ($item) {
184+
return (int) $item;
185+
};
186+
187+
$this->validator->validate([1, '2', 3, '4.0'], new Unique([
188+
'valueNormalizer' => $callback,
189+
]));
190+
191+
$this->assertNoViolation();
192+
}
193+
194+
public function testExpectsInvalidCaseInsensitiveComparison()
195+
{
196+
$callback = static function ($item) {
197+
return mb_strtolower($item);
198+
};
199+
200+
$this->validator->validate(['Hello', 'hello', 'HELLO', 'hellO'], new Unique([
201+
'message' => 'myMessage',
202+
'valueNormalizer' => $callback,
203+
]));
204+
205+
$this->buildViolation('myMessage')
206+
->setParameter('{{ value }}', 'array')
207+
->setCode(Unique::IS_NOT_UNIQUE)
208+
->assertRaised();
209+
}
210+
211+
public function testExpectsValidCaseInsensitiveComparison()
212+
{
213+
$callback = static function ($item) {
214+
return mb_strtolower($item);
215+
};
216+
217+
$this->validator->validate(['Hello', 'World'], new Unique([
218+
'valueNormalizer' => $callback,
219+
]));
220+
221+
$this->assertNoViolation();
222+
}
87223
}

0 commit comments

Comments
 (0)