2016-03-18 01:48:28 +05:30
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace LeagueTests\Utils;
|
|
|
|
|
2017-11-20 12:12:09 +05:30
|
|
|
use Defuse\Crypto\Key;
|
2016-03-18 01:48:28 +05:30
|
|
|
use LeagueTests\Stubs\CryptTraitStub;
|
2017-11-08 23:37:07 +05:30
|
|
|
use PHPUnit\Framework\TestCase;
|
2016-03-18 01:48:28 +05:30
|
|
|
|
2017-11-08 23:37:07 +05:30
|
|
|
class CryptTraitTest extends TestCase
|
2016-03-18 01:48:28 +05:30
|
|
|
{
|
2017-11-20 12:12:09 +05:30
|
|
|
public function testEncryptDecryptWithPassword()
|
2016-03-18 01:48:28 +05:30
|
|
|
{
|
2017-11-20 12:12:09 +05:30
|
|
|
$cryptStub = new CryptTraitStub();
|
|
|
|
$cryptStub->setEncryptionKey(base64_encode(random_bytes(36)));
|
|
|
|
|
|
|
|
return $this->encryptDecrypt($cryptStub);
|
2016-03-18 01:48:28 +05:30
|
|
|
}
|
|
|
|
|
2017-11-20 12:12:09 +05:30
|
|
|
public function testEncryptDecryptWithKey()
|
2016-03-18 01:48:28 +05:30
|
|
|
{
|
2017-11-20 12:12:09 +05:30
|
|
|
$cryptStub = new CryptTraitStub();
|
|
|
|
$cryptStub->setEncryptionKey(Key::createNewRandomKey());
|
|
|
|
|
|
|
|
return $this->encryptDecrypt($cryptStub);
|
|
|
|
}
|
|
|
|
|
2018-03-01 01:31:01 +05:30
|
|
|
protected function encryptDecrypt(CryptTraitStub $cryptStub)
|
|
|
|
{
|
2016-03-18 01:48:28 +05:30
|
|
|
$payload = 'alex loves whisky';
|
2017-11-20 12:12:09 +05:30
|
|
|
$encrypted = $cryptStub->doEncrypt($payload);
|
|
|
|
$plainText = $cryptStub->doDecrypt($encrypted);
|
2016-03-18 01:48:28 +05:30
|
|
|
|
|
|
|
$this->assertNotEquals($payload, $encrypted);
|
|
|
|
$this->assertEquals($payload, $plainText);
|
|
|
|
}
|
|
|
|
}
|