|
7 | 7 | use Internal\DLoad\Module\Repository\Internal\Collection; |
8 | 8 | use Internal\DLoad\Module\Repository\Internal\Paginator; |
9 | 9 | use PHPUnit\Framework\Attributes\CoversClass; |
| 10 | +use PHPUnit\Framework\Attributes\DataProvider; |
10 | 11 | use PHPUnit\Framework\TestCase; |
11 | 12 |
|
12 | 13 | #[CoversClass(Collection::class)] |
13 | 14 | final class CollectionTest extends TestCase |
14 | 15 | { |
| 16 | + public static function provideLimitCases(): \Generator |
| 17 | + { |
| 18 | + yield 'empty collection' => [ |
| 19 | + [], 5, [], |
| 20 | + ]; |
| 21 | + |
| 22 | + yield 'limit less than collection size' => [ |
| 23 | + [1, 2, 3, 4, 5], 3, [1, 2, 3], |
| 24 | + ]; |
| 25 | + |
| 26 | + yield 'limit equal to collection size' => [ |
| 27 | + [1, 2, 3], 3, [1, 2, 3], |
| 28 | + ]; |
| 29 | + |
| 30 | + yield 'limit greater than collection size' => [ |
| 31 | + [1, 2, 3], 5, [1, 2, 3], |
| 32 | + ]; |
| 33 | + |
| 34 | + yield 'zero limit returns all items' => [ |
| 35 | + [1, 2, 3, 4, 5], 0, [1, 2, 3, 4, 5], |
| 36 | + ]; |
| 37 | + } |
| 38 | + |
15 | 39 | public function testCollectionWithIterable(): void |
16 | 40 | { |
17 | 41 | // Create a test collection class |
@@ -145,4 +169,139 @@ public function testFirst(): void |
145 | 169 | $this->assertTrue($pagesLoaded[1]); |
146 | 170 | $this->assertFalse($pagesLoaded[2]); |
147 | 171 | } |
| 172 | + |
| 173 | + #[DataProvider('provideLimitCases')] |
| 174 | + public function testLimit(array $sourceItems, int $limit, array $expectedItems): void |
| 175 | + { |
| 176 | + // Arrange |
| 177 | + $testCollection = new class([]) extends Collection {}; |
| 178 | + $collection = $testCollection::create($sourceItems); |
| 179 | + |
| 180 | + // Act |
| 181 | + $limited = $collection->limit($limit); |
| 182 | + |
| 183 | + // Assert |
| 184 | + self::assertEquals($expectedItems, $limited->toArray()); |
| 185 | + |
| 186 | + // Check count matches expected |
| 187 | + self::assertCount(\count($expectedItems), $limited); |
| 188 | + } |
| 189 | + |
| 190 | + public function testLimitWithFilter(): void |
| 191 | + { |
| 192 | + // Arrange |
| 193 | + $testCollection = new class([]) extends Collection {}; |
| 194 | + $collection = $testCollection::create([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]); |
| 195 | + |
| 196 | + // Act |
| 197 | + $filtered = $collection |
| 198 | + ->filter(static fn($item) => $item > 3) |
| 199 | + ->limit(2); |
| 200 | + |
| 201 | + // Assert |
| 202 | + self::assertEquals([4, 5], $filtered->toArray()); |
| 203 | + self::assertCount(2, $filtered); |
| 204 | + } |
| 205 | + |
| 206 | + public function testLimitWithPaginator(): void |
| 207 | + { |
| 208 | + // Arrange |
| 209 | + $testCollection = new class([]) extends Collection {}; |
| 210 | + |
| 211 | + $pageLoader = static function (): \Generator { |
| 212 | + yield [1, 2, 3]; |
| 213 | + yield [4, 5, 6]; |
| 214 | + yield [7, 8, 9]; |
| 215 | + }; |
| 216 | + |
| 217 | + $paginator = Paginator::createFromGenerator($pageLoader(), null); |
| 218 | + $collection = $testCollection::create($paginator); |
| 219 | + |
| 220 | + // Act |
| 221 | + $limited = $collection->limit(4); |
| 222 | + |
| 223 | + // Assert |
| 224 | + self::assertEquals([1, 2, 3, 4], $limited->toArray()); |
| 225 | + self::assertCount(4, $limited); |
| 226 | + } |
| 227 | + |
| 228 | + public function testLimitWithNegativeCount(): void |
| 229 | + { |
| 230 | + // Arrange |
| 231 | + $testCollection = new class([]) extends Collection {}; |
| 232 | + $collection = $testCollection::create([1, 2, 3]); |
| 233 | + |
| 234 | + // Assert |
| 235 | + $this->expectException(\InvalidArgumentException::class); |
| 236 | + $this->expectExceptionMessage('Limit count must be non-negative'); |
| 237 | + |
| 238 | + // Act |
| 239 | + $collection->limit(-1); |
| 240 | + } |
| 241 | + |
| 242 | + public function testLimitWithZeroCountResetsLimit(): void |
| 243 | + { |
| 244 | + // Arrange |
| 245 | + $testCollection = new class([]) extends Collection {}; |
| 246 | + $collection = $testCollection::create([1, 2, 3, 4, 5]); |
| 247 | + $limitedCollection = $collection->limit(2); |
| 248 | + |
| 249 | + // Verify the limit was applied |
| 250 | + self::assertEquals([1, 2], $limitedCollection->toArray()); |
| 251 | + |
| 252 | + // Act - apply zero limit to reset the limit |
| 253 | + $resetCollection = $limitedCollection->limit(0); |
| 254 | + |
| 255 | + // Assert - should have all items |
| 256 | + self::assertEquals([1, 2, 3, 4, 5], $resetCollection->toArray()); |
| 257 | + } |
| 258 | + |
| 259 | + public function testLimitResetWithFilteredCollection(): void |
| 260 | + { |
| 261 | + // Arrange |
| 262 | + $testCollection = new class([]) extends Collection {}; |
| 263 | + $collection = $testCollection::create([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]); |
| 264 | + |
| 265 | + // Apply filter and limit |
| 266 | + $filtered = $collection |
| 267 | + ->filter(static fn($item) => $item > 3) |
| 268 | + ->limit(2); |
| 269 | + |
| 270 | + // Verify initial state |
| 271 | + self::assertEquals([4, 5], $filtered->toArray()); |
| 272 | + |
| 273 | + // Act - reset limit |
| 274 | + $resetLimited = $filtered->limit(0); |
| 275 | + |
| 276 | + // Assert - filter should still be applied, but not the limit |
| 277 | + self::assertEquals([4, 5, 6, 7, 8, 9, 10], $resetLimited->toArray()); |
| 278 | + } |
| 279 | + |
| 280 | + public function testCountWithLimit(): void |
| 281 | + { |
| 282 | + // Arrange |
| 283 | + $testCollection = new class([]) extends Collection {}; |
| 284 | + $collection = $testCollection::create([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]); |
| 285 | + |
| 286 | + // Act |
| 287 | + $limited = $collection->limit(3); |
| 288 | + |
| 289 | + // Assert |
| 290 | + self::assertCount(3, $limited); |
| 291 | + } |
| 292 | + |
| 293 | + public function testEmptyWithLimit(): void |
| 294 | + { |
| 295 | + // Arrange |
| 296 | + $testCollection = new class([]) extends Collection {}; |
| 297 | + $collection = $testCollection::create([1, 2, 3]); |
| 298 | + |
| 299 | + // Act & Assert |
| 300 | + self::assertFalse($collection->limit(1)->empty()); |
| 301 | + self::assertFalse($collection->limit(0)->empty()); |
| 302 | + |
| 303 | + // With an empty source collection |
| 304 | + $emptyCollection = $testCollection::create([]); |
| 305 | + self::assertTrue($emptyCollection->limit(5)->empty()); |
| 306 | + } |
148 | 307 | } |
0 commit comments