toString(); $libCustomer = new Customer([ 'hashcode' => $uuid, 'email' => 'john@example.com', 'password' => 'Secret123!', 'firstname' => 'John', 'lastname' => 'Doe', 'birth_date' => '1990-01-01', 'phone' => '0123456789', 'address' => '123 Main St', 'address2' => null, 'city' => 'Paris', 'postcode' => '75000', 'country' => EnumsCountry::france, 'opt_in' => true, 'status' => true, 'last_connection' => null, ]); $cacheKey = "crm_customer_hashcode_{$uuid}"; $cacheRepository->shouldReceive('remember') ->once() ->with($cacheKey, \Mockery::any(), \Mockery::on(function (\Closure $closure) use ($crmApi, $libCustomer, $uuid) { $crmApi->shouldReceive('getCustomer')->with((string) $uuid)->once()->andReturn($libCustomer); $result = $closure(); return $result instanceof CrmCustomer && $result->hashcode === (string) $uuid; })) ->andReturn(new CrmCustomer( hashcode: (string) $uuid, email: 'john@example.com', password: 'Secret123!', firstname: 'John', lastname: 'Doe', birthDate: '1990-01-01', phone: '0123456789', address: '123 Main St', address2: null, city: 'Paris', postcode: '75000', country: Country::france, optIn: true, status: true, lastConnection: null, )); $service = new CrmService($crmApi, $cacheRepository); $hashcode = new CrmHashcode($uuidObj); // WHEN $firstCall = $service->getCustomerFromHashCode($hashcode); // Second call should return from internal memory cache without querying CacheRepository again $secondCall = $service->getCustomerFromHashCode($hashcode); // THEN expect($firstCall)->toBeInstanceOf(CrmCustomer::class) ->and($firstCall instanceof CrmCustomer ? $firstCall->hashcode : null)->toBe($uuid) ->and($secondCall)->toBe($firstCall); }); it('returns customer from email with sha256 cache key', function (): void { // GIVEN $crmApi = \Mockery::mock(CrmApiV4::class); $cacheRepository = \Mockery::mock(CacheRepository::class); $email = 'test@example.com'; $cacheKey = 'crm_customer_email_' . hash('sha256', $email); $expectedCustomer = new CrmCustomer( hashcode: (string) UuidHelper::v4(), email: $email, password: 'Secret123!', firstname: 'Jane', lastname: 'Doe', birthDate: '1992-05-10', phone: '0123456789', address: '456 Side St', address2: null, city: 'Lyon', postcode: '69000', country: Country::france, optIn: false, status: true, lastConnection: null, ); $cacheRepository->shouldReceive('remember') ->once() ->with($cacheKey, \Mockery::any(), \Mockery::type('Closure')) ->andReturn($expectedCustomer); $service = new CrmService($crmApi, $cacheRepository); // WHEN $result = $service->getCustomerFromEmail($email); // THEN expect($result)->toBeInstanceOf(CrmCustomer::class) ->and($result instanceof CrmCustomer ? $result->email : null)->toBe($email); }); it('returns null on modelNotFound ApiErrorException and caches result', function (): void { // GIVEN $crmApi = \Mockery::mock(CrmApiV4::class); $cacheRepository = \Mockery::mock(CacheRepository::class); $uuid = UuidHelper::v4(); $cacheKey = "crm_customer_hashcode_{$uuid}"; $cacheRepository->shouldReceive('remember') ->once() ->with($cacheKey, \Mockery::any(), \Mockery::on(function (\Closure $closure) use ($crmApi, $uuid) { $exception = \Mockery::mock(ApiErrorException::class); $exception->shouldReceive('getApiError')->andReturn(ApiError::modelNotFound); $crmApi->shouldReceive('getCustomer')->with((string) $uuid)->once()->andThrow($exception); $res = $closure(); return $res === null; })) ->andReturnNull(); $service = new CrmService($crmApi, $cacheRepository); // WHEN $result = $service->getCustomerFromHashCode(new CrmHashcode($uuid)); // THEN expect($result)->toBeNull(); }); it('invalidates cache on activate', function (): void { // GIVEN $crmApi = \Mockery::mock(CrmApiV4::class); $cacheRepository = \Mockery::mock(CacheRepository::class); $uuid = (string) UuidHelper::v4(); $customer = new CrmCustomer( hashcode: $uuid, email: 'activate@example.com', password: 'Secret123!', firstname: 'John', lastname: 'Doe', birthDate: '1990-01-01', phone: '0123456789', address: '123 Main St', address2: null, city: 'Paris', postcode: '75000', country: Country::france, optIn: true, status: false, lastConnection: null, ); $expectedHashKey = "crm_customer_hashcode_{$uuid}"; $expectedEmailKey = 'crm_customer_email_' . hash('sha256', 'activate@example.com'); $cacheRepository->shouldReceive('delete')->with($expectedHashKey)->once(); $cacheRepository->shouldReceive('delete')->with($expectedEmailKey)->once(); $apiInterface = \Mockery::mock(ApiInterface::class); $apiInterface->shouldReceive('call')->once()->andReturn( new \LDC\LDCCRM\Transport\Response( ApiStatus::OK, new Response(200, [], (string) json_encode([ 'version' => Utils::getPackageInfo()['version'], 'data' => [], ])) ) ); $refProp = new \ReflectionProperty(ClientModel::class, 'apiInterface'); $refProp->setValue(null, $apiInterface); $service = new CrmService($crmApi, $cacheRepository); // WHEN $result = $service->activate($customer); // THEN expect($result)->toHaveProperty('result'); }); it('returns CrmServiceException without caching it when CRM fails on hashcode or email lookup', function (): void { // GIVEN $crmApi = \Mockery::mock(CrmApiV4::class); $cacheRepository = new ArrayCacheRepository(new ArrayStore()); $uuid = UuidHelper::v4(); $email = 'error@example.com'; $hashKey = "crm_customer_hashcode_{$uuid}"; $emailKey = 'crm_customer_email_' . hash('sha256', $email); $apiException = \Mockery::mock(ApiErrorException::class); $apiException->shouldReceive('getApiError')->andReturn(ApiError::apiAuthFailed); $crmApi->shouldReceive('getCustomer')->with((string) $uuid)->twice()->andThrow($apiException); $crmApi->shouldReceive('getCustomer')->with($email)->twice()->andThrow(new CrmException('Network failure')); $service = new CrmService($crmApi, $cacheRepository); // WHEN $hashFirst = $service->getCustomerFromHashCode(new CrmHashcode($uuid)); $hashSecond = $service->getCustomerFromHashCode(new CrmHashcode($uuid)); $emailFirst = $service->getCustomerFromEmail($email); $emailSec = $service->getCustomerFromEmail($email); // THEN expect($hashFirst)->toBeInstanceOf(CrmServiceException::class) ->and($hashSecond)->toBeInstanceOf(CrmServiceException::class) ->and($emailFirst)->toBeInstanceOf(CrmServiceException::class) ->and($emailSec)->toBeInstanceOf(CrmServiceException::class) ->and($cacheRepository->has($hashKey))->toBeFalse() ->and($cacheRepository->has($emailKey))->toBeFalse(); }); it('deletes customer by hashcode and clears cache', function (): void { // GIVEN $crmApi = \Mockery::mock(CrmApiV4::class); $cacheRepository = \Mockery::mock(CacheRepository::class); $uuid = UuidHelper::v4(); $expectedHashKey = "crm_customer_hashcode_{$uuid->toString()}"; $cacheRepository->shouldReceive('get')->with($expectedHashKey)->once()->andReturnNull(); $cacheRepository->shouldReceive('delete')->with($expectedHashKey)->once(); $apiInterface = \Mockery::mock(ApiInterface::class); $apiInterface->shouldReceive('call')->once()->andReturn( new \LDC\LDCCRM\Transport\Response( ApiStatus::OK, new Response(200, [], (string) json_encode([ 'version' => Utils::getPackageInfo()['version'], 'data' => [], ])) ) ); $refProp = new \ReflectionProperty(ClientModel::class, 'apiInterface'); $refProp->setValue(null, $apiInterface); $service = new CrmService($crmApi, $cacheRepository); // WHEN $deleted = $service->deleteByHashcode(new CrmHashcode($uuid)); // THEN expect($deleted)->toBeTrue(); }); it('returns true when deleting a customer already missing from CRM', function (): void { // GIVEN $crmApi = \Mockery::mock(CrmApiV4::class); $cacheRepository = new ArrayCacheRepository(new ArrayStore()); $uuid = UuidHelper::v4(); $apiException = \Mockery::mock(ApiErrorException::class); $apiException->shouldReceive('getApiError')->andReturn(ApiError::modelNotFound); $apiInterface = \Mockery::mock(ApiInterface::class); $apiInterface->shouldReceive('call')->once()->andThrow($apiException); $refProp = new \ReflectionProperty(ClientModel::class, 'apiInterface'); $refProp->setValue(null, $apiInterface); $service = new CrmService($crmApi, $cacheRepository); // WHEN $deleted = $service->deleteByHashcode(new CrmHashcode($uuid)); // THEN expect($deleted)->toBeTrue(); }); it('throws CrmServiceException when CRM deletion fails', function (): void { // GIVEN $crmApi = \Mockery::mock(CrmApiV4::class); $cacheRepository = new ArrayCacheRepository(new ArrayStore()); $uuid = UuidHelper::v4(); $apiInterface = \Mockery::mock(ApiInterface::class); $apiInterface->shouldReceive('call')->once()->andThrow(new CrmException('CRM delete error')); $refProp = new \ReflectionProperty(ClientModel::class, 'apiInterface'); $refProp->setValue(null, $apiInterface); $service = new CrmService($crmApi, $cacheRepository); // WHEN & THEN expect(fn () => $service->deleteByHashcode(new CrmHashcode($uuid))) ->toThrow(CrmServiceException::class); }); it('clears both hashcode and email caches when email is provided to deleteByHashcode', function (): void { // GIVEN /** @var CrmApiV4&\Mockery\MockInterface $crmApi */ $crmApi = \Mockery::mock(CrmApiV4::class); $cacheRepository = new ArrayCacheRepository(new ArrayStore()); $uuid = UuidHelper::v4(); $email = new Email('Rollback@Example.com'); $hashKey = "crm_customer_hashcode_{$uuid->toString()}"; $emailKey = 'crm_customer_email_' . hash('sha256', 'rollback@example.com'); $cachedCustomer = new CrmCustomer( hashcode: $uuid->toString(), email: $email->value, password: null, firstname: 'Jane', lastname: 'Doe', birthDate: '1990-01-01', phone: null, address: null, address2: null, city: 'Paris', postcode: '75001', country: Country::france, optIn: false, status: false, lastConnection: null, ); $crmApi->shouldReceive('getCustomer')->with('rollback@example.com')->once()->andReturnNull(); $cacheRepository->put($hashKey, $cachedCustomer, 600); $cacheRepository->put($emailKey, $cachedCustomer, 600); $apiInterface = \Mockery::mock(ApiInterface::class); $apiInterface->shouldReceive('call')->once()->andReturn( new \LDC\LDCCRM\Transport\Response( ApiStatus::OK, new Response(200, [], (string) json_encode([ 'version' => Utils::getPackageInfo()['version'], 'data' => [], ])) ) ); $refProp = new \ReflectionProperty(ClientModel::class, 'apiInterface'); $refProp->setValue(null, $apiInterface); $service = new CrmService($crmApi, $cacheRepository); // Prime internal in-memory email cache from repository expect($service->getCustomerFromEmail($email->value))->toBeInstanceOf(CrmCustomer::class); // WHEN $deleted = $service->deleteByHashcode(new CrmHashcode($uuid), $email); // THEN expect($deleted)->toBeTrue() ->and($cacheRepository->has($hashKey))->toBeFalse() ->and($cacheRepository->has($emailKey))->toBeFalse() ->and($service->getCustomerFromEmail($email->value))->toBeNull(); }); it('clears cached email entry when deleteByHashcode resolves email from hashcode cache', function (): void { // GIVEN /** @var CrmApiV4&\Mockery\MockInterface $crmApi */ $crmApi = \Mockery::mock(CrmApiV4::class); $cacheRepository = new ArrayCacheRepository(new ArrayStore()); $uuid = UuidHelper::v4(); $hashKey = "crm_customer_hashcode_{$uuid->toString()}"; $emailKey = 'crm_customer_email_' . hash('sha256', 'cached@example.com'); $cachedCustomer = new CrmCustomer( hashcode: $uuid->toString(), email: 'cached@example.com', password: null, firstname: 'Jane', lastname: 'Doe', birthDate: '1990-01-01', phone: null, address: null, address2: null, city: 'Paris', postcode: '75001', country: Country::france, optIn: false, status: false, lastConnection: null, ); $cacheRepository->put($hashKey, $cachedCustomer, 600); $cacheRepository->put($emailKey, $cachedCustomer, 600); $apiInterface = \Mockery::mock(ApiInterface::class); $apiInterface->shouldReceive('call')->once()->andReturn( new \LDC\LDCCRM\Transport\Response( ApiStatus::OK, new Response(200, [], (string) json_encode([ 'version' => Utils::getPackageInfo()['version'], 'data' => [], ])) ) ); $refProp = new \ReflectionProperty(ClientModel::class, 'apiInterface'); $refProp->setValue(null, $apiInterface); $service = new CrmService($crmApi, $cacheRepository); // WHEN $deleted = $service->deleteByHashcode(new CrmHashcode($uuid)); // THEN expect($deleted)->toBeTrue() ->and($cacheRepository->has($hashKey))->toBeFalse() ->and($cacheRepository->has($emailKey))->toBeFalse(); }); });