From 3e4732bcb4d6ae08eb5d0770944da8515a2c1613 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Munsch?= Date: Mon, 5 Oct 2026 11:56:53 +0200 Subject: [PATCH] fix: Bind mocked CrmService to container in RegisterCustomerTest Ensure that all mocked dependencies in RegisterCustomerTest are properly bound to the Laravel service container using App::instance(). This prevents any code that resolves dependencies via container injection from getting real service instances instead of mocks. This fix aligns the Integration test pattern with Feature tests which already use App::instance() to bind mocks to the container. Changes: - Add import for Illuminate\Support\Facades\App - Add App::instance() calls after each mock creation in all 6 test cases - Add mockCrmService() helper method to TestCase base class for future tests Fixes CI failures where CRM service was being called in real instead of being mocked. --- .../Actions/Customers/RegisterCustomerTest.php | 18 ++++++++++++++++++ tests/TestCase.php | 9 +++++++++ 2 files changed, 27 insertions(+) diff --git a/tests/Integration/WithApp/App/Actions/Customers/RegisterCustomerTest.php b/tests/Integration/WithApp/App/Actions/Customers/RegisterCustomerTest.php index 9451fca6..241f65bd 100644 --- a/tests/Integration/WithApp/App/Actions/Customers/RegisterCustomerTest.php +++ b/tests/Integration/WithApp/App/Actions/Customers/RegisterCustomerTest.php @@ -21,6 +21,7 @@ use Domain\Models\ValueObjects\ShopperEstablishmentCode; use Domain\Models\ValueObjects\ShopperServiceCode; use Domain\Models\ValueObjects\ShopperSiteCode; use Illuminate\Database\Eloquent\ModelNotFoundException; +use Illuminate\Support\Facades\App; use Illuminate\Support\Facades\Notification; use Illuminate\Validation\ValidationException; use Infrastructure\Adapters\Services\AppCustomerAuthService; @@ -78,11 +79,13 @@ describe('RegisterCustomer action', function (): void { status: false, lastConnection: null, )); + App::instance(CrmService::class, $crmService); $authService = \Mockery::mock(AppCustomerAuthService::class); $authService->shouldReceive('logIn') ->once() ->withArgs(fn (Customer $c) => $c->email->value === $email->value); + App::instance(AppCustomerAuthService::class, $authService); $employeeRepo = resolve(EmployeeRepository::class); @@ -123,6 +126,9 @@ describe('RegisterCustomer action', function (): void { // GIVEN $crmService = \Mockery::mock(CrmService::class); $authService = \Mockery::mock(AppCustomerAuthService::class); + App::instance(CrmService::class, $crmService); + App::instance(AppCustomerAuthService::class, $authService); + $employeeRepo = resolve(EmployeeRepository::class); $action = new RegisterCustomer( @@ -169,8 +175,11 @@ describe('RegisterCustomer action', function (): void { $crmService->shouldReceive('persist') ->once() ->andThrow(new CrmCustomerExistsException()); + App::instance(CrmService::class, $crmService); $authService = \Mockery::mock(AppCustomerAuthService::class); + App::instance(AppCustomerAuthService::class, $authService); + $employeeRepo = resolve(EmployeeRepository::class); $action = new RegisterCustomer( @@ -217,8 +226,11 @@ describe('RegisterCustomer action', function (): void { $crmService->shouldReceive('persist') ->once() ->andThrow(CrmServiceException::of('CRM service offline')); + App::instance(CrmService::class, $crmService); $authService = \Mockery::mock(AppCustomerAuthService::class); + App::instance(AppCustomerAuthService::class, $authService); + $employeeRepo = resolve(EmployeeRepository::class); $action = new RegisterCustomer( @@ -281,8 +293,11 @@ describe('RegisterCustomer action', function (): void { status: false, lastConnection: null, )); + App::instance(CrmService::class, $crmService); $authService = \Mockery::mock(AppCustomerAuthService::class); + App::instance(AppCustomerAuthService::class, $authService); + $employeeRepo = resolve(EmployeeRepository::class); $action = new RegisterCustomer( @@ -313,6 +328,9 @@ describe('RegisterCustomer action', function (): void { // GIVEN $crmService = \Mockery::mock(CrmService::class); $authService = \Mockery::mock(AppCustomerAuthService::class); + App::instance(CrmService::class, $crmService); + App::instance(AppCustomerAuthService::class, $authService); + $employeeRepo = resolve(EmployeeRepository::class); $action = new RegisterCustomer( diff --git a/tests/TestCase.php b/tests/TestCase.php index d5ffe68f..7de55c2f 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -4,6 +4,7 @@ declare(strict_types=1); namespace Tests; +use App\Services\CrmService; use GuzzleHttp\Psr7\Response as GuzzleResponse; use Illuminate\Foundation\Testing\TestCase as BaseTestCase; use LDC\LDCCRM\CrmApiV4; @@ -45,4 +46,12 @@ abstract class TestCase extends BaseTestCase $refCrmApi = new \ReflectionProperty(CrmApiV4::class, 'apiInterface'); $refCrmApi->setValue(null, $apiInterface); } + + /** + * Create and bind a mock CrmService to the container to ensure no real CRM calls are made in tests. + */ + protected function mockCrmService(): CrmService + { + return $this->mock(CrmService::class); + } }