Skip to content

Commit

Permalink
CryptKey tests
Browse files Browse the repository at this point in the history
  • Loading branch information
juliangut committed Jul 19, 2016
1 parent 039537e commit 065ef5d
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/CryptKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,18 @@ public function __construct($keyPath, $passPhrase = null)
/**
* @param string $key
*
* @throws \RuntimeException
*
* @return string
*/
private function saveKeyToFile($key)
{
$keyPath = sys_get_temp_dir() . '/' . sha1($key) . '.key';

if (!file_exists($keyPath) && !touch($keyPath)) {
// @codeCoverageIgnoreStart
throw new \RuntimeException('"%s" key file could not be created', $keyPath);
// @codeCoverageIgnoreEnd
}

file_put_contents($keyPath, $key);
Expand Down
36 changes: 36 additions & 0 deletions tests/CryptKeyTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace LeagueTests\Utils;

use League\OAuth2\Server\CryptKey;

class CryptKeyTest extends \PHPUnit_Framework_TestCase
{
/**
* @expectedException \LogicException
*/
public function testNoFile()
{
new CryptKey('undefined file');
}

public function testKeyCreation()
{
$keyFile = __DIR__ . '/Stubs/public.key';
$key = new CryptKey($keyFile, 'secret');

$this->assertEquals('file://' . $keyFile, $key->getKeyPath());
$this->assertEquals('secret', $key->getPassPhrase());
}

public function testKeyFileCreation()
{
$keyContent = file_get_contents(__DIR__ . '/Stubs/public.key');
$key = new CryptKey($keyContent);

$this->assertEquals(
'file://' . sys_get_temp_dir() . '/' . sha1($keyContent) . '.key',
$key->getKeyPath()
);
}
}

0 comments on commit 065ef5d

Please sign in to comment.