Passed
Push — develop ( 6e78c9...4aabf2 )
by Stan
04:01 queued 22s
created

BuilderTest   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 170
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 11
dl 0
loc 170
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A it_should_return_null_when_no_pushable_resource_is_available() 0 5 1
A it_should_remove_non_pushable_resources_when_building_the_push_link() 0 18 2
A it_should_transform_resources_into_a_processable_structure() 0 17 1
B it_should_only_push_resources_which_are_not_cached_yet() 0 25 1
A it_should_return_null_when_the_given_resources_already_got_cached() 0 16 1
A it_should_add_a_cache_cookie_if_said_cookie_is_not_already_set() 0 12 1
A transform() 0 8 1
A it_should_retrieve_or_create_a_file_hash() 0 22 2
A it_should_build_the_push_link_when_given_pushable_resources() 0 13 1
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\Builder;
5
use Illuminate\Support\Collection;
6
use Krenor\Http2Pusher\Tests\TestCase;
7
8
class BuilderTest extends TestCase
9
{
10
    /** @test */
11
    public function it_should_retrieve_or_create_a_file_hash()
12
    {
13
        $reflector = new ReflectionClass(Builder::class);
14
        $method = $reflector->getMethod('retrieveHash');
15
        $method->setAccessible(true);
16
17
        $resources = new Collection([
18
            $this->pushable[0],
19
            "{$this->pushable[1]}?id=516707d9f36d4fb7d866",
20
            'https://laravel.com/assets/img/laravel-logo-white.png',
21
        ]);
22
23
        $resources->each(function ($resource, $index) use ($resources, $method) {
24
            $hash = $method->invokeArgs($this->builder, [$resource]);
25
26
            $this->assertNotNull($hash);
27
28
            if ($index === 1) {
29
                $mixFile = $resources[$index];
30
                $mixHash = substr($mixFile, -20, 12);
31
32
                $this->assertSame($mixHash, $hash);
33
            }
34
        });
35
    }
36
37
    /** @test */
38
    public function it_should_transform_resources_into_a_processable_structure()
39
    {
40
        $transformed = $this->transform($this->pushable);
41
42
        $transformed->each(function ($item) {
43
            $this->assertArrayHasKey('type', $item);
44
            $this->assertFalse($item['type'] === null);
45
            $this->assertArrayHasKey('hash', $item);
46
            $this->assertFalse($item['hash'] === null);
47
        });
48
49
        $this->assertEquals('script', $transformed[0]['type']);
50
        $this->assertEquals('style', $transformed[1]['type']);
51
        $this->assertEquals('image', $transformed[2]['type']);
52
        $this->assertEquals('image', $transformed[3]['type']);
53
        $this->assertEquals('image', $transformed[4]['type']);
54
        $this->assertEquals('font', $transformed[5]['type']);
55
    }
56
57
    /** @test */
58
    public function it_should_return_null_when_no_pushable_resource_is_available()
59
    {
60
        $push = $this->builder->prepare($this->nonPushable);
61
62
        $this->assertFalse($push !== null);
63
    }
64
65
    /** @test */
66
    public function it_should_build_the_push_link_when_given_pushable_resources()
67
    {
68
        $push = $this->builder->prepare($this->pushable);
69
70
        $expected = "<{$this->pushable[0]}>; rel=preload; as=script,";
71
        $expected .= "<{$this->pushable[1]}>; rel=preload; as=style,";
72
        $expected .= "<{$this->pushable[2]}>; rel=preload; as=image,";
73
        $expected .= "<{$this->pushable[3]}>; rel=preload; as=image,";
74
        $expected .= "<{$this->pushable[4]}>; rel=preload; as=image,";
75
        $expected .= "<{$this->pushable[5]}>; rel=preload; as=font; crossorigin";
76
77
        $this->assertNotNull($push);
78
        $this->assertSame($expected, $push->getLink());
79
    }
80
81
    /** @test */
82
    public function it_should_remove_non_pushable_resources_when_building_the_push_link()
83
    {
84
        $pushable = $this->pushable[0];
85
86
        $resources = array_merge(
87
            $this->nonPushable,
88
            [$pushable]
89
        );
90
91
        $push = $this->builder->prepare($resources);
92
93
        $this->assertFalse($push === null);
94
95
        foreach ($this->nonPushable as $item) {
96
            $this->assertNotContains($item, $push->getLink());
97
        }
98
99
        $this->assertContains($pushable, $push->getLink());
100
    }
101
102
    /** @test */
103
    public function it_should_add_a_cache_cookie_if_said_cookie_is_not_already_set()
104
    {
105
        $transformed = $this->transform($this->pushable);
106
107
        $push = $this->builder->prepare($this->pushable);
108
109
        $this->assertNotNull($push);
110
        $this->assertNotNull($push->getCookie());
111
        $this->assertNotNull($push->getResources());
112
113
        $this->assertSame($push->getCookie()->getValue(), $transformed->toJson());
114
        $this->assertSame($push->getResources()->pluck('path')->toArray(), $this->pushable);
115
    }
116
117
    /** @test */
118
    public function it_should_return_null_when_the_given_resources_already_got_cached()
119
    {
120
        $cache = $this->transform($this->pushable)
121
                      ->toJson();
122
123
        $cookies = [
124
            $this->builderSettings['cookie']['name'] => $cache,
125
        ];
126
127
        $request = new Request([], [], [], $cookies);
128
129
        $builder = new Builder($request, $this->builderSettings);
130
131
        $push = $builder->prepare($this->pushable);
132
133
        $this->assertNull($push);
134
    }
135
136
    /** @test */
137
    public function it_should_only_push_resources_which_are_not_cached_yet()
138
    {
139
        $pushed = array_slice($this->pushable, 0, 4);
140
141
        $cache = $this->transform($pushed)
142
                      ->toJson();
143
144
        $cookies = [
145
            $this->builderSettings['cookie']['name'] => $cache,
146
        ];
147
148
        $request = new Request([], [], [], $cookies);
149
150
        $builder = new Builder($request, $this->builderSettings);
151
152
        $push = $builder->prepare($this->pushable);
153
154
        $this->assertNotNull($push);
155
        $this->assertCount(
156
            count($this->pushable) - 4,
157
            explode(',', $push->getLink())
158
        );
159
        $this->assertSame(
160
            $this->transform($this->pushable)->toJson(),
161
            $push->getCookie()->getValue()
162
        );
163
    }
164
165
    /**
166
     * @param array $resources
167
     *
168
     * @return Collection
169
     */
170
    private function transform(array $resources): Collection
171
    {
172
        $reflector = new ReflectionClass(Builder::class);
173
        $method = $reflector->getMethod('transform');
174
        $method->setAccessible(true);
175
176
        return $method->invokeArgs($this->builder, [
177
            new Collection($resources),
178
        ]);
179
    }
180
}
181