oauth2-server/tests/Utils/CryptTraitTest.php

37 lines
958 B
PHP
Raw Normal View History

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