Skip to content

Commit 8d37c72

Browse files
committed
WIP
1 parent ce23e0f commit 8d37c72

10 files changed

+259
-2
lines changed

src/Dto/Taxes/TaxDTO.php

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
3+
namespace CodebarAg\Bexio\Dto\Taxes;
4+
5+
use Exception;
6+
use Illuminate\Support\Arr;
7+
use Saloon\Http\Response;
8+
use Spatie\LaravelData\Data;
9+
10+
class TaxDTO extends Data
11+
{
12+
public function __construct(
13+
public int $id,
14+
public string $uuid,
15+
public string $name,
16+
public string $code,
17+
public string $digit,
18+
public string $type,
19+
public int $account_id,
20+
public string $tax_settlement_type,
21+
public float $value,
22+
public bool $is_active,
23+
public string $display_name,
24+
public ?int $start_year = null,
25+
public ?int $end_year = null,
26+
public mixed $net_tax_value = null,
27+
) {
28+
}
29+
30+
public static function fromResponse(Response $response): self
31+
{
32+
if ($response->failed()) {
33+
throw new \Exception('Failed to create DTO from Response');
34+
}
35+
36+
$data = $response->json();
37+
38+
return self::fromArray($data);
39+
}
40+
41+
public static function fromArray(array $data): self
42+
{
43+
if (! $data) {
44+
throw new Exception('Unable to create DTO. Data missing from response.');
45+
}
46+
47+
return new self(
48+
id: Arr::get($data, 'id'),
49+
uuid: Arr::get($data, 'uuid'),
50+
name: Arr::get($data, 'name'),
51+
code: Arr::get($data, 'code'),
52+
digit: Arr::get($data, 'digit'),
53+
type: Arr::get($data, 'type'),
54+
account_id: Arr::get($data, 'account_id'),
55+
tax_settlement_type: Arr::get($data, 'tax_settlement_type'),
56+
value: Arr::get($data, 'value'),
57+
is_active: Arr::get($data, 'is_active'),
58+
display_name: Arr::get($data, 'display_name'),
59+
start_year: Arr::get($data, 'start_year'),
60+
end_year: Arr::get($data, 'end_year'),
61+
net_tax_value: Arr::get($data, 'net_tax_value'),
62+
);
63+
}
64+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace CodebarAg\Bexio\Requests\Taxes;
4+
5+
use Exception;
6+
use Saloon\Enums\Method;
7+
use Saloon\Http\Request;
8+
use Saloon\Http\Response;
9+
10+
class DeleteATaxRequest extends Request
11+
{
12+
protected Method $method = Method::DELETE;
13+
14+
public function __construct(
15+
readonly int $id,
16+
) {
17+
}
18+
19+
public function resolveEndpoint(): string
20+
{
21+
return '/3.0/taxes/'.$this->id;
22+
}
23+
24+
public function createDtoFromResponse(Response $response): mixed
25+
{
26+
if (! $response->successful()) {
27+
throw new Exception('Request was not successful. Unable to create DTO.');
28+
}
29+
30+
return $response->json();
31+
}
32+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
3+
namespace CodebarAg\Bexio\Requests\Taxes;
4+
5+
use CodebarAg\Bexio\Dto\Taxes\TaxDTO;
6+
use Exception;
7+
use Saloon\Enums\Method;
8+
use Saloon\Http\Request;
9+
use Saloon\Http\Response;
10+
11+
class FetchAListOfTaxesRequest extends Request
12+
{
13+
protected Method $method = Method::GET;
14+
15+
public function __construct(
16+
readonly int $limit = 2000,
17+
readonly int $offset = 0,
18+
readonly ?string $scope = null,
19+
readonly ?string $date = null,
20+
readonly ?string $types = null,
21+
) {
22+
}
23+
24+
public function resolveEndpoint(): string
25+
{
26+
return '/3.0/taxes';
27+
}
28+
29+
public function defaultQuery(): array
30+
{
31+
$body = [
32+
'limit' => $this->limit,
33+
'offset' => $this->offset,
34+
];
35+
36+
if ($this->scope) {
37+
$body['scope'] = $this->scope;
38+
}
39+
40+
if ($this->date) {
41+
$body['date'] = $this->date;
42+
}
43+
44+
if ($this->types) {
45+
$body['types'] = $this->types;
46+
}
47+
48+
return $body;
49+
}
50+
51+
public function createDtoFromResponse(Response $response): mixed
52+
{
53+
if (! $response->successful()) {
54+
throw new Exception('Request was not successful. Unable to create DTO.');
55+
}
56+
57+
$res = $response->json();
58+
59+
$taxes = collect();
60+
61+
foreach ($res as $currency) {
62+
$taxes->push(TaxDTO::fromArray($currency));
63+
}
64+
65+
return $taxes;
66+
}
67+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace CodebarAg\Bexio\Requests\Taxes;
4+
5+
use CodebarAg\Bexio\Dto\Taxes\TaxDTO;
6+
use Exception;
7+
use Saloon\Enums\Method;
8+
use Saloon\Http\Request;
9+
use Saloon\Http\Response;
10+
11+
class FetchATaxRequest extends Request
12+
{
13+
protected Method $method = Method::GET;
14+
15+
public function __construct(
16+
readonly int $id,
17+
) {
18+
}
19+
20+
public function resolveEndpoint(): string
21+
{
22+
return '/3.0/taxes/'.$this->id;
23+
}
24+
25+
public function createDtoFromResponse(Response $response): mixed
26+
{
27+
if (! $response->successful()) {
28+
throw new Exception('Request was not successful. Unable to create DTO.');
29+
}
30+
31+
return TaxDTO::fromResponse($response);
32+
}
33+
}

tests/Fixtures/Saloon/Taxes/fetch-a-list-of-taxes.json

Lines changed: 1 addition & 0 deletions
Large diffs are not rendered by default.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"statusCode":200,"headers":{"Date":"Mon, 11 Dec 2023 17:03:04 GMT","Content-Type":"application\/json","Content-Length":"363","Connection":"keep-alive","ratelimit-reset":"57","x-ratelimit-remaining-minute":"999","x-ratelimit-limit-minute":"1000","ratelimit-remaining":"999","ratelimit-limit":"1000","etag":"a5f04d89c463c91d8fc844285bf2055e","Cache-Control":"no-store","pragma":"no-cache","vary":"Origin","access-control-allow-origin":"https:\/\/office.bexio.com","via":"1.1 google","alt-svc":"h3=\":443\"; ma=86400","CF-Cache-Status":"DYNAMIC","Server":"cloudflare","CF-RAY":"833f4c818cd4373e-FRA"},"data":"{\"id\":3,\"uuid\":\"b87bf4b2-db18-11e9-9d96-a4bf011ce47b\",\"name\":\"lib.model.tax.ch.sales_export.name\",\"code\":\"UEX\",\"digit\":\"220\",\"type\":\"not_taxable_turnover\",\"account_id\":127,\"tax_settlement_type\":\"none\",\"value\":0,\"net_tax_value\":null,\"start_year\":null,\"end_year\":null,\"is_active\":true,\"display_name\":\"UEX - Export\\\/Exempt 0.00%\",\"start_month\":null,\"end_month\":null}"}

tests/Requests/Currencies/FetchExchangeRatesForCurrenciesRequestTest.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,5 @@
1515

1616
$response = $connector->send(new FetchExchangeRatesForCurrenciesRequest(id: 2));
1717

18-
ray($response->dto());
19-
2018
$mockClient->assertSent(FetchExchangeRatesForCurrenciesRequest::class);
2119
});
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
use CodebarAg\Bexio\BexioConnector;
4+
use CodebarAg\Bexio\Requests\Taxes\DeleteATaxRequest;
5+
use Saloon\Http\Faking\MockResponse;
6+
use Saloon\Laravel\Http\Faking\MockClient;
7+
8+
it('can perform the request', closure: function () {
9+
$mockClient = new MockClient([
10+
DeleteATaxRequest::class => MockResponse::fixture('Taxes/delete-a-tax'),
11+
]);
12+
13+
$connector = new BexioConnector;
14+
$connector->withMockClient($mockClient);
15+
16+
$response = $connector->send(new DeleteATaxRequest(id: null));
17+
18+
$mockClient->assertSent(DeleteATaxRequest::class);
19+
})->skip('WAITING FOR SETUP IN DEV ENVIRONMENT')->todo();
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
use CodebarAg\Bexio\BexioConnector;
4+
use CodebarAg\Bexio\Requests\Taxes\FetchAListOfTaxesRequest;
5+
use Illuminate\Support\Collection;
6+
use Saloon\Http\Faking\MockResponse;
7+
use Saloon\Laravel\Http\Faking\MockClient;
8+
9+
it('can perform the request', closure: function () {
10+
$mockClient = new MockClient([
11+
FetchAListOfTaxesRequest::class => MockResponse::fixture('Taxes/fetch-a-list-of-taxes'),
12+
]);
13+
14+
$connector = new BexioConnector;
15+
$connector->withMockClient($mockClient);
16+
17+
$response = $connector->send(new FetchAListOfTaxesRequest());
18+
19+
$mockClient->assertSent(FetchAListOfTaxesRequest::class);
20+
21+
expect($response->dto())->toBeInstanceOf(Collection::class)
22+
->and($response->dto()->count())->toBe(40);
23+
});
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
use CodebarAg\Bexio\BexioConnector;
4+
use CodebarAg\Bexio\Requests\Taxes\FetchATaxRequest;
5+
use Saloon\Http\Faking\MockResponse;
6+
use Saloon\Laravel\Http\Faking\MockClient;
7+
8+
it('can perform the request', closure: function () {
9+
$mockClient = new MockClient([
10+
FetchATaxRequest::class => MockResponse::fixture('Taxes/fetch-a-tax'),
11+
]);
12+
13+
$connector = new BexioConnector;
14+
$connector->withMockClient($mockClient);
15+
16+
$response = $connector->send(new FetchATaxRequest(id: 3));
17+
18+
$mockClient->assertSent(FetchATaxRequest::class);
19+
});

0 commit comments

Comments
 (0)