Skip to content

Commit 8d3f484

Browse files
authored
Merge pull request #42 from netresearch/CI-241
Fix building addresses with province
2 parents 7a7db57 + dbd6391 commit 8d3f484

File tree

2 files changed

+54
-1
lines changed

2 files changed

+54
-1
lines changed

src/Customer/AddressBuilder.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,14 @@ public static function anAddress(
4444

4545
$faker = FakerFactory::create($locale);
4646
$countryCode = substr($locale, -2);
47-
$regionId = $objectManager->create(Region::class)->loadByName($faker->state, $countryCode)->getId();
47+
48+
try {
49+
$region = $faker->province;
50+
} catch (\InvalidArgumentException $exception) {
51+
$region = $faker->state;
52+
}
53+
54+
$regionId = $objectManager->create(Region::class)->loadByName($region, $countryCode)->getId();
4855

4956
/** @var AddressInterface $address */
5057
$address = $objectManager->create(AddressInterface::class);

tests/Sales/OrderBuilderTest.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,4 +235,50 @@ public function createMultipleOrders()
235235
$customerIds[$orderWithCustomerFixture->getCustomerId()] = 1;
236236
self::assertCount(3, $customerIds);
237237
}
238+
239+
/**
240+
* Create orders for faker addresses with either state or province. Assert both types have a `region_id` assigned.
241+
*
242+
* @test
243+
* @throws \Exception
244+
*/
245+
public function createIntlOrders()
246+
{
247+
$atLocale = 'de_AT';
248+
$atOrder = OrderBuilder::anOrder()
249+
->withCustomer(
250+
CustomerBuilder::aCustomer()->withAddresses(
251+
AddressBuilder::anAddress(null, $atLocale)->asDefaultBilling()->asDefaultShipping()
252+
)
253+
)
254+
->build();
255+
$this->orderFixtures[] = new OrderFixture($atOrder);
256+
257+
$usLocale = 'en_US';
258+
$usOrder = OrderBuilder::anOrder()
259+
->withCustomer(
260+
CustomerBuilder::aCustomer()->withAddresses(
261+
AddressBuilder::anAddress(null, $usLocale)->asDefaultBilling()->asDefaultShipping()
262+
)
263+
)
264+
->build();
265+
$this->orderFixtures[] = new OrderFixture($usOrder);
266+
267+
$caLocale = 'en_CA';
268+
$caOrder = OrderBuilder::anOrder()
269+
->withCustomer(
270+
CustomerBuilder::aCustomer()->withAddresses(
271+
AddressBuilder::anAddress(null, $caLocale)->asDefaultBilling()->asDefaultShipping()
272+
)
273+
)
274+
->build();
275+
$this->orderFixtures[] = new OrderFixture($caOrder);
276+
277+
self::assertSame(substr($atLocale, 3, 4), $atOrder->getBillingAddress()->getCountryId());
278+
self::assertNotEmpty($atOrder->getBillingAddress()->getRegionId());
279+
self::assertSame(substr($usLocale, 3, 4), $usOrder->getBillingAddress()->getCountryId());
280+
self::assertNotEmpty($usOrder->getBillingAddress()->getRegionId());
281+
self::assertSame(substr($caLocale, 3, 4), $caOrder->getBillingAddress()->getCountryId());
282+
self::assertNotEmpty($caOrder->getBillingAddress()->getRegionId());
283+
}
238284
}

0 commit comments

Comments
 (0)