Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
RhysLees committed Sep 30, 2024
1 parent 9182e9f commit 49f753a
Show file tree
Hide file tree
Showing 17 changed files with 244 additions and 57 deletions.
2 changes: 1 addition & 1 deletion .phpunit.cache/test-results

Large diffs are not rendered by default.

39 changes: 39 additions & 0 deletions src/Dto/PaymentTypes/PaymentTypeDTO.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace CodebarAg\Bexio\Dto\PaymentTypes;

use Exception;
use Illuminate\Support\Arr;
use Saloon\Http\Response;
use Spatie\LaravelData\Data;

class PaymentTypeDTO extends Data
{
public function __construct(
public int $id,
public string $name,
) {}

public static function fromResponse(Response $response): self
{
if ($response->failed()) {
throw new \Exception('Failed to create DTO from Response');
}

$data = $response->json();

return self::fromArray($data);
}

public static function fromArray(array $data): self
{
if (! $data) {
throw new Exception('Unable to create DTO. Data missing from response.');
}

return new self(
id: Arr::get($data, 'id'),
name: Arr::get($data, 'name'),
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace CodebarAg\Bexio\Requests\BusinessActivities;

use CodebarAg\Bexio\Dto\BusinessActivities\LanguageDTO;
use CodebarAg\Bexio\Dto\BusinessActivities\BusinessActivityDTO;
use Exception;
use Saloon\Enums\Method;
use Saloon\Http\Request;
Expand Down Expand Up @@ -43,7 +43,7 @@ public function createDtoFromResponse(Response $response): mixed
$businessActivities = collect();

foreach ($res as $businessActivity) {
$businessActivities->push(LanguageDTO::fromArray($businessActivity));
$businessActivities->push(BusinessActivityDTO::fromArray($businessActivity));
}

return $businessActivities;
Expand Down
51 changes: 51 additions & 0 deletions src/Requests/PaymentTypes/FetchAListOfPaymentTypesRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace CodebarAg\Bexio\Requests\PaymentTypes;

use CodebarAg\Bexio\Dto\PaymentTypes\PaymentTypeDTO;
use Exception;
use Saloon\Enums\Method;
use Saloon\Http\Request;
use Saloon\Http\Response;

class FetchAListOfPaymentTypesRequest extends Request
{
protected Method $method = Method::GET;

public function __construct(
readonly string $orderBy = 'id',
readonly int $limit = 500,
readonly int $offset = 0,
) {}

public function resolveEndpoint(): string
{
return '/2.0/payment_type';
}

public function defaultQuery(): array
{
return [
'order_by' => $this->orderBy,
'limit' => $this->limit,
'offset' => $this->offset,
];
}

public function createDtoFromResponse(Response $response): mixed
{
if (! $response->successful()) {
throw new Exception('Request was not successful. Unable to create DTO.');
}

$res = $response->json();

$paymentTypes = collect();

foreach ($res as $paymentType) {
$paymentTypes->push(PaymentTypeDTO::fromArray($paymentType));
}

return $paymentTypes;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"statusCode": 200,
"headers": {
"Date": "Mon, 30 Sep 2024 13:36:38 GMT",
"Content-Type": "application\/json",
"Content-Length": "528",
"Connection": "keep-alive",
"ratelimit-reset": "22",
"x-ratelimit-remaining-minute": "999",
"x-ratelimit-limit-minute": "1000",
"ratelimit-remaining": "999",
"ratelimit-limit": "1000",
"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": "8cb49a5e3bc7cd24-LHR"
},
"data": "[{\"id\":1,\"name\":\"Administration\",\"default_is_billable\":true,\"default_price_per_hour\":null,\"account_id\":null},{\"id\":2,\"name\":\"Meeting\",\"default_is_billable\":true,\"default_price_per_hour\":null,\"account_id\":null},{\"id\":3,\"name\":\"Projekt Management\",\"default_is_billable\":true,\"default_price_per_hour\":null,\"account_id\":null},{\"id\":4,\"name\":\"Umsetzung\",\"default_is_billable\":true,\"default_price_per_hour\":null,\"account_id\":null},{\"id\":5,\"name\":\"Allgemein\",\"default_is_billable\":true,\"default_price_per_hour\":null,\"account_id\":null}]"
}
24 changes: 24 additions & 0 deletions tests/Fixtures/Saloon/Languages/fetch-a-list-of-languages.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"statusCode": 200,
"headers": {
"Date": "Mon, 30 Sep 2024 13:33:50 GMT",
"Content-Type": "application\/json",
"Content-Length": "1067",
"Connection": "keep-alive",
"ratelimit-limit": "1000",
"ratelimit-remaining": "999",
"x-ratelimit-remaining-minute": "999",
"x-ratelimit-limit-minute": "1000",
"ratelimit-reset": "10",
"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": "8cb496447db660e9-LHR"
},
"data": "[{\"id\":1,\"name\":\"Deutsch\",\"decimal_point\":\".\",\"thousands_separator\":\"'\",\"date_format_id\":1,\"date_format\":\"d.m.Y\",\"iso_639_1\":\"de\"},{\"id\":2,\"name\":\"Franz\\u00f6sisch\",\"decimal_point\":\".\",\"thousands_separator\":\"'\",\"date_format_id\":1,\"date_format\":\"d.m.Y\",\"iso_639_1\":\"fr\"},{\"id\":3,\"name\":\"Italienisch\",\"decimal_point\":\".\",\"thousands_separator\":\"'\",\"date_format_id\":1,\"date_format\":\"d.m.Y\",\"iso_639_1\":\"it\"},{\"id\":4,\"name\":\"Englisch\",\"decimal_point\":\".\",\"thousands_separator\":\"'\",\"date_format_id\":1,\"date_format\":\"d.m.Y\",\"iso_639_1\":\"en\"},{\"id\":5,\"name\":\"Portugiesisch\",\"decimal_point\":\".\",\"thousands_separator\":\"'\",\"date_format_id\":1,\"date_format\":\"d.m.Y\",\"iso_639_1\":\"pt\"},{\"id\":6,\"name\":\"Chinesisch\",\"decimal_point\":\".\",\"thousands_separator\":\"'\",\"date_format_id\":1,\"date_format\":\"d.m.Y\",\"iso_639_1\":\"zh\"},{\"id\":7,\"name\":\"Japanisch\",\"decimal_point\":\".\",\"thousands_separator\":\"'\",\"date_format_id\":1,\"date_format\":\"d.m.Y\",\"iso_639_1\":\"ja\"},{\"id\":8,\"name\":\"Spanisch\",\"decimal_point\":\".\",\"thousands_separator\":\"'\",\"date_format_id\":1,\"date_format\":\"d.m.Y\",\"iso_639_1\":\"es\"}]"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"statusCode": 200,
"headers": {
"Date": "Mon, 30 Sep 2024 13:32:02 GMT",
"Content-Type": "application\/json",
"Content-Length": "208",
"Connection": "keep-alive",
"ratelimit-remaining": "999",
"ratelimit-reset": "58",
"x-ratelimit-limit-minute": "1000",
"x-ratelimit-remaining-minute": "999",
"ratelimit-limit": "1000",
"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": "8cb4939f6b4a60ed-LHR"
},
"data": "[{\"id\":1,\"name\":\"Vorauszahlung\"},{\"id\":2,\"name\":\"Barzahlung\"},{\"id\":3,\"name\":\"EC Karte\"},{\"id\":4,\"name\":\"Rechnung\"},{\"id\":5,\"name\":\"Nachnahme\"},{\"id\":6,\"name\":\"Kreditkarte\"},{\"id\":7,\"name\":\"Bankeinzug\\\/LSV\"}]"
}
24 changes: 24 additions & 0 deletions tests/Fixtures/Saloon/Taxes/delete-a-tax.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"statusCode": 200,
"headers": {
"Date": "Mon, 30 Sep 2024 13:35:44 GMT",
"Content-Type": "application\/json",
"Content-Length": "16",
"Connection": "keep-alive",
"ratelimit-remaining": "999",
"ratelimit-reset": "16",
"x-ratelimit-limit-minute": "1000",
"x-ratelimit-remaining-minute": "999",
"ratelimit-limit": "1000",
"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": "8cb4990f0936bd98-LHR"
},
"data": "{\"success\":true}"
}
24 changes: 24 additions & 0 deletions tests/Fixtures/Saloon/VatPeriods/fetch-a-vat-periods.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"statusCode": 200,
"headers": {
"Date": "Mon, 30 Sep 2024 13:34:35 GMT",
"Content-Type": "application\/json",
"Content-Length": "363",
"Connection": "keep-alive",
"ratelimit-remaining": "999",
"ratelimit-reset": "25",
"x-ratelimit-limit-minute": "1000",
"x-ratelimit-remaining-minute": "999",
"ratelimit-limit": "1000",
"etag": "d618434bea7ee1cd7f1562e89254c390",
"Cache-Control": "no-store",
"pragma": "no-cache",
"vary": "Origin",
"access-control-allow-origin": "https:\/\/office.bexio.com",
"via": "1.1 google",
"CF-Cache-Status": "DYNAMIC",
"Server": "cloudflare",
"CF-RAY": "8cb4975deb159406-LHR"
},
"data": "{\"id\":3,\"uuid\":\"4280f7e2-4772-4772-8b42-d8a0173c05ac\",\"name\":\"lib.model.tax.ch.sales_export.name\",\"code\":\"UEX\",\"digit\":\"220\",\"type\":\"not_taxable_turnover\",\"account_id\":105,\"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}"
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@
$mockClient->assertSent(FetchAListOfBusinessActivitesRequest::class);

expect($response->dto())->toBeInstanceOf(Collection::class)
->and($response->dto()->count())->toBe(0);
})->skip('WIP');
->and($response->dto()->count())->toBe(5);
});
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,8 @@

