/
collection.phpt
235 lines (170 loc) · 5.32 KB
/
collection.phpt
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
<?php declare(strict_types = 1);
/**
* @testCase
* @dataProvider ../../../sections.ini
*/
namespace NextrasTests\Orm\Integration\Collection;
use Nextras\Orm\Collection\ICollection;
use NextrasTests\Orm\Book;
use NextrasTests\Orm\DataTestCase;
use NextrasTests\Orm\Helper;
use NextrasTests\Orm\Publisher;
use NextrasTests\Orm\TagFollower;
use Tester\Assert;
use Tester\Environment;
$dic = require_once __DIR__ . '/../../../bootstrap.php';
class CollectionTest extends DataTestCase
{
public function testCountOnOrdered()
{
$collection = $this->orm->books->findAll();
$collection = $collection->orderBy('id');
Assert::same(4, $collection->countStored());
}
public function testCountInCycle()
{
$ids = [];
$books = $this->orm->authors->getById(1)->books;
foreach ($books as $book) {
$ids[] = $book->id;
Assert::equal(2, $books->count());
}
sort($ids);
Assert::equal([1, 2], $ids);
}
public function testCountOnLimited()
{
$collection = $this->orm->books->findAll();
$collection = $collection->orderBy('id')->limitBy(1, 1);
Assert::same(1, $collection->count());
$collection = $collection->limitBy(1, 10);
Assert::same(0, $collection->count());
$collection = $this->orm->books->findAll();
$collection = $collection->orderBy('id')->limitBy(1, 1);
Assert::same(1, $collection->countStored());
$collection = $collection->limitBy(1, 10);
Assert::same(0, $collection->countStored());
}
public function testCountOnLimitedWithJoin()
{
$collection = $this->orm->books->findBy(['this->author->name' => 'Writer 1'])->orderBy('id')->limitBy(5);
Assert::same(2, $collection->countStored());
$collection = $this->orm->tagFollowers->findBy(['this->tag->name' => 'Tag 1'])->orderBy('tag')->limitBy(3);
Assert::same(1, $collection->countStored());
}
public function testQueryByEntity()
{
$book = $this->orm->books->getById(1);
$books = $this->orm->books->findBy(['id' => $book]);
Assert::same(1, $books->countStored());
Assert::same(1, $books->count());
}
public function testOrdering()
{
$books = $this->orm->books->findAll()
->orderBy('this->author->id', ICollection::DESC)
->orderBy('title', ICollection::ASC);
$ids = [];
foreach ($books as $book) {
$ids[] = $book->id;
}
Assert::same([3, 4, 1, 2], $ids);
$books = $this->orm->books->findAll()
->orderBy('this->author->id', ICollection::DESC)
->orderBy('title', ICollection::DESC);
$ids = [];
foreach ($books as $book) {
$ids[] = $book->id;
}
Assert::same([4, 3, 2, 1], $ids);
}
public function testOrderingWithOptionalProperty()
{
$bookIds = $this->orm->books->findAll()
->orderBy('this->translator->name')
->orderBy('id', ICollection::DESC)
->fetchPairs(null, 'id');
if ($this->section === Helper::SECTION_PGSQL) {
Assert::same([1, 4, 3, 2], $bookIds);
} else {
Assert::same([2, 1, 4, 3], $bookIds);
}
}
public function testOrderingDateTimeImmutable()
{
$books = $this->orm->books->findAll()
->orderBy('publishedAt', ICollection::DESC);
$ids = [];
foreach ($books as $book) {
$ids[] = $book->id;
}
Assert::same([1, 3, 2, 4], $ids);
}
public function testEmptyArray()
{
$books = $this->orm->books->findBy(['id' => []]);
Assert::same(0, $books->count());
$books = $this->orm->books->findBy(['id!=' => []]);
Assert::same(4, $books->count());
}
public function testConditionsInSameJoin()
{
$books = $this->orm->books->findBy([
'this->author->name' => 'Writer 1',
'this->author->web' => 'http://example.com/1',
]);
Assert::same(2, $books->count());
}
public function testConditionsInDifferentJoinsAndSameTable()
{
$book = new Book();
$this->orm->books->attach($book);
$book->title = 'Books 5';
$book->author = 1;
$book->translator = 2;
$book->publisher = 1;
$this->orm->books->persistAndFlush($book);
$books = $this->orm->books->findBy([
'this->author->name' => 'Writer 1',
'this->translator->web' => 'http://example.com/2',
]);
Assert::same(1, $books->count());
}
public function testCompositePK()
{
$followers = $this->orm->tagFollowers->findById([2, 2]);
Assert::same(1, $followers->count());
/** @var TagFollower $follower */
$follower = $followers->fetch();
Assert::same(2, $follower->tag->id);
Assert::same(2, $follower->author->id);
$followers = $this->orm->tagFollowers->findById([[2, 2], [3, 1]])->orderBy('author');
Assert::same(2, $followers->count());
/** @var TagFollower $follower */
$follower = $followers->fetch();
Assert::same(3, $follower->tag->id);
Assert::same(1, $follower->author->id);
Assert::same(1, $this->orm->tagFollowers->findBy(['id!=' => [[2, 2], [3, 1]]])->count());
}
public function testPrimaryProxy()
{
/** @var Publisher $publisher */
$publisher = $this->orm->publishers->getBy(['publisherId' => 1]);
Assert::same('Nextras publisher A', $publisher->name);
Assert::equal(1, $publisher->id);
}
public function testMappingInCollection()
{
if ($this->section === 'array') Environment::skip('Test is only for Dbal mapper.');
$tags = $this->orm->tags->findBy(['isGlobal' => true]);
Assert::same(2, $tags->countStored());
Assert::same('Tag 1', $tags->fetch()->name);
}
public function testFindByNull()
{
$all = $this->orm->books->findBy(['printedAt' => NULL])->fetchAll();
Assert::count(4, $all);
}
}
$test = new CollectionTest($dic);
$test->run();