. */ namespace SP\Tests\Domain\Import\Services; use PHPUnit\Framework\Attributes\Group; use PHPUnit\Framework\MockObject\Exception; use PHPUnit\Framework\MockObject\MockObject; use RuntimeException; use SP\Domain\Common\Ports\Repository; use SP\Domain\File\Ports\FileHandlerInterface; use SP\Domain\Import\Dtos\ImportParamsDto; use SP\Domain\Import\Ports\ImportStrategyService; use SP\Domain\Import\Ports\ItemsImportService; use SP\Domain\Import\Services\Import; use SP\Tests\UnitaryTestCase; /** * Class ImportTest * */ #[Group('unitary')] class ImportTest extends UnitaryTestCase { private Repository|MockObject $repository; private ImportParamsDto $importParamsDto; private ImportStrategyService|MockObject $importStrategyService; /** * @throws Exception * @throws \Exception */ public function testDoImport() { $this->repository->expects(self::once()) ->method('transactionAware') ->with( self::callback(function (callable $callable) { $callable(); return true; }) ) ->willReturn($this->createMock(ItemsImportService::class)); $itemsImportService = $this->createMock(ItemsImportService::class); $itemsImportService->expects(self::once()) ->method('doImport') ->with($this->importParamsDto) ->willReturn($itemsImportService); $this->importStrategyService ->expects(self::once()) ->method('buildImport') ->with($this->importParamsDto) ->willReturn($itemsImportService); $import = new Import($this->application, $this->importStrategyService, $this->repository); $import->doImport($this->importParamsDto); } /** * @throws Exception * @throws \Exception */ public function testDoImportWithException() { $this->repository->expects(self::once()) ->method('transactionAware') ->willThrowException(new RuntimeException('test')); $import = new Import($this->application, $this->importStrategyService, $this->repository); $this->expectException(RuntimeException::class); $this->expectExceptionMessage('test'); $import->doImport($this->importParamsDto); } protected function setUp(): void { parent::setUp(); $this->repository = $this->createMock(Repository::class); $this->importStrategyService = $this->createMock(ImportStrategyService::class); $this->importParamsDto = new ImportParamsDto( $this->createMock(FileHandlerInterface::class), self::$faker->randomNumber(3), self::$faker->randomNumber(3), self::$faker->password, self::$faker->password ); } }