$response = $connector->send(new FetchAListOfDocumentSettingsRequest);

ray($response->dto());

$mockClient->assertSent(FetchAListOfDocumentSettingsRequest::class);

expect($response->dto())->toBeInstanceOf(Collection::class)
->and($response->dto()->count())->toBe(10);
})->only();
});
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
$connector = new BexioConnector;
$connector->withMockClient($mockClient);

$response = $connector->debug()->send(new FetchAListOfInvoicesRequest);
$response = $connector->send(new FetchAListOfInvoicesRequest);

$mockClient->assertSent(FetchAListOfInvoicesRequest::class);

Expand Down
6 changes: 2 additions & 4 deletions tests/Requests/Languages/FetchAListOfLanguagesRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,8 @@

$response = $connector->send(new FetchAListOfLanguagesRequest);

ray($response->json());

$mockClient->assertSent(FetchAListOfLanguagesRequest::class);

expect($response->dto())->toBeInstanceOf(Collection::class)
->and($response->dto()->count())->toBe(0);
})->skip('WIP');
->and($response->dto()->count())->toBe(8);
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

use CodebarAg\Bexio\BexioConnector;
use CodebarAg\Bexio\Requests\PaymentTypes\FetchAListOfPaymentTypesRequest;
use Illuminate\Support\Collection;
use Saloon\Http\Faking\MockResponse;
use Saloon\Laravel\Http\Faking\MockClient;

