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
|
|
|
/** |
32
|
|
|
* @return void |
33
|
|
|
*/ |
34
|
|
|
protected function setUp() |
35
|
|
|
{ |
36
|
|
|
$presenter = new class implements PresenterInterface |
37
|
|
|
{ |
38
|
|
|
public function present( |
39
|
|
|
ServerRequestInterface $request, |
40
|
|
|
PresentationModel $presentationModel |
41
|
|
|
): PresentationModel { |
42
|
|
|
return $presentationModel->withVariables(['variable' => 'variable']); |
43
|
|
|
} |
44
|
|
|
}; |
45
|
|
|
|
46
|
|
|
/** @var ContainerInterface|MockObject $container */ |
47
|
|
|
$container = $this->createMock(ContainerInterface::class); |
48
|
|
|
$container |
49
|
|
|
->expects($this->once()) |
50
|
|
|
->method('get') |
51
|
|
|
->with('MockPresenter') |
52
|
|
|
->willReturn($presenter); |
53
|
|
|
|
54
|
|
|
$this->middleware = new PresenterMiddleware($container); |
55
|
|
|
|
56
|
|
|
$this->next = function (View $view): View { |
57
|
|
|
return $view; |
58
|
|
|
}; |
59
|
|
|
|
60
|
|
|
$this->request = $this->createMock(ServerRequestInterface::class); |
61
|
|
|
|
62
|
|
|
$this->view = ViewFactory::create(); |
63
|
|
|
|
64
|
|
|
parent::setUp(); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @return void |
69
|
|
|
*/ |
70
|
|
|
public function testShouldLoadPresenterIfPresentationModelHasPresenter() |
71
|
|
|
{ |
72
|
|
|
$this->assertEmpty($this->view->getPresentationModel()->getVariable('variable', '')); |
73
|
|
|
|
74
|
|
|
$view = $this->middleware->process($this->view, $this->request, $this->next); |
75
|
|
|
|
76
|
|
|
$this->assertNotEmpty($view->getPresentationModel()->getVariable('variable', '')); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|