Skip to content

Commit

Permalink
rewrite tests, check for requests
Browse files Browse the repository at this point in the history
  • Loading branch information
Sysix committed Dec 6, 2023
1 parent 76379e2 commit 216b627
Show file tree
Hide file tree
Showing 23 changed files with 435 additions and 290 deletions.
2 changes: 1 addition & 1 deletion src/BaseClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ protected function createStream(mixed $content): StreamInterface
}

/**
* @param string[]|bool[]|resource[] $content
* @param array<string, string|bool|resource> $content
*/
protected function createMultipartStream(array $content, string $boundary = null): MultipartStream
{
Expand Down
4 changes: 0 additions & 4 deletions src/ClientInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,5 @@

interface ClientInterface
{
/**
* ClientInterface constructor.
* @param Api $lexOffice
*/
public function __construct(Api $lexOffice);
}
5 changes: 2 additions & 3 deletions src/Clients/VoucherList.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Sysix\LexOffice\PaginationClient;
use DateTimeInterface;
use Psr\Http\Message\ResponseInterface;

class VoucherList extends PaginationClient
{
Expand All @@ -13,10 +12,10 @@ class VoucherList extends PaginationClient
public string $sortColumn = 'voucherNumber';
public string $sortDirection = 'DESC';

/** @var string[] */
/** @var string[] $types */
public array $types = [];

/** @var string[] */
/** @var string[] $statuses */
public array $statuses = [];

public ?bool $archived = null;
Expand Down
11 changes: 4 additions & 7 deletions src/PaginationClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ abstract class PaginationClient extends BaseClient
{
public int $size = 100;

public function generateUrl(int $page): string
protected function generatePageUrl(int $page): string
{
return $this->resource . '?' . $this->buildQueryParams([
'page'=> $page
Expand All @@ -28,12 +28,9 @@ protected function buildQueryParams(array $params): string

public function getPage(int $page): ResponseInterface
{
$api = $this->api->newRequest(
'GET',
$this->generateUrl($page)
);

return $api->getResponse();
return $this->api
->newRequest('GET', $this->generatePageUrl($page))
->getResponse();
}

/**
Expand Down
65 changes: 38 additions & 27 deletions tests/Clients/ContactTest.php
Original file line number Diff line number Diff line change
@@ -1,61 +1,72 @@
<?php
<?php declare(strict_types=1);

namespace Sysix\LexOffice\Tests\Clients;

use Psr\Http\Message\ResponseInterface;
use Sysix\LexOffice\Clients\Contact;
use GuzzleHttp\Psr7\Response;
use Sysix\LexOffice\Tests\TestClient;

class ContactTest extends TestClient
{
public function testGenerateUrl(): void
public function testGetPage(): void
{
$stub = $this->createClientMockObject(
Contact::class,
new Response(200, [], 'body')
);
[$api, $client] = $this->createClientMockObject(Contact::class);

$response = $client->getPage(0);

$this->assertInstanceOf(ResponseInterface::class, $response);

$this->assertEquals('GET', $api->request->getMethod());
$this->assertEquals(
'contacts?page=0&direction=ASC&property=name&size=100',
$stub->generateUrl(0)
$api->apiUrl . '/v1/contacts?page=0&direction=ASC&property=name&size=100',
$api->request->getUri()->__toString()
);
}

public function testCreate(): void
{
$stub = $this->createClientMockObject(
Contact::class,
new Response(200, [], 'body')
);
[$api, $client] = $this->createClientMockObject(Contact::class);

$response = $stub->create([
$response = $client->create([
'version' => 0
]);

$this->assertEquals('body', $response->getBody()->__toString());
$this->assertInstanceOf(ResponseInterface::class, $response);

$this->assertEquals('POST', $api->request->getMethod());
$this->assertEquals(
$api->apiUrl . '/v1/contacts',
$api->request->getUri()->__toString()
);
}

public function testGet(): void
{
$stub = $this->createClientMockObject(
Contact::class,
new Response(200, [], 'body')
);
[$api, $client] = $this->createClientMockObject(Contact::class);

$response = $stub->get('resource-id');
$response = $client->get('resource-id');

$this->assertEquals('body', $response->getBody()->__toString());
$this->assertInstanceOf(ResponseInterface::class, $response);

$this->assertEquals('GET', $api->request->getMethod());
$this->assertEquals(
$api->apiUrl . '/v1/contacts/resource-id',
$api->request->getUri()->__toString()
);
}

public function testUpdate(): void
{
$stub = $this->createClientMockObject(
Contact::class,
new Response(200, [], 'body')
);
[$api, $client] = $this->createClientMockObject(Contact::class);

$response = $stub->update('resource-id', []);
$response = $client->update('resource-id', []);

$this->assertEquals('body', $response->getBody()->__toString());
$this->assertInstanceOf(ResponseInterface::class, $response);

$this->assertEquals('PUT', $api->request->getMethod());
$this->assertEquals(
$api->apiUrl . '/v1/contacts/resource-id',
$api->request->getUri()->__toString()
);
}
}
17 changes: 10 additions & 7 deletions tests/Clients/CountryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,24 @@

namespace Sysix\LexOffice\Tests\Clients;

use Psr\Http\Message\ResponseInterface;
use Sysix\LexOffice\Clients\Country;
use GuzzleHttp\Psr7\Response;
use Sysix\LexOffice\Tests\TestClient;

class CountryTest extends TestClient
{
public function testGetAll(): void
{
$stub = $this->createClientMockObject(
Country::class,
new Response(200, [], '{"content": [], "totalPages": 1}')
);
[$api, $client] = $this->createClientMockObject(Country::class);

$response = $client->getAll();

$response = $stub->getAll();
$this->assertInstanceOf(ResponseInterface::class, $response);

$this->assertEquals('{"content": [], "totalPages": 1}', $response->getBody()->__toString());
$this->assertEquals('GET', $api->request->getMethod());
$this->assertEquals(
$api->apiUrl . '/v1/countries',
$api->request->getUri()->__toString()
);
}
}
56 changes: 36 additions & 20 deletions tests/Clients/CreditNoteTest.php
Original file line number Diff line number Diff line change
@@ -1,56 +1,69 @@
<?php
<?php declare(strict_types=1);

namespace Sysix\LexOffice\Tests\Clients;

use Sysix\LexOffice\Clients\CreditNote;
use GuzzleHttp\Psr7\Response;
use Psr\Http\Message\ResponseInterface;
use Sysix\LexOffice\Clients\CreditNote;
use Sysix\LexOffice\Tests\TestClient;

class CreditNoteTest extends TestClient
{
public function testCreate(): void
{
$stub = $this->createClientMockObject(
CreditNote::class,
new Response(200, [], 'body')
);
[$api, $stub] = $this->createClientMockObject(CreditNote::class);

$response = $stub->create([
'version' => 0
]);

$this->assertEquals('body', $response->getBody()->__toString());
$this->assertInstanceOf(ResponseInterface::class, $response);

$this->assertEquals('POST', $api->request->getMethod());
$this->assertEquals(
$api->apiUrl . '/v1/credit-notes',
$api->request->getUri()->__toString()
);
}

public function testGetAll(): void
{
$this->expectDeprecationV1Warning('getAll');

$stub = $this->createClientMockObject(
[$api, $stub] = $this->createClientMultiMockObject(
CreditNote::class,
new Response(200, [], '{"content": [], "totalPages": 1}')
[new Response(200, [], '{"content": [], "totalPages": 1}')]
);

$response = $stub->getAll();

$this->assertEquals('{"content": [], "totalPages": 1}', $response->getBody()->__toString());
$this->assertInstanceOf(ResponseInterface::class, $response);

$this->assertEquals('GET', $api->request->getMethod());
$this->assertEquals(
$api->apiUrl . '/v1/voucherlist?page=0&sort=voucherNumber%2CDESC&voucherType=creditnote&voucherStatus=draft%2Copen%2Cpaid%2Cpaidoff%2Cvoided%2Caccepted%2Crejected&size=100',
$api->request->getUri()->__toString()
);
}

public function testDocument(): void
{
$stub = $this->createClientMockObject(
CreditNote::class,
new Response(200, [], '{"documentFileId": "fake-id"}')
);
[$api, $stub] = $this->createClientMockObject(CreditNote::class);

$response = $stub->document('resource-id');

$this->assertInstanceOf(ResponseInterface::class, $response);

$this->assertEquals('GET', $api->request->getMethod());
$this->assertEquals(
'{"documentFileId": "fake-id"}',
$response->getBody()->__toString()
);
$api->apiUrl . '/v1/credit-notes/resource-id/document',
$api->request->getUri()->__toString()
);
}

$stub = $this->createClientMultiMockObject(
public function testDocumentContent(): void
{
[$api, $stub] = $this->createClientMultiMockObject(
CreditNote::class,
[
new Response(200, [], '{"documentFileId": "fake-id"}'),
Expand All @@ -60,9 +73,12 @@ public function testDocument(): void

$response = $stub->document('resource-id', true);

$this->assertInstanceOf(ResponseInterface::class, $response);

$this->assertEquals('GET', $api->request->getMethod());
$this->assertEquals(
'{}',
$response->getBody()->__toString()
$api->apiUrl . '/v1/files/fake-id',
$api->request->getUri()->__toString()
);
}
}
41 changes: 27 additions & 14 deletions tests/Clients/DownPaymentInvoiceTest.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<?php
<?php declare(strict_types=1);

namespace Sysix\LexOffice\Tests\Clients;

use Psr\Http\Message\ResponseInterface;
use Sysix\LexOffice\Clients\DownPaymentInvoice;
use GuzzleHttp\Psr7\Response;
use Sysix\LexOffice\Tests\TestClient;
Expand All @@ -12,31 +13,40 @@ public function testGetAll(): void
{
$this->expectDeprecationV1Warning('getAll');

$stub = $this->createClientMockObject(
[$api, $stub] = $this->createClientMultiMockObject(
DownPaymentInvoice::class,
new Response(200, [], '{"content": [], "totalPages": 1}')
[new Response(200, [], '{"content": [], "totalPages": 1}')]
);

$response = $stub->getAll();

$this->assertEquals('{"content": [], "totalPages": 1}', $response->getBody()->__toString());
$this->assertInstanceOf(ResponseInterface::class, $response);

$this->assertEquals('GET', $api->request->getMethod());
$this->assertEquals(
$api->apiUrl . '/v1/voucherlist?page=0&sort=voucherNumber%2CDESC&voucherType=downpaymentinvoice&voucherStatus=draft%2Copen%2Cpaid%2Cpaidoff%2Cvoided%2Caccepted%2Crejected&size=100',
$api->request->getUri()->__toString()
);
}

public function testDocument(): void
{
$stub = $this->createClientMockObject(
DownPaymentInvoice::class,
new Response(200, [], '{"documentFileId": "fake-id"}')
);
[$api, $stub] = $this->createClientMockObject(DownPaymentInvoice::class);

$response = $stub->document('resource-id');

$this->assertInstanceOf(ResponseInterface::class, $response);

$this->assertEquals('GET', $api->request->getMethod());
$this->assertEquals(
'{"documentFileId": "fake-id"}',
$response->getBody()->__toString()
);
$api->apiUrl . '/v1/down-payment-invoices/resource-id/document',
$api->request->getUri()->__toString()
);
}

$stub = $this->createClientMultiMockObject(
public function testDocumentContent(): void
{
[$api, $stub] = $this->createClientMultiMockObject(
DownPaymentInvoice::class,
[
new Response(200, [], '{"documentFileId": "fake-id"}'),
Expand All @@ -46,9 +56,12 @@ public function testDocument(): void

$response = $stub->document('resource-id', true);

$this->assertInstanceOf(ResponseInterface::class, $response);

$this->assertEquals('GET', $api->request->getMethod());
$this->assertEquals(
'{}',
$response->getBody()->__toString()
$api->apiUrl . '/v1/files/fake-id',
$api->request->getUri()->__toString()
);
}
}
Loading

0 comments on commit 216b627

Please sign in to comment.