Skip to content
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
1 change: 1 addition & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
<issueHandlers>
<PropertyNotSetInConstructor errorLevel="suppress" />
<DeprecatedMethod errorLevel="suppress" />
<DeprecatedTrait errorLevel="suppress" />
<RedundantPropertyInitializationCheck errorLevel="suppress" />
<MissingOverrideAttribute errorLevel="suppress" />
<ClassMustBeFinal>
Expand Down
27 changes: 10 additions & 17 deletions src/Clients/Traits/DocumentClientTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,26 @@
namespace Sysix\LexOffice\Clients\Traits;

use Psr\Http\Message\ResponseInterface;
use Sysix\LexOffice\Clients\File;
use Sysix\LexOffice\Utils;

/**
* @deprecated use the FileClientTrait instead
*/
trait DocumentClientTrait
{
use FileClientTrait;

public function document(string $id, bool $asContent = false, string $acceptHeader = '*/*'): ResponseInterface
{
$response = $this->api
->newRequest('GET', $this->resource . '/' . rawurlencode($id) . '/document')
->getResponse();
trigger_error(__METHOD__.' should not be called anymore, in future versions this method WILL not exist', E_USER_DEPRECATED);

if ($asContent === false) {
return $response;
}

if ($response->getStatusCode() !== 200) {
return $response;
}
$response = $this->api
->newRequest('GET', $this->resource . '/' . rawurlencode($id) . '/document')
->getResponse();

$content = Utils::getJsonFromResponse($response);

if ($content === null || !is_object($content) || !property_exists($content, 'documentFileId') || !is_string($content->documentFileId)) {
return $response;
}

$fileClient = new File($this->api);

return $fileClient->get($content->documentFileId, $acceptHeader);
return $this->file($id, $acceptHeader);
}
}
19 changes: 19 additions & 0 deletions src/Clients/Traits/FileClientTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

namespace Sysix\LexOffice\Clients\Traits;

use Psr\Http\Message\ResponseInterface;

trait FileClientTrait
{
public function file(string $id, string $acceptHeader = '*/*'): ResponseInterface
{
$this->api->newRequest('GET', $this->resource . '/' . rawurlencode($id) . '/file');

return $this->api
->setRequest($this->api->getRequest()->withHeader('Accept', $acceptHeader))
->getResponse();
}
}
33 changes: 15 additions & 18 deletions tests/Clients/CreditNoteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,8 @@ public function testGetVoucherListClient(): void

public function testDocument(): void
{
$this->expectDeprecationV1Warning('document');

[$api, $stub] = $this->createClientMockObject(CreditNote::class);

$response = $stub->document('resource-id');
Expand All @@ -158,38 +160,33 @@ public function testDocument(): void

public function testDocumentContent(): void
{
[$api, $stub] = $this->createClientMultiMockObject(
CreditNote::class,
[
new Response(200, ['Content-Type' => 'application/json'], '{"documentFileId": "fake-id"}'),
new Response()
]
);
$this->expectDeprecationV1Warning('document');

[$api, $stub] = $this->createClientMockObject(CreditNote::class);

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

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

$this->assertEquals('GET', $api->getRequest()->getMethod());
$this->assertEquals(
$api->apiUrl . '/v1/files/fake-id',
$api->apiUrl . '/v1/credit-notes/resource-id/file',
$api->getRequest()->getUri()->__toString()
);
}

public function testFailedDocumentContent(): void
public function testFileContent(): void
{
[, $stub] = $this->createClientMultiMockObject(
CreditNote::class,
[
new Response(500),
new Response()
]
);
[$api, $stub] = $this->createClientMockObject(CreditNote::class);

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

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

$this->assertEquals('GET', $api->getRequest()->getMethod());
$this->assertEquals(
$api->apiUrl . '/v1/credit-notes/resource-id/file',
$api->getRequest()->getUri()->__toString()
);
}
}
33 changes: 15 additions & 18 deletions tests/Clients/DeliveryNoteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ public function testGetVoucherListClient(): void

public function testDocument(): void
{
$this->expectDeprecationV1Warning('document');

[$api, $stub] = $this->createClientMockObject(DeliveryNote::class);

$response = $stub->document('resource-id');
Expand All @@ -124,38 +126,33 @@ public function testDocument(): void

public function testDocumentContent(): void
{
[$api, $stub] = $this->createClientMultiMockObject(
DeliveryNote::class,
[
new Response(200, ['Content-Type' => 'application/json'], '{"documentFileId": "fake-id"}'),
new Response()
]
);
$this->expectDeprecationV1Warning('document');

[$api, $stub] = $this->createClientMockObject(DeliveryNote::class);

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

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

$this->assertEquals('GET', $api->getRequest()->getMethod());
$this->assertEquals(
$api->apiUrl . '/v1/files/fake-id',
$api->apiUrl . '/v1/delivery-notes/resource-id/file',
$api->getRequest()->getUri()->__toString()
);
}

public function testFailedDocumentContent(): void
public function testFileContent(): void
{
[, $stub] = $this->createClientMultiMockObject(
DeliveryNote::class,
[
new Response(500),
new Response()
]
);
[$api, $stub] = $this->createClientMockObject(DeliveryNote::class);

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

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

$this->assertEquals('GET', $api->getRequest()->getMethod());
$this->assertEquals(
$api->apiUrl . '/v1/delivery-notes/resource-id/file',
$api->getRequest()->getUri()->__toString()
);
}
}
33 changes: 15 additions & 18 deletions tests/Clients/DownPaymentInvoiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ public function testGetVoucherListClient(): void

