Passed
Pull Request — 1.x (#32)
by Wouter
01:47
created

KernelBrowserTests::browser()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
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
9
/**
10
 * @author Kevin Bond <[email protected]>
11
 */
12
trait KernelBrowserTests
13
{
14
    use BrowserKitBrowserTests;
15
16
    /**
17
     * @test
18
     */
19
    public function can_use_kernel_browser_as_typehint(): void
20
    {
21
        $this->browser()
22
            ->use(function(KernelBrowser $browser) {
23
                $browser->visit('/redirect1');
24
            })
25
            ->assertOn('/page1')
26
        ;
27
    }
28
29
    /**
30
     * @test
31
     */
32
    public function can_act_as_user(): void
33
    {
34
        if (!\method_exists(SymfonyKernelBrowser::class, 'loginUser')) {
35
            $this->markTestSkipped(SymfonyKernelBrowser::class.'::loginUser() is only available in Symfony 5.1+.');
0 ignored issues
show
Bug introduced by
It seems like markTestSkipped() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

35
            $this->/** @scrutinizer ignore-call */ 
36
                   markTestSkipped(SymfonyKernelBrowser::class.'::loginUser() is only available in Symfony 5.1+.');
Loading history...
36
        }
37
38
        $this->browser()
39
            ->throwExceptions()
40
            ->actingAs(new User('kevin', 'pass'))
41
            ->visit('/user')
42
            ->assertSee('user: kevin/pass')
43
        ;
44
    }
45
46
    /**
47
     * @test
48
     */
49
    public function can_enable_exception_throwing(): void
50
    {
51
        $this->expectException(\Exception::class);
0 ignored issues
show
Bug introduced by
It seems like expectException() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

51
        $this->/** @scrutinizer ignore-call */ 
52
               expectException(\Exception::class);
Loading history...
52
        $this->expectExceptionMessage('exception thrown');
0 ignored issues
show
Bug introduced by
It seems like expectExceptionMessage() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

52
        $this->/** @scrutinizer ignore-call */ 
53
               expectExceptionMessage('exception thrown');
Loading history...
53
54
        $this->browser()
55
            ->throwExceptions()
56
            ->visit('/exception')
57
        ;
58
    }
59
60
    /**
61
     * @test
62
     */
63
    public function can_re_enable_catching_exceptions(): void
64
    {
65
        $browser = $this->browser();
66
67
        try {
68
            $browser->throwExceptions()->visit('/exception');
69
        } catch (\Exception $e) {
70
            $browser
71
                ->catchExceptions()
72
                ->visit('/exception')
73
                ->assertStatus(500)
74
            ;
75
76
            return;
77
        }
78
79
        $this->fail('Exception was not caught.');
0 ignored issues
show
Bug introduced by
It seems like fail() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

79
        $this->/** @scrutinizer ignore-call */ 
80
               fail('Exception was not caught.');
Loading history...
80
    }
81
82
    /**
83
     * @test
84
     */
85
    public function can_enable_the_profiler(): void
86
    {
87
        $profile = $this->browser()
88
            ->withProfiling()
89
            ->visit('/page1')
90
            ->profile()
91
        ;
92
93
        $this->assertTrue($profile->hasCollector('request'));
0 ignored issues
show
Bug introduced by
The method assertTrue() does not exist on Zenstruck\Browser\Tests\KernelBrowserTests. Did you maybe mean assert_on()? ( Ignorable by Annotation )

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

93
        $this->/** @scrutinizer ignore-call */ 
94
               assertTrue($profile->hasCollector('request'));

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
94
    }
95
96
    protected function browser(): KernelBrowser
97
    {
98
        return $this->kernelBrowser();
99
    }
100
}
101