diff --git a/src/Fapi/FapiClient/EndPoints/Vouchers.php b/src/Fapi/FapiClient/EndPoints/Vouchers.php new file mode 100644 index 0000000..8c57128 --- /dev/null +++ b/src/Fapi/FapiClient/EndPoints/Vouchers.php @@ -0,0 +1,35 @@ +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); + } + +} diff --git a/src/Fapi/FapiClient/FapiClient.php b/src/Fapi/FapiClient/FapiClient.php index e17b82a..54d9c42 100644 --- a/src/Fapi/FapiClient/FapiClient.php +++ b/src/Fapi/FapiClient/FapiClient.php @@ -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; @@ -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); @@ -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); } /** @@ -238,4 +243,9 @@ public function getUserSetting(): UserSettings return $this->userSetting; } + public function getVouchers(): Vouchers + { + return $this->vouchers; + } + } diff --git a/tests/Fapi/FapiClientTests/FapiClientVouchersTest.phpt b/tests/Fapi/FapiClientTests/FapiClientVouchersTest.phpt new file mode 100644 index 0000000..9263a17 --- /dev/null +++ b/tests/Fapi/FapiClientTests/FapiClientVouchersTest.phpt @@ -0,0 +1,75 @@ +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(); diff --git a/tests/Fapi/FapiClientTests/MockHttpClients/FapiClientVouchersMockHttpClient.php b/tests/Fapi/FapiClientTests/MockHttpClients/FapiClientVouchersMockHttpClient.php new file mode 100644 index 0000000..e7f795e --- /dev/null +++ b/tests/Fapi/FapiClientTests/MockHttpClients/FapiClientVouchersMockHttpClient.php @@ -0,0 +1,115 @@ +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"}}}' + ) + ); + } + +}