Skip to content
This repository has been archived by the owner on Mar 13, 2024. It is now read-only.

Commit

Permalink
Merge pull request #192 from devuri/update-encryption-02
Browse files Browse the repository at this point in the history
feat: update encryption to `0.2`
  • Loading branch information
devuri authored Oct 28, 2023
2 parents 6f833ba + a96eb0a commit 32438e2
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 1 deletion.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"defuse/php-encryption": "^2.4",
"devuri/cpt-meta-box": "^0.4",
"devuri/dot-access": "^0.2.2",
"devuri/encryption": "^0.1",
"devuri/encryption": "^0.2",
"devuri/secure-password": "^0.1",
"devuri/uuid-generator": "^0.0.1",
"filp/whoops": "^2.15",
Expand Down
File renamed without changes.
101 changes: 101 additions & 0 deletions tests/Unit/EncryptionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
<?php

namespace Tests\Unit\App\Console;

use Defuse\Crypto\Crypto;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Filesystem\Filesystem;
use Urisoft\Encryption;

/**
* @internal
*
* @coversNothing
*/
class EncryptionTest extends TestCase
{
protected $encryption;
protected $secret_test_data;

protected function setUp(): void
{
$this->encryption = new Encryption(APP_TEST_PATH, new Filesystem());
$this->secret_test_data = 'This is our secret test string';
}

protected function tearDown(): void
{
$files = [
APP_TEST_PATH . '/.env.encrypted',
APP_TEST_PATH . '/.env.dencryptfile',
APP_TEST_PATH . '/.env.encryptfile',
];

foreach ( $files as $file ) {
if ( file_exists( $file ) ) {
unlink( $file );
}
}
}

public function test_encrypt_and_decrypt(): void
{
$encryptedData = $this->encryption->encrypt($this->secret_test_data, false );

$this->assertNotEmpty($encryptedData);

$decryptedData = $this->encryption->decrypt($encryptedData, false );

$this->assertEquals($this->secret_test_data, $decryptedData);
}

public function test_encoded_encrypt_and_decrypt(): void
{
$encryptedData = $this->encryption->encrypt($this->secret_test_data);

$this->assertNotEmpty($encryptedData);

$decryptedData = $this->encryption->decrypt( $encryptedData );

$this->assertEquals($this->secret_test_data, $decryptedData);
}

public function test_encrypt_env_file(): void
{
$this->encryption->encrypt_envfile('/.env.local');

$this->assertFileExists(APP_TEST_PATH . '/.env.encrypted');

$decryptedEnvContents = Crypto::decrypt(
file_get_contents(APP_TEST_PATH . '/.env.encrypted'),
$this->encryption->load_encryption_key()
);

$envContents = file_get_contents(APP_TEST_PATH . '/.env.local');

$this->assertEquals($envContents, $decryptedEnvContents);
}

public function test_file_encryption(): void
{
$this->encryption->encrypt_file(
APP_TEST_PATH . '/.env.local',
APP_TEST_PATH . '/.env.encryptfile'
);

$this->assertFileExists(APP_TEST_PATH . '/.env.encryptfile');

$this->encryption->decrypt_file(
APP_TEST_PATH . '/.env.encryptfile',
APP_TEST_PATH . '/.env.dencryptfile'
);

$this->assertFileExists(APP_TEST_PATH . '/.env.dencryptfile');

$fileContents = file_get_contents(APP_TEST_PATH . '/.env.local');

$decryptedfile = file_get_contents(APP_TEST_PATH . '/.env.dencryptfile');

$this->assertEquals($fileContents, $decryptedfile);
}
}

0 comments on commit 32438e2

Please sign in to comment.