Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Middleware unit tests #108

Merged
merged 10 commits into from Aug 27, 2019
@@ -0,0 +1,94 @@
<?php
namespace Yiisoft\Yii\Web\Tests\Middleware;
use Http\Message\ResponseFactory;
use Nyholm\Psr7\Response;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\ResponseFactoryInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Yiisoft\Yii\Web\Middleware\IpFilter;
use PHPUnit_Framework_MockObject_MockObject;
class IpFilterTest extends TestCase
{
private const REQUEST_PARAMS = [
'REMOTE_ADDR' => '8.8.8.8',
];
private const ALLOWED_IP = '1.1.1.1';
/**
* @var ResponseFactory|PHPUnit_Framework_MockObject_MockObject
*/
private $responseFactoryMock;
/**
* @var RequestHandlerInterface|PHPUnit_Framework_MockObject_MockObject
*/
private $requestHandlerMock;
private $ipFilter;
public function setUp()
{
parent::setUp();
$this->responseFactoryMock = $this->createMock(ResponseFactoryInterface::class);
$this->requestHandlerMock = $this->createMock(RequestHandlerInterface::class);
$this->ipFilter = new IpFilter(self::ALLOWED_IP, $this->responseFactoryMock);
}
/**
* @test
*/
public function processReturnsAccessDeniedResponseWhenIpIsNotAllowed()
{
$this->setUpResponseFactory();
$requestMock = $this->createMock(ServerRequestInterface::class);
$requestMock
->expects($this->once())
->method('getServerParams')
->willReturn(self::REQUEST_PARAMS);
$this->requestHandlerMock
->expects($this->never())
->method('handle')
->with($requestMock);
$response = $this->ipFilter->process($requestMock, $this->requestHandlerMock);
$this->assertEquals(403, $response->getStatusCode());
}
/**
* @test
*/
public function processCallsRequestHandlerWhenRemoteAddressIsAllowed()
{
$requestParams = [
'REMOTE_ADDR' => self::ALLOWED_IP,
];
$requestMock = $this->createMock(ServerRequestInterface::class);
$requestMock
->expects($this->once())
->method('getServerParams')
->willReturn($requestParams);
$this->requestHandlerMock
->expects($this->once())
->method('handle')
->with($requestMock);
$this->ipFilter->process($requestMock, $this->requestHandlerMock);
}
public function setUpResponseFactory()
{
$response = new Response(403);
$this->responseFactoryMock
->expects($this->once())
->method('createResponse')
->willReturn($response);
}
}
@@ -0,0 +1,114 @@
<?php
namespace Yiisoft\Yii\Web\Tests;
use PHPUnit\Framework\TestCase;
use Psr\Container\ContainerInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Yiisoft\Di\Container;
use PHPUnit_Framework_MockObject_MockObject;
use Yiisoft\Yii\Web\Emitter\SapiEmitter;
use Yiisoft\Yii\Web\MiddlewareDispatcher;
class MiddlewareDispatcherTest extends TestCase
{
/**
* @var MiddlewareDispatcher
*/
private $middlewareDispatcher;
/**
* @var Container|PHPUnit_Framework_MockObject_MockObject
*/
private $containerMock;
/**
* @var RequestHandlerInterface|PHPUnit_Framework_MockObject_MockObject
*/
private $fallbackHandlerMock;
/**
* @var MiddlewareInterface[]|PHPUnit_Framework_MockObject_MockObject[]
*/
private $middlewareMocks;
public function setUp()
{
parent::setUp();
$this->containerMock = $this->createMock(ContainerInterface::class);
$this->fallbackHandlerMock = $this->createMock(RequestHandlerInterface::class);
$this->middlewareMocks = [
$this->createMock(MiddlewareInterface::class),
$this->createMock(MiddlewareInterface::class)
];
$this->middlewareDispatcher = new MiddlewareDispatcher($this->middlewareMocks, $this->containerMock, $this->fallbackHandlerMock);
}
/**
* @test
*/
public function constructThrowsExceptionWhenMiddlewaresAreNotDefined()
{
$this->expectException(\InvalidArgumentException::class);
new MiddlewareDispatcher(
[],
$this->containerMock,
$this->fallbackHandlerMock
);
}
/**
* @test
*/
public function addThrowsInvalidArgumentExceptionWhenMiddlewareIsNotOfCorrectType()
{
$this->expectException(\InvalidArgumentException::class);
$exampleInput = new SapiEmitter();
$this->middlewareDispatcher->add($exampleInput);
}
/**
* @test
* @doesNotPerformAssertions
*/
public function addAddsCallableToMiddlewareArrayWithoutThrowingException()
{
$callable = function () {
echo 'example function for testing purposes';
};
$this->middlewareDispatcher->add($callable);
}
/**
* @test
* @doesNotPerformAssertions
*/
public function addAddsMiddlewareInterfaceToMiddlewareArrayWithoutThrowingException()
{
$middleware = $this->createMock(MiddlewareInterface::class);
$this->middlewareDispatcher->add($middleware);
}
/**
* @test
*/
public function handleCallsMiddlewareFromQueueToProcessRequest()
{
$request = $this->createMock(ServerRequestInterface::class);
$this->fallbackHandlerMock
->expects($this->never())
->method('handle')
->with($request);
$this->middlewareMocks[0]
->expects($this->once())
->method('process')
->with($request, $this->middlewareDispatcher);
$this->middlewareDispatcher->handle($request);
}
}
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.