|
| 1 | +<?php |
| 2 | + |
| 3 | +use Dcblogdev\MsGraph\Facades\MsGraph; |
| 4 | +use Dcblogdev\MsGraph\Resources\Contacts; |
| 5 | + |
| 6 | +beforeEach(function () { |
| 7 | + MsGraph::shouldReceive('isConnected')->andReturn(true); |
| 8 | + $this->contacts = new Contacts; |
| 9 | +}); |
| 10 | + |
| 11 | +test('get contacts with params', function () { |
| 12 | + |
| 13 | + $messageQueryParams = [ |
| 14 | + '$orderby' => 'displayName', |
| 15 | + '$skip' => 0, |
| 16 | + '$top' => 5, |
| 17 | + '$count' => 'true', |
| 18 | + ]; |
| 19 | + |
| 20 | + MsGraph::shouldReceive('get') |
| 21 | + ->with('me/contacts?'.http_build_query($messageQueryParams)) |
| 22 | + ->andReturn([ |
| 23 | + '@odata.count' => 10, |
| 24 | + 'value' => [ |
| 25 | + ['id' => '1', 'displayName' => 'John Doe'], |
| 26 | + ['id' => '2', 'displayName' => 'Jane Doe'], |
| 27 | + ], |
| 28 | + ]); |
| 29 | + |
| 30 | + $response = (new Contacts)->get($messageQueryParams); |
| 31 | + |
| 32 | + expect($response)->toHaveKeys(['contacts', 'total', 'links', 'links_array']) |
| 33 | + ->and($response['contacts']['value'])->toBeArray() |
| 34 | + ->and($response['total'])->toBe(10); |
| 35 | +}); |
| 36 | + |
| 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 | + |
| 95 | +test('find method retrieves a specific contact', function () { |
| 96 | + |
| 97 | + MsGraph::shouldReceive('get') |
| 98 | + ->with('me/contacts/1') |
| 99 | + ->andReturn([ |
| 100 | + 'id' => '1', |
| 101 | + 'displayName' => 'John Doe', |
| 102 | + 'email' => 'johndoe@example.com', |
| 103 | + ]); |
| 104 | + |
| 105 | + $response = (new Contacts)->find('1'); |
| 106 | + |
| 107 | + expect($response) |
| 108 | + ->toHaveKeys(['id', 'displayName', 'email']) |
| 109 | + ->and($response['id'])->toBe('1') |
| 110 | + ->and($response['displayName'])->toBe('John Doe') |
| 111 | + ->and($response['email'])->toBe('johndoe@example.com'); |
| 112 | +}); |
| 113 | + |
| 114 | +test('can create contact', function () { |
| 115 | + |
| 116 | + $data = [ |
| 117 | + 'displayName' => 'John Doe', |
| 118 | + 'givenName' => 'John Doe', |
| 119 | + 'emailAddresses' => [ |
| 120 | + [ |
| 121 | + 'address' => 'john@doe.com', |
| 122 | + 'name' => 'John Doe', |
| 123 | + ], |
| 124 | + ], |
| 125 | + ]; |
| 126 | + |
| 127 | + MsGraph::shouldReceive('post') |
| 128 | + ->with('me/contacts', $data) |
| 129 | + ->andReturn([ |
| 130 | + 'id' => '1', |
| 131 | + 'displayName' => 'John Doe', |
| 132 | + 'email' => 'johndoe@example.com', |
| 133 | + ]); |
| 134 | + |
| 135 | + $response = (new Contacts)->store($data); |
| 136 | + |
| 137 | + expect($response) |
| 138 | + ->toHaveKeys(['id', 'displayName', 'email']) |
| 139 | + ->and($response['id'])->toBe('1') |
| 140 | + ->and($response['displayName'])->toBe('John Doe') |
| 141 | + ->and($response['email'])->toBe('johndoe@example.com'); |
| 142 | +}); |
| 143 | + |
| 144 | +test('can update contact', function () { |
| 145 | + |
| 146 | + $data = [ |
| 147 | + 'displayName' => 'John Doe', |
| 148 | + 'givenName' => 'John Doe', |
| 149 | + 'emailAddresses' => [ |
| 150 | + [ |
| 151 | + 'address' => 'john@doe.com', |
| 152 | + 'name' => 'John Doe', |
| 153 | + ], |
| 154 | + ], |
| 155 | + ]; |
| 156 | + |
| 157 | + MsGraph::shouldReceive('patch') |
| 158 | + ->with('me/contacts/1', $data) |
| 159 | + ->andReturn([ |
| 160 | + 'id' => '1', |
| 161 | + 'displayName' => 'John Doe', |
| 162 | + 'email' => 'johndoe@example.com', |
| 163 | + ]); |
| 164 | + |
| 165 | + $response = (new Contacts)->update(1, $data); |
| 166 | + |
| 167 | + expect($response) |
| 168 | + ->toHaveKeys(['id', 'displayName', 'email']) |
| 169 | + ->and($response['id'])->toBe('1') |
| 170 | + ->and($response['displayName'])->toBe('John Doe') |
| 171 | + ->and($response['email'])->toBe('johndoe@example.com'); |
| 172 | +}); |
| 173 | + |
| 174 | +test('can delete contact', function () { |
| 175 | + |
| 176 | + MsGraph::shouldReceive('delete') |
| 177 | + ->with('me/contacts/1') |
| 178 | + ->andReturn(''); |
| 179 | + |
| 180 | + $response = (new Contacts)->delete(1); |
| 181 | + |
| 182 | + expect($response)->toBe(''); |
| 183 | +}); |
0 commit comments