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 default value for houseNumber. #24

Merged
merged 5 commits into from
Jul 4, 2023
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
8 changes: 4 additions & 4 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -554,9 +554,9 @@ protected function getParcelData(
$parcelData = array_merge($parcelData, [
'name' => $shippingAddress->getName(),
'company_name' => $shippingAddress->getCompanyName() ?? '',
'address' => $shippingAddress->getStreet(),
'address' => $shippingAddress->getAddressLine1(),
'address_2' => $shippingAddress->getAddressLine2() ?? '',
'house_number' => $shippingAddress->getHouseNumber(),
'house_number' => $shippingAddress->getHouseNumber() ?? '',
'city' => $shippingAddress->getCity(),
'postal_code' => $shippingAddress->getPostalCode(),
'country' => $shippingAddress->getCountryCode(),
Expand Down Expand Up @@ -643,9 +643,9 @@ protected function getParcelData(
$parcelData = array_merge($parcelData, [
'from_name' => $senderAddress->getName(),
'from_company_name' => $senderAddress->getCompanyName() ?? '',
'from_address_1' => $senderAddress->getStreet(),
'from_address_1' => $senderAddress->getAddressLine1(),
'from_address_2' => $senderAddress->getAddressLine2() ?? '',
'from_house_number' => $senderAddress->getHouseNumber(),
'from_house_number' => $senderAddress->getHouseNumber() ?? '',
'from_city' => $senderAddress->getCity(),
'from_postal_code' => $senderAddress->getPostalCode(),
'from_country' => $senderAddress->getCountryCode(),
Expand Down
43 changes: 30 additions & 13 deletions src/Model/Address.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,43 @@

class Address
{
/** Street parsed from address by Sendcloud. */
protected ?string $street = null;

public static function fromParcelData(array $data): self
{
return new self(
$address = new self(
(string)$data['name'],
(string)$data['company_name'],
(string)$data['address_divided']['street'],
(string)$data['address_divided']['house_number'],
(string)$data['address'],
(string)$data['city'],
(string)$data['postal_code'],
(string)$data['country']['iso_2'],
(string)$data['email'],
(string)$data['address_divided']['house_number'],
((string)$data['telephone'] ?: null),
((string)$data['address_2'] ?: null),
((string)$data['to_state'] ?: null)
);

$address->street = (string)$data['address_divided']['street'];

return $address;
}

/**
* @param string $addressLine1 Full address line 1. Includes house number unless explicitly specifying {@see $houseNumber}.
* @param string|null $houseNumber Will be added onto {@see $addressLine1}. Leave out if {@see $addressLine1} already contains a house number.
*/
public function __construct(
protected string $name,
protected ?string $companyName,
protected string $street,
protected string $houseNumber,
protected string $addressLine1,
protected string $city,
protected string $postalCode,
protected string $countryCode,
protected string $emailAddress,
protected ?string $houseNumber = null,
protected ?string $phoneNumber = null,
protected ?string $addressLine2 = null,
protected ?string $countryStateCode = null
Expand All @@ -46,14 +57,9 @@ public function getCompanyName(): ?string
return $this->companyName;
}

public function getStreet(): string
public function getAddressLine1(): string
{
return $this->street;
}

public function getHouseNumber(): string
{
return $this->houseNumber;
return $this->addressLine1;
}

public function getCity(): string
Expand All @@ -76,6 +82,16 @@ public function getEmailAddress(): string
return $this->emailAddress;
}

public function getStreet(): ?string
{
return $this->street;
}

public function getHouseNumber(): ?string
{
return $this->houseNumber;
}

public function getPhoneNumber(): ?string
{
return $this->phoneNumber;
Expand Down Expand Up @@ -110,11 +126,12 @@ public function toArray(): array
'countryCode' => $this->getCountryCode(),
'displayName' => $this->getDisplayName(),
'emailAddress' => $this->getEmailAddress(),
'houseNumber' => $this->getHouseNumber(),
'name' => $this->getName(),
'phoneNumber' => $this->getPhoneNumber(),
'postalCode' => $this->getPostalCode(),
'address' => $this->getAddressLine1(),
'street' => $this->getStreet(),
'houseNumber' => $this->getHouseNumber(),
'countryStateCode' => $this->getCountryStateCode(),
];
}
Expand Down
12 changes: 6 additions & 6 deletions test/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,15 @@ public function testGetUser(): void
$this->guzzleClientMock->expects($this->once())->method('request')->willReturn(new Response(
200,
[],
'{"user":{"address":"Insulindelaan","city":"Eindhoven","company_logo":null,"company_name":"SendCloud","data":[],"email":"[email protected]","invoices":[{"date":"05-06-201811:58:52","id":1,"isPayed":false,"items":"https://local.sendcloud.sc/api/v2/user/invoices/1","price_excl":77.4,"price_incl":93.65,"ref":"1","type":"periodic"}],"modules":[{"activated":true,"id":5,"name":"SendCloudClient","settings":null,"short_name":"sendcloud_client"},{"id":3,"name":"PrestashopIntegration","settings":{"url_webshop":"http://localhost/testing/prestashop","api_key":"O8ALXHMM24QULWM213CC6SGQ5VDJKC8W"},"activated":true,"short_name":"prestashop"}],"postal_code":"5642CV","registered":"2018-05-2912:52:51","telephone":"+31626262626","username":"johndoe"}}'
'{"user":{"address":"Insulindelaan 115","city":"Eindhoven","company_logo":null,"company_name":"SendCloud","data":[],"email":"[email protected]","invoices":[{"date":"05-06-201811:58:52","id":1,"isPayed":false,"items":"https://local.sendcloud.sc/api/v2/user/invoices/1","price_excl":77.4,"price_incl":93.65,"ref":"1","type":"periodic"}],"modules":[{"activated":true,"id":5,"name":"SendCloudClient","settings":null,"short_name":"sendcloud_client"},{"id":3,"name":"PrestashopIntegration","settings":{"url_webshop":"http://localhost/testing/prestashop","api_key":"O8ALXHMM24QULWM213CC6SGQ5VDJKC8W"},"activated":true,"short_name":"prestashop"}],"postal_code":"5642CV","registered":"2018-05-2912:52:51","telephone":"+31626262626","username":"johndoe"}}'
));

$user = $this->client->getUser();

$this->assertEquals('johndoe', $user->getUsername());
$this->assertEquals('SendCloud', $user->getCompanyName());
$this->assertEquals('+31626262626', $user->getPhoneNumber());
$this->assertEquals('Insulindelaan', $user->getAddress());
$this->assertEquals('Insulindelaan 115', $user->getAddress());
$this->assertEquals('Eindhoven', $user->getCity());
$this->assertEquals('5642CV', $user->getPostalCode());
$this->assertEquals('[email protected]', $user->getEmailAddress());
Expand Down Expand Up @@ -276,7 +276,7 @@ public function testCreateParcelCustoms(): void
});

$parcel = $this->client->createParcel(
new Address('Dr. Coffee', null, 'Street', '123', 'Place', '7837', 'BM', '[email protected]', null, 'Unit 83'),
new Address('Dr. Coffee', null, 'Street', 'Place', '7837', 'BM', '[email protected]', '123', null, 'Unit 83'),
null,
null,
null,
Expand Down Expand Up @@ -320,11 +320,11 @@ public function testUpdateParcel(): void
return new Response(
200,
[],
'{"parcel":{"id":8293794,"address":"Rosebud 2134A","address_2":"Above the skies","address_divided":{"street":"Rosebud","house_number":"2134"},"city":"Almanda","company_name":"Some company","country":{"iso_2":"NL","iso_3":"NLD","name":"Netherlands"},"data":{},"date_created":"11-03-2019 14:35:10","email":"[email protected]","name":"Completely different person","postal_code":"9238DD","reference":"0","shipment":null,"status":{"id":999,"message":"No label"},"to_service_point":null,"telephone":"+31699999999","tracking_number":"","weight":"2.490","label":{},"customs_declaration":{},"order_number":"201900001","insured_value":0,"total_insured_value":0,"to_state":"CS","customs_invoice_nr":"","customs_shipment_type":null,"parcel_items":[],"type":null,"shipment_uuid":"7ade61ad-c21a-4beb-b7fd-2f579feacdb6","shipping_method":null,"external_order_id":"8293794","external_shipment_id":"201900001"}}'
'{"parcel":{"id":8293794,"address":"Rosebud 2134 A","address_2":"Above the skies","address_divided":{"street":"Rosebud","house_number":"2134"},"city":"Almanda","company_name":"Some company","country":{"iso_2":"NL","iso_3":"NLD","name":"Netherlands"},"data":{},"date_created":"11-03-2019 14:35:10","email":"[email protected]","name":"Completely different person","postal_code":"9238DD","reference":"0","shipment":null,"status":{"id":999,"message":"No label"},"to_service_point":null,"telephone":"+31699999999","tracking_number":"","weight":"2.490","label":{},"customs_declaration":{},"order_number":"201900001","insured_value":0,"total_insured_value":0,"to_state":"CS","customs_invoice_nr":"","customs_shipment_type":null,"parcel_items":[],"type":null,"shipment_uuid":"7ade61ad-c21a-4beb-b7fd-2f579feacdb6","shipping_method":null,"external_order_id":"8293794","external_shipment_id":"201900001"}}'
);
});

$parcel = $this->client->updateParcel(8293794, new Address('Completely different person', 'Some company', 'Rosebud', '2134A', 'Almanda', '9238DD', 'NL', '[email protected]', '+31699999999', 'Above the skies', 'CS'));
$parcel = $this->client->updateParcel(8293794, new Address('Completely different person', 'Some company', 'Rosebud', 'Almanda', '9238DD', 'NL', '[email protected]', '2134A', '+31699999999', 'Above the skies', 'CS'));

$this->assertEquals('Some company', $parcel->getAddress()->getCompanyName());
}
Expand All @@ -334,7 +334,7 @@ public function testCreateLabel(): void
$this->guzzleClientMock->expects($this->once())->method('request')->willReturn(new Response(
200,
[],
'{"parcel":{"id":8293794,"address":"Rosebud 2134A","address_2":"","address_divided":{"street":"Rosebud","house_number":"2134"},"city":"Almanda","company_name":"Some company","country":{"iso_2":"NL","iso_3":"NLD","name":"Netherlands"},"data":{},"date_created":"11-03-2019 14:35:10","email":"[email protected]","name":"Completely different person","postal_code":"9238 DD","reference":"0","shipment":{"id":117,"name":"DHLForYou Drop Off"},"status":{"id":1000,"message":"Ready to send"},"to_service_point":null,"telephone":"+31699999999","tracking_number":"JVGL4004421100020097","weight":"2.490","label":{"label_printer":"https://panel.sendcloud.sc/api/v2/labels/label_printer/8293794","normal_printer":["https://panel.sendcloud.sc/api/v2/labels/normal_printer/8293794?start_from=0","https://panel.sendcloud.sc/api/v2/labels/normal_printer/8293794?start_from=1","https://panel.sendcloud.sc/api/v2/labels/normal_printer/8293794?start_from=2","https://panel.sendcloud.sc/api/v2/labels/normal_printer/8293794?start_from=3"]},"customs_declaration":{},"order_number":"201900001","insured_value":0,"total_insured_value":0,"to_state":null,"customs_invoice_nr":"","customs_shipment_type":null,"parcel_items":[],"type":"parcel","shipment_uuid":"7ade61ad-c21a-4beb-b7fd-2f579feacdb6","shipping_method":117,"external_order_id":"8293794","external_shipment_id":"201900001","carrier":{"code":"dhl"},"tracking_url":"https://jouwweb.shipping-portal.com/tracking/?country=nl&tracking_number=jvgl4004421100020097&postal_code=9238dd"}}'
'{"parcel":{"id":8293794,"address":"Rosebud 2134 A","address_2":"","address_divided":{"street":"Rosebud","house_number":"2134"},"city":"Almanda","company_name":"Some company","country":{"iso_2":"NL","iso_3":"NLD","name":"Netherlands"},"data":{},"date_created":"11-03-2019 14:35:10","email":"[email protected]","name":"Completely different person","postal_code":"9238 DD","reference":"0","shipment":{"id":117,"name":"DHLForYou Drop Off"},"status":{"id":1000,"message":"Ready to send"},"to_service_point":null,"telephone":"+31699999999","tracking_number":"JVGL4004421100020097","weight":"2.490","label":{"label_printer":"https://panel.sendcloud.sc/api/v2/labels/label_printer/8293794","normal_printer":["https://panel.sendcloud.sc/api/v2/labels/normal_printer/8293794?start_from=0","https://panel.sendcloud.sc/api/v2/labels/normal_printer/8293794?start_from=1","https://panel.sendcloud.sc/api/v2/labels/normal_printer/8293794?start_from=2","https://panel.sendcloud.sc/api/v2/labels/normal_printer/8293794?start_from=3"]},"customs_declaration":{},"order_number":"201900001","insured_value":0,"total_insured_value":0,"to_state":null,"customs_invoice_nr":"","customs_shipment_type":null,"parcel_items":[],"type":"parcel","shipment_uuid":"7ade61ad-c21a-4beb-b7fd-2f579feacdb6","shipping_method":117,"external_order_id":"8293794","external_shipment_id":"201900001","carrier":{"code":"dhl"},"tracking_url":"https://jouwweb.shipping-portal.com/tracking/?country=nl&tracking_number=jvgl4004421100020097&postal_code=9238dd"}}'
));

$parcel = $this->client->createLabel(8293794, 117, 61361);
Expand Down
2 changes: 2 additions & 0 deletions test/UtilityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ public function testParseWebhookRequest(): void
$event = $client->parseWebhookRequest($request);

$this->assertEquals(WebhookEvent::TYPE_PARCEL_STATUS_CHANGED, $event->getType());
$this->assertEquals('Insulindelaan 115', $event->getParcel()->getAddress()->getAddressLine1());
$this->assertEquals('Insulindelaan', $event->getParcel()->getAddress()->getStreet());
$this->assertEquals('115', $event->getParcel()->getAddress()->getHouseNumber());
$this->assertEquals(new \DateTimeImmutable('2018-05-02 14:38:05.993'), $event->getCreated());
$this->assertCount(1, $event->getPayload());
$this->assertArrayHasKey('parcel', $event->getPayload());
Expand Down