Skip to content

Commit

Permalink
Merge pull request #9 from IlicMiljan/allow-null-value-to-be-skipped
Browse files Browse the repository at this point in the history
Allow `NULL` Value Skipping
  • Loading branch information
IlicMiljan committed Mar 10, 2024
2 parents 805cd19 + 9df84b7 commit b210580
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/ObjectEncryptionService.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ private function updatePropertyValue(ReflectionProperty $property, object $objec

$currentValue = $property->getValue($object);

if ($currentValue === null) {
return;
}

if (!is_string($currentValue)) {
throw new InvalidArgumentException('Value must be string.');
}
Expand Down
64 changes: 64 additions & 0 deletions tests/ObjectEncryptionServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,70 @@ public function testDecryptSuccess(): void
$this->assertEquals('plainText', $decryptedObject->sensitive);
}

/**
* @throws ReflectionException
* @throws CipherException
* @throws ReaderException
*/
public function testEncryptSkipsNullProperties(): void
{
$object = new class {
#[Encrypted]
public ?string $sensitive = null;
};

$reflectionProperty = new ReflectionProperty($object, 'sensitive');
$reflectionProperty->setAccessible(true);

$this->objectPropertiesReaderMock
->expects($this->once())
->method('getPropertiesWithAttribute')
->with($object, Encrypted::class)
->willReturn([$reflectionProperty]);

$this->cipherMock
->expects($this->never()) // Ensure encrypt is never called since the property is null
->method('encrypt');

$encryptedObject = $this->service->encrypt($object);

// Assert the property remains null, indicating it was skipped
/** @phpstan-ignore-next-line */
$this->assertNull($encryptedObject->sensitive);
}

/**
* @throws ReflectionException
* @throws CipherException
* @throws ReaderException
*/
public function testDecryptSkipsNullProperties(): void
{
$object = new class {
#[Encrypted]
public ?string $sensitive = null;
};

$reflectionProperty = new ReflectionProperty($object, 'sensitive');
$reflectionProperty->setAccessible(true);

$this->objectPropertiesReaderMock
->expects($this->once())
->method('getPropertiesWithAttribute')
->with($object, Encrypted::class)
->willReturn([$reflectionProperty]);

$this->cipherMock
->expects($this->never()) // Ensure decrypt is never called since the property is null
->method('decrypt');

$decryptedObject = $this->service->decrypt($object);

// Assert the property remains null, indicating it was skipped
/** @phpstan-ignore-next-line */
$this->assertNull($decryptedObject->sensitive);
}

/**
* @throws ReaderException
* @throws CipherException
Expand Down

0 comments on commit b210580

Please sign in to comment.