|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Vvval\Spiral\Validation\Tests\Checkers; |
|
4
|
|
|
|
|
5
|
|
|
use TestApplication\Database\Sources\TestSource; |
|
6
|
|
|
use TestApplication\Database\TestRecord; |
|
7
|
|
|
use Vvval\Spiral\Validation\Tests\BaseTest; |
|
8
|
|
|
|
|
9
|
|
|
class EntityCheckerTest extends BaseTest |
|
10
|
|
|
{ |
|
11
|
|
|
public function testIsUnique() |
|
12
|
|
|
{ |
|
13
|
|
|
$rules = [ |
|
14
|
|
|
'field' => [ |
|
15
|
|
|
['entity::isUnique', TestSource::class, 'field'] |
|
16
|
|
|
], |
|
17
|
|
|
]; |
|
18
|
|
|
$validator = $this->createValidator($rules); |
|
19
|
|
|
$validator->setData(['field' => 'value']); |
|
20
|
|
|
|
|
21
|
|
|
//nothing in db |
|
22
|
|
|
$this->assertTrue($validator->isValid(), 'Validation FAILED'); |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var TestSource $source |
|
26
|
|
|
* @var TestRecord $record |
|
27
|
|
|
*/ |
|
28
|
|
|
$source = $this->container->get(TestSource::class); |
|
29
|
|
|
$entity = $source->create(); |
|
30
|
|
|
|
|
31
|
|
|
//nothing in db (entity not saved) |
|
32
|
|
|
$validator->setContext($entity); |
|
33
|
|
|
$this->assertTrue($validator->isValid(), 'Validation FAILED'); |
|
34
|
|
|
|
|
35
|
|
|
//nothing in db (entity not saved) |
|
36
|
|
|
$entity->field = 'value'; |
|
37
|
|
|
$this->assertTrue($validator->isValid(), 'Validation FAILED'); |
|
38
|
|
|
|
|
39
|
|
|
//entity in db, but it is passed as context, no conflicts with another entities |
|
40
|
|
|
$entity->save(); |
|
|
|
|
|
|
41
|
|
|
$this->assertTrue($validator->isValid(), 'Validation FAILED'); |
|
42
|
|
|
|
|
43
|
|
|
//entity in db and it isn't passed as context, fail with conflict |
|
44
|
|
|
$validator->setContext(null); |
|
45
|
|
|
$this->assertFalse($validator->isValid(), 'Validation PASSED'); |
|
46
|
|
|
} |
|
47
|
|
|
} |
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: