|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Zenstruck\Browser\Tests; |
|
4
|
|
|
|
|
5
|
|
|
use Symfony\Component\VarDumper\VarDumper; |
|
6
|
|
|
use Zenstruck\Browser; |
|
7
|
|
|
use Zenstruck\Browser\Test\HasBrowser; |
|
8
|
|
|
use Zenstruck\Browser\Tests\Extension\HtmlTests; |
|
9
|
|
|
use Zenstruck\Browser\Tests\Fixture\TestComponent1; |
|
10
|
|
|
use Zenstruck\Browser\Tests\Fixture\TestComponent2; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* @author Kevin Bond <[email protected]> |
|
14
|
|
|
*/ |
|
15
|
|
|
trait BrowserTests |
|
16
|
|
|
{ |
|
17
|
|
|
use HasBrowser, HtmlTests; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @test |
|
21
|
|
|
*/ |
|
22
|
|
|
public function multiple_browsers(): void |
|
23
|
|
|
{ |
|
24
|
|
|
$browser1 = $this->browser() |
|
25
|
|
|
->visit('/page1') |
|
26
|
|
|
->assertOn('/page1') |
|
27
|
|
|
; |
|
28
|
|
|
|
|
29
|
|
|
$browser2 = $this->browser() |
|
|
|
|
|
|
30
|
|
|
->visit('/page2') |
|
31
|
|
|
->assertOn('/page2') |
|
32
|
|
|
; |
|
33
|
|
|
|
|
34
|
|
|
// this ensures a different browser is actually used |
|
35
|
|
|
$browser1->assertOn('/page1'); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @test |
|
40
|
|
|
*/ |
|
41
|
|
|
public function assert_on(): void |
|
42
|
|
|
{ |
|
43
|
|
|
$this->browser() |
|
44
|
|
|
->visit('/page1') |
|
45
|
|
|
->assertOn('/page1') |
|
46
|
|
|
->assertOn('http://www.example.com/page1') |
|
47
|
|
|
->assertNotOn('/page2') |
|
48
|
|
|
->assertNotOn('http://www.example.com/page1', ['path', 'host']) |
|
49
|
|
|
->visit('/page1?foo=bar') |
|
50
|
|
|
->assertOn('/page1?foo=bar') |
|
51
|
|
|
->assertOn('/page1', ['path']) |
|
52
|
|
|
->assertOn('/page1', ['path', 'fragment']) |
|
53
|
|
|
->assertNotOn('/page1?foo=baz') |
|
54
|
|
|
; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @test |
|
59
|
|
|
* @dataProvider encodedUrlProvider |
|
60
|
|
|
*/ |
|
61
|
|
|
public function assert_on_encoded($url, $expected): void |
|
62
|
|
|
{ |
|
63
|
|
|
$this->browser() |
|
64
|
|
|
->visit($url) |
|
65
|
|
|
->assertOn($expected) |
|
66
|
|
|
; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
public static function encodedUrlProvider(): iterable |
|
70
|
|
|
{ |
|
71
|
|
|
yield ['/page1?filter[q]=value', '/page1?filter[q]=value']; |
|
72
|
|
|
yield ['/page1?filter%5Bq%5D=value', '/page1?filter[q]=value']; |
|
73
|
|
|
yield ['/page1?filter[q]=value', '/page1?filter%5Bq%5D=value']; |
|
74
|
|
|
yield ['/page1#foo bar', '/page1#foo bar']; |
|
75
|
|
|
yield ['/page1#foo%20bar', '/page1#foo bar']; |
|
76
|
|
|
yield ['/page1#foo bar', '/page1#foo%20bar']; |
|
77
|
|
|
yield ['/page1#foo+bar', '/page1#foo bar']; |
|
78
|
|
|
yield ['/page1#foo bar', '/page1#foo+bar']; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @test |
|
83
|
|
|
*/ |
|
84
|
|
|
public function can_use_current_browser(): void |
|
85
|
|
|
{ |
|
86
|
|
|
$browser = $this->browser(); |
|
87
|
|
|
|
|
88
|
|
|
$browser |
|
89
|
|
|
->use(function(Browser $b) use ($browser) { |
|
90
|
|
|
$this->assertSame($b, $browser); |
|
|
|
|
|
|
91
|
|
|
|
|
92
|
|
|
$browser->visit('/redirect1'); |
|
93
|
|
|
}) |
|
94
|
|
|
->assertOn('/page1') |
|
95
|
|
|
->use(function() { |
|
96
|
|
|
$this->assertTrue(true); |
|
|
|
|
|
|
97
|
|
|
}) |
|
98
|
|
|
; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* @test |
|
103
|
|
|
*/ |
|
104
|
|
|
public function can_use_components(): void |
|
105
|
|
|
{ |
|
106
|
|
|
$this->browser() |
|
107
|
|
|
->use(function(TestComponent1 $component) { |
|
108
|
|
|
$component->assertTitle('h1 title'); |
|
109
|
|
|
}) |
|
110
|
|
|
->assertOn('/page1') |
|
111
|
|
|
; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* @test |
|
116
|
|
|
*/ |
|
117
|
|
|
public function component_pre_assertions_and_actions_are_called(): void |
|
118
|
|
|
{ |
|
119
|
|
|
$this->browser() |
|
120
|
|
|
->use(function(TestComponent2 $component) { |
|
121
|
|
|
$this->assertTrue($component->preActionsCalled); |
|
122
|
|
|
$this->assertTrue($component->preAssertionsCalled); |
|
123
|
|
|
}) |
|
124
|
|
|
; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* @test |
|
129
|
|
|
*/ |
|
130
|
|
|
public function with_can_accept_multiple_browsers_and_components(): void |
|
131
|
|
|
{ |
|
132
|
|
|
$browser = $this->browser(); |
|
133
|
|
|
|
|
134
|
|
|
$browser |
|
135
|
|
|
->use(function(Browser $browser1, $browser2, TestComponent1 $component1, TestComponent2 $component2) use ($browser) { |
|
136
|
|
|
$this->assertInstanceOf(Browser::class, $browser1); |
|
|
|
|
|
|
137
|
|
|
$this->assertInstanceOf(Browser::class, $browser2); |
|
138
|
|
|
$this->assertInstanceOf(\get_class($browser), $browser1); |
|
139
|
|
|
$this->assertInstanceOf(\get_class($browser), $browser2); |
|
140
|
|
|
$this->assertInstanceOf(TestComponent1::class, $component1); |
|
141
|
|
|
$this->assertInstanceOf(TestComponent2::class, $component2); |
|
142
|
|
|
}) |
|
143
|
|
|
; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* @test |
|
148
|
|
|
*/ |
|
149
|
|
|
public function invalid_with_callback_parameter_throws_type_error(): void |
|
150
|
|
|
{ |
|
151
|
|
|
$this->expectException(\TypeError::class); |
|
|
|
|
|
|
152
|
|
|
|
|
153
|
|
|
$this->browser()->use(function(string $invalidType) {}); |
|
|
|
|
|
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* @test |
|
158
|
|
|
*/ |
|
159
|
|
|
public function redirects_are_followed_by_default(): void |
|
160
|
|
|
{ |
|
161
|
|
|
$this->browser() |
|
162
|
|
|
->visit('/redirect1') |
|
163
|
|
|
->assertOn('/page1') |
|
164
|
|
|
; |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
/** |
|
168
|
|
|
* @test |
|
169
|
|
|
*/ |
|
170
|
|
|
public function content_assertions(): void |
|
171
|
|
|
{ |
|
172
|
|
|
$this->browser() |
|
173
|
|
|
->visit('/page1') |
|
174
|
|
|
->assertContains('h1 title') |
|
175
|
|
|
->assertNotContains('invalid text') |
|
176
|
|
|
; |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* @test |
|
181
|
|
|
*/ |
|
182
|
|
|
public function can_dump_response(): void |
|
183
|
|
|
{ |
|
184
|
|
|
$output = self::catchVarDumperOutput(function() { |
|
185
|
|
|
$this->browser() |
|
186
|
|
|
->visit('/page1') |
|
187
|
|
|
->dump() |
|
188
|
|
|
; |
|
189
|
|
|
}); |
|
190
|
|
|
|
|
191
|
|
|
$this->assertStringContainsString('/page1', $output[0]); |
|
|
|
|
|
|
192
|
|
|
$this->assertStringContainsString('<html', $output[0]); |
|
193
|
|
|
$this->assertStringContainsString('<h1>h1 title</h1>', $output[0]); |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
/** |
|
197
|
|
|
* @test |
|
198
|
|
|
*/ |
|
199
|
|
|
public function can_save_source(): void |
|
200
|
|
|
{ |
|
201
|
|
|
$file = __DIR__.'/../var/browser/source/source.txt'; |
|
202
|
|
|
|
|
203
|
|
|
if (\file_exists($file)) { |
|
204
|
|
|
\unlink($file); |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
|
|
$this->browser() |
|
208
|
|
|
->visit('/page1') |
|
209
|
|
|
->saveSource('source.txt') |
|
210
|
|
|
; |
|
211
|
|
|
|
|
212
|
|
|
$this->assertFileExists($file); |
|
|
|
|
|
|
213
|
|
|
|
|
214
|
|
|
$contents = \file_get_contents($file); |
|
215
|
|
|
|
|
216
|
|
|
$this->assertStringContainsString('/page1', $contents); |
|
217
|
|
|
$this->assertStringContainsString('<html', $contents); |
|
218
|
|
|
$this->assertStringContainsString('<h1>h1 title</h1>', $contents); |
|
219
|
|
|
|
|
220
|
|
|
\unlink($file); |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
|
|
protected static function catchVarDumperOutput(callable $callback): array |
|
224
|
|
|
{ |
|
225
|
|
|
$output[] = null; |
|
|
|
|
|
|
226
|
|
|
|
|
227
|
|
|
VarDumper::setHandler(function($var) use (&$output) { |
|
228
|
|
|
$output[] = $var; |
|
229
|
|
|
}); |
|
230
|
|
|
|
|
231
|
|
|
$callback(); |
|
232
|
|
|
|
|
233
|
|
|
// reset to default handler |
|
234
|
|
|
VarDumper::setHandler(); |
|
235
|
|
|
|
|
236
|
|
|
// a null value is added to the beginning |
|
237
|
|
|
return \array_values(\array_filter($output)); |
|
238
|
|
|
} |
|
239
|
|
|
|
|
240
|
|
|
abstract protected function browser(): Browser; |
|
241
|
|
|
} |
|
242
|
|
|
|