diff --git a/src/NieuwbouwOffice.php b/src/NieuwbouwOffice.php index 4e1c134..8c9afd4 100755 --- a/src/NieuwbouwOffice.php +++ b/src/NieuwbouwOffice.php @@ -2,6 +2,7 @@ namespace NieuwbouwOffice\PhpSdk; +use NieuwbouwOffice\PhpSdk\Resources\ProjectResource; use Saloon\Http\Auth\TokenAuthenticator; use Saloon\Http\Connector; use Saloon\Traits\Plugins\AcceptsJson; @@ -26,4 +27,9 @@ protected function defaultAuth(): TokenAuthenticator { return new TokenAuthenticator($this->token, 'apikey'); } + + public function projects(): ProjectResource + { + return new ProjectResource($this); + } } diff --git a/src/Requests/Projects/GetProjectRequest.php b/src/Requests/Projects/GetProjectRequest.php new file mode 100644 index 0000000..e107b81 --- /dev/null +++ b/src/Requests/Projects/GetProjectRequest.php @@ -0,0 +1,18 @@ +uuid}/"; + } +} diff --git a/src/Requests/Projects/GetProjectsRequest.php b/src/Requests/Projects/GetProjectsRequest.php new file mode 100644 index 0000000..34ffecb --- /dev/null +++ b/src/Requests/Projects/GetProjectsRequest.php @@ -0,0 +1,16 @@ +connector->send(new GetProjectsRequest); + } + + public function get(string $uuid): Response + { + return $this->connector->send(new GetProjectRequest($uuid)); + } +} diff --git a/tests/NieuwbouwOfficeTest.php b/tests/NieuwbouwOfficeTest.php index 5db9645..6553d82 100644 --- a/tests/NieuwbouwOfficeTest.php +++ b/tests/NieuwbouwOfficeTest.php @@ -1,6 +1,7 @@ headers()->get('Accept'))->toBe('application/json'); }); + +it('exposes the projects resource', function () { + $connector = new NieuwbouwOffice('test-token'); + + expect($connector->projects())->toBeInstanceOf(ProjectResource::class); +}); diff --git a/tests/Requests/Projects/GetProjectRequestTest.php b/tests/Requests/Projects/GetProjectRequestTest.php new file mode 100644 index 0000000..e4c0e55 --- /dev/null +++ b/tests/Requests/Projects/GetProjectRequestTest.php @@ -0,0 +1,17 @@ +getMethod())->toBe(Method::GET); +}); + +it('stores the uuid on a public property', function () { + expect((new GetProjectRequest('abc-123'))->uuid)->toBe('abc-123'); +}); + +it('resolves to the /projects/{uuid}/ endpoint', function () { + expect((new GetProjectRequest('abc-123'))->resolveEndpoint()) + ->toBe('/projects/abc-123/'); +}); diff --git a/tests/Requests/Projects/GetProjectsRequestTest.php b/tests/Requests/Projects/GetProjectsRequestTest.php new file mode 100644 index 0000000..5189c59 --- /dev/null +++ b/tests/Requests/Projects/GetProjectsRequestTest.php @@ -0,0 +1,12 @@ +getMethod())->toBe(Method::GET); +}); + +it('resolves to the /projects/ endpoint', function () { + expect((new GetProjectsRequest)->resolveEndpoint())->toBe('/projects/'); +}); diff --git a/tests/Resources/ProjectResourceTest.php b/tests/Resources/ProjectResourceTest.php new file mode 100644 index 0000000..af6713f --- /dev/null +++ b/tests/Resources/ProjectResourceTest.php @@ -0,0 +1,67 @@ + MockResponse::make([ + 'data' => [ + ['uuid' => 'a', 'name' => 'Project A'], + ['uuid' => 'b', 'name' => 'Project B'], + ], + ]), + ]); + + $connector = new NieuwbouwOffice('test-token'); + $connector->withMockClient($mockClient); + + $response = $connector->projects()->list(); + + expect($response)->toBeInstanceOf(Response::class) + ->and($response->json())->toBe([ + 'data' => [ + ['uuid' => 'a', 'name' => 'Project A'], + ['uuid' => 'b', 'name' => 'Project B'], + ], + ]); + + $mockClient->assertSent(GetProjectsRequest::class); +}); + +it('get() sends a GetProjectRequest with the given uuid and returns the response', function () { + $mockClient = new MockClient([ + GetProjectRequest::class => MockResponse::make([ + 'uuid' => 'abc-123', + 'name' => 'Project ABC', + ]), + ]); + + $connector = new NieuwbouwOffice('test-token'); + $connector->withMockClient($mockClient); + + $response = $connector->projects()->get('abc-123'); + + expect($response)->toBeInstanceOf(Response::class) + ->and($response->json())->toBe([ + 'uuid' => 'abc-123', + 'name' => 'Project ABC', + ]); + + $mockClient->assertSent(function ($request) { + return $request instanceof GetProjectRequest + && $request->uuid === 'abc-123' + && $request->resolveEndpoint() === '/projects/abc-123/'; + }); +}); + +it('can be instantiated directly with a connector', function () { + $connector = new NieuwbouwOffice('test-token'); + + expect(new ProjectResource($connector))->toBeInstanceOf(ProjectResource::class); +});