|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
use Illuminate\Http\Request; |
|
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
|
|
|
|