|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Zenstruck\Browser\Tests; |
|
4
|
|
|
|
|
5
|
|
|
use Symfony\Bundle\FrameworkBundle\KernelBrowser as SymfonyKernelBrowser; |
|
6
|
|
|
use Symfony\Component\Security\Core\User\User; |
|
7
|
|
|
use Zenstruck\Browser\KernelBrowser; |
|
8
|
|
|
use Zenstruck\Browser\Test\HasBrowser; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* @author Kevin Bond <[email protected]> |
|
12
|
|
|
*/ |
|
13
|
|
|
trait KernelBrowserTests |
|
14
|
|
|
{ |
|
15
|
|
|
use BrowserKitBrowserTests, HasBrowser; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @test |
|
19
|
|
|
*/ |
|
20
|
|
|
public function can_use_kernel_browser_as_typehint(): void |
|
21
|
|
|
{ |
|
22
|
|
|
$this->browser() |
|
23
|
|
|
->use(function(KernelBrowser $browser) { |
|
24
|
|
|
$browser->visit('/redirect1'); |
|
25
|
|
|
}) |
|
26
|
|
|
->assertOn('/page1') |
|
27
|
|
|
; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @test |
|
32
|
|
|
*/ |
|
33
|
|
|
public function can_act_as_user(): void |
|
34
|
|
|
{ |
|
35
|
|
|
if (!\method_exists(SymfonyKernelBrowser::class, 'loginUser')) { |
|
36
|
|
|
$this->markTestSkipped(SymfonyKernelBrowser::class.'::loginUser() is only available in Symfony 5.1+.'); |
|
|
|
|
|
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
$this->browser() |
|
40
|
|
|
->throwExceptions() |
|
|
|
|
|
|
41
|
|
|
->actingAs(new User('kevin', 'pass')) |
|
42
|
|
|
->visit('/user') |
|
43
|
|
|
->assertSee('user: kevin/pass') |
|
44
|
|
|
; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @test |
|
49
|
|
|
*/ |
|
50
|
|
|
public function can_enable_exception_throwing(): void |
|
51
|
|
|
{ |
|
52
|
|
|
$this->expectException(\Exception::class); |
|
|
|
|
|
|
53
|
|
|
$this->expectExceptionMessage('exception thrown'); |
|
|
|
|
|
|
54
|
|
|
|
|
55
|
|
|
$this->browser() |
|
56
|
|
|
->throwExceptions() |
|
57
|
|
|
->visit('/exception') |
|
58
|
|
|
; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @test |
|
63
|
|
|
*/ |
|
64
|
|
|
public function can_re_enable_catching_exceptions(): void |
|
65
|
|
|
{ |
|
66
|
|
|
$browser = $this->browser(); |
|
67
|
|
|
|
|
68
|
|
|
try { |
|
69
|
|
|
$browser->throwExceptions()->visit('/exception'); |
|
70
|
|
|
} catch (\Exception $e) { |
|
71
|
|
|
$browser |
|
72
|
|
|
->catchExceptions() |
|
|
|
|
|
|
73
|
|
|
->visit('/exception') |
|
74
|
|
|
->assertStatus(500) |
|
75
|
|
|
; |
|
76
|
|
|
|
|
77
|
|
|
return; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
$this->fail('Exception was not caught.'); |
|
|
|
|
|
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @test |
|
85
|
|
|
*/ |
|
86
|
|
|
public function can_enable_the_profiler(): void |
|
87
|
|
|
{ |
|
88
|
|
|
$profile = $this->browser() |
|
89
|
|
|
->withProfiling() |
|
|
|
|
|
|
90
|
|
|
->visit('/page1') |
|
91
|
|
|
->profile() |
|
92
|
|
|
; |
|
93
|
|
|
|
|
94
|
|
|
$this->assertTrue($profile->hasCollector('request')); |
|
|
|
|
|
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|