1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Spiral\Tests\DataGrid\Specification; |
6
|
|
|
|
7
|
|
|
use PHPUnit\Framework\TestCase; |
8
|
|
|
use Spiral\DataGrid\Specification\Filter\InArray; |
9
|
|
|
use Spiral\DataGrid\Specification\Filter\NotInArray; |
10
|
|
|
use Spiral\DataGrid\Specification\Value\Accessor\Split; |
11
|
|
|
use Spiral\DataGrid\Specification\Value\ArrayValue; |
12
|
|
|
use Spiral\DataGrid\Specification\Value\IntValue; |
13
|
|
|
use Spiral\DataGrid\SpecificationInterface; |
14
|
|
|
|
15
|
|
|
class InArrayTest extends TestCase |
16
|
|
|
{ |
17
|
|
|
public function testWithSplit(): void |
18
|
|
|
{ |
19
|
|
|
$str = '1|2|3'; |
20
|
|
|
|
21
|
|
|
$split = new Split(new ArrayValue(new IntValue()), '|'); |
22
|
|
|
$this->assertTrue($split->accepts($str)); |
23
|
|
|
$this->assertSame([1, 2, 3], $split->convert($str)); |
24
|
|
|
|
25
|
|
|
//failure |
26
|
|
|
$inArray = new InArray('field', $split); |
27
|
|
|
$this->assertNotInstanceOf(SpecificationInterface::class, $inArray->withValue($str)); |
28
|
|
|
|
29
|
|
|
$notInArray = new NotInArray('field', $split); |
30
|
|
|
$this->assertNotInstanceOf(SpecificationInterface::class, $notInArray->withValue($str)); |
31
|
|
|
|
32
|
|
|
//success |
33
|
|
|
$inArray = new InArray('field', $split, false); |
34
|
|
|
$this->assertInstanceOf(SpecificationInterface::class, $inArray->withValue($str)); |
35
|
|
|
$this->assertSame([1, 2, 3], $inArray->withValue($str)->getValue()); |
36
|
|
|
|
37
|
|
|
$notInArray = new NotInArray('field', $split, false); |
38
|
|
|
$this->assertInstanceOf(SpecificationInterface::class, $notInArray->withValue($str)); |
39
|
|
|
$this->assertSame([1, 2, 3], $notInArray->withValue($str)->getValue()); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function testWithoutSplit(): void |
43
|
|
|
{ |
44
|
|
|
$arr = ['1', 2]; |
45
|
|
|
$value = new IntValue(); |
46
|
|
|
|
47
|
|
|
$inArray = new InArray('field', $value); |
48
|
|
|
$this->assertInstanceOf(SpecificationInterface::class, $inArray->withValue($arr)); |
49
|
|
|
$this->assertSame([1, 2], $inArray->withValue($arr)->getValue()); |
50
|
|
|
|
51
|
|
|
$notInArray = new NotInArray('field', $value); |
52
|
|
|
$this->assertInstanceOf(SpecificationInterface::class, $notInArray->withValue($arr)); |
53
|
|
|
$this->assertSame([1, 2], $notInArray->withValue($arr)->getValue()); |
54
|
|
|
|
55
|
|
|
$inArray = new InArray('field', $value, false); |
56
|
|
|
$this->assertNotInstanceOf(SpecificationInterface::class, $inArray->withValue($arr)); |
57
|
|
|
|
58
|
|
|
$notInArray = new NotInArray('field', $value, false); |
59
|
|
|
$this->assertNotInstanceOf(SpecificationInterface::class, $notInArray->withValue($arr)); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function testWithOwnWrapping(): void |
63
|
|
|
{ |
64
|
|
|
$arr = ['1', 2]; |
65
|
|
|
$value = new ArrayValue(new IntValue()); |
66
|
|
|
|
67
|
|
|
$inArray = new InArray('field', $value); |
68
|
|
|
$this->assertInstanceOf(SpecificationInterface::class, $inArray->withValue($arr)); |
69
|
|
|
$this->assertSame([1, 2], $inArray->withValue($arr)->getValue()); |
70
|
|
|
|
71
|
|
|
$notInArray = new NotInArray('field', $value); |
72
|
|
|
$this->assertInstanceOf(SpecificationInterface::class, $notInArray->withValue($arr)); |
73
|
|
|
$this->assertSame([1, 2], $notInArray->withValue($arr)->getValue()); |
74
|
|
|
|
75
|
|
|
$inArray = new InArray('field', $value, false); |
76
|
|
|
$this->assertInstanceOf(SpecificationInterface::class, $inArray->withValue($arr)); |
77
|
|
|
$this->assertSame([1, 2], $inArray->withValue($arr)->getValue()); |
78
|
|
|
|
79
|
|
|
$notInArray = new NotInArray('field', $value, false); |
80
|
|
|
$this->assertInstanceOf(SpecificationInterface::class, $notInArray->withValue($arr)); |
81
|
|
|
$this->assertSame([1, 2], $notInArray->withValue($arr)->getValue()); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|