public function testDocument(): void
{
$this->expectDeprecationV1Warning('document');

[$api, $stub] = $this->createClientMockObject(DownPaymentInvoice::class);

$response = $stub->document('resource-id');
Expand All @@ -73,38 +75,33 @@ public function testDocument(): void

public function testDocumentContent(): void
{
[$api, $stub] = $this->createClientMultiMockObject(
DownPaymentInvoice::class,
[
new Response(200, ['Content-Type' => 'application/json'], '{"documentFileId": "fake-id"}'),
new Response()
]
);
$this->expectDeprecationV1Warning('document');

[$api, $stub] = $this->createClientMockObject(DownPaymentInvoice::class);

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

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

$this->assertEquals('GET', $api->getRequest()->getMethod());
$this->assertEquals(
$api->apiUrl . '/v1/files/fake-id',
$api->apiUrl . '/v1/down-payment-invoices/resource-id/file',
$api->getRequest()->getUri()->__toString()
);
}

public function testFailedDocumentContent(): void
public function testFileContent(): void
{
[, $stub] = $this->createClientMultiMockObject(
DownPaymentInvoice::class,
[
new Response(500),
new Response()
]
);
[$api, $stub] = $this->createClientMockObject(DownPaymentInvoice::class);

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

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

$this->assertEquals('GET', $api->getRequest()->getMethod());
$this->assertEquals(
$api->apiUrl . '/v1/down-payment-invoices/resource-id/file',
$api->getRequest()->getUri()->__toString()
);
}
}
34 changes: 15 additions & 19 deletions tests/Clients/DunningTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Sysix\LexOffice\Tests\Clients;

use GuzzleHttp\Psr7\Response;
use Psr\Http\Message\ResponseInterface;
use Sysix\LexOffice\Clients\Dunning;
use Sysix\LexOffice\Tests\TestClient;
Expand Down Expand Up @@ -45,6 +44,8 @@ public function testPursue(): void

public function testDocument(): void
{
$this->expectDeprecationV1Warning('document');

[$api, $stub] = $this->createClientMockObject(Dunning::class);

$response = $stub->document('resource-id');
Expand All @@ -60,38 +61,33 @@ public function testDocument(): void

public function testDocumentContent(): void
{
[$api, $stub] = $this->createClientMultiMockObject(
Dunning::class,
[
new Response(200, ['Content-Type' => 'application/json'], '{"documentFileId": "fake-id"}'),
new Response()
]
);
$this->expectDeprecationV1Warning('document');

[$api, $stub] = $this->createClientMockObject(Dunning::class);

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

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

$this->assertEquals('GET', $api->getRequest()->getMethod());
$this->assertEquals(
$api->apiUrl . '/v1/files/fake-id',
$api->apiUrl . '/v1/dunnings/resource-id/file',
$api->getRequest()->getUri()->__toString()
);
}

public function testFailedDocumentContent(): void
public function testFileContent(): void
{
[, $stub] = $this->createClientMultiMockObject(
Dunning::class,
[
new Response(500),
new Response()
]
);
[$api, $stub] = $this->createClientMockObject(Dunning::class);

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

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

$this->assertEquals('GET', $api->getRequest()->getMethod());
$this->assertEquals(
$api->apiUrl . '/v1/dunnings/resource-id/file',
$api->getRequest()->getUri()->__toString()
);
}
}
33 changes: 15 additions & 18 deletions tests/Clients/InvoiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,8 @@ public function testGetVoucherListClient(): void

public function testDocument(): void
{
$this->expectDeprecationV1Warning('document');

[$api, $stub] = $this->createClientMockObject(Invoice::class);

$response = $stub->document('resource-id');
Expand All @@ -158,38 +160,33 @@ public function testDocument(): void

public function testDocumentContent(): void
{
[$api, $stub] = $this->createClientMultiMockObject(
Invoice::class,
[
new Response(200, ['Content-Type' => 'application/json'], '{"documentFileId": "fake-id"}'),
new Response()
]
);
$this->expectDeprecationV1Warning('document');

[$api, $stub] = $this->createClientMockObject(Invoice::class);

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

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

$this->assertEquals('GET', $api->getRequest()->getMethod());
$this->assertEquals(
$api->apiUrl . '/v1/files/fake-id',
$api->apiUrl . '/v1/invoices/resource-id/file',
$api->getRequest()->getUri()->__toString()
);
}

public function testFailedDocumentContent(): void
public function testFileContent(): void
{
[, $stub] = $this->createClientMultiMockObject(
Invoice::class,
[
new Response(500),
new Response()
]
);
[$api, $stub] = $this->createClientMockObject(Invoice::class);

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

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

$this->assertEquals('GET', $api->getRequest()->getMethod());
$this->assertEquals(
$api->apiUrl . '/v1/invoices/resource-id/file',
$api->getRequest()->getUri()->__toString()
);
}
}
Loading