Skip to content

Commit a39586c

Browse files
committed
updated tests for contacts resource
1 parent 8a799a0 commit a39586c

File tree

2 files changed

+64
-6
lines changed

2 files changed

+64
-6
lines changed

src/Resources/Contacts.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,9 @@ protected function getParams(array $params, int $perPage): string
4949
{
5050
$skip = $params['skip'] ?? 0;
5151
$page = request('p', $skip);
52-
if ($page > 0) {
53-
$page--;
54-
}
52+
// if ($page > 0) {
53+
// $page--;
54+
// }
5555

5656
if ($params == []) {
5757
$params = http_build_query([
@@ -62,15 +62,15 @@ protected function getParams(array $params, int $perPage): string
6262
]);
6363
} else {
6464
// ensure $top, $skip and $count are part of params
65-
if (! in_array('$top', $params)) {
65+
if (! array_key_exists('$top', $params)) {
6666
$params['$top'] = $perPage;
6767
}
6868

69-
if (! in_array('$skip', $params)) {
69+
if (! array_key_exists('$skip', $params)) {
7070
$params['$skip'] = $page;
7171
}
7272

73-
if (! in_array('$count', $params)) {
73+
if (! array_key_exists('$count', $params)) {
7474
$params['$count'] = 'true';
7575
}
7676

tests/Resources/ContactsTest.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,64 @@
3434
->and($response['total'])->toBe(10);
3535
});
3636

37+
test('getParams returns default values when no params provided', function () {
38+
39+
$contacts = new Contacts();
40+
$reflection = new ReflectionMethod(Contacts::class, 'getParams');
41+
$response = $reflection->invoke($contacts, [], 25);
42+
43+
parse_str($response, $parsedParams);
44+
45+
expect($parsedParams)->toMatchArray([
46+
'$orderby' => 'displayName',
47+
'$top' => '25',
48+
'$skip' => '0',
49+
'$count' => 'true',
50+
]);
51+
});
52+
53+
test('getParams includes custom top parameter', function () {
54+
$contacts = new Contacts();
55+
$reflection = new ReflectionMethod(Contacts::class, 'getParams');
56+
$response = $reflection->invoke($contacts, ['$top' => 10], 25);
57+
58+
parse_str($response, $parsedParams);
59+
60+
expect($parsedParams)->toMatchArray([
61+
'$top' => '10',
62+
'$skip' => '0',
63+
'$count' => 'true',
64+
]);
65+
});
66+
67+
test('getParams includes custom skip parameter', function () {
68+
$contacts = new Contacts();
69+
$reflection = new ReflectionMethod(Contacts::class, 'getParams');
70+
$response = $reflection->invoke($contacts, ['$skip' => 15], 25);
71+
72+
parse_str($response, $parsedParams);
73+
74+
expect($parsedParams)->toMatchArray([
75+
'$top' => '25',
76+
'$skip' => '15',
77+
'$count' => 'true',
78+
]);
79+
});
80+
81+
test('getParams forces count to be true when missing', function () {
82+
$contacts = new Contacts();
83+
$reflection = new ReflectionMethod(Contacts::class, 'getParams');
84+
$response = $reflection->invoke($contacts, ['$top' => 10, '$skip' => 5], 25);
85+
86+
parse_str($response, $parsedParams);
87+
88+
expect($parsedParams)->toMatchArray([
89+
'$top' => '10',
90+
'$skip' => '5',
91+
'$count' => 'true',
92+
]);
93+
});
94+
3795
test('find method retrieves a specific contact', function () {
3896

3997
MsGraph::shouldReceive('get')

0 commit comments

Comments
 (0)