Add unit tests for EconomicCustomerModel parsing and validation
This commit is contained in:
@@ -2,7 +2,6 @@
|
||||
|
||||
namespace customers;
|
||||
|
||||
use classes\response;
|
||||
use objects\users_o;
|
||||
|
||||
class economic_customer_mo
|
||||
@@ -33,11 +32,14 @@ class economic_customer_mo
|
||||
|
||||
}
|
||||
|
||||
public function parseCustomer($customer): static
|
||||
public function parseCustomer(mixed $customer): static
|
||||
{
|
||||
global /** @var response $response */
|
||||
$response;
|
||||
$this->customer_number = $customer->customerNumber;
|
||||
$customer_number = $this->extractCustomerNumber($customer);
|
||||
if ($customer_number === null) {
|
||||
return $this;
|
||||
}
|
||||
|
||||
$this->customer_number = $customer_number;
|
||||
$this->name = ($customer->name ?? null);
|
||||
$this->address = ($customer->address ?? null);
|
||||
$this->city = ($customer->city ?? null);
|
||||
@@ -48,6 +50,33 @@ class economic_customer_mo
|
||||
$this->currency = ($customer->currency ?? null);
|
||||
$this->country = ($customer->country ?? null);
|
||||
$this->barred = (isset($customer->barred) ? (bool)$customer->barred : false);
|
||||
$this->cacheCustomerForImportedUser();
|
||||
// Add the customer to the debug log (IF DEBUG IS ENABLED)
|
||||
//$response->add_debug_list('economic_customer', $this->customer_number, $customer);
|
||||
return $this;
|
||||
}
|
||||
|
||||
protected function extractCustomerNumber(mixed $customer): ?int
|
||||
{
|
||||
if (!is_object($customer)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$customer_number = $customer->customerNumber ?? $customer->customer_number ?? null;
|
||||
if (!is_int($customer_number) && !is_float($customer_number) && !is_string($customer_number)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$customer_number = (int)$customer_number;
|
||||
if ($customer_number <= 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $customer_number;
|
||||
}
|
||||
|
||||
protected function cacheCustomerForImportedUser(): void
|
||||
{
|
||||
$users_o = new users_o();
|
||||
$user = $users_o->getUserByCustomerNumber($this->customer_number);
|
||||
|
||||
@@ -55,9 +84,6 @@ class economic_customer_mo
|
||||
if ($user->exists()) {
|
||||
$user->cache('economic_customer', $this);
|
||||
}
|
||||
// Add the customer to the debug log (IF DEBUG IS ENABLED)
|
||||
//$response->add_debug_list('economic_customer', $this->customer_number, $customer);
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function asArray(): array
|
||||
@@ -80,4 +106,4 @@ class economic_customer_mo
|
||||
'barred' => $this->barred,
|
||||
];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user