2021-08-04 09:42:48 +02:00
|
|
|
<?php
|
|
|
|
|
|
2021-12-19 15:57:24 +01:00
|
|
|
namespace Tests\Client;
|
2021-08-04 09:42:48 +02:00
|
|
|
|
2021-12-19 15:57:24 +01:00
|
|
|
use App\Models\Brand;
|
2021-08-05 15:00:28 +02:00
|
|
|
use App\Models\Customer as ModelsCustomer;
|
2021-12-19 15:57:24 +01:00
|
|
|
use App\Models\SingleOptin;
|
2021-08-04 09:42:48 +02:00
|
|
|
use Illuminate\Support\Str;
|
2021-08-04 17:16:05 +02:00
|
|
|
use LDC\LDCCRM\CrmApiV2;
|
2021-08-05 16:18:09 +02:00
|
|
|
use LDC\LDCCRM\Enums\ApiError;
|
2021-08-04 09:42:48 +02:00
|
|
|
use LDC\LDCCRM\Enums\Country;
|
2021-12-19 15:57:24 +01:00
|
|
|
use LDC\LDCCRM\Exceptions\AccessError;
|
2021-08-05 16:18:09 +02:00
|
|
|
use LDC\LDCCRM\Exceptions\ApiErrorException;
|
2021-08-06 15:57:48 +02:00
|
|
|
use LDC\LDCCRM\Exceptions\ValidationException;
|
2021-08-04 09:42:48 +02:00
|
|
|
use LDC\LDCCRM\Models\Customer;
|
|
|
|
|
|
|
|
|
|
class CrmClientV2CustomerTest extends CrmClientTester
|
|
|
|
|
{
|
2021-12-19 15:57:24 +01:00
|
|
|
/**
|
|
|
|
|
* Test get customer prop.
|
|
|
|
|
*
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
2021-08-04 09:42:48 +02:00
|
|
|
public function testGetter()
|
|
|
|
|
{
|
|
|
|
|
$this->expectNotToPerformAssertions();
|
|
|
|
|
$client = new Customer();
|
|
|
|
|
$client->email;
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-19 15:57:24 +01:00
|
|
|
/**
|
|
|
|
|
* Test set customer prop
|
|
|
|
|
*
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
2021-08-04 09:42:48 +02:00
|
|
|
public function testSetter()
|
|
|
|
|
{
|
|
|
|
|
$this->expectNotToPerformAssertions();
|
|
|
|
|
$client = new Customer();
|
|
|
|
|
|
|
|
|
|
$client->firstname = 'toto';
|
|
|
|
|
$client->firstname;
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-19 15:57:24 +01:00
|
|
|
/**
|
|
|
|
|
* Test client wrong type.
|
|
|
|
|
*
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
2021-08-04 09:42:48 +02:00
|
|
|
public function testSetterFailWrongType()
|
|
|
|
|
{
|
2021-12-19 15:57:24 +01:00
|
|
|
$this->expectException(ValidationException::class);
|
2021-08-04 09:42:48 +02:00
|
|
|
$client = new Customer();
|
|
|
|
|
|
2021-08-04 17:17:13 +02:00
|
|
|
$client->opt_in = 'toto';
|
2021-08-04 09:42:48 +02:00
|
|
|
}
|
|
|
|
|
|
2021-12-19 15:57:24 +01:00
|
|
|
/**
|
|
|
|
|
* Test country validation.
|
|
|
|
|
*
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
2021-08-04 09:42:48 +02:00
|
|
|
public function testSetterFailWrongTypeCountry()
|
|
|
|
|
{
|
2021-12-19 15:57:24 +01:00
|
|
|
$this->expectException(ValidationException::class);
|
2021-08-04 09:42:48 +02:00
|
|
|
$client = new Customer();
|
|
|
|
|
|
|
|
|
|
$client->country = 'toto';
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-19 15:57:24 +01:00
|
|
|
/**
|
|
|
|
|
* Test set all props ok.
|
|
|
|
|
*
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
2021-08-04 09:42:48 +02:00
|
|
|
public function testPropsAllSetOk()
|
|
|
|
|
{
|
|
|
|
|
$this->expectNotToPerformAssertions();
|
|
|
|
|
$client = new Customer();
|
|
|
|
|
|
|
|
|
|
$propTypes = [
|
|
|
|
|
'hashcode' => (string)Str::uuid(),
|
|
|
|
|
'email' => 'toto@gmail.com',
|
|
|
|
|
'firstname' => 'string',
|
|
|
|
|
'lastname' => 'string',
|
|
|
|
|
'password' => 'StrongPassword!7',
|
2021-12-19 15:57:24 +01:00
|
|
|
'birth_date' => '1992-02-03',
|
|
|
|
|
'phone' => '0673891630',
|
|
|
|
|
'address' => 'string',
|
|
|
|
|
'address2' => 'string',
|
|
|
|
|
'city' => 'string',
|
|
|
|
|
'postcode' => 'string',
|
|
|
|
|
'country' => Country::afghanistan(),
|
|
|
|
|
'opt_in' => 1,
|
|
|
|
|
'status' => 1,
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
foreach ($propTypes as $prop => $type) {
|
|
|
|
|
$client->{$prop} = $type;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Test customer is json serializable.
|
|
|
|
|
*
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
public function testJsonSerialize()
|
|
|
|
|
{
|
|
|
|
|
$client = new Customer();
|
|
|
|
|
|
|
|
|
|
$propTypes = [
|
|
|
|
|
'hashcode' => (string)Str::uuid(),
|
|
|
|
|
'email' => 'toto@gmail.com',
|
|
|
|
|
'firstname' => 'string',
|
|
|
|
|
'lastname' => 'string',
|
|
|
|
|
'password' => 'StrongPassword!7',
|
|
|
|
|
'birth_date' => '1992-02-03',
|
2021-08-04 09:42:48 +02:00
|
|
|
'phone' => '0673891630',
|
|
|
|
|
'address' => 'string',
|
|
|
|
|
'address2' => 'string',
|
|
|
|
|
'city' => 'string',
|
|
|
|
|
'postcode' => 'string',
|
|
|
|
|
'country' => Country::afghanistan(),
|
2021-12-19 15:57:24 +01:00
|
|
|
'opt_in' => true,
|
2021-08-04 09:42:48 +02:00
|
|
|
'status' => true,
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
foreach ($propTypes as $prop => $type) {
|
|
|
|
|
$client->{$prop} = $type;
|
|
|
|
|
}
|
2021-12-19 15:57:24 +01:00
|
|
|
$jsonDecoded = \json_decode(\json_encode($client), true);
|
|
|
|
|
$this->assertIsArray($jsonDecoded);
|
|
|
|
|
unset($propTypes['password']);
|
|
|
|
|
$propTypes['country'] = Country::afghanistan()->value;
|
|
|
|
|
foreach ($propTypes as $key => $value) {
|
|
|
|
|
$this->assertArrayHasKey($key, $jsonDecoded);
|
|
|
|
|
$this->assertEquals($value, $jsonDecoded[$key]);
|
|
|
|
|
}
|
2021-08-04 09:42:48 +02:00
|
|
|
}
|
|
|
|
|
|
2021-12-19 15:57:24 +01:00
|
|
|
/**
|
|
|
|
|
* Test set all props fail.
|
|
|
|
|
*
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
2021-08-04 09:42:48 +02:00
|
|
|
public function testPropsAllSetNotOk()
|
|
|
|
|
{
|
2021-12-19 15:57:24 +01:00
|
|
|
$this->expectException(ValidationException::class);
|
2021-08-04 09:42:48 +02:00
|
|
|
$client = new Customer();
|
|
|
|
|
|
|
|
|
|
$propTypes = [
|
|
|
|
|
'hashcode' => (string)Str::uuid(),
|
|
|
|
|
'email' => 'toto@gmail.com',
|
|
|
|
|
'firstname' => 'string',
|
|
|
|
|
'lastname' => 'string',
|
|
|
|
|
'password' => 'StrongPassword!7',
|
2021-12-19 15:57:24 +01:00
|
|
|
'birth_date' => '1992-02-03',
|
2021-08-04 09:42:48 +02:00
|
|
|
'phone' => '0673891630',
|
|
|
|
|
'address' => 'string',
|
|
|
|
|
'address2' => 'string',
|
2021-08-06 09:46:16 +02:00
|
|
|
'city' => 18,
|
2021-08-04 09:42:48 +02:00
|
|
|
'postcode' => 'string',
|
2021-08-06 09:46:16 +02:00
|
|
|
'country' => Country::afghanistan(),
|
2021-12-19 15:57:24 +01:00
|
|
|
'opt_in' => true,
|
2021-08-04 09:42:48 +02:00
|
|
|
'status' => true,
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
foreach ($propTypes as $prop => $type) {
|
|
|
|
|
$client->{$prop} = $type;
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-08-04 17:16:05 +02:00
|
|
|
|
2021-12-19 15:57:24 +01:00
|
|
|
/**
|
|
|
|
|
* Test set unkown prop fail
|
|
|
|
|
*
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
public function testPropsAssignUnknown()
|
|
|
|
|
{
|
|
|
|
|
$this->expectException(AccessError::class);
|
|
|
|
|
|
|
|
|
|
$customer = new Customer([
|
|
|
|
|
'hashcode' => (string)Str::uuid(),
|
|
|
|
|
'email' => 'toto@gmail.com',
|
|
|
|
|
'firstname' => 'string',
|
|
|
|
|
'lastname' => 'string',
|
|
|
|
|
'password' => 'StrongPassword!7',
|
|
|
|
|
'birth_date' => '1992-02-03',
|
|
|
|
|
'phone' => '0673891630',
|
|
|
|
|
'address' => 'string',
|
|
|
|
|
'address2' => 'string',
|
|
|
|
|
'city' => 'string',
|
|
|
|
|
'postcode' => 'string',
|
|
|
|
|
'country' => Country::afghanistan(),
|
|
|
|
|
'opt_in' => true,
|
|
|
|
|
'status' => true,
|
|
|
|
|
]);
|
|
|
|
|
$this->assertNull($customer->unknownProp);
|
|
|
|
|
$customer->unknownProp = 'value';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Test set prop fail wrong type
|
|
|
|
|
*
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
public function testPropsAssignWrongType()
|
|
|
|
|
{
|
|
|
|
|
$this->expectException(ValidationException::class);
|
|
|
|
|
|
|
|
|
|
$customer = new Customer([
|
|
|
|
|
'hashcode' => (string)Str::uuid(),
|
|
|
|
|
'email' => 'toto@gmail.com',
|
|
|
|
|
'firstname' => 'string',
|
|
|
|
|
'lastname' => 'string',
|
|
|
|
|
'password' => 'StrongPassword!7',
|
|
|
|
|
'birth_date' => '1992-02-03',
|
|
|
|
|
'phone' => '0673891630',
|
|
|
|
|
'address' => 'string',
|
|
|
|
|
'address2' => 'string',
|
|
|
|
|
'city' => 'string',
|
|
|
|
|
'postcode' => 'string',
|
|
|
|
|
'country' => Country::afghanistan(),
|
|
|
|
|
'opt_in' => true,
|
|
|
|
|
'status' => true,
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$customer->country = 'value';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Test get customer from hashcode.
|
|
|
|
|
*
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
2021-08-04 17:16:05 +02:00
|
|
|
public function testCustomerGet()
|
|
|
|
|
{
|
|
|
|
|
$api = new CrmApiV2();
|
|
|
|
|
$customer = $api->getCustomerOrNew(static::$info->hashcode);
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(Customer::class, get_class($customer));
|
|
|
|
|
$this->assertEquals($customer->firstname, static::$info->firstname);
|
|
|
|
|
$this->assertEquals($customer->lastname, static::$info->lastname);
|
|
|
|
|
$this->assertEquals($customer->birth_date, static::$info->birth_date);
|
|
|
|
|
$this->assertEquals($customer->address, static::$info->address);
|
|
|
|
|
$this->assertEquals($customer->address2, static::$info->address2);
|
|
|
|
|
$this->assertEquals($customer->phone, static::$info->phone);
|
|
|
|
|
$this->assertEquals($customer->city, static::$info->city);
|
|
|
|
|
$this->assertEquals($customer->postcode, static::$info->postcode);
|
|
|
|
|
$this->assertEquals($customer->opt_in, static::$info->opt_in);
|
|
|
|
|
$this->assertEquals($customer->hashcode, static::$info->hashcode);
|
2021-12-19 15:57:24 +01:00
|
|
|
$this->assertEquals($customer->country->value, static::$info->country->value);
|
|
|
|
|
$this->assertEquals($customer->country->label, static::$info->country->label);
|
2021-08-04 17:16:05 +02:00
|
|
|
}
|
2021-08-05 15:00:28 +02:00
|
|
|
|
2021-12-19 15:57:24 +01:00
|
|
|
/**
|
|
|
|
|
* Test customer activate.
|
|
|
|
|
*
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
2021-08-05 15:00:28 +02:00
|
|
|
public function testCustomerActivate()
|
|
|
|
|
{
|
2021-12-19 15:57:24 +01:00
|
|
|
// Get non activated customer.
|
2021-08-05 15:00:28 +02:00
|
|
|
/** @var \App\Models\Customer */
|
|
|
|
|
$customer = ModelsCustomer::whereHas('logins', function ($q) {
|
|
|
|
|
$q->where('status', 0)->whereHas('brand', function ($q) {
|
|
|
|
|
$q->whereId(static::$brand->id);
|
|
|
|
|
});
|
|
|
|
|
})->firstOrFail();
|
2021-12-19 15:57:24 +01:00
|
|
|
|
|
|
|
|
$info = $customer->info(static::$brand, ModelsCustomer::FIRST_OR_FAIL);
|
2021-08-05 15:00:28 +02:00
|
|
|
|
|
|
|
|
$api = new CrmApiV2();
|
|
|
|
|
$customer = $api->getCustomerOrNew($info->hashcode);
|
|
|
|
|
|
|
|
|
|
$this->assertTrue($customer->activate());
|
2021-08-05 16:18:09 +02:00
|
|
|
$this->assertTrue($customer->status);
|
|
|
|
|
|
|
|
|
|
$this->expectException(ApiErrorException::class);
|
|
|
|
|
$this->expectExceptionMessage(ApiError::customerAlreadyActive()->label);
|
|
|
|
|
$this->assertTrue($customer->activate());
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-19 15:57:24 +01:00
|
|
|
/**
|
|
|
|
|
* Test customer update
|
|
|
|
|
*
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
2021-08-06 09:46:16 +02:00
|
|
|
public function testCustomerUpdateProps()
|
|
|
|
|
{
|
|
|
|
|
$api = new CrmApiV2();
|
|
|
|
|
$customer = $api->getCustomerOrNew(static::$info->hashcode);
|
|
|
|
|
|
|
|
|
|
$customer->firstname = 'toto';
|
|
|
|
|
$this->assertTrue($customer->save());
|
|
|
|
|
|
|
|
|
|
$customer = $api->getCustomerOrNew(static::$info->hashcode);
|
2021-10-26 09:59:15 +02:00
|
|
|
$this->assertEquals('Toto', $customer->firstname);
|
2021-08-06 09:46:16 +02:00
|
|
|
|
|
|
|
|
$customer->lastname = 'pouet';
|
|
|
|
|
$this->assertTrue($customer->save());
|
|
|
|
|
|
|
|
|
|
$customer = $api->getCustomerOrNew(static::$info->hashcode);
|
2021-10-26 09:59:15 +02:00
|
|
|
$this->assertEquals('Pouet', $customer->lastname);
|
2021-08-06 09:46:16 +02:00
|
|
|
|
2021-10-20 15:30:21 +02:00
|
|
|
$customer->country = Country::belgium();
|
2021-08-06 09:46:16 +02:00
|
|
|
$customer->save();
|
|
|
|
|
|
|
|
|
|
$customer = $api->getCustomerOrNew(static::$info->hashcode);
|
2021-10-20 15:30:21 +02:00
|
|
|
$this->assertEquals(Country::belgium(), $customer->country);
|
2021-08-06 09:46:16 +02:00
|
|
|
|
|
|
|
|
$customer->email = 'pouet@gmail.com';
|
|
|
|
|
$customer->birth_date = '1992-03-08';
|
|
|
|
|
$customer->phone = '0388977213';
|
|
|
|
|
$customer->address = '30 rue des toto';
|
|
|
|
|
$customer->address2 = 'batiment c';
|
|
|
|
|
$customer->city = 'toto ville';
|
|
|
|
|
$customer->postcode = '83160';
|
|
|
|
|
$this->assertTrue($customer->save());
|
|
|
|
|
|
|
|
|
|
$customer = $api->getCustomerOrNew(static::$info->hashcode);
|
|
|
|
|
$this->assertEquals('pouet@gmail.com', $customer->email);
|
|
|
|
|
$this->assertEquals('1992-03-08', $customer->birth_date);
|
|
|
|
|
$this->assertEquals('0388977213', $customer->phone);
|
|
|
|
|
$this->assertEquals('30 rue des toto', $customer->address);
|
|
|
|
|
$this->assertEquals('batiment c', $customer->address2);
|
|
|
|
|
$this->assertEquals('toto ville', $customer->city);
|
|
|
|
|
$this->assertEquals('83160', $customer->postcode);
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-19 15:57:24 +01:00
|
|
|
/**
|
|
|
|
|
* Test customer toggle optin.
|
|
|
|
|
*
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
2021-08-05 16:18:09 +02:00
|
|
|
public function testCustomerToggleOptin()
|
|
|
|
|
{
|
|
|
|
|
$api = new CrmApiV2();
|
|
|
|
|
$customer = $api->getCustomerOrNew(static::$info->hashcode);
|
|
|
|
|
|
2021-08-06 09:46:16 +02:00
|
|
|
$customer->opt_in = false;
|
|
|
|
|
$this->assertTrue($customer->save());
|
|
|
|
|
|
2021-08-05 16:18:09 +02:00
|
|
|
$this->assertTrue($customer->toggleOptin());
|
|
|
|
|
$this->assertTrue($customer->opt_in);
|
|
|
|
|
|
|
|
|
|
$this->assertTrue($customer->toggleOptin());
|
|
|
|
|
$this->assertFalse($customer->opt_in);
|
|
|
|
|
|
|
|
|
|
$this->assertTrue($customer->toggleOptin());
|
|
|
|
|
$this->assertTrue($customer->opt_in);
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-19 15:57:24 +01:00
|
|
|
/**
|
|
|
|
|
* test customer store
|
|
|
|
|
*
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
2021-08-06 15:57:48 +02:00
|
|
|
public function testCustomerStoreProps()
|
|
|
|
|
{
|
|
|
|
|
$customer = new Customer();
|
|
|
|
|
|
|
|
|
|
$customer->firstname = 'toto';
|
|
|
|
|
$customer->lastname = 'delarogerie';
|
|
|
|
|
$customer->email = 'toto.delarogerie@gmail.com';
|
|
|
|
|
$customer->password = 'Complic4tEd!P4assW0rd';
|
|
|
|
|
$customer->birth_date = '1992-03-08';
|
|
|
|
|
$customer->phone = '0673891160';
|
|
|
|
|
$customer->address = 'rue des totos';
|
|
|
|
|
$customer->city = 'Toto ville';
|
|
|
|
|
$customer->postcode = '72160';
|
|
|
|
|
$customer->country = Country::france();
|
2021-08-05 16:18:09 +02:00
|
|
|
|
2021-08-06 15:57:48 +02:00
|
|
|
$this->assertTrue($customer->save());
|
2021-08-05 16:18:09 +02:00
|
|
|
|
2021-10-26 12:37:21 +02:00
|
|
|
$this->assertEquals('Toto', $customer->firstname);
|
2021-08-06 15:57:48 +02:00
|
|
|
$this->assertEquals(null, $customer->password);
|
|
|
|
|
}
|
2021-08-05 16:18:09 +02:00
|
|
|
|
2021-12-19 15:57:24 +01:00
|
|
|
/**
|
|
|
|
|
* test customer login
|
|
|
|
|
*
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
2021-08-12 08:39:51 +02:00
|
|
|
public function testCustomerLogin()
|
|
|
|
|
{
|
|
|
|
|
$api = new CrmApiV2();
|
|
|
|
|
$customer = $api->getCustomerOrNew('toto.delarogerie@gmail.com');
|
|
|
|
|
$this->assertTrue($customer->activate());
|
|
|
|
|
$this->assertTrue($customer->status);
|
|
|
|
|
|
|
|
|
|
$this->assertTrue(Customer::login($customer->email, 'Complic4tEd!P4assW0rd'));
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-19 15:57:24 +01:00
|
|
|
/**
|
|
|
|
|
* test customer login does not belong to brand.
|
|
|
|
|
*
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
public function testCustomerLoginUsingCustomerfromAnotherBrand()
|
|
|
|
|
{
|
|
|
|
|
$this->expectException(ApiErrorException::class);
|
|
|
|
|
$this->expectExceptionCode(ApiError::authFailed());
|
|
|
|
|
$email = Str::uuid() . 'loginfromantoherbrand@hotmail.fr';
|
|
|
|
|
$data = [
|
|
|
|
|
'firstname' => 'Jean2',
|
|
|
|
|
'lastname' => 'Bond2',
|
|
|
|
|
'birth_date' => \date('1992-03-09'),
|
|
|
|
|
'address' => '10 rue des toto',
|
|
|
|
|
'email' => $email,
|
|
|
|
|
'password' => '123.Mot.De.P4sseNew',
|
|
|
|
|
'phone' => '+33654821563',
|
|
|
|
|
'country' => Country::luxembourg()->value,
|
|
|
|
|
'city' => 'Connerré2',
|
|
|
|
|
'postcode' => '72161',
|
|
|
|
|
'opt_in' => 0
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
$brand = Brand::where('id', '!=', static::$brand->id)->firstOrFail();
|
|
|
|
|
|
|
|
|
|
$this->createNewCustomer($data, $brand);
|
|
|
|
|
|
|
|
|
|
$api = new CrmApiV2();
|
|
|
|
|
$crmCustomer = $api->getCustomerOrNew($email);
|
|
|
|
|
$this->assertNull($crmCustomer->hashcode);
|
|
|
|
|
|
|
|
|
|
CrmApiV2::login($email, '123.Mot.De.P4sseNew');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Test prop store fail.
|
|
|
|
|
*
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
2021-08-06 15:57:48 +02:00
|
|
|
public function testCustomerStorePropsFails()
|
|
|
|
|
{
|
|
|
|
|
$this->expectException(ValidationException::class);
|
|
|
|
|
$customer = new Customer();
|
|
|
|
|
|
|
|
|
|
$customer->firstname = 'toto';
|
|
|
|
|
$customer->lastname = 'delarogerie';
|
|
|
|
|
$customer->email = 'toto.delarogerie@gmail.com';
|
|
|
|
|
$customer->password = 'simplepassword';
|
|
|
|
|
$customer->birth_date = '1992-03-08';
|
|
|
|
|
$customer->phone = '0673891160';
|
|
|
|
|
$customer->address = 'rue des totos';
|
|
|
|
|
$customer->city = 'Toto ville';
|
|
|
|
|
$customer->postcode = '72160';
|
|
|
|
|
$customer->country = Country::france();
|
|
|
|
|
|
|
|
|
|
$this->assertTrue($customer->save());
|
2021-08-05 16:18:09 +02:00
|
|
|
|
2021-08-06 15:57:48 +02:00
|
|
|
$this->assertEquals('toto', $customer->firstname);
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-19 15:57:24 +01:00
|
|
|
/**
|
|
|
|
|
* Create a new customer
|
|
|
|
|
* then save it into crm
|
|
|
|
|
* then deletes it from crm
|
|
|
|
|
* then try again to fetch it
|
|
|
|
|
* check if it exists.
|
|
|
|
|
*
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
2021-08-06 15:57:48 +02:00
|
|
|
public function testCustomerDelete()
|
|
|
|
|
{
|
2021-12-19 15:57:24 +01:00
|
|
|
$customer = new Customer();
|
|
|
|
|
|
|
|
|
|
$customer->firstname = 'toto';
|
|
|
|
|
$customer->lastname = 'delarogerie';
|
|
|
|
|
$customer->email = 'delete.delarogerie@gmail.com';
|
|
|
|
|
$customer->password = 'Complic4tEd!P4assW0rd';
|
|
|
|
|
$customer->birth_date = '1992-03-08';
|
|
|
|
|
$customer->phone = '0673891160';
|
|
|
|
|
$customer->address = 'rue des totos';
|
|
|
|
|
$customer->city = 'Toto ville';
|
|
|
|
|
$customer->postcode = '72160';
|
|
|
|
|
$customer->country = Country::france();
|
|
|
|
|
|
|
|
|
|
$this->assertTrue($customer->save());
|
2021-08-06 15:57:48 +02:00
|
|
|
|
|
|
|
|
$this->assertTrue($customer->delete());
|
|
|
|
|
|
2021-12-19 15:57:24 +01:00
|
|
|
$api = new CrmApiV2();
|
|
|
|
|
$customer = $api->getCustomerOrNew('delete.delarogerie@gmail.com');
|
|
|
|
|
|
|
|
|
|
// * If hash code is null then it is a new customer
|
2021-08-06 15:57:48 +02:00
|
|
|
$this->assertEquals(null, $customer->hashcode);
|
|
|
|
|
}
|
2021-12-19 15:57:24 +01:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Make sute a customer exists and contains data
|
|
|
|
|
*
|
2023-06-23 17:18:59 +02:00
|
|
|
* @param array $data
|
|
|
|
|
* @param \App\Models\Brand $brand
|
2021-12-19 15:57:24 +01:00
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
private function createNewCustomer(array $data, Brand $brand)
|
|
|
|
|
{
|
|
|
|
|
$customer = \App\Models\Customer::whereEmail($data['email'])
|
|
|
|
|
->whereHas('brands', function ($q) use ($brand) {
|
|
|
|
|
$q->where('brands.id', '=', $brand->id);
|
|
|
|
|
})->firstOrNew();
|
|
|
|
|
$customer->fill($data);
|
|
|
|
|
$saved = $customer->save();
|
|
|
|
|
|
|
|
|
|
// * Delete Single optin if exists.
|
|
|
|
|
/** @var \App\Models\SingleOptin|null $singleOptin */
|
2023-06-26 15:10:28 +02:00
|
|
|
$singleOptin = SingleOptin::query()->where('email', $data['email'])
|
2021-12-19 15:57:24 +01:00
|
|
|
->whereHas('brand', function ($q) use ($brand) {
|
|
|
|
|
$q->where('brands.id', '=', $brand->id);
|
|
|
|
|
})->first();
|
|
|
|
|
if ($singleOptin) {
|
|
|
|
|
$saved = $singleOptin->delete() && $saved;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** @var \App\Models\CustomerInfo */
|
|
|
|
|
$customerInfo = $customer->info($brand, \App\Models\Customer::FIRST_OR_NEW);
|
|
|
|
|
$customerInfo->fill($data);
|
|
|
|
|
$customerInfo->brand()->associate($brand);
|
|
|
|
|
$customerInfo->customer()->associate($customer);
|
|
|
|
|
$saved = $customerInfo->save() && $saved;
|
|
|
|
|
|
|
|
|
|
/** @var \App\Models\CustomerLogin */
|
|
|
|
|
$customerLogin = $customer->login($brand, \App\Models\Customer::FIRST_OR_NEW);
|
|
|
|
|
$customerLogin->fill($data);
|
|
|
|
|
$customerLogin->brand()->associate($brand);
|
|
|
|
|
$customerLogin->customer()->associate($customer);
|
|
|
|
|
$saved = $customerLogin->save() && $saved;
|
|
|
|
|
|
|
|
|
|
$this->assertTrue($saved, 'A new customer should have been created.');
|
|
|
|
|
}
|
2021-08-04 09:42:48 +02:00
|
|
|
}
|