Skip to content

Commit

Permalink
Merge pull request #2 from CrossKnowledge/fix/CKLS-12322
Browse files Browse the repository at this point in the history
Symfony 4.0 compatibility.
  • Loading branch information
rafaelpires815 authored Mar 23, 2022
2 parents e2ece78 + e54d951 commit 665af93
Show file tree
Hide file tree
Showing 5 changed files with 2,572 additions and 818 deletions.
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
!var/logs/.gitkeep
!var/sessions/.gitkeep

# PHPUnit
logs/*
.phpunit.result.cache

# Parameters
/app/config/parameters.yml
/app/config/parameters.ini
Expand All @@ -36,4 +40,4 @@
# Backup entities generated with doctrine:generate:entities command
*/Entity/*~
/.project
/.idea
/.idea
4 changes: 1 addition & 3 deletions Services/DataEncrypter.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
<?php


namespace CrossKnowledge\FeedbackDataEncrypterBundle\Services;


class DataEncrypter
{
/**
Expand Down Expand Up @@ -45,6 +43,6 @@ public function decrypt($data, $key) {
MCRYPT_MODE_ECB
);

return json_decode($string, true);
return json_decode(rtrim($string, "\0"), true);
}
}
84 changes: 46 additions & 38 deletions Tests/Services/DataEncrypterTest.php
Original file line number Diff line number Diff line change
@@ -1,61 +1,69 @@
<?php


namespace AppBundle\Tests\Services;

use CrossKnowledge\FeedbackDataEncrypterBundle\Services\DataEncrypter;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use PHPUnit\Framework\TestCase;

class DataEncrypterTest extends WebTestCase
class DataEncrypterTest extends TestCase
{
public function testEncryptString()
{
$data = "This is a test";
$encrypter = new DataEncrypter();
$encrypted = $encrypter->encrypt($data, "test");
$decrypted = $encrypter->decrypt($encrypted, "test");
/** @var string */
const TEST_KEY_16 = '7sLGx1aCk9Hx3l9w';

$this->assertEquals($data, $decrypted);
}
/** @var string */
const TEST_KEY_32 = 'xe7w5dKMy8yTpKENL6THonWBLFlTNMwa';

/** @var DataEncrypter */
private static $dataEncrypter;

public function testEncryptStringWithMaxKey()
/**
* @inheritDoc
*/
public static function setUpBeforeClass(): void
{
$data = "This is a test with a long key of 32 chars";
$encrypter = new DataEncrypter();
$encrypted = $encrypter->encrypt($data, "maximum_key_with_thirty_two_char");
$decrypted = $encrypter->decrypt($encrypted, "maximum_key_with_thirty_two_char");
parent::setUpBeforeClass();

$this->assertEquals($data, $decrypted);
self::$dataEncrypter = new DataEncrypter();
}

public function testEncryptArray()
/**
* Data with different scenarios to test encrypt and decrypt.
*
* @return array
*/
public function successDataProvider(): array
{
$data = array("title" => "This is a test", "description" => "This is a test about encryption");
$encrypter = new DataEncrypter();
$encrypted = $encrypter->encrypt($data, "test");
$decrypted = $encrypter->decrypt($encrypted, "test");

$this->assertEquals($data, $decrypted);
return [
['Test data', self::TEST_KEY_16],
['Test data', self::TEST_KEY_32],
[['title' => 'Test', 'description' => 'Data'], self::TEST_KEY_16],
['', self::TEST_KEY_16],
[null, self::TEST_KEY_16],
];
}

public function testEncryptStringFail()
/**
* Test to encrypt and decrypt data successfully.
*
* @dataProvider successDataProvider
*/
public function testDecryptSuccess($data, $key): void
{
$data = "This is a test";
$encrypter = new DataEncrypter();
$encrypted = $encrypter->encrypt($data, "test");
$decrypted = $encrypter->decrypt($encrypted, "testkey");
$encrypted = self::$dataEncrypter->encrypt($data, $key);
$decrypted = self::$dataEncrypter->decrypt($encrypted, $key);

$this->assertNotEquals($data, $decrypted);
$this->assertEquals($data, $decrypted);
}

public function testEncryptStringEmpty()
/**
* Test to encrypt and decrypt using different keys.
*/
public function testDecryptFail(): void
{
$data = "";
$encrypter = new DataEncrypter();
$encrypted = $encrypter->encrypt($data, "test");
$decrypted = $encrypter->decrypt($encrypted, "testkey");
$data = "Test data";
$encrypted = self::$dataEncrypter->encrypt($data, self::TEST_KEY_16);
$decrypted = self::$dataEncrypter->decrypt($encrypted, self::TEST_KEY_32);

$this->assertEquals($data, $encrypted);
$this->assertEquals($data, $decrypted);
$this->assertNotEquals($data, $decrypted);
}
}
}
9 changes: 6 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,13 @@
"psr-4": { "CrossKnowledge\\FeedbackDataEncrypterBundle\\": "" }
},
"require": {
"php": ">=5.5",
"symfony/symfony": ">=2.6 <4.0"
"php": ">=7.2",
"phpseclib/mcrypt_compat": "^1.0",
"ext-json": "*",
"symfony/symfony": "^3.0|^4.0"
},
"require-dev": {
"phpunit/phpunit": "~3.7"
"phpunit/phpunit": "^8",
"symfony/phpunit-bridge": "^6"
}
}
Loading

0 comments on commit 665af93

Please sign in to comment.