Skip to content
This repository has been archived by the owner on Mar 2, 2022. It is now read-only.

Commit

Permalink
Brreg updated it's api endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
HelgeSverre committed Jan 2, 2020
1 parent b5eba2d commit c70c707
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 25 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Usage

```php

Route::get("/companies", function (Request $request, \HelgeSverre\Brreg\BrregService $brreg) {
Route::get("/companies", function (Request $request, \HelgeSverre\Brreg\BrregDataService $brreg) {

$searchTerm = $request->input("q");

Expand Down
7 changes: 4 additions & 3 deletions src/Providers/BrregDataServiceProvider.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
<?php

namespace HelgeSverre\Brreg;
namespace HelgeSverre\Brreg\Providers;

use GuzzleHttp\Client;
use HelgeSverre\Brreg\Services\BrregDataService;
use Illuminate\Support\ServiceProvider;
use League\Fractal\Manager;

Expand All @@ -25,8 +26,8 @@ public function boot()
*/
public function register()
{
$this->app->singleton(BrregService::class, function () {
return new BrregService(new Manager(), new Client());
$this->app->singleton(BrregDataService::class, function () {
return new BrregDataService(new Manager(), new Client());
});
}
}
8 changes: 4 additions & 4 deletions src/Services/BrregDataService.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php


namespace HelgeSverre\Brreg;
namespace HelgeSverre\Brreg\Services;


use GuzzleHttp\Client;
Expand All @@ -11,9 +11,9 @@
use League\Fractal\Resource\Collection;
use League\Fractal\Resource\Item;

class BrregService
class BrregDataService
{
protected $apiEndpoint = "https://data.brreg.no/enhetsregisteret/enhet/";
protected $apiEndpoint = "https://data.brreg.no/enhetsregisteret/api/enheter/";
protected $format = "json";

/**
Expand All @@ -40,7 +40,7 @@ public function searchByName($name, $page = 0, $size = 10)
"query" => [
"page" => $page,
"size" => $size,
"\$filter" => "startswith(navn, '$name')"
"navn" => $name,
]
])->getBody()->getContents();
} catch (ClientException $exception) {
Expand Down
43 changes: 26 additions & 17 deletions tests/BrregServiceTest.php
Original file line number Diff line number Diff line change
@@ -1,32 +1,41 @@
<?php


class BrregServiceTest extends TestCase
{
use HelgeSverre\Brreg\Services\BrregDataService;

/**
* @var \App\Services\BrregDataService
*/
protected $service;
class BrregServiceTest extends \PHPUnit\Framework\TestCase
{

public function setUp()
public function buildService()
{
$this->service = new \App\Services\BrregDataService(
var_dump(class_exists(BrregDataService::class));
return new BrregDataService(
new \League\Fractal\Manager(),
new \GuzzleHttp\Client()
);

static $service = null;

if ($service == null) {
$service = new BrregDataService(
new \League\Fractal\Manager(),
new \GuzzleHttp\Client()
);
}

return $service;
}

/** @test */
public function it_returns_false_if_the_company_not_found()
{
$this->assertFalse($this->service->getCompanyData(999999999999));
$this->assertFalse($this->buildService()->getCompanyData(999999999999));
}

/** @test */
public function it_returns_empty_array_if_search_yields_no_results()
{
$results = $this->service->searchByName("xxxxxxxxxxxxxx");
$results = $this->buildService()->searchByName("xxxxxxxxxxxxxx");
$this->assertEmpty($results["data"]);
}

Expand All @@ -35,7 +44,7 @@ public function it_can_return_array_of_company_data_when_searching_for_valid_com
{
// There is at least 50 + companies who's name starts with "webutvikler",
// if this for some reason is not the case in the future, use a more generic term that yields more results
$companies = $this->service->searchByName("webutvikler");
$companies = $this->buildService()->searchByName("webutvikler");

$this->assertArrayHasKey("data", $companies);
$this->assertTrue(is_array($companies["data"]));
Expand All @@ -45,14 +54,14 @@ public function it_can_return_array_of_company_data_when_searching_for_valid_com
/** @test */
public function it_returns_the_requested_number_of_results()
{
$companies = $this->service->searchByName("proff", 0, 17);
$companies = $this->buildService()->searchByName("proff", 0, 17);
$this->assertCount(17, $companies["data"]);
}

/** @test */
public function it_returns_expected_array_items_when_returning_valid_company_data()
{
$companies = $this->service->searchByName("Webutvikler");
$companies = $this->buildService()->searchByName("Webutvikler");

$firstCompanyInList = $companies["data"][0];

Expand All @@ -70,9 +79,9 @@ public function it_can_sanitize_company_numbers_with_whitespace()
{
$expected = "123123123";

$this->assertSame($expected, $this->service->sanitizeRegistrationNumber("123 123 123"));
$this->assertSame($expected, $this->service->sanitizeRegistrationNumber("123 123123"));
$this->assertSame($expected, $this->service->sanitizeRegistrationNumber("123123 123"));
$this->assertSame($expected, $this->service->sanitizeRegistrationNumber(" 123 123 123 "));
$this->assertSame($expected, $this->buildService()->sanitizeRegistrationNumber("123 123 123"));
$this->assertSame($expected, $this->buildService()->sanitizeRegistrationNumber("123 123123"));
$this->assertSame($expected, $this->buildService()->sanitizeRegistrationNumber("123123 123"));
$this->assertSame($expected, $this->buildService()->sanitizeRegistrationNumber(" 123 123 123 "));
}
}

0 comments on commit c70c707

Please sign in to comment.