Skip to content
This repository has been archived by the owner on Jan 29, 2020. It is now read-only.

Commit

Permalink
Refactors factory tests
Browse files Browse the repository at this point in the history
Ensures that the factories are testing expected behavior:

- TypeError should be raised for a non-callable response factory.
- TypeError should be raised for a response instance returned instead of a response factory.
  • Loading branch information
weierophinney committed Feb 27, 2018
1 parent f9ce613 commit 6caabba
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 13 deletions.
31 changes: 25 additions & 6 deletions test/OAuth2AdapterFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
use Prophecy\Prophecy\ObjectProphecy;
use Psr\Container\ContainerInterface;
use Psr\Http\Message\ResponseInterface;
use stdClass;
use TypeError;
use Zend\Expressive\Authentication\AuthenticationInterface;
use Zend\Expressive\Authentication\OAuth2\OAuth2Adapter;
use Zend\Expressive\Authentication\OAuth2\OAuth2AdapterFactory;
Expand Down Expand Up @@ -58,7 +60,7 @@ public function testInvokeWithEmptyContainer()
$oauth2Adapter = $factory($this->container->reveal());
}

public function testInvokeWithResourceServerEmptyResponse()
public function testFactoryRaisesTypeErrorForNonCallableResponseFactory()
{
$this->container
->has(ResourceServer::class)
Expand All @@ -69,17 +71,34 @@ public function testInvokeWithResourceServerEmptyResponse()

$this->container
->get(ResponseInterface::class)
->willReturn(function () {
});
->willReturn(new stdClass());

$factory = new OAuth2AdapterFactory();

$this->expectException(TypeError::class);
$adapter = $factory($this->container->reveal());
}

$this->assertInstanceOf(OAuth2Adapter::class, $adapter);
$this->assertInstanceOf(AuthenticationInterface::class, $adapter);
public function testFactoryRaisesTypeErrorWhenResponseServiceProvidesResponseInstance()
{
$this->container
->has(ResourceServer::class)
->willReturn(true);
$this->container
->get(ResourceServer::class)
->willReturn($this->resourceServer->reveal());

$this->container
->get(ResponseInterface::class)
->will([$this->response, 'reveal']);

$factory = new OAuth2AdapterFactory();

$this->expectException(TypeError::class);
$adapter = $factory($this->container->reveal());
}

public function testInvokeResourceServerAndResponse()
public function testFactoryReturnsInstanceWhenAppropriateDependenciesArePresentInContainer()
{
$this->container
->has(ResourceServer::class)
Expand Down
41 changes: 34 additions & 7 deletions test/OAuth2MiddlewareFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@

use League\OAuth2\Server\AuthorizationServer;
use PHPUnit\Framework\TestCase;
use Prophecy\Prophecy\ObjectProphecy;
use Psr\Container\ContainerInterface;
use Psr\Http\Message\ResponseInterface;
use stdClass;
use TypeError;
use Zend\Expressive\Authentication\OAuth2\Exception\InvalidConfigException;
use Zend\Expressive\Authentication\OAuth2\OAuth2Middleware;
use Zend\Expressive\Authentication\OAuth2\OAuth2MiddlewareFactory;
Expand All @@ -23,6 +26,15 @@
*/
class OAuth2MiddlewareFactoryTest extends TestCase
{
/** @var AuthorizationServer|ObjectProphecy */
private $authServer;

/** @var AuthServer|ObjectProphecy */
private $container;

/** @var ResponseInterface|ObjectProphecy */
private $response;

public function setUp()
{
$this->container = $this->prophesize(ContainerInterface::class);
Expand All @@ -44,7 +56,7 @@ public function testInvokeWithEmptyContainer()
$middleware = $factory($this->container->reveal());
}

public function testInvokeWithAuthServerWithoutResponseInterface()
public function testFactoryRaisesTypeErrorForNonCallableResponseFactory()
{
$this->container
->has(AuthorizationServer::class)
Expand All @@ -54,21 +66,36 @@ public function testInvokeWithAuthServerWithoutResponseInterface()
->willReturn($this->authServer->reveal());
$this->container
->get(ResponseInterface::class)
->willReturn(function () {
});
->willReturn(new stdClass());

$factory = new OAuth2MiddlewareFactory();
$middleware = $factory($this->container->reveal());
$this->assertInstanceOf(OAuth2Middleware::class, $middleware);

$this->expectException(TypeError::class);
$factory($this->container->reveal());
}

public function testInvokeWithAuthServerWithResponseInterface()
public function testFactoryRaisesTypeErrorWhenResponseServiceProvidesResponseInstance()
{
$this->container
->has(AuthorizationServer::class)
->willReturn(true);
$this->container
->has(ResponseInterface::class)
->get(AuthorizationServer::class)
->willReturn($this->authServer->reveal());
$this->container
->get(ResponseInterface::class)
->will([$this->response, 'reveal']);

$factory = new OAuth2MiddlewareFactory();

$this->expectException(TypeError::class);
$factory($this->container->reveal());
}

public function testFactoryReturnsInstanceWhenAppropriateDependenciesArePresentInContainer()
{
$this->container
->has(AuthorizationServer::class)
->willReturn(true);
$this->container
->get(AuthorizationServer::class)
Expand Down

0 comments on commit 6caabba

Please sign in to comment.