. */ namespace SP\Tests\Domain\CustomField\Services; use PHPUnit\Framework\Attributes\Group; use PHPUnit\Framework\Constraint\Callback; use PHPUnit\Framework\MockObject\Exception; use PHPUnit\Framework\MockObject\MockObject; use RuntimeException; use SP\Domain\Common\Services\ServiceException; use SP\Domain\Core\Crypt\CryptInterface; use SP\Domain\Crypt\Dtos\UpdateMasterPassRequest; use SP\Domain\CustomField\Models\CustomFieldData as CustomFieldDataModel; use SP\Domain\CustomField\Ports\CustomFieldDataService; use SP\Domain\CustomField\Services\CustomFieldCrypt; use SP\Domain\Task\Ports\TaskInterface; use SP\Tests\Generators\CustomFieldDataGenerator; use SP\Tests\UnitaryTestCase; /** * Class CustomFieldCryptTest * */ #[Group('unitary')] class CustomFieldCryptTest extends UnitaryTestCase { private CustomFieldDataService|MockObject $customFieldService; private CryptInterface|MockObject $crypt; private CustomFieldCrypt $customFieldCrypt; /** * @throws ServiceException */ public function testUpdateMasterPassword() { $hash = self::$faker->sha1; $request = new UpdateMasterPassRequest('secret', 'test_secret', $hash); $customFieldData = CustomFieldDataGenerator::factory()->buildCustomFieldData(); $this->customFieldService ->expects(self::once()) ->method('getAllEncrypted') ->willReturn([$customFieldData]); $data = self::$faker->text(); $this->crypt ->expects(self::once()) ->method('decrypt') ->with($customFieldData->getData(), $customFieldData->getKey(), $request->getCurrentMasterPass()) ->willReturn($data); $this->customFieldService ->expects(self::once()) ->method('updateMasterPass') ->with( new Callback(static function (CustomFieldDataModel $customFieldData) use ($data) { return $customFieldData->getData() === $data; }), $request->getNewMasterPass() ); $this->customFieldCrypt->updateMasterPassword($request); } /** * @throws ServiceException */ public function testUpdateMasterPasswordWithNoData() { $hash = self::$faker->sha1; $request = new UpdateMasterPassRequest('secret', 'test_secret', $hash); $this->customFieldService ->expects(self::once()) ->method('getAllEncrypted') ->willReturn([]); $this->crypt ->expects(self::never()) ->method('decrypt'); $this->customFieldService ->expects(self::never()) ->method('updateMasterPass'); $this->customFieldCrypt->updateMasterPassword($request); } /** * @throws Exception * @throws ServiceException */ public function testUpdateMasterPasswordWithTask() { $hash = self::$faker->sha1; $request = new UpdateMasterPassRequest('secret', 'test_secret', $hash); $customFieldData = CustomFieldDataGenerator::factory()->buildCustomFieldData(); $this->customFieldService ->expects(self::once()) ->method('getAllEncrypted') ->willReturn([$customFieldData]); $data = self::$faker->text(); $this->crypt ->expects(self::once()) ->method('decrypt') ->with($customFieldData->getData(), $customFieldData->getKey(), $request->getCurrentMasterPass()) ->willReturn($data); $this->customFieldService ->expects(self::once()) ->method('updateMasterPass') ->with( new Callback(static function (CustomFieldDataModel $customFieldData) use ($data) { return $customFieldData->getData() === $data; }), $request->getNewMasterPass() ); $this->customFieldCrypt->updateMasterPassword($request); } /** * @throws ServiceException */ public function testUpdateMasterPasswordWithCryptError() { $hash = self::$faker->sha1; $request = new UpdateMasterPassRequest('secret', 'test_secret', $hash); $customFieldData = CustomFieldDataGenerator::factory()->buildCustomFieldData(); $this->customFieldService ->expects(self::once()) ->method('getAllEncrypted') ->willReturn([$customFieldData]); $this->crypt ->expects(self::once()) ->method('decrypt') ->willThrowException(new RuntimeException('test')); $this->customFieldService ->expects(self::never()) ->method('updateMasterPass'); $this->customFieldCrypt->updateMasterPassword($request); } public function testUpdateMasterPasswordWithError() { $hash = self::$faker->sha1; $request = new UpdateMasterPassRequest('secret', 'test_secret', $hash); $this->customFieldService ->expects(self::once()) ->method('getAllEncrypted') ->willThrowException(new RuntimeException('test')); $this->crypt ->expects(self::never()) ->method('decrypt'); $this->customFieldService ->expects(self::never()) ->method('updateMasterPass'); $this->expectException(ServiceException::class); $this->expectExceptionMessage('Error while updating the custom fields data'); $this->customFieldCrypt->updateMasterPassword($request); } protected function setUp(): void { parent::setUp(); $this->customFieldService = $this->createMock(CustomFieldDataService::class); $this->crypt = $this->createMock(CryptInterface::class); $this->customFieldCrypt = new CustomFieldCrypt($this->application, $this->customFieldService, $this->crypt); } }