-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCriteriaTest.php
More file actions
161 lines (130 loc) · 4.6 KB
/
CriteriaTest.php
File metadata and controls
161 lines (130 loc) · 4.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
<?php
declare(strict_types=1);
namespace ComplexHeart\Test\Domain\Criteria;
use ComplexHeart\Domain\Criteria\Contracts\CriteriaSource;
use ComplexHeart\Domain\Criteria\Criteria;
use ComplexHeart\Domain\Criteria\Errors\CriteriaError;
use ComplexHeart\Domain\Criteria\FilterGroup;
use ComplexHeart\Domain\Criteria\Order;
use ComplexHeart\Domain\Criteria\Page;
test('Criteria should be successfully created from source.', function () {
$c = Criteria::fromSource(new class () implements CriteriaSource {
public function filterGroups(): array
{
return [
[
['field' => 'name', 'operator' => '=', 'value' => 'Jules'],
['field' => 'surname', 'operator' => '=', 'value' => 'Winnfield'],
]
];
}
public function orderType(): string
{
return 'ASC';
}
public function orderBy(): string
{
return 'name';
}
public function pageLimit(): int
{
return 25;
}
public function pageOffset(): int
{
return 0;
}
public function pageNumber(): int
{
return 3;
}
});
expect($c->groups())->toHaveCount(1)
->and($c->groups()[0])->toHaveCount(2)
->and($c->orderBy())->toBe('name')
->and($c->orderType())->toBe('ASC')
->and($c->pageLimit())->toBe(25)
->and($c->pageOffset())->toBe(50);
});
test('Criteria should throw exception for invalid filter groups.', function () {
try {
Criteria::default()->withFilterGroup(fn () => [1, 2, 3]);
} catch (CriteriaError $e) {
expect($e->violations())->toHaveCount(1);
}
});
test('Criteria should change complete and partially the criteria order parameter.', function () {
$c = Criteria::default()
->withOrder(Order::desc('name'));
expect($c->orderBy())->toBe('name')
->and($c->orderType())->toBe('DESC')
->and($c->order()->isNone())->toBeFalse();
$c = $c->withOrder(Order::asc('name'));
expect($c->orderType())->toBe('ASC');
$c = $c->withOrder(Order::random());
expect($c->order()->isRandom())->toBeTrue();
$c = $c->withOrderRandom();
expect($c->order()->isRandom())->toBeTrue();
$c = $c->withOrderBy('surname');
expect($c->orderBy())->toBe('surname');
$c = $c->withOrderType('ASC');
expect($c->orderType())->toBe('ASC');
});
test('Criteria should change complete and partially the criteria page parameter.', function () {
$c = Criteria::default()
->withPage(Page::create(100, 100));
expect($c->page()->limit())->toBe(100)
->and($c->page()->offset())->toBe(100);
$c = $c->withPageLimit(42);
expect($c->pageLimit())->toBe(42);
$c = $c->withPageOffset(10);
expect($c->pageOffset())->toBe(10);
});
test('Criteria should change the complete filter groups.', function () {
$c = Criteria::default()
->withFilterGroups([
FilterGroup::fromArray([['name', '=', 'Vicent']])
])
->withFilterGroups([
FilterGroup::fromArray([['name', '=', 'Jules']]),
FilterGroup::fromArray([['name', '=', 'Marcellus']]),
FilterGroup::fromArray([['name', '=', 'Butch']]),
]);
expect($c->groups())->toHaveCount(3);
});
test('Criteria should add or filter group to criteria object.', function () {
$c = Criteria::default()
->withFilterGroup(
FilterGroup::create()
->addFilterEqual('name', 'Vincent')
->addFilterEqual('status', 'deceased')
)
->withFilterGroup(
FilterGroup::create()
->addFilterEqual('name', 'Jules')
->addFilterEqual('deceased', 'alive')
);
$groups = $c->groups();
expect($groups)->toHaveCount(2)
->and($groups[0])->toHaveCount(2)
->and($groups[1])->toHaveCount(2);
});
test('Criteria should be correctly serialized to string.', function () {
$c = Criteria::default()
->withFilterGroup(
FilterGroup::create()
->addFilterEqual('name', 'Vincent')
->addFilterGreaterOrEqualThan('age', '35')
)
->withPageLimit(100)
->withPageOffset(0)
->withOrderBy('name')
->withOrderType('ASC');
expect($c->__toString())->toBe('name.=.Vincent+age.>=.35#name ASC#100 0');
});
test('Criteria should configure limit and offset using page number.', function () {
$c = Criteria::default()
->withPageNumber(3, 25);
expect($c->pageLimit())->toBe(25)
->and($c->pageOffset())->toBe(50);
});