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 address & company methods #1

Merged
merged 2 commits into from
Dec 18, 2024
Merged
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
101 changes: 99 additions & 2 deletions src/Extensions/AddressExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,48 @@ class AddressExtension extends Extension
];

protected $regions = [
'England', 'Scotland', 'Wales', 'Northern Ireland',
// ENG - England (regions)
'East Midlands', 'East of England', 'London', 'North East', 'North West',
'South East', 'South West', 'West Midlands', 'Yorkshire and the Humber',

// SCT - Scotland (Council Areas)
'Aberdeenshire', 'Angus', 'Argyll and Bute', 'Clackmannanshire',
'Dumfries and Galloway', 'Dundee', 'East Ayrshire', 'East Dunbartonshire',
'East Lothian', 'East Renfrewshire', 'Edinburgh', 'Falkirk', 'Fife',
'Glasgow', 'Highland', 'Inverclyde', 'Midlothian', 'Moray',
'North Ayrshire', 'North Lanarkshire', 'Orkney Islands', 'Perth and Kinross',
'Renfrewshire', 'Scottish Borders', 'Shetland Islands', 'South Ayrshire',
'South Lanarkshire', 'Stirling', 'West Dunbartonshire', 'West Lothian',

// WLS - Wales (Principal Areas)
'Blaenau Gwent', 'Bridgend', 'Caerphilly', 'Cardiff', 'Carmarthenshire',
'Ceredigion', 'Conwy', 'Denbighshire', 'Flintshire', 'Gwynedd',
'Isle of Anglesey', 'Merthyr Tydfil', 'Monmouthshire', 'Neath Port Talbot',
'Newport', 'Pembrokeshire', 'Powys', 'Rhondda Cynon Taf', 'Swansea',
'Torfaen', 'Vale of Glamorgan', 'Wrexham',

// NI - Northern Ireland (Local Government Districts)
'Antrim and Newtownabbey', 'Ards and North Down', 'Armagh, Banbridge and Craigavon',
'Belfast', 'Causeway Coast and Glens', 'Derry and Strabane',
'Fermanagh and Omagh', 'Lisburn and Castlereagh', 'Mid and East Antrim',
'Mid Ulster', 'Newry, Mourne and Down'
];

protected $cities = [
'London', 'Birmingham', 'Manchester', 'Leeds', 'Glasgow', 'Liverpool', 'Newcastle', 'Sheffield',
'Bristol', 'Edinburgh', 'Cardiff', 'Belfast', 'Nottingham', 'Leicester', 'Coventry', 'Hull',
'Stoke-on-Trent', 'Southampton', 'Reading', 'Aberdeen', 'Cambridge', 'Oxford', 'Plymouth',
'Derby', 'Exeter', 'Brighton', 'Norwich', 'York', 'Swansea', 'Wolverhampton', 'Portsmouth',
'Dundee', 'Blackpool', 'Luton', 'Milton Keynes'
];

protected $streetNames = [
'High Street', 'Station Road', 'Church Lane', 'Park Avenue', 'London Road', 'Victoria Street',
'Green Lane', 'King\'s Road', 'Queen\'s Avenue', 'Elm Street', 'The Crescent', 'York Road',
'Main Street', 'Market Place', 'Mill Lane', 'School Lane', 'Chester Avenue', 'Parkside',
'Brook Street', 'Grange Road', 'Hilltop', 'Bridge Street', 'New Street', 'Church Road',
'Oakwood Drive', 'Maple Close', 'Meadow View', 'Waterloo Road', 'Clarence Street', 'Beechwood Avenue',
'West End', 'Northgate', 'Eastfield Road', 'Southfield Drive'
];

public function region(): string
Expand All @@ -48,4 +89,60 @@ public function county(): array
{
return $this->pickArrayRandomElement($this->counties);
}
}

public function city(): string
{
return $this->pickArrayRandomElement($this->cities);
}

public function streetName(): string
{
return $this->pickArrayRandomElement($this->streetNames);
}

public function streetAddress(): string
{
return sprintf('%d %s', $this->houseNumber(), $this->streetName());
}

public function houseNumber(): int
{
return $this->randomizer->getInt(1, 200);
}

public function postcode(): string
{
$prefixLetters = $this->randomizer->getInt(0, 1) === 0
? chr($this->randomizer->getInt(65, 90))
: chr($this->randomizer->getInt(65, 90)) . chr($this->randomizer->getInt(65, 90));

$prefixDigits = $this->randomizer->getInt(0, 2);
switch ($prefixDigits) {
case 0:
$middlePart = $this->randomizer->getInt(1, 9);
break;
case 1:
$middlePart = $this->randomizer->getInt(1, 9) . $this->randomizer->getInt(0, 9);
break;
case 2:
$middlePart = $this->randomizer->getInt(1, 9) . chr($this->randomizer->getInt(65, 90));
break;
}

$suffix = $this->randomizer->getInt(0, 9) . chr($this->randomizer->getInt(65, 90)) . chr($this->randomizer->getInt(65, 90));

return sprintf('%s%s %s', $prefixLetters, $middlePart, $suffix);
}

public function fullAddress(): string
{
return sprintf(
'%d %s, %s, %s, %s',
$this->houseNumber(),
$this->streetName(),
$this->city(),
array_values($this->county())[0],
$this->postcode()
);
}
}
18 changes: 18 additions & 0 deletions src/Extensions/CompanyExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,19 @@

