|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace nickurt\PostcodeApi\tests\Providers\nl_NL; |
| 4 | + |
| 5 | +use Illuminate\Http\Client\HttpClientException; |
| 6 | +use Illuminate\Support\Facades\Http; |
| 7 | +use nickurt\PostcodeApi\Entity\Address; |
| 8 | +use nickurt\PostcodeApi\Providers\nl_NL\PostcodeNL; |
| 9 | +use nickurt\PostcodeApi\tests\TestCase; |
| 10 | + |
| 11 | +class PostcodeNLTest extends TestCase |
| 12 | +{ |
| 13 | + /** @var PostcodeNL */ |
| 14 | + protected $postcodeNL; |
| 15 | + |
| 16 | + public function setUp(): void |
| 17 | + { |
| 18 | + $this->postcodeNL = (new PostcodeNL) |
| 19 | + ->setRequestUrl('https://api.postcode.nl/rest/addresses/%s/%s') |
| 20 | + ->setApiKey('api-key') |
| 21 | + ->setApiSecret('api-secret'); |
| 22 | + } |
| 23 | + |
| 24 | + public function test_it_can_get_the_default_config_values_for_this_provider() |
| 25 | + { |
| 26 | + $this->assertSame('api-key', $this->postcodeNL->getApiKey()); |
| 27 | + $this->assertSame('api-secret', $this->postcodeNL->getApiSecret()); |
| 28 | + $this->assertSame('https://api.postcode.nl/rest/addresses/%s/%s', $this->postcodeNL->getRequestUrl()); |
| 29 | + } |
| 30 | + |
| 31 | + public function test_it_can_get_the_correct_values_for_find_by_postcode_and_house_number_a_valid_postal_code() |
| 32 | + { |
| 33 | + Http::fake(['https://json.api-postcode.nl?postcode=1118CP&number=202' => Http::response('{"street":"Evert van de Beekstraat","houseNumber":202,"houseNumberAddition":"","postcode":"1118CP","city":"Schiphol","municipality":"Haarlemmermeer","province":"Noord-Holland","rdX":111396,"rdY":479739,"latitude":52.30389933,"longitude":4.74791023,"bagNumberDesignationId":"0394200001001951","bagAddressableObjectId":"0394010001001991","addressType":"building","purposes":["office"],"surfaceArea":16800,"houseNumberAdditions":[""]}')]); |
| 34 | + |
| 35 | + $address = $this->postcodeNL->findByPostcodeAndHouseNumber('1118CP', '202'); |
| 36 | + |
| 37 | + $this->assertSame('api-key', $this->postcodeNL->getApiKey()); |
| 38 | + $this->assertSame('api-secret', $this->postcodeNL->getApiSecret()); |
| 39 | + $this->assertSame('https://api.postcode.nl/rest/addresses/1118CP/202', $this->postcodeNL->getRequestUrl()); |
| 40 | + |
| 41 | + $this->assertInstanceOf(Address::class, $address); |
| 42 | + |
| 43 | + $this->assertSame([ |
| 44 | + 'street' => 'Evert van de Beekstraat', |
| 45 | + 'house_no' => '202', |
| 46 | + 'town' => 'Schiphol', |
| 47 | + 'municipality' => 'Haarlemmermeer', |
| 48 | + 'province' => 'Noord-Holland', |
| 49 | + 'latitude' => 52.30389933, |
| 50 | + 'longitude' => 4.74791023, |
| 51 | + ], $address->toArray()); |
| 52 | + } |
| 53 | + |
| 54 | + public function test_it_can_get_the_correct_values_for_find_by_postcode_and_house_number_an_invalid_postal_code() |
| 55 | + { |
| 56 | + Http::fake(['https://json.api-postcode.nl?postcode=1118CP&number=1' => fn () => throw new HttpClientException('{"exception":"Combination does not exist.","exceptionId":"PostcodeNl_Service_PostcodeAddress_AddressNotFoundException"}', 404)]); |
| 57 | + |
| 58 | + // GuzzleHttp\Exception\ClientException: Client error: `GET https://json.api-postcode.nl?postcode=1118CP&number=1` resulted in a `404 Not Found` response: |
| 59 | + // {"error":"Cannot resolve address for postcode: 1118CP"} |
| 60 | + |
| 61 | + $address = $this->postcodeNL->findByPostcodeAndHouseNumber('1118CP', '1'); |
| 62 | + |
| 63 | + $this->assertInstanceOf(Address::class, $address); |
| 64 | + |
| 65 | + $this->assertSame([ |
| 66 | + 'street' => null, |
| 67 | + 'house_no' => null, |
| 68 | + 'town' => null, |
| 69 | + 'municipality' => null, |
| 70 | + 'province' => null, |
| 71 | + 'latitude' => null, |
| 72 | + 'longitude' => null, |
| 73 | + ], $address->toArray()); |
| 74 | + } |
| 75 | +} |
0 commit comments