ldc-crm/tests/Client/CrmClientSingleOptinTest.php
2025-09-08 14:59:53 +02:00

114 lines
3.9 KiB
PHP

<?php
namespace Tests\Client;
use App\Models\SingleOptin as ModelsSingleOptin;
use Illuminate\Support\Str;
use LDC\LDCCRM\CrmApiV4;
use LDC\LDCCRM\Enums\Country;
use LDC\LDCCRM\Models\Customer;
use LDC\LDCCRM\Models\SingleOptin;
class CrmClientSingleOptinTest extends CrmClientTester
{
/** @var string $email */
private $email = 'singleoptin@hotmail.fr';
/**
* Create and remove single optin test.
* Calling multiple times single optin true should not fail.
*
* @return void
*/
public function testCreateAndRemoveSingleOptin(): void
{
// Set single optin and check.
$this->assertTrue(SingleOptin::setStatus($this->email, true));
$this->assertTrue(ModelsSingleOptin::query()->where('email', $this->email)->exists());
// Set single optin again should not fail.
$this->assertTrue(CrmApiV4::setStatus($this->email, true));
// Remove single optin test and check.
$this->assertTrue(SingleOptin::setStatus($this->email, false));
$this->assertTrue(!ModelsSingleOptin::query()->where('email', $this->email)->exists());
}
/**
* Test set single optin on when customer exists.
*
* @return void
*/
public function testSetSingleOptinWhenCustomerExists()
{
$randomMail = Str::uuid() . 'totosingleoptin.delarogerie@gmail.com';
$customer = new Customer();
$customer->fill([
'firstname' => 'toto singleoptin',
'lastname' => 'delarogerie',
'email' => $randomMail,
'password' => 'Complic4tEd!P4assW0rd',
'birth_date' => '1992-03-08',
'phone' => '0673891160',
'address' => 'rue des totos',
'city' => 'Toto ville',
'postcode' => '72160',
'country' => Country::france->value(),
'opt_in' => false,
]);
$this->assertTrue($customer->save());
$this->assertTrue(SingleOptin::setStatus($randomMail, true));
$this->assertTrue(Customer::findWithEmail($randomMail)->opt_in);
}
/**
* Remove single optin with hashcode test.
* Calling multiple times single optin true should not fail.
*
* @return void
*/
public function testRemoveSingleOptinWithHashcode(): void
{
// Set single optin and check.
$this->assertTrue(SingleOptin::setStatus($this->email, true));
$this->assertTrue(ModelsSingleOptin::query()->where('email', $this->email)->exists());
$hashcode = ModelsSingleOptin::query()
->where('email', $this->email)->sole('hashcode')->hashcode;
// Remove single optin test and check.
$this->assertTrue(SingleOptin::unsetStatusWithHashcode($hashcode));
$this->assertTrue(!ModelsSingleOptin::query()->where('email', $this->email)->exists());
}
/**
* Remove single optin with hashcode test
*
* Calling multiple times single optin true should not fail.
*
* @return void
*/
public function testRemoveSingleOptinWithHashcodeWhenCustomerExists()
{
$randomMail = Str::uuid() . 'totosingleoptin.delarogerie@gmail.com';
$customer = new Customer();
$customer->fill([
'firstname' => 'toto singleoptin',
'lastname' => 'delarogerie',
'email' => $randomMail,
'password' => 'Complic4tEd!P4assW0rd',
'birth_date' => '1992-03-08',
'phone' => '0673891160',
'address' => 'rue des totos',
'city' => 'Toto ville',
'postcode' => '72160',
'country' => Country::france->value(),
'opt_in' => true,
]);
$this->assertTrue($customer->save());
$this->assertTrue(SingleOptin::setStatus($randomMail, false));
$this->assertFalse(Customer::findWithEmail($randomMail)->opt_in);
}
}