class CompanyExtension extends Extension
{
private array $companies = [
"TechWave Innovations", "CloudSphere Solutions", "Quantum Nexus Labs",
"FutureSync Systems", "DigitalFoundry Inc.", "PrimeCore Investments",
"EverTrust Capital", "SafeHaven Financials", "WealthWise Group",
"BlueChip Advisors", "VitalBloom Healthcare", "PureWellness Clinics",
"MediCore Diagnostics", "BrightPath Medical", "CareUnity Group",
"GreenPeak Energy", "SolarSphere Dynamics", "EcoCurrent Solutions",
"Infinity Power Corp.", "BrightGrid Utilities", "GoldenOak Ventures",
"UrbanEdge Retail", "NextGen Supplies", "ClearSky Services",
"AnchorPoint Logistics", "IronClad Manufacturing", "SteelCore Industries",
"PrimeMotion Fabricators", "EverCraft Machineries", "Vertex Assembly Co."
];

public function vatNumber(): string
{
$prefix = 'GB';
Expand All @@ -23,4 +36,9 @@ public function companyRegistrationNumber(): string

return $this->formatString($format);
}

public function company(): string
{
return $this->pickArrayRandomElement($this->companies);
}
}
84 changes: 81 additions & 3 deletions tests/Unit/AddressExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,29 @@

namespace Xefi\Faker\EnGb\Tests\Unit;

use Random\Randomizer;
use Xefi\Faker\EnGb\Extensions\AddressExtension;
use ReflectionClass;

final class AddressExtensionTest extends TestCase
{
protected AddressExtension $addressExtension;
protected array $counties = [];
protected array $regions = [];
protected array $cities = [];
protected array $streetNames = [];

protected function setUp(): void
{
parent::setUp();

$addressExtension = new \Xefi\Faker\EnGb\Extensions\AddressExtension(new \Random\Randomizer());
$this->regions = (new ReflectionClass($addressExtension))->getProperty('regions')->getValue($addressExtension);
$this->counties = (new ReflectionClass($addressExtension))->getProperty('counties')->getValue($addressExtension);
$addressExtension = new AddressExtension(new Randomizer());
$reflection = new ReflectionClass($addressExtension);

$this->regions = $reflection->getProperty('regions')->getValue($addressExtension);
$this->counties = $reflection->getProperty('counties')->getValue($addressExtension);
$this->cities = $reflection->getProperty('cities')->getValue($addressExtension);
$this->streetNames = $reflection->getProperty('streetNames')->getValue($addressExtension);
}

public function testRegion(): void
Expand Down Expand Up @@ -43,4 +52,73 @@ public function testCounty(): void
sort($results)
);
}

public function testCities(): void
{
$results = [];
for ($i = 0; $i < count($this->cities); $i++) {
$results[] = $this->faker->unique()->city();
}

$this->assertEqualsCanonicalizing(
$this->cities,
$results
);
}

public function testStreetName(): void
{
$results = [];
for ($i = 0; $i < count($this->streetNames); $i++) {
$results[] = $this->faker->unique()->streetName();
}

$this->assertEqualsCanonicalizing(
$this->streetNames,
$results
);
}

public function testStreetAddress(): void
{
for ($i = 0; $i < 100; $i++) {
$streetAddress = $this->faker->unique()->streetAddress();

[$houseNumber, $streetName] = explode(' ', $streetAddress, 2);
$this->assertGreaterThanOrEqual(1, $houseNumber);
$this->assertLessThanOrEqual(200, $houseNumber);
$this->assertContains($streetName, $this->streetNames);
}
}

public function testHouseNumber(): void
{
for ($i = 0; $i < 100; $i++) {
$houseNumber = $this->faker->unique()->houseNumber();
$this->assertGreaterThanOrEqual(1, $houseNumber);
$this->assertLessThanOrEqual(200, $houseNumber);
}
}

public function testPostcodeFormat(): void
{
$pattern = '/^[A-Z]{1,2}[1-9][0-9A-Z]? \d[A-Z]{2}$/';

for ($i = 0; $i < 100; $i++) {
$postcode = $this->faker->postcode();
$this->assertMatchesRegularExpression($pattern, $postcode);
}
}

public function testFullAddress(): void
{
for ($i = 0; $i < 100; $i++) {
$fullAddress = $this->faker->unique()->fullAddress();

$this->assertMatchesRegularExpression(
'/^\d{1,3} .+, .+, .+, [A-Z]{1,2}[1-9][0-9A-Z]? \d[A-Z]{2}$/',
$fullAddress
);
}
}
}
27 changes: 25 additions & 2 deletions tests/Unit/CompanyExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,21 @@

namespace Xefi\Faker\EnGb\Tests\Unit;

use Xefi\Faker\Calculators\Luhn;
use Xefi\Faker\Container\Container;
use Random\Randomizer;
use Xefi\Faker\EnGb\Extensions\CompanyExtension;

final class CompanyExtensionTest extends TestCase
{
protected array $companies = [];

protected function setUp(): void
{
parent::setUp();

$companyExtension = new CompanyExtension(new Randomizer());
$this->companies = (new \ReflectionClass($companyExtension))->getProperty('companies')->getValue($companyExtension);
}

public function testVatNumber()
{
$results = [];
Expand Down Expand Up @@ -37,4 +47,17 @@ public function testCompanyRegistrationNumber()
}
}
}

public function testCompany()
{
$results = [];
for ($i = 0; $i < count($this->companies); $i++) {
$results[] = $this->faker->unique()->company();
}

$this->assertEqualsCanonicalizing(
$this->companies,
$results
);
}
}
Loading