From a086f66efb376672ce69d69f8f918f0d096c529f Mon Sep 17 00:00:00 2001 From: Hasnayeen Date: Wed, 23 Aug 2023 00:06:03 +0600 Subject: [PATCH] Add tests --- .gitignore | 4 +- composer.json | 3 +- config/vat.php | 1 + src/Drivers/CvrApi.php | 13 ++- src/VatServiceManager.php | 10 ++- tests/CvrApiTest.php | 89 +++++++++++++++++++ .../CvrApi/search-by-vat-number-response.json | 60 +++++++++++++ tests/TestCase.php | 17 +++- 8 files changed, 190 insertions(+), 7 deletions(-) create mode 100644 tests/CvrApiTest.php create mode 100644 tests/Fixtures/CvrApi/search-by-vat-number-response.json diff --git a/.gitignore b/.gitignore index c8aa593..496953f 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,7 @@ .php_cs .php_cs.cache .phpunit.result.cache +.phpunit.cache/* build composer.lock coverage @@ -10,4 +11,5 @@ phpunit.xml psalm.xml testbench.yaml vendor -node_modules \ No newline at end of file +node_modules +.env diff --git a/composer.json b/composer.json index 800ce9e..213654a 100644 --- a/composer.json +++ b/composer.json @@ -21,6 +21,7 @@ ], "require": { "php": "~8.1.0 || ~8.2.0", + "guzzlehttp/guzzle": "^7.7", "illuminate/contracts": "^10.0", "laravel/framework": "^10.0" }, @@ -56,4 +57,4 @@ }, "minimum-stability": "dev", "prefer-stable": true -} \ No newline at end of file +} diff --git a/config/vat.php b/config/vat.php index a5fb818..751e420 100644 --- a/config/vat.php +++ b/config/vat.php @@ -29,6 +29,7 @@ ], 'cvr_api' => [ 'access_token' => env('VAT_CVR_API_ACCESS_TOKEN'), + 'user_agent' => env('VAT_CVR_API_USER_AGENT'), ], 'abstract_api' => [ 'api_key' => env('VAT_ABSTRACT_API_KEY'), diff --git a/src/Drivers/CvrApi.php b/src/Drivers/CvrApi.php index 4a735af..8c8307a 100644 --- a/src/Drivers/CvrApi.php +++ b/src/Drivers/CvrApi.php @@ -11,6 +11,7 @@ use Illuminate\Http\Client\Response; use Illuminate\Support\Collection; use Illuminate\Support\Facades\Http; +use InvalidArgumentException; class CvrApi implements VatServiceInterface { @@ -94,7 +95,7 @@ public function getInformation(string $countryCode, string $vatNumber): VatInfor throw InvalidVatException::doesNotExist($vatNumber); } - if ($response->isSuccessful()) { + if (! $response->successful()) { throw new DriverUnavailable('CVR API is currently unavailable.'); } @@ -114,6 +115,12 @@ public function getInformation(string $countryCode, string $vatNumber): VatInfor ); } + /** @inheritDoc */ + public function search(string $countryCode, string $search): Collection + { + return collect(); + } + private function getUrl(): string { return 'https://cvrapi.dk/api'; @@ -122,7 +129,7 @@ private function getUrl(): string private function getUserAgent(): string { // Required format described here: https://cvrapi.dk/documentation - return 'SmartSend - VatService - Anders Bilfeldt anders@smartsend.io'; + return config('vat.drivers.cvr_api.user_agent') ?? throw new InvalidArgumentException('Missing user agent.'); } private function searchByVatNumber(string $countryCode, string $vatNumber): Response @@ -136,4 +143,4 @@ private function searchByVatNumber(string $countryCode, string $vatNumber): Resp 'token' => $this->accessToken, ]); } -} \ No newline at end of file +} diff --git a/src/VatServiceManager.php b/src/VatServiceManager.php index 2620d5e..5877bb4 100644 --- a/src/VatServiceManager.php +++ b/src/VatServiceManager.php @@ -3,6 +3,7 @@ namespace Bilfeldt\VatService; use Illuminate\Support\Manager; +use Bilfeldt\VatService\Drivers\CvrApi; class VatServiceManager extends Manager { @@ -10,4 +11,11 @@ public function getDefaultDriver(): string { return $this->config->get('vat.default'); } -} \ No newline at end of file + + public function createCvrApiDriver(): CvrApi + { + return new CvrApi( + config('vat.drivers.cvr_api.access_token'), + ); + } +} diff --git a/tests/CvrApiTest.php b/tests/CvrApiTest.php new file mode 100644 index 0000000..b19df20 --- /dev/null +++ b/tests/CvrApiTest.php @@ -0,0 +1,89 @@ +driver = resolve(VatServiceManager::class)->driver('cvr_api'); + } + + public function test_resolves_correct_driver(): void + { + $this->assertInstanceOf(CvrApi::class, $this->driver); + } + + public function test_support_returns_true() + { + $this->assertTrue( + $this->driver->supports('DK') + ); + $this->assertTrue( + $this->driver->supports('NO') + ); + } + + public function test_details_return_vat_information_object() + { + Http::fake([ + 'cvrapi.dk/*' => Http::response( + file_get_contents(__DIR__ . '/Fixtures/CvrApi/search-by-vat-number-response.json'), + 200 + ), + ]); + + $vatInfo = $this->driver->getInformation('DK', '29910251'); + $this->assertInstanceOf( + VatInformation::class, + $vatInfo, + ); + $this->assertEquals('DK', $vatInfo->country); + $this->assertEquals('29910251', $vatInfo->vatNumber); + $this->assertEquals('I/S Just Iversen', $vatInfo->company); + $this->assertEquals('Jonsvangen 10', $vatInfo->address); + $this->assertEquals('4200', $vatInfo->postalCode); + $this->assertEquals('Slagelse', $vatInfo->city); + $this->assertEquals('61401169', $vatInfo->phone); + $this->assertEquals('kontakt@justiversen.dk', $vatInfo->email); + } + + public function test_is_valid_returns_true() + { + Http::fake([ + 'cvrapi.dk/*' => Http::response( + file_get_contents(__DIR__ . '/Fixtures/CvrApi/search-by-vat-number-response.json'), + 200 + ), + ]); + + $this->assertTrue( + $this->driver->isValid('DK', '12345678') + ); + } + + public function test_is_valid_returns_false() + { + Http::fake([ + 'cvrapi.dk/*' => Http::response( + [ + "error" => "NOT_FOUND", + "t" => 0, + "version" => 6, + ], + 404 + ), + ]); + + $this->assertFalse( + $this->driver->isValid('DK', '12345678') + ); + } +} diff --git a/tests/Fixtures/CvrApi/search-by-vat-number-response.json b/tests/Fixtures/CvrApi/search-by-vat-number-response.json new file mode 100644 index 0000000..d72365e --- /dev/null +++ b/tests/Fixtures/CvrApi/search-by-vat-number-response.json @@ -0,0 +1,60 @@ +{ + "vat": 29910251, + "name": "I/S Just Iversen", + "address": "Jonsvangen 10", + "zipcode": 4200, + "city": "Slagelse", + "cityname": null, + "protected": true, + "phone": 61401169, + "email": "kontakt@justiversen.dk", + "fax": null, + "startdate": "01/11 - 2006", + "enddate": "20/08 - 2021", + "employees": 3, + "addressco": "Kristian Just Iversen", + "industrycode": 620200, + "industrydesc": "Konsulentbistand vedrørende informationsteknologi", + "companycode": 30, + "companydesc": "Interessentskab", + "creditstartdate": null, + "creditbankrupt": false, + "creditstatus": null, + "owners": [ + { + "name": "Jens Just Iversen" + }, + { + "name": "Kristian Just Kastrup" + }, + { + "name": "Hans Peter Iversen" + }, + { + "name": "Jens Just Iversen" + } + ], + "productionunits": [ + { + "pno": 1012697712, + "main": true, + "name": "I/S Just Iversen", + "address": "Jonsvangen 10", + "zipcode": 4200, + "city": "Slagelse", + "cityname": null, + "protected": true, + "phone": 61401169, + "email": "kontakt@justiversen.dk", + "fax": null, + "startdate": "01/11 - 2006", + "enddate": "20/08 - 2021", + "employees": 3, + "addressco": "Kristian Just Iversen", + "industrycode": 620200, + "industrydesc": "Konsulentbistand vedrørende informationsteknologi" + } + ], + "t": 100, + "version": 6 +} diff --git a/tests/TestCase.php b/tests/TestCase.php index 84aa979..4fe4844 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -2,8 +2,9 @@ namespace Bilfeldt\VatService\Tests; -use Bilfeldt\VatService\VatServiceServiceProvider; use Orchestra\Testbench\TestCase as Orchestra; +use Bilfeldt\VatService\VatServiceServiceProvider; +use Illuminate\Foundation\Bootstrap\LoadEnvironmentVariables; abstract class TestCase extends Orchestra { @@ -13,4 +14,18 @@ protected function getPackageProviders($app): array VatServiceServiceProvider::class, ]; } + + /** + * Define environment setup. + * + * @param \Illuminate\Foundation\Application $app + * @return void + */ + protected function defineEnvironment($app) + { + $app->useEnvironmentPath(__DIR__.'/..'); + $app->bootstrapWith([LoadEnvironmentVariables::class]); + parent::getEnvironmentSetUp($app); + $app['config']->set('vat.drivers.cvr_api.user_agent', env('VAT_CVR_API_USER_AGENT')); + } }