engelsystem/tests/Unit/Middleware/LegacyMiddlewareTest.php

79 lines
2.9 KiB
PHP
Raw Permalink Normal View History

2018-08-11 23:46:28 +02:00
<?php
declare(strict_types=1);
2018-08-11 23:46:28 +02:00
namespace Engelsystem\Test\Unit\Middleware;
use Engelsystem\Helpers\Authenticator;
2018-08-11 23:46:28 +02:00
use Engelsystem\Http\Request;
use Engelsystem\Http\Response;
2018-08-11 23:46:28 +02:00
use Engelsystem\Middleware\LegacyMiddleware;
use Engelsystem\Test\Unit\TestCase;
use PHPUnit\Framework\MockObject\MockObject;
2018-08-11 23:46:28 +02:00
use Psr\Http\Server\RequestHandlerInterface;
class LegacyMiddlewareTest extends TestCase
{
/**
* @covers \Engelsystem\Middleware\LegacyMiddleware::__construct
* @covers \Engelsystem\Middleware\LegacyMiddleware::process
*/
public function testRegisterNotFound(): void
2018-08-11 23:46:28 +02:00
{
/** @var RequestHandlerInterface|MockObject $handler */
$handler = $this->getMockForAbstractClass(RequestHandlerInterface::class);
/** @var Authenticator|MockObject $auth */
$auth = $this->createMock(Authenticator::class);
$response = new Response();
$request = new Request(['p' => 'foo']);
$this->app->instance('request', $request);
$this->mockTranslator();
2018-08-11 23:46:28 +02:00
/** @var LegacyMiddleware|MockObject $middleware */
$middleware = $this->getMockBuilder(LegacyMiddleware::class)
->setConstructorArgs([$this->app, $auth])
->onlyMethods(['renderPage'])
2018-08-11 23:46:28 +02:00
->getMock();
$middleware->expects($this->once())
->method('renderPage')
->with(404, 'page.404.title', 'page.404.text')
->willReturn($response);
$middleware->process($request, $handler);
}
/**
* @covers \Engelsystem\Middleware\LegacyMiddleware::process
*/
public function testRegisterHasPermission(): void
{
2018-08-11 23:46:28 +02:00
/** @var RequestHandlerInterface|MockObject $handler */
$handler = $this->getMockForAbstractClass(RequestHandlerInterface::class);
/** @var Authenticator|MockObject $auth */
$auth = $this->createMock(Authenticator::class);
$auth->expects($this->exactly(2))
->method('can')
->withConsecutive(['users.arrive.list'], ['admin_arrive'])
->willReturnOnConsecutiveCalls(true, false);
$response = new Response();
$request = new Request([], [], [], [], [], ['REQUEST_URI' => 'admin-arrive']);
$this->app->instance('request', $request);
2018-08-11 23:46:28 +02:00
/** @var LegacyMiddleware|MockObject $middleware */
$middleware = $this->getMockBuilder(LegacyMiddleware::class)
->setConstructorArgs([$this->app, $auth])
->onlyMethods(['loadPage', 'renderPage'])
->getMock();
2018-11-21 12:17:28 +01:00
$middleware->expects($this->once())
2018-08-11 23:46:28 +02:00
->method('loadPage')
->with('admin_arrive')
2018-11-21 12:17:28 +01:00
->willReturn(['title', 'content']);
$middleware->expects($this->once())
2018-08-11 23:46:28 +02:00
->method('renderPage')
->with('admin_arrive', 'title', 'content')
2018-08-11 23:46:28 +02:00
->willReturn($response);
$middleware->process($request, $handler);
}
}