2018-08-11 23:46:28 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Engelsystem\Test\Unit\Middleware;
|
|
|
|
|
2018-10-08 19:30:37 +02:00
|
|
|
use Engelsystem\Helpers\Authenticator;
|
2019-07-08 01:31:59 +02:00
|
|
|
use Engelsystem\Helpers\Translation\Translator;
|
2018-08-11 23:46:28 +02:00
|
|
|
use Engelsystem\Http\Request;
|
|
|
|
use Engelsystem\Middleware\LegacyMiddleware;
|
2019-04-24 10:45:00 +02:00
|
|
|
use PHPUnit\Framework\MockObject\MockObject;
|
2018-08-11 23:46:28 +02:00
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
use Psr\Container\ContainerInterface;
|
|
|
|
use Psr\Http\Message\ResponseInterface;
|
|
|
|
use Psr\Http\Message\ServerRequestInterface;
|
|
|
|
use Psr\Http\Server\RequestHandlerInterface;
|
|
|
|
use Symfony\Component\HttpFoundation\ParameterBag;
|
|
|
|
|
|
|
|
class LegacyMiddlewareTest extends TestCase
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Middleware\LegacyMiddleware::__construct
|
|
|
|
* @covers \Engelsystem\Middleware\LegacyMiddleware::process
|
|
|
|
*/
|
|
|
|
public function testRegister()
|
|
|
|
{
|
|
|
|
/** @var ContainerInterface|MockObject $container */
|
|
|
|
$container = $this->getMockForAbstractClass(ContainerInterface::class);
|
2018-10-08 19:30:37 +02:00
|
|
|
/** @var Authenticator|MockObject $auth */
|
|
|
|
$auth = $this->createMock(Authenticator::class);
|
2018-08-11 23:46:28 +02:00
|
|
|
/** @var LegacyMiddleware|MockObject $middleware */
|
|
|
|
$middleware = $this->getMockBuilder(LegacyMiddleware::class)
|
2018-10-08 19:30:37 +02:00
|
|
|
->setConstructorArgs([$container, $auth])
|
2018-08-11 23:46:28 +02:00
|
|
|
->setMethods(['loadPage', 'renderPage'])
|
|
|
|
->getMock();
|
|
|
|
/** @var Request|MockObject $defaultRequest */
|
|
|
|
$defaultRequest = $this->createMock(Request::class);
|
|
|
|
/** @var ParameterBag|MockObject $parameters */
|
|
|
|
$parameters = $this->createMock(ParameterBag::class);
|
|
|
|
/** @var ResponseInterface|MockObject $response */
|
|
|
|
$response = $this->getMockForAbstractClass(ResponseInterface::class);
|
|
|
|
/** @var RequestHandlerInterface|MockObject $handler */
|
|
|
|
$handler = $this->getMockForAbstractClass(RequestHandlerInterface::class);
|
|
|
|
/** @var ServerRequestInterface|MockObject $request */
|
|
|
|
$request = $this->getMockForAbstractClass(ServerRequestInterface::class);
|
2018-08-29 22:05:38 +02:00
|
|
|
/** @var Translator|MockObject $translator */
|
|
|
|
$translator = $this->getMockBuilder(Translator::class)
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
2018-08-11 23:46:28 +02:00
|
|
|
|
2018-11-21 12:17:28 +01:00
|
|
|
$middleware->expects($this->once())
|
2018-08-11 23:46:28 +02:00
|
|
|
->method('loadPage')
|
2018-11-21 12:17:28 +01:00
|
|
|
->with('user_worklog')
|
|
|
|
->willReturn(['title', 'content']);
|
2018-08-11 23:46:28 +02:00
|
|
|
|
2018-11-21 12:17:28 +01:00
|
|
|
$middleware->expects($this->exactly(2))
|
2018-08-11 23:46:28 +02:00
|
|
|
->method('renderPage')
|
|
|
|
->withConsecutive(
|
|
|
|
['user_worklog', 'title', 'content'],
|
2018-11-21 12:17:28 +01:00
|
|
|
['404', 'Page not found', 'It\'s not available!']
|
2018-08-11 23:46:28 +02:00
|
|
|
)
|
|
|
|
->willReturn($response);
|
|
|
|
|
2018-11-21 12:17:28 +01:00
|
|
|
$container->expects($this->exactly(3))
|
2018-08-11 23:46:28 +02:00
|
|
|
->method('get')
|
2018-11-21 12:17:28 +01:00
|
|
|
->withConsecutive(['request'], ['request'], ['translator'])
|
2018-08-29 22:05:38 +02:00
|
|
|
->willReturnOnConsecutiveCalls(
|
|
|
|
$defaultRequest,
|
|
|
|
$defaultRequest,
|
2018-11-21 12:17:28 +01:00
|
|
|
$translator
|
2018-08-29 22:05:38 +02:00
|
|
|
);
|
|
|
|
|
2018-11-12 14:41:23 +01:00
|
|
|
$auth->expects($this->atLeastOnce())
|
|
|
|
->method('can')
|
|
|
|
->willReturn(false);
|
2018-10-08 19:30:37 +02:00
|
|
|
|
2018-08-29 22:05:38 +02:00
|
|
|
$translator->expects($this->exactly(2))
|
|
|
|
->method('translate')
|
|
|
|
->willReturnOnConsecutiveCalls(
|
|
|
|
'Page not found',
|
|
|
|
'It\'s not available!'
|
|
|
|
);
|
2018-08-11 23:46:28 +02:00
|
|
|
|
|
|
|
$defaultRequest->query = $parameters;
|
|
|
|
$defaultRequest->expects($this->once())
|
|
|
|
->method('path')
|
|
|
|
->willReturn('user-worklog');
|
|
|
|
|
2018-11-21 12:17:28 +01:00
|
|
|
$parameters->expects($this->exactly(2))
|
2018-08-11 23:46:28 +02:00
|
|
|
->method('get')
|
|
|
|
->with('p')
|
|
|
|
->willReturnOnConsecutiveCalls(
|
|
|
|
null,
|
2018-11-21 12:17:28 +01:00
|
|
|
'foo'
|
2018-08-11 23:46:28 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
$middleware->process($request, $handler);
|
|
|
|
$middleware->process($request, $handler);
|
|
|
|
}
|
|
|
|
}
|