|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace Shoot\Shoot\Tests\Unit\Middleware; |
|
5
|
|
|
|
|
6
|
|
|
use PHPUnit\Framework\MockObject\MockObject; |
|
7
|
|
|
use PHPUnit\Framework\TestCase; |
|
8
|
|
|
use Psr\Container\ContainerInterface; |
|
9
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
10
|
|
|
use Shoot\Shoot\Middleware\PresenterMiddleware; |
|
11
|
|
|
use Shoot\Shoot\MiddlewareInterface; |
|
12
|
|
|
use Shoot\Shoot\PresentationModel; |
|
13
|
|
|
use Shoot\Shoot\PresenterInterface; |
|
14
|
|
|
use Shoot\Shoot\Tests\Fixtures\ViewFactory; |
|
15
|
|
|
use Shoot\Shoot\View; |
|
16
|
|
|
|
|
17
|
|
|
final class PresenterMiddlewareTest extends TestCase |
|
18
|
|
|
{ |
|
19
|
|
|
/** @var MiddlewareInterface */ |
|
20
|
|
|
private $middleware; |
|
21
|
|
|
|
|
22
|
|
|
/** @var callable */ |
|
23
|
|
|
private $next; |
|
24
|
|
|
|
|
25
|
|
|
/** @var ServerRequestInterface|MockObject */ |
|
26
|
|
|
private $request; |
|
27
|
|
|
|
|
28
|
|
|
/** @var View */ |
|
29
|
|
|
private $view; |
|
30
|
|
|
|
|
31
|
|
|
protected function setUp(): void |
|
32
|
|
|
{ |
|
33
|
|
|
$presenter = new class implements PresenterInterface |
|
34
|
|
|
{ |
|
35
|
|
|
public function present( |
|
36
|
|
|
ServerRequestInterface $request, |
|
37
|
|
|
PresentationModel $presentationModel |
|
38
|
|
|
): PresentationModel { |
|
39
|
|
|
return $presentationModel->withVariables(['variable' => 'variable']); |
|
40
|
|
|
} |
|
41
|
|
|
}; |
|
42
|
|
|
|
|
43
|
|
|
/** @var ContainerInterface|MockObject $container */ |
|
44
|
|
|
$container = $this->createMock(ContainerInterface::class); |
|
45
|
|
|
$container |
|
46
|
|
|
->expects($this->once()) |
|
47
|
|
|
->method('get') |
|
48
|
|
|
->with('MockPresenter') |
|
49
|
|
|
->willReturn($presenter); |
|
50
|
|
|
|
|
51
|
|
|
$this->middleware = new PresenterMiddleware($container); |
|
52
|
|
|
|
|
53
|
|
|
$this->next = function (View $view): View { |
|
54
|
|
|
return $view; |
|
55
|
|
|
}; |
|
56
|
|
|
|
|
57
|
|
|
$this->request = $this->createMock(ServerRequestInterface::class); |
|
58
|
|
|
|
|
59
|
|
|
$this->view = ViewFactory::create(); |
|
60
|
|
|
|
|
61
|
|
|
parent::setUp(); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
public function testShouldLoadPresenterIfPresentationModelHasPresenter(): void |
|
65
|
|
|
{ |
|
66
|
|
|
$this->assertEmpty($this->view->getPresentationModel()->getVariable('variable', '')); |
|
67
|
|
|
|
|
68
|
|
|
$view = $this->middleware->process($this->view, $this->request, $this->next); |
|
69
|
|
|
|
|
70
|
|
|
$this->assertNotEmpty($view->getPresentationModel()->getVariable('variable', '')); |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|