Skip to content

Commit

Permalink
Add /receive-code
Browse files Browse the repository at this point in the history
  • Loading branch information
chadicus committed Sep 22, 2015
1 parent b4aca53 commit f22a7c7
Show file tree
Hide file tree
Showing 3 changed files with 166 additions and 0 deletions.
59 changes: 59 additions & 0 deletions src/ReceiveCode.php
@@ -0,0 +1,59 @@
<?php

namespace Chadicus\Slim\OAuth2\Routes;

use Slim\Slim;

final class ReceiveCode
{
const ROUTE = '/receive-code';

/**
* The slim framework application instance.
*
* @var Slim $slim
*/
private $slim;

/**
* The template for /receive-code
*
* @var string
*/
private $template;

/**
* Construct a new instance of ReceiveCode route.
*
* @param Slim $slim The slim framework application instance.
* @param string $template The template for /receive-code
*/
public function __construct(Slim $slim, $template = 'receive-code.phtml')
{
$this->slim = $slim;
$this->template = $template;
}

/**
* Call this class as a function.
*
* @return void
*/
public function __invoke()
{
$this->slim->render($this->template, ['code' => $this->slim->request()->params('code')]);
}

/**
* Register this route with the given Slim application and OAuth2 server
*
* @param Slim $slim The slim framework application instance.
* @param string $template The template for /receive-code
*
* @return void
*/
public static function register(Slim $slim, $template = 'receive-code.phtml')
{
$slim->map(self::ROUTE, new self($slim, $template))->via('GET', 'POST')->name('receive-code');
}
}
1 change: 1 addition & 0 deletions templates/receive-code.phtml
@@ -0,0 +1 @@
<h2>The authorization code is <?php echo $this->data['code']; ?></h2>
106 changes: 106 additions & 0 deletions tests/ReceiveCodeTest.php
@@ -0,0 +1,106 @@
<?php

namespace ChadicusTest\Slim\OAuth2\Routes;

use Chadicus\Slim\OAuth2\Routes\ReceiveCode;

/**
* Unit tests for the \Chadicus\Slim\OAuth2\Routes\ReceiveCode class.
*
* @coversDefaultClass \Chadicus\Slim\OAuth2\Routes\ReceiveCode
* @covers ::<private>
* @covers ::__construct
*/
final class ReceiveCodeTest extends \PHPUnit_Framework_TestCase
{
/**
* Verify basic behavior of __invoke()
*
* @test
* @covers ::__invoke
*
* @return void
*/
public function invoke()
{
$storage = new \OAuth2\Storage\Memory(
[
'client_credentials' => [
'testClientId' => [
'client_id' => 'testClientId',
'client_secret' => 'testClientSecret',
'redirect_uri' => '/receive-code',
],
],
]
);

$server = new \OAuth2\Server(
$storage,
[
'access_lifetime' => 3600,
],
[
new \OAuth2\GrantType\ClientCredentials($storage),
]
);

$code = md5(time());

\Slim\Environment::mock(
[
'REQUEST_METHOD' => 'POST',
'CONTENT_TYPE' => 'application/json',
'PATH_INFO' => '/receive-code',
'QUERY_STRING' => "code={$code}&state=xyz",
]
);

$slim = new \Slim\Slim();
$slim->post('/receive-code', new ReceiveCode($slim));

ob_start();

$slim->run();

ob_get_clean();

$this->assertSame(200, $slim->response->status());

$expected = <<<HTML
<h2>The authorization code is {$code}</h2>
HTML;

$this->assertSame($expected, $slim->response->getBody());
}

/**
* Verify basic behavior of register
*
* @test
* @covers ::register
*
* @return void
*/
public function register()
{
$storage = new \OAuth2\Storage\Memory([]);
$server = new \OAuth2\Server($storage, [], []);

\Slim\Environment::mock();

$slim = new \Slim\Slim();

ReceiveCode::register($slim);

$route = $slim->router()->getNamedRoute('receive-code');

$this->assertInstanceOf('\Slim\Route', $route);
$this->assertInstanceOf('\Chadicus\Slim\OAuth2\Routes\ReceiveCode', $route->getCallable());
$this->assertSame(
[\Slim\Http\Request::METHOD_GET, \Slim\Http\Request::METHOD_POST],
$route->getHttpMethods()
);
}
}

0 comments on commit f22a7c7

Please sign in to comment.