-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from CrossKnowledge/fix/CKLS-12322
Symfony 4.0 compatibility.
- Loading branch information
Showing
5 changed files
with
2,572 additions
and
818 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.