Skip to content

Commit f379ccd

Browse files
authored
Merge pull request #159 from Sysix/feature/articles
add articles endpoint
2 parents 72f44b6 + f5ca9f3 commit f379ccd

File tree

5 files changed

+176
-0
lines changed

5 files changed

+176
-0
lines changed

README.md

+26
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,36 @@ $api = new \Sysix\LexOffice\Api($apiKey, $httpClient);
5555

5656
## Endpoints
5757

58+
5859
### Contact Endpoint
5960
```php
6061

6162
/** @var \Sysix\LexOffice\Api $api */
63+
$client = $api->article();
64+
65+
// filters
66+
$client->size = 100;
67+
$client->sortDirection = 'DESC';
68+
69+
$client->articleNumber = 'LXW-BUHA-2024-001';
70+
$client->gtin = '9783648170632';
71+
$client->type = 'PRODUCT';
72+
73+
74+
// get a page
75+
$response = $client->getPage(0);
76+
77+
// other methods
78+
$response = $client->get($entityId);
79+
$response = $client->create($data);
80+
$response = $client->update($entityId, $data);
81+
$response = $client->delete($entityId);
82+
83+
```
84+
85+
### Contact Endpoint
86+
```php
87+
6288
$client = $api->contact();
6389

6490
// filters

src/Api.php

+6
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Psr\Http\Message\ResponseInterface;
1212
use Psr\Http\Message\UriInterface;
1313
use SensitiveParameter;
14+
use Sysix\LexOffice\Clients\Article;
1415
use Sysix\LexOffice\Clients\Contact;
1516
use Sysix\LexOffice\Clients\Country;
1617
use Sysix\LexOffice\Clients\CreditNote;
@@ -87,6 +88,11 @@ protected function createApiUri(string $resource): UriInterface
8788
return new Uri($this->apiUrl . '/' . $this->apiVersion . '/' . $resource);
8889
}
8990

91+
public function article(): Article
92+
{
93+
return new Article($this);
94+
}
95+
9096
public function contact(): Contact
9197
{
9298
return new Contact($this);

src/Clients/Article.php

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Sysix\LexOffice\Clients;
6+
7+
use Sysix\LexOffice\Clients\Traits\CreateTrait;
8+
use Sysix\LexOffice\Clients\Traits\DeleteTrait;
9+
use Sysix\LexOffice\Clients\Traits\GetTrait;
10+
use Sysix\LexOffice\Clients\Traits\UpdateTrait;
11+
use Sysix\LexOffice\PaginationClient;
12+
13+
class Article extends PaginationClient
14+
{
15+
use CreateTrait;
16+
use DeleteTrait;
17+
use GetTrait;
18+
use UpdateTrait;
19+
20+
protected string $resource = 'articles';
21+
22+
public ?string $articleNumber = null;
23+
24+
public ?string $gtin = null;
25+
26+
/** can be "PRODUCT" or "SERVICE" */
27+
public ?string $type = null;
28+
29+
protected function buildQueryParams(array $params): string
30+
{
31+
$params['articleNumber'] = $this->articleNumber;
32+
$params['gtin'] = $this->gtin;
33+
$params['type'] = $this->type;
34+
35+
return parent::buildQueryParams($params);
36+
}
37+
}

tests/ApiTest.php

+2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use GuzzleHttp\Psr7\Response;
88
use Psr\Http\Message\ResponseInterface;
9+
use Sysix\LexOffice\Clients\Article;
910
use Sysix\LexOffice\Clients\Contact;
1011
use Sysix\LexOffice\Clients\Country;
1112
use Sysix\LexOffice\Clients\CreditNote;
@@ -40,6 +41,7 @@ public function testClients(): void
4041
{
4142
$stub = $this->createApiMockObject(new Response());
4243

44+
$this->assertInstanceOf(Article::class, $stub->article());
4345
$this->assertInstanceOf(Contact::class, $stub->contact());
4446
$this->assertInstanceOf(Country::class, $stub->country());
4547
$this->assertInstanceOf(CreditNote::class, $stub->creditNote());

tests/Clients/ArticleTest.php

+105
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Sysix\LexOffice\Tests\Clients;
6+
7+
use Psr\Http\Message\ResponseInterface;
8+
use Sysix\LexOffice\Clients\Article;
9+
use Sysix\LexOffice\Tests\TestClient;
10+
11+
class ArticleTest extends TestClient
12+
{
13+
public function testGetPage(): void
14+
{
15+
[$api, $client] = $this->createClientMockObject(Article::class);
16+
17+
$response = $client->getPage(0);
18+
19+
$this->assertInstanceOf(ResponseInterface::class, $response);
20+
21+
$this->assertEquals('GET', $api->getRequest()->getMethod());
22+
$this->assertEquals(
23+
$api->apiUrl . '/v1/articles?page=0&size=100',
24+
$api->getRequest()->getUri()->__toString()
25+
);
26+
}
27+
28+
public function testGetPageWithFilters(): void
29+
{
30+
[$api, $client] = $this->createClientMockObject(Article::class);
31+
32+
$client->articleNumber = 'LXW-BUHA-2024-001';
33+
$client->gtin = '9783648170632';
34+
$client->type = 'PRODUCT';
35+
36+
$client->getPage(0);
37+
38+
$this->assertEquals(
39+
$api->apiUrl . '/v1/articles?page=0&articleNumber=LXW-BUHA-2024-001&gtin=9783648170632&type=PRODUCT&size=100',
40+
$api->getRequest()->getUri()->__toString()
41+
);
42+
}
43+
44+
public function testCreate(): void
45+
{
46+
[$api, $client] = $this->createClientMockObject(Article::class);
47+
48+
$response = $client->create([
49+
'title' => 'test'
50+
]);
51+
52+
$this->assertInstanceOf(ResponseInterface::class, $response);
53+
54+
$this->assertEquals('POST', $api->getRequest()->getMethod());
55+
$this->assertEquals(
56+
$api->apiUrl . '/v1/articles',
57+
$api->getRequest()->getUri()->__toString()
58+
);
59+
}
60+
61+
public function testGet(): void
62+
{
63+
[$api, $client] = $this->createClientMockObject(Article::class);
64+
65+
$response = $client->get('resource-id');
66+
67+
$this->assertInstanceOf(ResponseInterface::class, $response);
68+
69+
$this->assertEquals('GET', $api->getRequest()->getMethod());
70+
$this->assertEquals(
71+
$api->apiUrl . '/v1/articles/resource-id',
72+
$api->getRequest()->getUri()->__toString()
73+
);
74+
}
75+
76+
public function testUpdate(): void
77+
{
78+
[$api, $client] = $this->createClientMockObject(Article::class);
79+
80+
$response = $client->update('resource-id', []);
81+
82+
$this->assertInstanceOf(ResponseInterface::class, $response);
83+
84+
$this->assertEquals('PUT', $api->getRequest()->getMethod());
85+
$this->assertEquals(
86+
$api->apiUrl . '/v1/articles/resource-id',
87+
$api->getRequest()->getUri()->__toString()
88+
);
89+
}
90+
91+
public function testDelete(): void
92+
{
93+
[$api, $client] = $this->createClientMockObject(Article::class);
94+
95+
$response = $client->delete('resource-id');
96+
97+
$this->assertInstanceOf(ResponseInterface::class, $response);
98+
99+
$this->assertEquals('DELETE', $api->getRequest()->getMethod());
100+
$this->assertEquals(
101+
$api->apiUrl . '/v1/articles/resource-id',
102+
$api->getRequest()->getUri()->__toString()
103+
);
104+
}
105+
}

0 commit comments

Comments
 (0)