Skip to content

Commit

Permalink
Merge pull request #4 from IlicMiljan/add-cipher-exception-tests
Browse files Browse the repository at this point in the history
Add Cipher Exceptions Unit Tests
  • Loading branch information
ilicmiljan authored Mar 10, 2024
2 parents 55814cc + 15ab6ee commit e94a387
Show file tree
Hide file tree
Showing 16 changed files with 178 additions and 18 deletions.
8 changes: 4 additions & 4 deletions src/Cipher/AdvancedEncryptionStandardCipher.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

namespace IlicMiljan\SecureProps\Cipher;

use IlicMiljan\SecureProps\Reader\Exception\FailedCalculatingInitializationVectorLength;
use IlicMiljan\SecureProps\Reader\Exception\FailedDecryptingValue;
use IlicMiljan\SecureProps\Reader\Exception\FailedEncryptingValue;
use IlicMiljan\SecureProps\Reader\Exception\FailedGeneratingInitializationVector;
use IlicMiljan\SecureProps\Cipher\Exception\FailedCalculatingInitializationVectorLength;
use IlicMiljan\SecureProps\Cipher\Exception\FailedDecryptingValue;
use IlicMiljan\SecureProps\Cipher\Exception\FailedEncryptingValue;
use IlicMiljan\SecureProps\Cipher\Exception\FailedGeneratingInitializationVector;
use InvalidArgumentException;
use SensitiveParameter;

Expand Down
4 changes: 2 additions & 2 deletions src/Cipher/AsymmetricEncryptionCipher.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace IlicMiljan\SecureProps\Cipher;

use IlicMiljan\SecureProps\Reader\Exception\FailedDecryptingValue;
use IlicMiljan\SecureProps\Reader\Exception\FailedEncryptingValue;
use IlicMiljan\SecureProps\Cipher\Exception\FailedDecryptingValue;
use IlicMiljan\SecureProps\Cipher\Exception\FailedEncryptingValue;
use SensitiveParameter;

class AsymmetricEncryptionCipher implements Cipher
Expand Down
2 changes: 1 addition & 1 deletion src/Cipher/Cipher.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace IlicMiljan\SecureProps\Cipher;

use IlicMiljan\SecureProps\Reader\Exception\CipherException;
use IlicMiljan\SecureProps\Cipher\Exception\CipherException;
use SensitiveParameter;

interface Cipher
Expand Down
2 changes: 1 addition & 1 deletion src/Cipher/Exception/CipherException.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace IlicMiljan\SecureProps\Reader\Exception;
namespace IlicMiljan\SecureProps\Cipher\Exception;

use Throwable;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace IlicMiljan\SecureProps\Reader\Exception;
namespace IlicMiljan\SecureProps\Cipher\Exception;

use LogicException;
use RuntimeException;
Expand Down
2 changes: 1 addition & 1 deletion src/Cipher/Exception/FailedDecryptingValue.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace IlicMiljan\SecureProps\Reader\Exception;
namespace IlicMiljan\SecureProps\Cipher\Exception;

use LogicException;
use RuntimeException;
Expand Down
2 changes: 1 addition & 1 deletion src/Cipher/Exception/FailedEncryptingValue.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace IlicMiljan\SecureProps\Reader\Exception;
namespace IlicMiljan\SecureProps\Cipher\Exception;

use LogicException;
use RuntimeException;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace IlicMiljan\SecureProps\Reader\Exception;
namespace IlicMiljan\SecureProps\Cipher\Exception;

use LogicException;
use RuntimeException;
Expand Down
2 changes: 1 addition & 1 deletion src/ObjectEncryptionService.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use IlicMiljan\SecureProps\Attribute\Encrypted;
use IlicMiljan\SecureProps\Cipher\Cipher;
use IlicMiljan\SecureProps\Reader\Exception\CipherException;
use IlicMiljan\SecureProps\Cipher\Exception\CipherException;
use IlicMiljan\SecureProps\Reader\Exception\ReaderException;
use IlicMiljan\SecureProps\Reader\ObjectPropertiesReader;
use InvalidArgumentException;
Expand Down
5 changes: 3 additions & 2 deletions src/StringEncryptionService.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace IlicMiljan\SecureProps;

use IlicMiljan\SecureProps\Cipher\Cipher;
use IlicMiljan\SecureProps\Cipher\Exception\CipherException;
use InvalidArgumentException;
use SensitiveParameter;

