Skip to content

Commit

Permalink
Added voucher endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
slischka committed Mar 11, 2021
1 parent 9bfaeaf commit 563c314
Show file tree
Hide file tree
Showing 4 changed files with 235 additions and 0 deletions.
35 changes: 35 additions & 0 deletions src/Fapi/FapiClient/EndPoints/Vouchers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php
declare(strict_types = 1);

namespace Fapi\FapiClient\EndPoints;

use Fapi\FapiClient\EndPoints\Traits\Find;
use Fapi\FapiClient\Rest\FapiRestClient;

final class Vouchers
{

use Find;

/** @var FapiRestClient */
private $client;

/** @var string */
private $path;

public function __construct(FapiRestClient $client)
{
$this->client = $client;
$this->path = '/vouchers';
}

/**
* @param mixed[] $data
* @return mixed[]
*/
public function applyVoucher(string $code, array $data = []): array
{
return $this->client->updateResource($this->path, $code . '/apply', $data);
}

}
10 changes: 10 additions & 0 deletions src/Fapi/FapiClient/FapiClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Fapi\FapiClient\EndPoints\Statistics;
use Fapi\FapiClient\EndPoints\User;
use Fapi\FapiClient\EndPoints\UserSettings;
use Fapi\FapiClient\EndPoints\Vouchers;
use Fapi\FapiClient\Rest\FapiRestClient;
use Fapi\HttpClient\IHttpClient;

Expand Down Expand Up @@ -124,6 +125,9 @@ class FapiClient implements IFapiClient
*/
public $userSetting;

/** @var Vouchers */
private $vouchers;

public function __construct(string $username, string $password, string $apiUrl, IHttpClient $httpClient)
{
$this->restClient = new FapiRestClient($username, $password, $apiUrl, $httpClient);
Expand All @@ -143,6 +147,7 @@ public function __construct(string $username, string $password, string $apiUrl,
$this->periodicInvoices = new PeriodicInvoices($this->restClient);
$this->exchangeRates = new ExchangeRates($this->restClient);
$this->userSetting = new UserSettings($this->restClient);
$this->vouchers = new Vouchers($this->restClient);
}

/**
Expand Down Expand Up @@ -238,4 +243,9 @@ public function getUserSetting(): UserSettings
return $this->userSetting;
}

public function getVouchers(): Vouchers
{
return $this->vouchers;
}

}
75 changes: 75 additions & 0 deletions tests/Fapi/FapiClientTests/FapiClientVouchersTest.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php
declare(strict_types = 1);

namespace Fapi\FapiClientTests;

use Fapi\FapiClient\FapiClient;
use Fapi\HttpClient\CapturingHttpClient;
use Fapi\HttpClient\GuzzleHttpClient;
use Tester\Assert;
use Tester\Environment;
use Tester\TestCase;

require __DIR__ . '/../../bootstrap.php';

class FapiClientVouchersTest extends TestCase
{

/** @var CapturingHttpClient */
private $httpClient;

/** @var FapiClient */
private $fapiClient;

protected function setUp()
{
Environment::lock('FapiClient', \LOCKS_DIR);

$this->httpClient = new CapturingHttpClient(
new GuzzleHttpClient(),
__DIR__ . '/MockHttpClients/FapiClientVouchersMockHttpClient.php',
'Fapi\FapiClientTests\MockHttpClients\FapiClientVouchersMockHttpClient'
);

$this->fapiClient = new FapiClient(
'slischka@test-fapi.cz',
'AaleYCMwUdSZjgK02NTCiSEVC',
'https://api.fapi.cz/',
$this->httpClient
);
}

protected function tearDown()
{
$this->httpClient->close();
}

public function testDefaults()
{
$voucher = $this->fapiClient->getVouchers()->find(1656);

Assert::type('array', $voucher);
Assert::type('int', $voucher['id']);
Assert::same('ABUCRQ', $voucher['code']);
Assert::same('valid', $voucher['status']);
Assert::same('2021-03-31', $voucher['expiration_date']);

$result = $this->fapiClient->getVouchers()->applyVoucher($voucher['code'], ['applicant' => [
'email' => 'test@fapi.cz',
'form_url' => 'https://xx.fapi.cz',
]]);

Assert::true($result['applied']);
$voucher = $result['voucher'];

Assert::type('array', $voucher);
Assert::type('int', $voucher['id']);
Assert::same('ABUCRQ', $voucher['code']);
Assert::same('applied', $voucher['status']);
Assert::same('2021-03-31', $voucher['expiration_date']);
Assert::same('2021-03-11 17:44:35', $voucher['applied_on']);
}

}

(new FapiClientVouchersTest())->run();
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
<?php
declare(strict_types = 1);

namespace Fapi\FapiClientTests\MockHttpClients;

use Fapi\HttpClient\HttpRequest;
use Fapi\HttpClient\HttpResponse;
use Fapi\HttpClient\MockHttpClient;

