Completed
Push — master ( 47b9ab...741c62 )
by Thomas Mauro
01:54
created

FunctionalTest::assertNotResponseHeaderContains()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 28
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 28
rs 8.439
c 0
b 0
f 0
cc 5
eloc 16
nc 4
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Facile\ZFLinkHeadersModule\Functional;
6
7
use Facile\ZFLinkHeadersModule\Options;
8
use Facile\ZFLinkHeadersModule\OptionsInterface;
9
use PHPUnit\Framework\ExpectationFailedException;
10
use Zend\Http\Header\HeaderInterface;
11
use Zend\Http\Headers;
12
use Zend\ServiceManager\ServiceManager;
13
use Zend\Test\PHPUnit\Controller\AbstractHttpControllerTestCase;
14
15
class FunctionalTest extends AbstractHttpControllerTestCase
16
{
17
    public function setUp()
18
    {
19
        $this->setApplicationConfig(
20
            include __DIR__ . '/../config/application.config.php'
21
        );
22
23
        parent::setUp();
24
    }
25
26
    /**
27
     * @coversNothing
28
     */
29
    public function testIndexActionWithDefaultOptions()
30
    {
31
        $this->dispatch('/');
32
        // Links
33
        $this->assertResponseHeaderContains('Link', '</style/foo.css>; rel="preload"');
34
        $this->assertResponseHeaderContains('Link', '</style/bar.css>; rel="prefetch"; as="style"; type="text/stylesheet"');
35
        // Stylesheets
36
        $this->assertNotResponseHeaderContains('Link', '</style/stylesheet.css>; rel="preload"; as="style"; type="text/css"; media="screen"');
37
        // Scripts
38
        $this->assertNotResponseHeaderContains('Link', '</script/foo.js>; rel="preload"; as="script"; type="text/javascript"');
39
        $this->assertNotResponseHeaderContains('Link', '</script/bar.js>; rel="preload"; as="script"; type="text/text"');
40
    }
41
42
    /**
43
     * @coversNothing
44
     */
45
    public function testIndexActionWithStylesheetModeDefault()
46
    {
47
        /** @var ServiceManager $serviceManager */
48
        $serviceManager = $this->getApplication()->getServiceManager();
49
        $serviceManager->setAllowOverride(true);
50
        /** @var Options $options */
51
        $options = $serviceManager->get(OptionsInterface::class);
52
        $options->setStylesheetEnabled(true);
53
54
        $this->dispatch('/');
55
        // Links
56
        $this->assertResponseHeaderContains('Link', '</style/foo.css>; rel="preload"');
57
        $this->assertResponseHeaderContains('Link', '</style/bar.css>; rel="prefetch"; as="style"; type="text/stylesheet"');
58
        // Stylesheets
59
        $this->assertResponseHeaderContains('Link', '</style/stylesheet.css>; rel="preload"; as="style"; type="text/css"; media="screen"');
60
        // Scripts
61
        $this->assertNotResponseHeaderContains('Link', '</script/foo.js>; rel="preload"; as="script"; type="text/javascript"');
62
        $this->assertNotResponseHeaderContains('Link', '</script/bar.js>; rel="preload"; as="script"; type="text/text"');
63
    }
64
65
    /**
66
     * @coversNothing
67
     */
68
    public function testIndexActionWithStylesheetModePrefetch()
69
    {
70
        /** @var ServiceManager $serviceManager */
71
        $serviceManager = $this->getApplication()->getServiceManager();
72
        $serviceManager->setAllowOverride(true);
73
        /** @var Options $options */
74
        $options = $serviceManager->get(OptionsInterface::class);
75
        $options->setStylesheetEnabled(true);
76
        $options->setStylesheetMode('prefetch');
77
78
        $this->dispatch('/');
79
        // Links
80
        $this->assertResponseHeaderContains('Link', '</style/foo.css>; rel="preload"');
81
        $this->assertResponseHeaderContains('Link', '</style/bar.css>; rel="prefetch"; as="style"; type="text/stylesheet"');
82
        // Stylesheets
83
        $this->assertResponseHeaderContains('Link', '</style/stylesheet.css>; rel="prefetch"; as="style"; type="text/css"; media="screen"');
84
        // Scripts
85
        $this->assertNotResponseHeaderContains('Link', '</script/foo.js>; rel="preload"; as="script"; type="text/javascript"');
86
        $this->assertNotResponseHeaderContains('Link', '</script/bar.js>; rel="preload"; as="script"; type="text/text"');
87
    }
88
89
    /**
90
     * @coversNothing
91
     */
92
    public function testIndexActionWithJavascriptModeDefault()
93
    {
94
        /** @var ServiceManager $serviceManager */
95
        $serviceManager = $this->getApplication()->getServiceManager();
96
        $serviceManager->setAllowOverride(true);
97
        /** @var Options $options */
98
        $options = $serviceManager->get(OptionsInterface::class);
99
        $options->setScriptEnabled(true);
100
101
        $this->dispatch('/');
102
        // Links
103
        $this->assertResponseHeaderContains('Link', '</style/foo.css>; rel="preload"');
104
        $this->assertResponseHeaderContains('Link', '</style/bar.css>; rel="prefetch"; as="style"; type="text/stylesheet"');
105
        // Stylesheets
106
        $this->assertNotResponseHeaderContains('Link', '</style/stylesheet.css>; rel="preload"; as="style"; type="text/css"; media="screen"');
107
        // Scripts
108
        $this->assertResponseHeaderContains('Link', '</script/foo.js>; rel="preload"; as="script"; type="text/javascript"');
109
        $this->assertResponseHeaderContains('Link', '</script/bar.js>; rel="preload"; as="script"; type="text/text"');
110
    }
111
112
    /**
113
     * @coversNothing
114
     */
115
    public function testIndexActionWithJavascriptModePrefetch()
116
    {
117
        /** @var ServiceManager $serviceManager */
118
        $serviceManager = $this->getApplication()->getServiceManager();
119
        $serviceManager->setAllowOverride(true);
120
        /** @var Options $options */
121
        $options = $serviceManager->get(OptionsInterface::class);
122
        $options->setScriptEnabled(true);
123
        $options->setScriptMode('prefetch');
124
125
        $this->dispatch('/');
126
        // Links
127
        $this->assertResponseHeaderContains('Link', '</style/foo.css>; rel="preload"');
128
        $this->assertResponseHeaderContains('Link', '</style/bar.css>; rel="prefetch"; as="style"; type="text/stylesheet"');
129
        // Stylesheets
130
        $this->assertNotResponseHeaderContains('Link', '</style/stylesheet.css>; rel="prefetch"; as="style"; type="text/css"; media="screen"');
131
        // Scripts
132
        $this->assertResponseHeaderContains('Link', '</script/foo.js>; rel="prefetch"; as="script"; type="text/javascript"');
133
        $this->assertResponseHeaderContains('Link', '</script/bar.js>; rel="prefetch"; as="script"; type="text/text"');
134
    }
135
136
    /**
137
     * @coversNothing
138
     */
139
    public function testIndexActionWithAllEnabledAndNoPush()
140
    {
141
        /** @var ServiceManager $serviceManager */
142
        $serviceManager = $this->getApplication()->getServiceManager();
143
        $serviceManager->setAllowOverride(true);
144
        /** @var Options $options */
145
        $options = $serviceManager->get(OptionsInterface::class);
146
        $options->setScriptEnabled(true);
147
        $options->setStylesheetEnabled(true);
148
        $options->setHttp2PushEnabled(false);
149
150
        $this->dispatch('/');
151
        // Links
152
        $this->assertResponseHeaderContains('Link', '</style/foo.css>; rel="preload"; nopush');
153
        $this->assertResponseHeaderContains('Link', '</style/bar.css>; rel="prefetch"; as="style"; type="text/stylesheet"; nopush');
154
        // Stylesheets
155
        $this->assertResponseHeaderContains('Link', '</style/stylesheet.css>; rel="preload"; as="style"; type="text/css"; media="screen"; nopush');
156
        // Scripts
157
        $this->assertResponseHeaderContains('Link', '</script/foo.js>; rel="preload"; as="script"; type="text/javascript"; nopush');
158
        $this->assertResponseHeaderContains('Link', '</script/bar.js>; rel="preload"; as="script"; type="text/text"; nopush');
159
    }
160
161
    /**
162
     * Assert response header exists and contains the given string
163
     *
164
     * @param  string $header
165
     * @param  string $match
166
     */
167
    public function assertResponseHeaderContains($header, $match)
168
    {
169
        $responseHeader = $this->getResponseHeader($header);
170
        if (! $responseHeader) {
171
            throw new ExpectationFailedException($this->createFailureMessage(sprintf(
172
                'Failed asserting response header, header "%s" doesn\'t exist',
173
                $header
174
            )));
175
        }
176
177
        $response = $this->getResponse();
178
        /** @var Headers|HeaderInterface[] $headers */
179
        $headers = $response->getHeaders();
180
181
        $headerMatched = false;
182
        $currentHeader = null;
183
184
        foreach ($headers as $currentHeader) {
185
            if ($match === $currentHeader->getFieldValue()) {
186
                $headerMatched = true;
187
                break;
188
            }
189
        }
190
191
        if (! $headerMatched) {
192
            throw new ExpectationFailedException($this->createFailureMessage(sprintf(
193
                'Failed asserting response header "%s" exists and contains "%s"',
194
                $header,
195
                $match
196
            )));
197
        }
198
199
        $this->assertEquals($match, $currentHeader ? $currentHeader->getFieldValue() : null);
200
    }
201
202
    /**
203
     * Assert response header exists and contains the given string
204
     *
205
     * @param  string $header
206
     * @param  string $match
207
     */
208
    public function assertNotResponseHeaderContains($header, $match)
209
    {
210
        $responseHeader = $this->getResponseHeader($header);
211
        if (! $responseHeader) {
212
            throw new ExpectationFailedException($this->createFailureMessage(sprintf(
213
                'Failed asserting response header, header "%s" doesn\'t exist',
214
                $header
215
            )));
216
        }
217
218
        $response = $this->getResponse();
219
        /** @var Headers|HeaderInterface[] $headers */
220
        $headers = $response->getHeaders();
221
222
        $currentHeader = null;
223
224
        foreach ($headers as $currentHeader) {
225
            if ($match === $currentHeader->getFieldValue()) {
226
                throw new ExpectationFailedException($this->createFailureMessage(sprintf(
227
                    'Failed asserting response header "%s" DOES NOT CONTAIN "%s"',
228
                    $header,
229
                    $match
230
                )));
231
            }
232
        }
233
234
        $this->assertNotEquals($match, $currentHeader ? $currentHeader->getFieldValue() : null);
235
    }
236
}
237