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
|
|
|
{
|
2018-03-01 02:03:19 +05:30
|
|
|
protected $cryptStub;
|
|
|
|
|
|
|
|
protected function setUp()
|
|
|
|
{
|
|
|
|
$this->cryptStub = new CryptTraitStub();
|
|
|
|
}
|
|
|
|
|
2017-11-20 12:12:09 +05:30
|
|
|
public function testEncryptDecryptWithPassword()
|
2016-03-18 01:48:28 +05:30
|
|
|
{
|
2018-03-01 02:03:19 +05:30
|
|
|
$this->cryptStub->setEncryptionKey(base64_encode(random_bytes(36)));
|
2017-11-20 12:12:09 +05:30
|
|
|
|
2018-03-01 02:03:19 +05:30
|
|
|
$this->encryptDecrypt();
|
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
|
|
|
{
|
2018-03-01 02:03:19 +05:30
|
|
|
$this->cryptStub->setEncryptionKey(Key::createNewRandomKey());
|
2017-11-20 12:12:09 +05:30
|
|
|
|
2018-03-01 02:03:19 +05:30
|
|
|
$this->encryptDecrypt();
|
2017-11-20 12:12:09 +05:30
|
|
|
}
|
|
|
|
|
2018-03-01 02:03:19 +05:30
|
|
|
private function encryptDecrypt()
|
2018-03-01 01:31:01 +05:30
|
|
|
{
|
2016-03-18 01:48:28 +05:30
|
|
|
$payload = 'alex loves whisky';
|
2018-03-01 02:03:19 +05:30
|
|
|
$encrypted = $this->cryptStub->doEncrypt($payload);
|
|
|
|
$plainText = $this->cryptStub->doDecrypt($encrypted);
|
2016-03-18 01:48:28 +05:30
|
|
|
|
|
|
|
$this->assertNotEquals($payload, $encrypted);
|
|
|
|
$this->assertEquals($payload, $plainText);
|
|
|
|
}
|
|
|
|
}
|