it('can perform the request', closure: function () {
$mockClient = new MockClient([
FetchAListOfPaymentTypesRequest::class => MockResponse::fixture('PaymentTypes/fetch-a-list-of-payment-types'),
]);

$connector = new BexioConnector;
$connector->withMockClient($mockClient);

$response = $connector->send(new FetchAListOfPaymentTypesRequest);

$mockClient->assertSent(FetchAListOfPaymentTypesRequest::class);

expect($response->dto())->toBeInstanceOf(Collection::class)
->and($response->dto()->count())->toBe(7);
});
4 changes: 2 additions & 2 deletions tests/Requests/Taxes/DeleteATaxRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
$connector = new BexioConnector;
$connector->withMockClient($mockClient);

$response = $connector->send(new DeleteATaxRequest(id: null));
$response = $connector->send(new DeleteATaxRequest(id: 3));

$mockClient->assertSent(DeleteATaxRequest::class);
})->skip('WAITING FOR SETUP IN DEV ENVIRONMENT')->todo();
});
23 changes: 0 additions & 23 deletions tests/Requests/VatPeriods/FetchAListOfTaxesRequestTest.php

This file was deleted.

19 changes: 0 additions & 19 deletions tests/Requests/VatPeriods/FetchATaxRequestTest.php

This file was deleted.

0 comments on commit 49f753a

Please sign in to comment.