diff --git a/README.md b/README.md index daad1e0..80c2016 100644 --- a/README.md +++ b/README.md @@ -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"); diff --git a/src/Providers/BrregDataServiceProvider.php b/src/Providers/BrregDataServiceProvider.php index f87ecee..38f0787 100644 --- a/src/Providers/BrregDataServiceProvider.php +++ b/src/Providers/BrregDataServiceProvider.php @@ -1,8 +1,9 @@ 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()); }); } } diff --git a/src/Services/BrregDataService.php b/src/Services/BrregDataService.php index 83a70f1..36bdf2c 100644 --- a/src/Services/BrregDataService.php +++ b/src/Services/BrregDataService.php @@ -1,7 +1,7 @@ [ "page" => $page, "size" => $size, - "\$filter" => "startswith(navn, '$name')" + "navn" => $name, ] ])->getBody()->getContents(); } catch (ClientException $exception) { diff --git a/tests/BrregServiceTest.php b/tests/BrregServiceTest.php index 8012a2d..866aa61 100644 --- a/tests/BrregServiceTest.php +++ b/tests/BrregServiceTest.php @@ -1,32 +1,41 @@ 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"]); } @@ -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"])); @@ -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]; @@ -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 ")); } } \ No newline at end of file