Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add US & CZ address formats and use localized Faker #1

Closed
wants to merge 5 commits into from
Closed
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: 4 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,13 @@
"illuminate/support": "^6.0 || ^7.0"
},
"require-dev": {
"fzaninotto/faker": "^1.6.0",
"orchestra/testbench": "^4.0 || ^5.0",
"phpunit/phpunit": "^8.0 || ^9.0"
},
"suggest": {
"fzaninotto/faker": "^1.6.0"
},
"config": {
"sort-packages": true
},
Expand Down
9 changes: 8 additions & 1 deletion src/AddressFormats/BaseFormat.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,17 @@ public function rules(?string $prefix = null): array

public function fake(string $country, array $data = []): array
{
$faker = Factory::create([
'CH' => 'de_CH',
'CZ' => 'cs_CZ',
'DE' => 'de_DE',
'US' => 'en_US',
][$country] ?? null);

return Arr::only(
array_merge(
['country_code' => $country],
$this->factory(Factory::create()),
$this->factory($faker),
$data
),
$this->fields()
Expand Down
47 changes: 47 additions & 0 deletions src/AddressFormats/CzechRepublic.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace Elbgoods\LaravelAddressable\AddressFormats;

use Faker\Generator;

class CzechRepublic extends BaseFormat
{
protected function formatConfig(): array
{
return [
'street' => [
'required',
'string',
],
'house_number' => [
'required',
'string',
],
'postal_code' => [
'required',
'string',
'regex:/^([0-9]{5}|[0-9]{3} [0-9]{2})$/',
],
'city' => [
'required',
'string',
],
'post_office' => [
'required',
'string',
],
];
}

protected function factory(Generator $faker): array
{
return [
'country_code' => 'CZ',
'street' => $faker->streetName,
'house_number' => $faker->buildingNumber,
'postal_code' => $faker->postcode,
'city' => $faker->city,
'post_office' => 'P.O. Box '.$faker->randomNumber(2),
];
}
}
2 changes: 1 addition & 1 deletion src/AddressFormats/Germany.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ protected function factory(Generator $faker): array
'country_code' => 'DE',
'street' => $faker->streetName,
'house_number' => $faker->buildingNumber,
'postal_code' => strval($faker->numberBetween(10000, 99999)),
'postal_code' => $faker->postcode,
'city' => $faker->city,
];
}
Expand Down
3 changes: 1 addition & 2 deletions src/AddressFormats/Switzerland.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use Elbgoods\SwissCantonRule\Rules\SwissCantonZipCodeRule;
use Faker\Generator;
use Illuminate\Support\Arr;
use Wnx\SwissCantons\Cantons;
use Wnx\SwissCantons\ZipcodeSearch;

class Switzerland extends BaseFormat
Expand Down Expand Up @@ -47,7 +46,7 @@ protected function factory(Generator $faker): array
'house_number' => $faker->buildingNumber,
'postal_code' => strval(Arr::random(array_column((new ZipcodeSearch)->getDataSet(), 'zipcode'))),
'city' => $faker->city,
'canton' => Arr::random(array_keys((new Cantons)->getAllAsArray())),
'canton' => $faker->cantonShort,
];
}
}
48 changes: 48 additions & 0 deletions src/AddressFormats/UnitedStatesOfAmerica.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace Elbgoods\LaravelAddressable\AddressFormats;

use Faker\Generator;

class UnitedStatesOfAmerica extends BaseFormat
{
protected function formatConfig(): array
{
return [
'street' => [
'required',
'string',
],
'house_number' => [
'required',
'string',
],
'postal_code' => [
'required',
'string',
'regex:/^[0-9]{5}(|-[0-9]{4})$/',
],
'city' => [
'required',
'string',
],
'state' => [
'required',
'string',
'regex:/^[A-Z]{2}$/',
],
];
}

protected function factory(Generator $faker): array
{
return [
'country_code' => 'US',
'street' => $faker->streetName,
'house_number' => $faker->buildingNumber,
'postal_code' => $faker->postcode,
'city' => $faker->city,
'state' => $faker->stateAbbr,
];
}
}
12 changes: 7 additions & 5 deletions src/Managers/AddressFormats.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

namespace Elbgoods\LaravelAddressable\Managers;

use Elbgoods\LaravelAddressable\AddressFormats\CzechRepublic;
use Elbgoods\LaravelAddressable\AddressFormats\Germany;
use Elbgoods\LaravelAddressable\AddressFormats\International;
use Elbgoods\LaravelAddressable\AddressFormats\Switzerland;
use Elbgoods\LaravelAddressable\AddressFormats\UnitedStatesOfAmerica;
use Elbgoods\LaravelAddressable\Contracts\AddressFormat;
use Illuminate\Support\Manager;
use Illuminate\Support\Str;
Expand All @@ -15,9 +17,11 @@ class AddressFormats extends Manager
protected const DEFAULT_DRIVER = 'international';

protected const FORMATS = [
'international' => International::class,
'de' => Germany::class,
'ch' => Switzerland::class,
self::DEFAULT_DRIVER => International::class,
'CH' => Switzerland::class,
'CZ' => CzechRepublic::class,
'DE' => Germany::class,
'US' => UnitedStatesOfAmerica::class,
];

public function country(?string $country = null): AddressFormat
Expand Down Expand Up @@ -47,8 +51,6 @@ public function getDefaultDriver(): string

public function driver($driver = null): AddressFormat
{
$driver = is_string($driver) ? strtolower($driver) : $driver;

try {
return parent::driver($driver);
} catch (InvalidArgumentException $exception) {
Expand Down
22 changes: 22 additions & 0 deletions tests/AddressFormats/CzechRepublicTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Elbgoods\LaravelAddressable\Tests\AddressFormats;

use Elbgoods\LaravelAddressable\Facades\AddressFormats;
use Illuminate\Support\Facades\Validator;

final class CzechRepublicTest extends BaseFormatTest
{
/**
* @test
* @dataProvider repeatTest
*/
public function faked_address_passes_validation_rules(): void
{
$data = AddressFormats::fake('CZ');

$validator = Validator::make($data, AddressFormats::rules('CZ'));

$this->assertFalse($validator->fails(), $validator->errors()->first());
}
}
22 changes: 22 additions & 0 deletions tests/AddressFormats/UnitedStatesOfAmericaTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Elbgoods\LaravelAddressable\Tests\AddressFormats;

use Elbgoods\LaravelAddressable\Facades\AddressFormats;
use Illuminate\Support\Facades\Validator;

final class UnitedStatesOfAmericaTest extends BaseFormatTest
{
/**
* @test
* @dataProvider repeatTest
*/
public function faked_address_passes_validation_rules(): void
{
$data = AddressFormats::fake('US');

$validator = Validator::make($data, AddressFormats::rules('US'));

$this->assertFalse($validator->fails(), $validator->errors()->first());
}
}
2 changes: 1 addition & 1 deletion tests/Managers/AddressFormatsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function specific_format_by_identifier(): void
{
$this->assertInstanceOf(
Germany::class,
AddressFormats::country('de')
AddressFormats::country('DE')
);
}

Expand Down