1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | namespace VM\SlimHtmlMinify\Tests\Middlewares; |
||
6 | |||
7 | use PHPUnit\Framework\TestCase; |
||
8 | use Psr\Container\ContainerInterface; |
||
9 | use VM\Psr15Mocks\Middleware; |
||
10 | use VM\SlimHtmlMinify\Middlewares\HtmlMinify; |
||
11 | use voku\helper\HtmlMin; |
||
12 | |||
13 | class HtmlMinifyTest extends TestCase |
||
14 | { |
||
15 | use Middleware; |
||
16 | |||
17 | public function setup(): void |
||
18 | { |
||
19 | $this->buildHtmlMinify(); |
||
20 | $this->buildRequestHandler(); |
||
21 | $this->buildResponse(); |
||
22 | $this->buildRequest(); |
||
23 | $this->buildBody(); |
||
24 | $this->buildContainer(); |
||
25 | } |
||
26 | |||
27 | public function tearDown(): void |
||
28 | { |
||
29 | $this->destroyContainer(); |
||
30 | $this->destroyBody(); |
||
31 | $this->destroyRequest(); |
||
32 | $this->destroyResponse(); |
||
33 | $this->destroyRequestHandler(); |
||
34 | $this->destroyHtmlMin(); |
||
35 | } |
||
36 | |||
37 | public function testInvokeShouldMinifyTheHtml() |
||
38 | { |
||
39 | $unminifiedHtml = ' foo '; |
||
40 | $minifiedHtml = 'foo'; |
||
41 | $this->requestHandler->expects($this->once())->method('handle')->willReturn($this->response); |
||
42 | $this->htmlMin->expects($this->once())->method('minify')->willReturn($minifiedHtml); |
||
43 | $this->container->expects($this->once())->method('get')->with('body')->willReturn($this->body); |
||
44 | $this->body->expects($this->once())->method('__toString')->willReturn($unminifiedHtml); |
||
45 | $this->response->expects($this->once())->method('getBody')->willReturn($this->body); |
||
46 | $this->response->expects($this->once())->method('withBody')->willReturn($this->response); |
||
47 | |||
48 | $middleware = new HtmlMinify($this->htmlMin, $this->container, true); |
||
49 | $middleware->process($this->request, $this->requestHandler); |
||
50 | } |
||
51 | |||
52 | public function testInvokeShouldNotMinifyWhenInitializedDeactivated() |
||
53 | { |
||
54 | $this->requestHandler->expects($this->once())->method('handle')->willReturn($this->response); |
||
55 | $this->htmlMin->expects($this->never())->method('minify'); |
||
56 | $this->container->expects($this->never())->method('get'); |
||
57 | $this->body->expects($this->never())->method('__toString'); |
||
58 | |||
59 | $middleware = new HtmlMinify($this->htmlMin, $this->container, false); |
||
60 | $middleware->process($this->request, $this->requestHandler); |
||
61 | } |
||
62 | |||
63 | private function buildHtmlMinify(): void |
||
64 | { |
||
65 | $this->htmlMin = $this->getMockBuilder(HtmlMin::class) |
||
0 ignored issues
–
show
Bug
Best Practice
introduced
by
![]() |
|||
66 | ->disableOriginalConstructor() |
||
67 | ->setMethods(['minify']) |
||
68 | ->getMock() |
||
69 | ; |
||
70 | } |
||
71 | |||
72 | private function destroyHtmlMin(): void |
||
73 | { |
||
74 | $this->htmlMin = null; |
||
0 ignored issues
–
show
|
|||
75 | } |
||
76 | |||
77 | private function buildContainer(): void |
||
78 | { |
||
79 | $this->container = $this->getMockBuilder(ContainerInterface::class) |
||
0 ignored issues
–
show
|
|||
80 | ->disableOriginalConstructor() |
||
81 | ->setMethods(['get', 'has']) |
||
82 | ->getMock() |
||
83 | ; |
||
84 | } |
||
85 | |||
86 | private function destroyContainer(): void |
||
87 | { |
||
88 | $this->container = null; |
||
0 ignored issues
–
show
|
|||
89 | } |
||
90 | } |
||
91 |