oauth2-server/tests/Utils/CryptTraitTest.php

42 lines
960 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
{
2018-03-01 02:03:19 +05:30
protected $cryptStub;
protected function setUp(): void
2018-03-01 02:03:19 +05:30
{
$this->cryptStub = new CryptTraitStub();
}
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)));
2018-03-01 02:03:19 +05:30
$this->encryptDecrypt();
2016-03-18 01:48:28 +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());
2018-03-01 02:03:19 +05:30
$this->encryptDecrypt();
}
2018-03-01 02:03:19 +05:30
private function encryptDecrypt()
{
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);
}
}