Expand All @@ -16,7 +17,7 @@ public function __construct(
/**
* @param mixed $value
* @return string
* @throws Reader\Exception\CipherException
* @throws CipherException
*/
public function encrypt(#[SensitiveParameter] mixed $value): string
{
Expand All @@ -30,7 +31,7 @@ public function encrypt(#[SensitiveParameter] mixed $value): string
/**
* @param mixed $value
* @return string
* @throws Reader\Exception\CipherException
* @throws CipherException
*/
public function decrypt(#[SensitiveParameter] mixed $value): string
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace IlicMiljan\SecureProps\Tests\Cipher\Exception;

use IlicMiljan\SecureProps\Cipher\Exception\CipherException;
use IlicMiljan\SecureProps\Cipher\Exception\FailedCalculatingInitializationVectorLength;
use LogicException;
use PHPUnit\Framework\TestCase;

class FailedCalculatingInitializationVectorLengthTest extends TestCase
{
private string $cipher;

protected function setUp(): void
{
$this->cipher = 'AES-256-GCM';
}

public function testCanBeCreated(): void
{
$exception = new FailedCalculatingInitializationVectorLength($this->cipher);

$this->assertInstanceOf(FailedCalculatingInitializationVectorLength::class, $exception);
}

public function testReturnsCipher(): void
{
$exception = new FailedCalculatingInitializationVectorLength($this->cipher);

$this->assertEquals($this->cipher, $exception->getCipher());
}

public function testPreviousExceptionIsStored(): void
{
$previous = new LogicException('Previous exception');
$exception = new FailedCalculatingInitializationVectorLength($this->cipher, $previous);

$this->assertSame($previous, $exception->getPrevious());
}

public function testImplementsCipherExceptionInterface(): void
{
$exception = new FailedCalculatingInitializationVectorLength($this->cipher);

$this->assertInstanceOf(CipherException::class, $exception);
}
}
33 changes: 33 additions & 0 deletions tests/Cipher/Exception/FailedDecryptingValueTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace IlicMiljan\SecureProps\Tests\Cipher\Exception;

use IlicMiljan\SecureProps\Cipher\Exception\CipherException;
use IlicMiljan\SecureProps\Cipher\Exception\FailedDecryptingValue;
use LogicException;
use PHPUnit\Framework\TestCase;

class FailedDecryptingValueTest extends TestCase
{
public function testCanBeCreated(): void
{
$exception = new FailedDecryptingValue();

$this->assertInstanceOf(FailedDecryptingValue::class, $exception);
}

public function testPreviousExceptionIsStored(): void
{
$previous = new LogicException('Previous exception');
$exception = new FailedDecryptingValue($previous);

$this->assertSame($previous, $exception->getPrevious());
}

public function testImplementsCipherExceptionInterface(): void
{
$exception = new FailedDecryptingValue();

$this->assertInstanceOf(CipherException::class, $exception);
}
}
33 changes: 33 additions & 0 deletions tests/Cipher/Exception/FailedEncryptingValueTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace IlicMiljan\SecureProps\Tests\Cipher\Exception;

use IlicMiljan\SecureProps\Cipher\Exception\CipherException;
use IlicMiljan\SecureProps\Cipher\Exception\FailedEncryptingValue;
use LogicException;
use PHPUnit\Framework\TestCase;

class FailedEncryptingValueTest extends TestCase
{
public function testCanBeCreated(): void
{
$exception = new FailedEncryptingValue();

$this->assertInstanceOf(FailedEncryptingValue::class, $exception);
}

public function testPreviousExceptionIsStored(): void
{
$previous = new LogicException('Previous exception');
$exception = new FailedEncryptingValue($previous);

$this->assertSame($previous, $exception->getPrevious());
}

public function testImplementsCipherExceptionInterface(): void
{
$exception = new FailedEncryptingValue();

$this->assertInstanceOf(CipherException::class, $exception);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace IlicMiljan\SecureProps\Tests\Cipher\Exception;

use IlicMiljan\SecureProps\Cipher\Exception\CipherException;
use IlicMiljan\SecureProps\Cipher\Exception\FailedGeneratingInitializationVector;
use LogicException;
use PHPUnit\Framework\TestCase;

class FailedGeneratingInitializationVectorTest extends TestCase
{
private int $length;

protected function setUp(): void
{
$this->length = 16;
}

public function testCanBeCreated(): void
{
$exception = new FailedGeneratingInitializationVector($this->length);

$this->assertInstanceOf(FailedGeneratingInitializationVector::class, $exception);
}

public function testReturnsLength(): void
{
$exception = new FailedGeneratingInitializationVector($this->length);

$this->assertEquals($this->length, $exception->getLength());
}

public function testPreviousExceptionIsStored(): void
{
$previous = new LogicException('Previous exception');
$exception = new FailedGeneratingInitializationVector($this->length, $previous);

$this->assertSame($previous, $exception->getPrevious());
}

public function testImplementsCipherExceptionInterface(): void
{
$exception = new FailedGeneratingInitializationVector($this->length);

$this->assertInstanceOf(CipherException::class, $exception);
}
}
2 changes: 1 addition & 1 deletion tests/ObjectEncryptionServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
namespace IlicMiljan\SecureProps\Tests;

use IlicMiljan\SecureProps\Cipher\Cipher;
use IlicMiljan\SecureProps\Cipher\Exception\CipherException;
use IlicMiljan\SecureProps\ObjectEncryptionService;
use IlicMiljan\SecureProps\Reader\Exception\CipherException;
use IlicMiljan\SecureProps\Reader\Exception\ReaderException;
use IlicMiljan\SecureProps\Reader\ObjectPropertiesReader;
use IlicMiljan\SecureProps\Attribute\Encrypted;
Expand Down
3 changes: 1 addition & 2 deletions tests/StringEncryptionServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@

namespace IlicMiljan\SecureProps\Tests;

use IlicMiljan\SecureProps\Reader\Exception\CipherException;
use IlicMiljan\SecureProps\Cipher\Exception\CipherException;
use IlicMiljan\SecureProps\StringEncryptionService;
use IlicMiljan\SecureProps\Cipher\Cipher;
use InvalidArgumentException;
use PHPUnit\Framework\MockObject\Exception;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;

Expand Down

0 comments on commit e94a387

Please sign in to comment.