diff --git a/tests/SPT/Domain/CustomField/Services/UpgradeCustomFieldDataTest.php b/tests/SPT/Domain/CustomField/Services/UpgradeCustomFieldDataTest.php new file mode 100644 index 00000000..3de9c4b0 --- /dev/null +++ b/tests/SPT/Domain/CustomField/Services/UpgradeCustomFieldDataTest.php @@ -0,0 +1,177 @@ +. + */ + +namespace SPT\Domain\CustomField\Services; + +use Exception; +use PHPUnit\Framework\Constraint\Callback; +use PHPUnit\Framework\MockObject\MockObject; +use RuntimeException; +use SP\Domain\Common\Services\ServiceException; +use SP\Domain\CustomField\Models\CustomFieldData as CustomFieldDataModel; +use SP\Domain\CustomField\Ports\CustomFieldDataRepository; +use SP\Domain\CustomField\Services\UpgradeCustomFieldData; +use SP\Infrastructure\Database\QueryResult; +use SPT\Generators\CustomFieldDataGenerator; +use SPT\UnitaryTestCase; + +/** + * Class UpgradeCustomFieldDataTest + * + * @group unitary + */ +class UpgradeCustomFieldDataTest extends UnitaryTestCase +{ + + private CustomFieldDataRepository|MockObject $customFieldDataRepository; + private UpgradeCustomFieldData $upgradeCustomFieldData; + + /** + * @throws Exception + */ + public function testUpgradeV300B18072902WithMappedModuleId() + { + $this->customFieldDataRepository + ->expects(self::once()) + ->method('transactionAware') + ->with(new Callback(static fn(callable $callable) => $callable() === null)); + + $modulesId = [10, 61, 62, 71, 72]; + + $customFieldsData = array_map( + static fn() => CustomFieldDataGenerator::factory() + ->buildCustomFieldData() + ->mutate(['moduleId' => array_pop($modulesId)]), + range(0, 4) + ); + + $queryResults = new QueryResult($customFieldsData); + + $this->customFieldDataRepository + ->expects(self::once()) + ->method('getAll') + ->willReturn($queryResults); + + $this->customFieldDataRepository + ->expects(self::exactly(5)) + ->method('deleteBatch') + ->with( + ... + self::withConsecutive( + ... + array_map( + static fn(CustomFieldDataModel $customFieldData) => [ + [$customFieldData->getItemId()], + $customFieldData->getModuleId() + ], + $customFieldsData + ) + ) + ); + + $this->customFieldDataRepository + ->expects(self::exactly(5)) + ->method('create'); + + $this->upgradeCustomFieldData->upgradeV300B18072902(); + } + + /** + * @throws Exception + */ + public function testUpgradeV300B18072902WithUnmappedModuleId() + { + $this->customFieldDataRepository + ->expects(self::once()) + ->method('transactionAware') + ->with(new Callback(static fn(callable $callable) => $callable() === null)); + + $modulesId = [1, 101, 301, 701, 801]; + + $customFieldsData = array_map( + static fn() => CustomFieldDataGenerator::factory() + ->buildCustomFieldData() + ->mutate(['moduleId' => array_pop($modulesId)]), + range(0, 4) + ); + + $queryResults = new QueryResult($customFieldsData); + + $this->customFieldDataRepository + ->expects(self::once()) + ->method('getAll') + ->willReturn($queryResults); + + $this->customFieldDataRepository + ->expects(self::never()) + ->method('deleteBatch'); + + $this->customFieldDataRepository + ->expects(self::never()) + ->method('create'); + + $this->upgradeCustomFieldData->upgradeV300B18072902(); + } + + /** + * @throws Exception + */ + public function testUpgradeV300B18072902WithException() + { + $this->customFieldDataRepository + ->expects(self::once()) + ->method('transactionAware') + ->willThrowException(new RuntimeException('test')); + + $this->customFieldDataRepository + ->expects(self::never()) + ->method('getAll'); + + $this->customFieldDataRepository + ->expects(self::never()) + ->method('deleteBatch'); + + $this->customFieldDataRepository + ->expects(self::never()) + ->method('create'); + + $this->expectException(ServiceException::class); + $this->expectExceptionMessage('test'); + + $this->upgradeCustomFieldData->upgradeV300B18072902(); + } + + protected function setUp(): void + { + parent::setUp(); + + $this->customFieldDataRepository = $this->createMock(CustomFieldDataRepository::class); + + $this->upgradeCustomFieldData = new UpgradeCustomFieldData( + $this->application, + $this->customFieldDataRepository + ); + } + +}