Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
.php_cs
.php_cs.cache
.phpunit.result.cache
.phpunit.cache/*
build
composer.lock
coverage
Expand All @@ -10,4 +11,5 @@ phpunit.xml
psalm.xml
testbench.yaml
vendor
node_modules
node_modules
.env
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
],
"require": {
"php": "~8.1.0 || ~8.2.0",
"guzzlehttp/guzzle": "^7.7",
"illuminate/contracts": "^10.0",
"laravel/framework": "^10.0"
},
Expand Down Expand Up @@ -56,4 +57,4 @@
},
"minimum-stability": "dev",
"prefer-stable": true
}
}
1 change: 1 addition & 0 deletions config/vat.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand Down
13 changes: 10 additions & 3 deletions src/Drivers/CvrApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Illuminate\Http\Client\Response;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Http;
use InvalidArgumentException;

class CvrApi implements VatServiceInterface
{
Expand Down Expand Up @@ -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.');
}

Expand All @@ -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';
Expand All @@ -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 [email protected]';
return config('vat.drivers.cvr_api.user_agent') ?? throw new InvalidArgumentException('Missing user agent.');
}

private function searchByVatNumber(string $countryCode, string $vatNumber): Response
Expand All @@ -136,4 +143,4 @@ private function searchByVatNumber(string $countryCode, string $vatNumber): Resp
'token' => $this->accessToken,
]);
}
}
}
10 changes: 9 additions & 1 deletion src/VatServiceManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,19 @@
namespace Bilfeldt\VatService;

use Illuminate\Support\Manager;
use Bilfeldt\VatService\Drivers\CvrApi;

class VatServiceManager extends Manager
{
public function getDefaultDriver(): string
{
return $this->config->get('vat.default');
}
}

public function createCvrApiDriver(): CvrApi
{
return new CvrApi(
config('vat.drivers.cvr_api.access_token'),
);
}
}
89 changes: 89 additions & 0 deletions tests/CvrApiTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?php

namespace Bilfeldt\VatService\Tests;

use Bilfeldt\VatService\Drivers\CvrApi;
use Illuminate\Support\Facades\Http;
use Bilfeldt\VatService\VatInformation;
use Bilfeldt\VatService\VatServiceManager;
class CvrApiTest extends TestCase
{
protected CvrApi $driver;

public function setup(): void
{
parent::setup();
$this->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('[email protected]', $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')
);
}
}
60 changes: 60 additions & 0 deletions tests/Fixtures/CvrApi/search-by-vat-number-response.json
Original file line number Diff line number Diff line change
@@ -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": "[email protected]",
"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": "[email protected]",
"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
}
17 changes: 16 additions & 1 deletion tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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'));
}
}