From f5ca9f3fa45a887eaa24fa66ae7aaeb2ca96b441 Mon Sep 17 00:00:00 2001 From: Sysix Date: Wed, 24 Jul 2024 23:10:09 +0200 Subject: [PATCH] add articles endpoint --- README.md | 26 +++++++++ src/Api.php | 6 ++ src/Clients/Article.php | 37 ++++++++++++ tests/ApiTest.php | 2 + tests/Clients/ArticleTest.php | 105 ++++++++++++++++++++++++++++++++++ 5 files changed, 176 insertions(+) create mode 100644 src/Clients/Article.php create mode 100644 tests/Clients/ArticleTest.php diff --git a/README.md b/README.md index 686546f..6ffbf2b 100644 --- a/README.md +++ b/README.md @@ -55,10 +55,36 @@ $api = new \Sysix\LexOffice\Api($apiKey, $httpClient); ## Endpoints + ### Contact Endpoint ```php /** @var \Sysix\LexOffice\Api $api */ +$client = $api->article(); + +// filters +$client->size = 100; +$client->sortDirection = 'DESC'; + +$client->articleNumber = 'LXW-BUHA-2024-001'; +$client->gtin = '9783648170632'; +$client->type = 'PRODUCT'; + + +// get a page +$response = $client->getPage(0); + +// other methods +$response = $client->get($entityId); +$response = $client->create($data); +$response = $client->update($entityId, $data); +$response = $client->delete($entityId); + +``` + +### Contact Endpoint +```php + $client = $api->contact(); // filters diff --git a/src/Api.php b/src/Api.php index 677f884..7734a09 100644 --- a/src/Api.php +++ b/src/Api.php @@ -11,6 +11,7 @@ use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\UriInterface; use SensitiveParameter; +use Sysix\LexOffice\Clients\Article; use Sysix\LexOffice\Clients\Contact; use Sysix\LexOffice\Clients\Country; use Sysix\LexOffice\Clients\CreditNote; @@ -87,6 +88,11 @@ protected function createApiUri(string $resource): UriInterface return new Uri($this->apiUrl . '/' . $this->apiVersion . '/' . $resource); } + public function article(): Article + { + return new Article($this); + } + public function contact(): Contact { return new Contact($this); diff --git a/src/Clients/Article.php b/src/Clients/Article.php new file mode 100644 index 0000000..34a3f35 --- /dev/null +++ b/src/Clients/Article.php @@ -0,0 +1,37 @@ +articleNumber; + $params['gtin'] = $this->gtin; + $params['type'] = $this->type; + + return parent::buildQueryParams($params); + } +} diff --git a/tests/ApiTest.php b/tests/ApiTest.php index e8ce60c..f58d6fd 100644 --- a/tests/ApiTest.php +++ b/tests/ApiTest.php @@ -6,6 +6,7 @@ use GuzzleHttp\Psr7\Response; use Psr\Http\Message\ResponseInterface; +use Sysix\LexOffice\Clients\Article; use Sysix\LexOffice\Clients\Contact; use Sysix\LexOffice\Clients\Country; use Sysix\LexOffice\Clients\CreditNote; @@ -40,6 +41,7 @@ public function testClients(): void { $stub = $this->createApiMockObject(new Response()); + $this->assertInstanceOf(Article::class, $stub->article()); $this->assertInstanceOf(Contact::class, $stub->contact()); $this->assertInstanceOf(Country::class, $stub->country()); $this->assertInstanceOf(CreditNote::class, $stub->creditNote()); diff --git a/tests/Clients/ArticleTest.php b/tests/Clients/ArticleTest.php new file mode 100644 index 0000000..1b8e7ec --- /dev/null +++ b/tests/Clients/ArticleTest.php @@ -0,0 +1,105 @@ +createClientMockObject(Article::class); + + $response = $client->getPage(0); + + $this->assertInstanceOf(ResponseInterface::class, $response); + + $this->assertEquals('GET', $api->getRequest()->getMethod()); + $this->assertEquals( + $api->apiUrl . '/v1/articles?page=0&size=100', + $api->getRequest()->getUri()->__toString() + ); + } + + public function testGetPageWithFilters(): void + { + [$api, $client] = $this->createClientMockObject(Article::class); + + $client->articleNumber = 'LXW-BUHA-2024-001'; + $client->gtin = '9783648170632'; + $client->type = 'PRODUCT'; + + $client->getPage(0); + + $this->assertEquals( + $api->apiUrl . '/v1/articles?page=0&articleNumber=LXW-BUHA-2024-001>in=9783648170632&type=PRODUCT&size=100', + $api->getRequest()->getUri()->__toString() + ); + } + + public function testCreate(): void + { + [$api, $client] = $this->createClientMockObject(Article::class); + + $response = $client->create([ + 'title' => 'test' + ]); + + $this->assertInstanceOf(ResponseInterface::class, $response); + + $this->assertEquals('POST', $api->getRequest()->getMethod()); + $this->assertEquals( + $api->apiUrl . '/v1/articles', + $api->getRequest()->getUri()->__toString() + ); + } + + public function testGet(): void + { + [$api, $client] = $this->createClientMockObject(Article::class); + + $response = $client->get('resource-id'); + + $this->assertInstanceOf(ResponseInterface::class, $response); + + $this->assertEquals('GET', $api->getRequest()->getMethod()); + $this->assertEquals( + $api->apiUrl . '/v1/articles/resource-id', + $api->getRequest()->getUri()->__toString() + ); + } + + public function testUpdate(): void + { + [$api, $client] = $this->createClientMockObject(Article::class); + + $response = $client->update('resource-id', []); + + $this->assertInstanceOf(ResponseInterface::class, $response); + + $this->assertEquals('PUT', $api->getRequest()->getMethod()); + $this->assertEquals( + $api->apiUrl . '/v1/articles/resource-id', + $api->getRequest()->getUri()->__toString() + ); + } + + public function testDelete(): void + { + [$api, $client] = $this->createClientMockObject(Article::class); + + $response = $client->delete('resource-id'); + + $this->assertInstanceOf(ResponseInterface::class, $response); + + $this->assertEquals('DELETE', $api->getRequest()->getMethod()); + $this->assertEquals( + $api->apiUrl . '/v1/articles/resource-id', + $api->getRequest()->getUri()->__toString() + ); + } +}