Skip to content

Commit 1e8a0d5

Browse files
committed
feat: added getCount to retrieve total of records
1 parent 9ac860f commit 1e8a0d5

File tree

7 files changed

+33
-4
lines changed

7 files changed

+33
-4
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ foreach ($organizations as $organization) {
6868
// Convert to array
6969
$organizationArray = $organization->toArray();
7070
}
71+
72+
RegistroSportESalute::getCount() // The total number of matched records, useful for pagination
7173
```
7274

7375
### Filtering

composer.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@
5959
"test:unit": "pest --coverage --exactly=100",
6060
"test:types": "phpstan",
6161
"test:refactor": "rector --dry-run",
62+
"fix": [
63+
"@refactor",
64+
"@lint"
65+
],
6266
"test": [
6367
"@test:lint",
6468
"@test:type-coverage",

src/Facades/RegistroSportESalute.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
* @method static \CarloEusebi\RegistroSportESalute\RegistroSportESalute page(int $page = 1, int $pageSize = 10)
2121
* @method static Collection<int, Organization> get()
2222
* @method static array<string, int|string> getById(int $id)
23+
* @method static int getCount()
2324
*/
2425
class RegistroSportESalute extends Facade
2526
{

src/RegistroSportESalute.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ class RegistroSportESalute
4545

4646
private string $orderBy = 'societa__codiceFiscale';
4747

48+
public int $count = 0;
49+
4850
public function builder(): self
4951
{
5052
return $this;
@@ -100,6 +102,8 @@ public function get(): Collection
100102
->throwUnlessStatus(200)
101103
->json();
102104

105+
$this->count = $res['payload']['count'];
106+
103107
return collect($res['payload']['data'])
104108
->map(fn (array $organization): Organization => Organization::fromArray($organization));
105109
});
@@ -127,4 +131,9 @@ public function getById(int $id): array
127131
return Arr::mapWithKeys($res['payload']['corpo']['dati'], fn (array $dato) => [$dato['label'] => $dato['value']]);
128132
});
129133
}
134+
135+
public function getCount(): int
136+
{
137+
return $this->count;
138+
}
130139
}

src/Testing/Fakes/RegistroSportESaluteFake.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,11 @@
1515
class RegistroSportESaluteFake extends RegistroSportESalute implements Fake
1616
{
1717
public function __construct(
18-
private readonly int $count = 1,
18+
int $count = 1,
1919
private readonly bool $shouldThrowHttpException = false,
20-
) {}
20+
) {
21+
$this->count = $count;
22+
}
2123

2224
public function get(): Collection
2325
{

tests/Feature/RegistroSportESaluteTest.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
beforeEach(function (): void {
1313
Http::fake([
14-
'https://registro.sportesalute.eu/api/istruttoria/lista*' => Http::response(['payload' => ['data' => []]]),
14+
'https://registro.sportesalute.eu/api/istruttoria/lista*' => Http::response(['payload' => ['count' => 0, 'data' => []]]),
1515
'https://registro.sportesalute.eu/api/istruttoria/*/sidebar*' => Http::response(['payload' => ['corpo' => ['dati' => [['label' => 'test', 'value' => 'test']]]]]),
1616
]);
1717
});
@@ -62,6 +62,16 @@
6262
});
6363
});
6464

65+
test('count', function (): void {
66+
Http::fake([
67+
'https://registro.sportesalute.eu/api/istruttoria/lista*' => Http::response(['payload' => ['count' => 5, 'data' => []]]),
68+
]);
69+
70+
RegistroSportESalute::get();
71+
72+
expect(RegistroSportESalute::getCount())->toBe(5);
73+
});
74+
6575
describe('exceptions', function (): void {
6676
beforeEach(function (): void {
6777
Http::fake([

tests/Feature/Testing/FakingTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@
2727
test('returns correct amount of organizations', function (): void {
2828
RegistroSportESalute::fake(3);
2929

30-
expect(RegistroSportESalute::get())->toHaveCount(3);
30+
expect(RegistroSportESalute::get())->toHaveCount(3)
31+
->and(RegistroSportESalute::getCount())->toBe(3);
3132
});
3233

3334
test('get by id', function (): void {

0 commit comments

Comments
 (0)