shouldReceive('selectAll') ->once() ->with($pagination) ->andReturn($expectedPage); $service = new ArcadiaBrandService($repository); // WHEN $result = $service->getAll($pagination); // THEN expect($result)->toBe($expectedPage); }); it('returns ApiError when repository fails', function () { // GIVEN $pagination = Pagination::firstPage(); $error = new ServerError('Internal Server Error', 500); $repository = Mockery::mock(BrandRepository::class); $repository->shouldReceive('selectAll') ->once() ->with($pagination) ->andReturn($error); $service = new ArcadiaBrandService($repository); // WHEN $result = $service->getAll($pagination); // THEN expect($result)->toBeInstanceOf(ApiError::class); expect($result)->toBe($error); }); }); describe('streamAll', function () { it('returns StreamOf that yields brands from repository', function () { // GIVEN $brand = new Brand(code: new BrandCode('LGL39'), label: 'Le Gaulois'); $expectedPage = new PageOf( new PaginationDto(page: 1, pageSize: 50, totalItems: 1, totalPages: 1), [$brand] ); $repository = Mockery::mock(BrandRepository::class); $repository->shouldReceive('selectAll') ->once() ->andReturn($expectedPage); $service = new ArcadiaBrandService($repository); // WHEN $stream = $service->streamAll(new PageSize(50)); $items = iterator_to_array($stream); // THEN expect($stream)->toBeInstanceOf(StreamOf::class); expect($items)->toHaveCount(1); expect($items[0])->toBe($brand); }); }); describe('findBy', function () { it('returns Brand when brand is found', function () { // GIVEN $code = new BrandCode('LGL39'); $expectedBrand = new Brand(code: $code, label: 'Le Gaulois'); $repository = Mockery::mock(BrandRepository::class); $repository->shouldReceive('find') ->once() ->with($code) ->andReturn($expectedBrand); $service = new ArcadiaBrandService($repository); // WHEN $result = $service->findBy($code); // THEN expect($result)->toBe($expectedBrand); }); it('returns null when brand is not found', function () { // GIVEN $code = new BrandCode('LGL39'); $repository = Mockery::mock(BrandRepository::class); $repository->shouldReceive('find') ->once() ->with($code) ->andReturnNull(); $service = new ArcadiaBrandService($repository); // WHEN $result = $service->findBy($code); // THEN expect($result)->toBeNull(); }); it('returns ApiError when repository fails', function () { // GIVEN $code = new BrandCode('LGL39'); $error = new ServerError('Internal Server Error', 500); $repository = Mockery::mock(BrandRepository::class); $repository->shouldReceive('find') ->once() ->with($code) ->andReturn($error); $service = new ArcadiaBrandService($repository); // WHEN $result = $service->findBy($code); // THEN expect($result)->toBeInstanceOf(ApiError::class); expect($result)->toBe($error); }); }); });