oauth2-server/tests/Utils/CryptTraitTest.php

37 lines
954 B
PHP
Raw Normal View History

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