Passed
Push — develop ( 6e78c9...4aabf2 )
by Stan
04:01 queued 22s
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

10 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 7 1
A it_should_not_push_anything_when_its_an_ajax_request() 14 14 1
A tearDown() 0 6 2
A it_should_crawl_for_style_sheets_and_push_them() 21 21 2
B it_should_crawl_for_images_and_push_them() 24 24 2
A it_should_not_push_anything_when_its_a_json_request() 14 14 1
A getResponse() 0 5 1
A it_should_crawl_for_scripts_and_push_them() 23 23 2
A createManifestFile() 0 10 1
A it_should_not_push_anything_when_its_a_redirect_response() 13 13 1

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 Illuminate\Http\Request;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Request. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
4
use Krenor\Http2Pusher\Response;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Response. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
5
use Illuminate\Http\RedirectResponse;
6
use Krenor\Http2Pusher\Tests\TestCase;
7
use Krenor\Http2Pusher\Middleware\ServerPush;
8
9
class ServerPushMiddlewareTest extends TestCase
10
{
11
    /**
12
     * @var ServerPush
13
     */
14
    private $middleware;
15
16
    /**
17
     * Bootstrap the test environment.
18
     */
19
    public function setUp()
20
    {
21
        parent::setUp();
22
23
        $this->createManifestFile();
24
25
        $this->middleware = new ServerPush($this->builder);
26
    }
27
28
    /**
29
     * Clean the test environment for the next test.
30
     */
31
    public function tearDown()
32
    {
33
        $manifest = public_path('mix-manifest.json');
34
35
        if (file_exists($manifest)) {
36
            unlink($manifest);
37
        }
38
    }
39
40
    /** @test */
41 View Code Duplication
    public function it_should_not_push_anything_when_its_a_redirect_response()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
42
    {
43
        $response = function ($request) {
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

43
        $response = function (/** @scrutinizer ignore-unused */ $request) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
44
            return new RedirectResponse('http://laravel.com');
45
        };
46
47
        $response = $this->middleware->handle(
48
            $this->request,
49
            $response
50
        );
51
52
        $this->assertFalse($response->headers->has('Link'));
53
        $this->assertInstanceOf(RedirectResponse::class, $response);
54
    }
55
56
    /** @test */
57 View Code Duplication
    public function it_should_not_push_anything_when_its_a_json_request()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
58
    {
59
        $this->request->headers->set('Content-Type', 'application/json');
60
61
        $response = function ($request) {
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

61
        $response = function (/** @scrutinizer ignore-unused */ $request) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
62
            return new Response;
63
        };
64
65
        $response = $this->middleware->handle(
66
            $this->request,
67
            $response
68
        );
69
70
        $this->assertFalse($response->headers->has('Link'));
71
    }
72
73
    /** @test */
74 View Code Duplication
    public function it_should_not_push_anything_when_its_an_ajax_request()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
75
    {
76
        $this->request->headers->set('X-Requested-With', 'XMLHttpRequest');
77
78
        $response = function ($request) {
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

78
        $response = function (/** @scrutinizer ignore-unused */ $request) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
79
            return new Response;
80
        };
81
82
        $response = $this->middleware->handle(
83
            $this->request,
84
            $response
85
        );
86
87
        $this->assertFalse($response->headers->has('Link'));
88
    }
89
90
    /** @test */
91 View Code Duplication
    public function it_should_crawl_for_scripts_and_push_them()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
92
    {
93
        $includes = [
94
            'https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-beta/js/bootstrap.min.js',
95
            '/js/manifest.js',
96
            '/js/vendor.js',
97
            '/js/app.js',
98
        ];
99
100
        $response = $this->middleware->handle(
101
            $this->request,
102
            $this->getResponse('with-scripts')
103
        );
104
105
        $this->assertTrue($response->headers->has('Link'));
106
107
        $link = $response->headers->get('Link');
108
109
        foreach ($includes as $include) {
110
            $this->assertContains($include, $link);
111
        }
112
113
        $this->assertCount(count($includes), explode(',', $link));
114
    }
115
116
    /** @test */
117 View Code Duplication
    public function it_should_crawl_for_style_sheets_and_push_them()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
118
    {
119
        $includes = [
120
            'https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-beta/css/bootstrap-grid.min.css',
121
            '/css/app.css',
122
        ];
123
124
        $response = $this->middleware->handle(
125
            $this->request,
126
            $this->getResponse('with-styles')
127
        );
128
129
        $this->assertTrue($response->headers->has('Link'));
130
131
        $link = $response->headers->get('Link');
132
133
        foreach ($includes as $include) {
134
            $this->assertContains($include, $link);
135
        }
136
137
        $this->assertCount(count($includes), explode(',', $link));
138
    }
139
140
    /** @test */
141 View Code Duplication
    public function it_should_crawl_for_images_and_push_them()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
142
    {
143
        $includes = [
144
            'http://stylecampaign.com/blog/blogimages/SVG/fox-1.svg',
145
            'https://laravel.com/assets/img/laravel-logo-white.png',
146
            '/images/laravel.jpg',
147
            '/images/chrome.svg',
148
            '/images/github.png',
149
        ];
150
151
        $response = $this->middleware->handle(
152
            $this->request,
153
            $this->getResponse('with-images')
154
        );
155
156
        $this->assertTrue($response->headers->has('Link'));
157
158
        $link = $response->headers->get('Link');
159
160
        foreach ($includes as $include) {
161
            $this->assertContains($include, $link);
162
        }
163
164
        $this->assertCount(count($includes), explode(',', $link));
165
    }
166
167
    /**
168
     * @param string $page
169
     *
170
     * @return Closure
171
     */
172
    private function getResponse($page)
173
    {
174
        return function ($request) use ($page) {
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

174
        return function (/** @scrutinizer ignore-unused */ $request) use ($page) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
175
            return new Response(
176
                file_get_contents(__DIR__ . "/fixtures/pages/{$page}.html")
177
            );
178
        };
179
    }
180
181
    /**
182
     * Create the manifest file manually for tests regarding said file.
183
     *
184
     * @return void
185
     */
186
    private function createManifestFile()
187
    {
188
        $content = '{
189
            "/js/vendor.js": "/js/vendor.js?id=911084212bac1b5ea2a5",
190
            "/js/app.js": "/js/app.js?id=8e38929b2d5501e6808e",
191
            "/css/app.css": "/css/app.css?id=516707d9f36d4fb7d866",
192
            "/js/manifest.js": "/js/manifest.js?id=ac5def271276f7bf7ec1"
193
        }';
194
195
        file_put_contents(public_path('mix-manifest.json'), $content);
196
    }
197
}
198