ldc-crm/tests/Unit/CrmClientV2CustomerTest.php
2021-10-26 12:37:21 +02:00

272 lines
9 KiB
PHP

<?php
namespace Tests\Unit;
use App\Models\Customer as ModelsCustomer;
use Illuminate\Support\Str;
use LDC\LDCCRM\CrmApiV2;
use LDC\LDCCRM\Enums\ApiError;
use LDC\LDCCRM\Enums\Country;
use LDC\LDCCRM\Exceptions\ApiErrorException;
use LDC\LDCCRM\Exceptions\ValidationException;
use LDC\LDCCRM\Models\Customer;
use TypeError;
class CrmClientV2CustomerTest extends CrmClientTester
{
public function testGetter()
{
$this->expectNotToPerformAssertions();
$client = new Customer();
$client->email;
}
public function testSetter()
{
$this->expectNotToPerformAssertions();
$client = new Customer();
$client->firstname = 'toto';
$client->firstname;
}
public function testSetterFailWrongType()
{
$this->expectException(TypeError::class);
$client = new Customer();
$client->opt_in = 'toto';
}
public function testSetterFailWrongTypeCountry()
{
$this->expectException(TypeError::class);
$client = new Customer();
$client->country = 'toto';
}
public function testPropsAllSetOk()
{
$this->expectNotToPerformAssertions();
$client = new Customer();
$propTypes = [
'hashcode' => (string)Str::uuid(),
'email' => 'toto@gmail.com',
'firstname' => 'string',
'lastname' => 'string',
'password' => 'StrongPassword!7',
'birthdate' => '1992-02-03',
'phone' => '0673891630',
'address' => 'string',
'address2' => 'string',
'city' => 'string',
'postcode' => 'string',
'country' => Country::afghanistan(),
'optin' => true,
'status' => true,
];
foreach ($propTypes as $prop => $type) {
$client->{$prop} = $type;
}
}
public function testPropsAllSetNotOk()
{
$this->expectException(TypeError::class);
$client = new Customer();
$propTypes = [
'hashcode' => (string)Str::uuid(),
'email' => 'toto@gmail.com',
'firstname' => 'string',
'lastname' => 'string',
'password' => 'StrongPassword!7',
'birthdate' => '1992-02-03',
'phone' => '0673891630',
'address' => 'string',
'address2' => 'string',
'city' => 18,
'postcode' => 'string',
'country' => Country::afghanistan(),
'optin' => true,
'status' => true,
];
foreach ($propTypes as $prop => $type) {
$client->{$prop} = $type;
}
}
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);
$this->assertEquals($customer->country->label, static::$info->country);
$this->assertEquals($customer->country->value, static::$info->newcountry->value);
}
public function testCustomerActivate()
{
// Get non activated customer
/** @var \App\Models\Customer */
$customer = ModelsCustomer::whereHas('logins', function ($q) {
$q->where('status', 0)->whereHas('brand', function ($q) {
$q->whereId(static::$brand->id);
});
})->firstOrFail();
$login = $customer->logins()->where('status', 0)->first();
/** @var \App\Models\Brand */
$brand = $login->brand;
/** @var \App\Models\CustomerInfo */
$info = $customer->info($brand, ModelsCustomer::FIRST_OR_FAIL);
$api = new CrmApiV2();
$customer = $api->getCustomerOrNew($info->hashcode);
$this->assertTrue($customer->activate());
$this->assertTrue($customer->status);
$this->expectException(ApiErrorException::class);
$this->expectExceptionMessage(ApiError::customerAlreadyActive()->label);
$this->assertTrue($customer->activate());
}
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);
$this->assertEquals('Toto', $customer->firstname);
$customer->lastname = 'pouet';
$this->assertTrue($customer->save());
$customer = $api->getCustomerOrNew(static::$info->hashcode);
$this->assertEquals('Pouet', $customer->lastname);
$customer->country = Country::belgium();
$customer->save();
$customer = $api->getCustomerOrNew(static::$info->hashcode);
$this->assertEquals(Country::belgium(), $customer->country);
$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);
}
public function testCustomerToggleOptin()
{
$api = new CrmApiV2();
$customer = $api->getCustomerOrNew(static::$info->hashcode);
$customer->opt_in = false;
$this->assertTrue($customer->save());
$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);
}
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();
$this->assertTrue($customer->save());
$this->assertEquals('Toto', $customer->firstname);
$this->assertEquals(null, $customer->password);
}
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'));
}
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());
$this->assertEquals('toto', $customer->firstname);
}
public function testCustomerDelete()
{
$api = new CrmApiV2();
$customer = $api->getCustomer('toto.delarogerie@gmail.com');
$this->assertTrue($customer->delete());
$customer = $api->getCustomerOrNew('toto.delarogerie@gmail.com');
$this->assertEquals(null, $customer->hashcode);
}
}