Skip to content

Commit f89828c

Browse files
committed
Feature/Add PaginatedResult interface with required methods and tests
1 parent d5b2742 commit f89828c

2 files changed

Lines changed: 150 additions & 0 deletions

File tree

src/Contracts/PaginatedResult.php

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace ComplexHeart\Domain\Criteria\Contracts;
6+
7+
/**
8+
* Domain pagination contract
9+
*
10+
* Represents a paginated collection of items with metadata.
11+
* NO web/URL concerns - pure business pagination logic.
12+
*
13+
* @author Unay Santisteban <usantisteban@othercode.io>
14+
* @package ComplexHeart\Domain\Criteria\Contracts
15+
*/
16+
interface PaginatedResult
17+
{
18+
/**
19+
* Get the items for the current page
20+
*
21+
* @return array<int, mixed>
22+
*/
23+
public function items(): array;
24+
25+
/**
26+
* Get total number of items across all pages
27+
*
28+
* @return int
29+
*/
30+
public function total(): int;
31+
32+
/**
33+
* Get items per page
34+
*
35+
* @return int
36+
*/
37+
public function perPage(): int;
38+
39+
/**
40+
* Get current page number (1-indexed)
41+
*
42+
* @return int
43+
*/
44+
public function currentPage(): int;
45+
46+
/**
47+
* Get last page number
48+
*
49+
* @return int
50+
*/
51+
public function lastPage(): int;
52+
53+
/**
54+
* Check if there are more pages after the current one
55+
*
56+
* @return bool
57+
*/
58+
public function hasMorePages(): bool;
59+
60+
/**
61+
* Get the count of items on current page
62+
*
63+
* @return int
64+
*/
65+
public function count(): int;
66+
67+
/**
68+
* Check if the current page is empty
69+
*
70+
* @return bool
71+
*/
72+
public function isEmpty(): bool;
73+
74+
/**
75+
* Check if the current page is not empty
76+
*
77+
* @return bool
78+
*/
79+
public function isNotEmpty(): bool;
80+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use ComplexHeart\Domain\Criteria\Contracts\PaginatedResult;
6+
7+
test('PaginatedResult interface exists and is accessible', function () {
8+
expect(interface_exists(PaginatedResult::class))->toBeTrue();
9+
});
10+
11+
test('PaginatedResult interface has all required methods', function () {
12+
$reflection = new ReflectionClass(PaginatedResult::class);
13+
14+
$expectedMethods = [
15+
'items',
16+
'total',
17+
'perPage',
18+
'currentPage',
19+
'lastPage',
20+
'hasMorePages',
21+
'count',
22+
'isEmpty',
23+
'isNotEmpty',
24+
];
25+
26+
foreach ($expectedMethods as $method) {
27+
expect($reflection->hasMethod($method))
28+
->toBeTrue("Method '{$method}' should exist in PaginatedResult interface");
29+
}
30+
});
31+
32+
test('PaginatedResult methods have correct return types', function () {
33+
$reflection = new ReflectionClass(PaginatedResult::class);
34+
35+
$methodReturnTypes = [
36+
'items' => 'array',
37+
'total' => 'int',
38+
'perPage' => 'int',
39+
'currentPage' => 'int',
40+
'lastPage' => 'int',
41+
'hasMorePages' => 'bool',
42+
'count' => 'int',
43+
'isEmpty' => 'bool',
44+
'isNotEmpty' => 'bool',
45+
];
46+
47+
foreach ($methodReturnTypes as $methodName => $expectedType) {
48+
$method = $reflection->getMethod($methodName);
49+
expect($method->hasReturnType())->toBeTrue("Method {$methodName} should have return type");
50+
51+
$returnType = $method->getReturnType();
52+
expect($returnType)->not->toBeNull("Method {$methodName} return type should not be null");
53+
54+
if ($returnType instanceof ReflectionNamedType) {
55+
expect($returnType->getName())->toBe($expectedType, "Method {$methodName} should return {$expectedType}");
56+
}
57+
}
58+
});
59+
60+
test('PaginatedResult is an interface not a class', function () {
61+
$reflection = new ReflectionClass(PaginatedResult::class);
62+
63+
expect($reflection->isInterface())->toBeTrue()
64+
->and($reflection->isTrait())->toBeFalse();
65+
});
66+
67+
test('PaginatedResult interface has correct namespace', function () {
68+
expect(PaginatedResult::class)
69+
->toBe('ComplexHeart\Domain\Criteria\Contracts\PaginatedResult');
70+
});

0 commit comments

Comments
 (0)