final class FapiClientVouchersMockHttpClient extends MockHttpClient
{

public function __construct()
{
$this->add(
new HttpRequest(
'GET',
'https://api.fapi.cz/vouchers/1656',
[
'Host' => ['api.fapi.cz'],
'verify' => ['1'],
'Content-Type' => ['application/json'],
'Accept' => ['application/json'],
'Authorization' => [
'Basic c2xpc2Noa2FAdGVzdC1mYXBpLmN6OkFhbGVZQ013VWRTWmpnSzAyTlRDaVNFVkM=',
],
],
'',
'1.1'
),
new HttpResponse(
200,
[
'Date' => ['Thu, 11 Mar 2021 16:44:35 GMT'],
'Content-Type' => ['application/json'],
'Content-Length' => ['227'],
'Connection' => ['keep-alive'],
'Set-Cookie' => [
'_nss=1; path=/; HttpOnly; SameSite=Strict',
'_nss=1; path=/; HttpOnly; SameSite=Strict',
],
'Server' => ['nginx'],
'X-Powered-By' => ['Nette Framework 3'],
'X-Frame-Options' => ['SAMEORIGIN', 'sameorigin', 'sameorigin'],
'X-NewRelic-App-Data' => [
'PxQFWFBbCAUIR1BSDgIAU1UEDxFORDQHUjZKA1ZLVVFHDFYPbU5yARBfWA86TFlDWThOFAZtGBALRFVbBxQQPh8ZUQYCYwQfCjgSHBNNA0xUBgdRVk8IHQBWUlcOHQVUUx0UBVJSWlsIBgoDCg4GV1BVAUMdB1IOF1Nq',
],
'Strict-Transport-Security' => [
'max-age=63072000; includeSubDomains; preload',
'max-age=63072000; includeSubDomains; preload',
],
'X-Content-Type-Options' => ['nosniff', 'nosniff'],
'X-Origin-Instance' => ['web3.prod.fapi.cloud', 'web1.prod.fapi.cloud'],
'Access-Control-Allow-Origin' => ['*', '*'],
'Access-Control-Allow-Headers' => [
'Origin, X-Requested-With, Content-Type, Accept',
'Origin, X-Requested-With, Content-Type, Accept',
],
],
'{"id":1656,"user_id":13057,"code":"ABUCRQ","status":"valid","created_on":"2021-03-11 17:36:23","expiration_date":"2021-03-31","applied_on":null,"invoice_id":null,"product_name":"test","item_template_code":null,"applicant":null}'
)
);
$this->add(
new HttpRequest(
'PUT',
'https://api.fapi.cz/vouchers/ABUCRQ/apply',
[
'Host' => ['api.fapi.cz'],
'verify' => ['1'],
'Content-Type' => ['application/json'],
'Accept' => ['application/json'],
'Authorization' => [
'Basic c2xpc2Noa2FAdGVzdC1mYXBpLmN6OkFhbGVZQ013VWRTWmpnSzAyTlRDaVNFVkM=',
],
],
'{"applicant":{"email":"test@fapi.cz","form_url":"https://xx.fapi.cz"}}',
'1.1'
),
new HttpResponse(
200,
[
'Date' => ['Thu, 11 Mar 2021 16:44:35 GMT'],
'Content-Type' => ['application/json'],
'Transfer-Encoding' => ['chunked'],
'Connection' => ['keep-alive'],
'Vary' => ['Accept-Encoding', 'Accept-Encoding'],
'Set-Cookie' => [
'_nss=1; path=/; HttpOnly; SameSite=Strict',
'_nss=1; path=/; HttpOnly; SameSite=Strict',
],
'Server' => ['nginx'],
'X-Powered-By' => ['Nette Framework 3'],
'X-Frame-Options' => ['SAMEORIGIN', 'sameorigin', 'sameorigin'],
'X-NewRelic-App-Data' => [
'PxQFWFBbCAUIR1BSDgIAU1UEDxFORDQHUjZKA1ZLVVFHDFYPbU5yARBfWA86TFlDWThOFAZtGBALRFVbBxQQPh8ZWw1cXQ5pUkhDIBw7A09daxwcOkxZQ0AIGEAbARlWVAEGA05WTVIFUg1WFAsCCh9HDVFTAwdSVgFVVlBbAFFUVENOUVBbFQFs',
],
'Strict-Transport-Security' => [
'max-age=63072000; includeSubDomains; preload',
'max-age=63072000; includeSubDomains; preload',
],
'X-Content-Type-Options' => ['nosniff', 'nosniff'],
'X-Origin-Instance' => ['web1.prod.fapi.cloud', 'web2.prod.fapi.cloud'],
'Access-Control-Allow-Origin' => ['*', '*'],
'Access-Control-Allow-Headers' => [
'Origin, X-Requested-With, Content-Type, Accept',
'Origin, X-Requested-With, Content-Type, Accept',
],
'x-encoded-content-encoding' => ['gzip'],
],
'{"applied":true,"message":"voucherUpdater.apply.success","voucher":{"id":1656,"user_id":13057,"code":"ABUCRQ","status":"applied","created_on":"2021-03-11 17:36:23","expiration_date":"2021-03-31","applied_on":"2021-03-11 17:44:35","invoice_id":null,"product_name":"test","item_template_code":null,"applicant":{"email":"test@fapi.cz","form_url":"https:\\/\\/xx.fapi.cz"}}}'
)
);
}

}

0 comments on commit 563c314

Please sign in to comment.