From 684cab5768dbeda307c9d7d49696c411b477097a Mon Sep 17 00:00:00 2001 From: Piotr Witkowicki Date: Tue, 23 May 2023 11:05:27 +0200 Subject: [PATCH] Throw invalid NIP exception in wFirma contractor book (#60) This can be used to throw a specific exception when the contractor NIP (PL Vat Id) is invalid and wFirma returns an error response. --- .../Exception/InvalidVatIdException.php | 11 ++ .../Exception/ErrorResponseException.php | 15 +++ src/Wfirma/Client/WfirmaClient.php | 101 ++++++++++-------- .../Contractor/WfirmaContractorBook.php | 47 ++++++-- .../Resources/companies_invalid.json | 14 +++ .../Contractor/WfirmaContractorBookTest.php | 32 ++++++ 6 files changed, 165 insertions(+), 55 deletions(-) create mode 100644 src/Bookkeeping/Contractor/Exception/InvalidVatIdException.php create mode 100644 src/Wfirma/Client/Exception/ErrorResponseException.php create mode 100644 tests/Integration/Wfirma/Contractor/Resources/companies_invalid.json diff --git a/src/Bookkeeping/Contractor/Exception/InvalidVatIdException.php b/src/Bookkeeping/Contractor/Exception/InvalidVatIdException.php new file mode 100644 index 0000000..fc9f6f9 --- /dev/null +++ b/src/Bookkeeping/Contractor/Exception/InvalidVatIdException.php @@ -0,0 +1,11 @@ +contractorFactory->getContractor( - $this->getContractorResult( - $this->client->requestPOST( - sprintf( - self::CONTRACTOR_API_URL, - sprintf('%s', 'add') - ), - $contractor->print(WfirmaMedia::api())->toString() + try { + return $this->contractorFactory->getContractor( + $this->getContractorResult( + $this->client->requestPOST( + sprintf( + self::CONTRACTOR_API_URL, + sprintf('%s', 'add') + ), + $contractor->print(WfirmaMedia::api())->toString() + ) ) - ) - ); + ); + } catch (ErrorResponseException $e) { + try { + $contractor = $this->getContractorResult($e->getResult()); + } catch (WfirmaException $invalidResponseStructureException) { + throw $e; + } + + $this->tryToThrowSpecificExceptionFromResponse($contractor); + throw $e; + } } /** @@ -111,4 +124,18 @@ private function getContractorResult(array $response): array return $response['contractors'][0]['contractor']; } + + private function tryToThrowSpecificExceptionFromResponse(array $contractor): void + { + $error = $contractor['errors'][0]['error'] ?? []; + + if (isset($error['field'])) { + switch ($error['field']) { + case 'nip': + throw new InvalidVatIdException($error['message'] ?: 'Invalid Vat Id provided'); + default: + // Do nothing + } + } + } } diff --git a/tests/Integration/Wfirma/Contractor/Resources/companies_invalid.json b/tests/Integration/Wfirma/Contractor/Resources/companies_invalid.json new file mode 100644 index 0000000..17b3989 --- /dev/null +++ b/tests/Integration/Wfirma/Contractor/Resources/companies_invalid.json @@ -0,0 +1,14 @@ +{ + "companies": [ + { + "id": "1234", + "name": "Polish Company", + "email": "polish.company@gmail.com", + "street": "Witkiewicza", + "zip": "44-100", + "city": "Gliwice", + "country": "PL", + "nip": "12345678" + } + ] +} diff --git a/tests/Integration/Wfirma/Contractor/WfirmaContractorBookTest.php b/tests/Integration/Wfirma/Contractor/WfirmaContractorBookTest.php index a43644f..7cc0932 100644 --- a/tests/Integration/Wfirma/Contractor/WfirmaContractorBookTest.php +++ b/tests/Integration/Wfirma/Contractor/WfirmaContractorBookTest.php @@ -8,6 +8,7 @@ use Landingi\BookkeepingBundle\Bookkeeping\Contractor\Company; use Landingi\BookkeepingBundle\Bookkeeping\Contractor\ContractorBook; use Landingi\BookkeepingBundle\Bookkeeping\Contractor\ContractorEmail; +use Landingi\BookkeepingBundle\Bookkeeping\Contractor\Exception\InvalidVatIdException; use Landingi\BookkeepingBundle\Bookkeeping\Contractor\Person; use Landingi\BookkeepingBundle\Integration\IntegrationTestCase; use Landingi\BookkeepingBundle\Memory\Contractor\Company\ValueAddedTax\MemoryIdentifierFactory; @@ -17,6 +18,7 @@ use Landingi\BookkeepingBundle\Wfirma\Client\WfirmaConditionTransformer; use Landingi\BookkeepingBundle\Wfirma\Contractor\Factory\ContractorFactory; use Landingi\BookkeepingBundle\Wfirma\Contractor\WfirmaContractorBook; +use Throwable; final class WfirmaContractorBookTest extends IntegrationTestCase { @@ -111,4 +113,34 @@ public function companies(): Generator yield [$factory->getContractor($company)]; } } + + /** + * @dataProvider invalidCompanies + * + * @param Contractor $company + * @param class-string $exceptionClass + */ + public function testErrorResponseThrowsException(Contractor $company, string $exceptionClass): void + { + $this->expectException($exceptionClass); + $this->book->create($company); + } + + /** + * @internal use only in testErrorResponseThrowsException function + */ + public function invalidCompanies(): Generator + { + $factory = new ContractorFactory(new MemoryIdentifierFactory()); + $data = json_decode( + (string) file_get_contents(__DIR__ . '/Resources/companies_invalid.json'), + true, + 512, + JSON_THROW_ON_ERROR + ); + + foreach ($data['companies'] as $company) { + yield [$factory->getContractor($company), InvalidVatIdException::class]; + } + } }