Completed
Push — develop ( 3c50bf...126c89 )
by Stan
02:00
created

ServerPushMiddlewareTest   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 187
Duplicated Lines 61.5 %

Importance

Changes 0
Metric Value
wmc 14
dl 115
loc 187
rs 10
c 0
b 0
f 0

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
use Krenor\Http2Pusher\Response;
4
use Illuminate\Http\RedirectResponse;
5
use Krenor\Http2Pusher\Tests\TestCase;
6
use Krenor\Http2Pusher\Middleware\ServerPush;
7
8
class ServerPushMiddlewareTest extends TestCase
9
{
10
    /**
11
     * @var ServerPush
12
     */
13
    private $middleware;
14
15
    /**
16
     * Bootstrap the test environment.
17
     */
18
    public function setUp()
19
    {
20
        parent::setUp();
21
22
        $this->createManifestFile();
23
24
        $this->middleware = new ServerPush($this->builder);
25
    }
26
27
    /**
28
     * Clean the test environment for the next test.
29
     */
30
    public function tearDown()
31
    {
32
        $manifest = public_path('mix-manifest.json');
33
34
        if (file_exists($manifest)) {
35
            unlink($manifest);
36
        }
37
    }
38
39
    /** @test */
40
    public function it_should_not_push_anything_when_its_a_redirect_response()
41
    {
42
        $next = function () {
43
            return new RedirectResponse('http://laravel.com');
44
        };
45
46
        $response = $this->middleware->handle(
47
            $this->request,
48
            $next
49
        );
50
51
        $this->assertFalse($response->headers->has('Link'));
52
        $this->assertInstanceOf(RedirectResponse::class, $response);
53
    }
54
55
    /** @test */
56
    public function it_should_not_push_anything_when_its_a_json_request()
57
    {
58
        $this->request->headers->set('Content-Type', 'application/json');
59
60
        $next = function () {
61
            return new Response;
62
        };
63
64
        $response = $this->middleware->handle(
65
            $this->request,
66
            $next
67
        );
68
69
        $this->assertFalse($response->headers->has('Link'));
70
    }
71
72
    /** @test */
73
    public function it_should_not_push_anything_when_its_an_ajax_request()
74
    {
75
        $this->request->headers->set('X-Requested-With', 'XMLHttpRequest');
76
77
        $next = function () {
78
            return new Response;
79
        };
80
81
        $response = $this->middleware->handle(
82
            $this->request,
83
            $next
84
        );
85
86
        $this->assertFalse($response->headers->has('Link'));
87
    }
88
89
    /** @test */
90
    public function it_should_crawl_for_scripts_and_push_them()
91
    {
92
        $includes = [
93
            'https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-beta/js/bootstrap.min.js',
94
            '/js/manifest.js',
95
            '/js/vendor.js',
96
            '/js/app.js',
97
        ];
98
99
        $response = $this->middleware->handle(
100
            $this->request,
101
            $this->getResponse('with-scripts')
102
        );
103
104
        $this->assertTrue($response->headers->has('Link'));
105
106
        $link = $response->headers->get('Link');
107
108
        foreach ($includes as $include) {
109
            $this->assertContains($include, $link);
110
        }
111
112
        $this->assertCount(count($includes), explode(',', $link));
113
    }
114
115
    /** @test */
116
    public function it_should_crawl_for_style_sheets_and_push_them()
117
    {
118
        $includes = [
119
            'https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-beta/css/bootstrap-grid.min.css',
120
            '/css/app.css',
121
        ];
122
123
        $response = $this->middleware->handle(
124
            $this->request,
125
            $this->getResponse('with-styles')
126
        );
127
128
        $this->assertTrue($response->headers->has('Link'));
129
130
        $link = $response->headers->get('Link');
131
132
        foreach ($includes as $include) {
133
            $this->assertContains($include, $link);
134
        }
135
136
        $this->assertCount(count($includes), explode(',', $link));
137
    }
138
139
    /** @test */
140
    public function it_should_crawl_for_images_and_push_them()
141
    {
142
        $includes = [
143
            'http://stylecampaign.com/blog/blogimages/SVG/fox-1.svg',
144
            'https://laravel.com/assets/img/laravel-logo-white.png',
145
            '/images/laravel.jpg',
146
            '/images/chrome.svg',
147
            '/images/github.png',
148
        ];
149
150
        $response = $this->middleware->handle(
151
            $this->request,
152
            $this->getResponse('with-images')
153
        );
154
155
        $this->assertTrue($response->headers->has('Link'));
156
157
        $link = $response->headers->get('Link');
158
159
        foreach ($includes as $include) {
160
            $this->assertContains($include, $link);
161
        }
162
163
        $this->assertCount(count($includes), explode(',', $link));
164
    }
165
166
    /**
167
     * @param string $page
168
     *
169
     * @return Closure
170
     */
171
    private function getResponse($page)
172
    {
173
        return function () use ($page) {
174
            return new Response(
175
                file_get_contents(__DIR__ . "/fixtures/pages/{$page}.html")
176
            );
177
        };
178
    }
179
180
    /**
181
     * Create the manifest file manually for tests regarding said file.
182
     *
183
     * @return void
184
     */
185
    private function createManifestFile()
186
    {
187
        $content = '{
188
            "/js/vendor.js": "/js/vendor.js?id=911084212bac1b5ea2a5",
189
            "/js/app.js": "/js/app.js?id=8e38929b2d5501e6808e",
190
            "/css/app.css": "/css/app.css?id=516707d9f36d4fb7d866",
191
            "/js/manifest.js": "/js/manifest.js?id=ac5def271276f7bf7ec1"
192
        }';
193
194
        file_put_contents(public_path('mix-manifest.json'), $content);
195
    }
196
}
197