Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add articles endpoint #159

Merged
merged 1 commit into from
Jul 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions src/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
37 changes: 37 additions & 0 deletions src/Clients/Article.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);

namespace Sysix\LexOffice\Clients;

use Sysix\LexOffice\Clients\Traits\CreateTrait;
use Sysix\LexOffice\Clients\Traits\DeleteTrait;
use Sysix\LexOffice\Clients\Traits\GetTrait;
use Sysix\LexOffice\Clients\Traits\UpdateTrait;
use Sysix\LexOffice\PaginationClient;

class Article extends PaginationClient
{
use CreateTrait;
use DeleteTrait;
use GetTrait;
use UpdateTrait;

protected string $resource = 'articles';

public ?string $articleNumber = null;

public ?string $gtin = null;

/** can be "PRODUCT" or "SERVICE" */
public ?string $type = null;

protected function buildQueryParams(array $params): string
{
$params['articleNumber'] = $this->articleNumber;
$params['gtin'] = $this->gtin;
$params['type'] = $this->type;

return parent::buildQueryParams($params);
}
}
2 changes: 2 additions & 0 deletions tests/ApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
Expand Down
105 changes: 105 additions & 0 deletions tests/Clients/ArticleTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<?php

declare(strict_types=1);

namespace Sysix\LexOffice\Tests\Clients;

use Psr\Http\Message\ResponseInterface;
use Sysix\LexOffice\Clients\Article;
use Sysix\LexOffice\Tests\TestClient;

class ArticleTest extends TestClient
{
public function testGetPage(): void
{
[$api, $client] = $this->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&gtin=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()
);
}
}
Loading