Passed
Push — 1.x ( a9c58b...abb6cb )
by Kevin
02:13
created

KernelBrowserTests::can_act_as_user()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 7
c 1
b 0
f 1
nc 2
nop 0
dl 0
loc 11
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
use Zenstruck\Browser\Test\HasKernelBrowser;
9
10
/**
11
 * @author Kevin Bond <[email protected]>
12
 */
13
trait KernelBrowserTests
14
{
15
    use BrowserKitBrowserTests, HasKernelBrowser;
16
17
    /**
18
     * @test
19
     */
20
    public function can_act_as_user(): void
21
    {
22
        if (!\method_exists(SymfonyKernelBrowser::class, 'loginUser')) {
23
            $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

23
            $this->/** @scrutinizer ignore-call */ 
24
                   markTestSkipped(SymfonyKernelBrowser::class.'::loginUser() is only available in Symfony 5.1+.');
Loading history...
24
        }
25
26
        $this->browser()
27
            ->throwExceptions()
28
            ->actingAs(new User('kevin', 'pass'))
29
            ->visit('/user')
30
            ->assertSee('user: kevin/pass')
31
        ;
32
    }
33
34
    /**
35
     * @test
36
     */
37
    public function can_enable_exception_throwing(): void
38
    {
39
        $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

39
        $this->/** @scrutinizer ignore-call */ 
40
               expectException(\Exception::class);
Loading history...
40
        $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

40
        $this->/** @scrutinizer ignore-call */ 
41
               expectExceptionMessage('exception thrown');
Loading history...
41
42
        $this->browser()
43
            ->throwExceptions()
44
            ->visit('/exception')
45
        ;
46
    }
47
48
    /**
49
     * @test
50
     */
51
    public function can_re_enable_catching_exceptions(): void
52
    {
53
        $browser = $this->browser();
54
55
        try {
56
            $browser->throwExceptions()->visit('/exception');
57
        } catch (\Exception $e) {
58
            $browser
59
                ->catchExceptions()
60
                ->visit('/exception')
61
                ->assertStatus(500)
62
            ;
63
64
            return;
65
        }
66
67
        $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

67
        $this->/** @scrutinizer ignore-call */ 
68
               fail('Exception was not caught.');
Loading history...
68
    }
69
70
    /**
71
     * @test
72
     */
73
    public function can_enable_the_profiler(): void
74
    {
75
        $profile = $this->browser()
76
            ->withProfiling()
77
            ->visit('/page1')
78
            ->profile()
79
        ;
80
81
        $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

81
        $this->/** @scrutinizer ignore-call */ 
82
               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...
82
    }
83
84
    protected static function browserClass(): string
85
    {
86
        return KernelBrowser::class;
87
    }
88
}
89