Passed
Push — 1.x ( e60ed5...86ffba )
by Kevin
02:00
created

KernelBrowser::actingAs()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 5
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 9
rs 10
1
<?php
2
3
namespace Zenstruck\Browser;
4
5
use Symfony\Bundle\FrameworkBundle\KernelBrowser as SymfonyKernelBrowser;
6
use Symfony\Component\HttpKernel\Profiler\Profile;
7
use Symfony\Component\Security\Core\User\UserInterface;
8
9
/**
10
 * @author Kevin Bond <[email protected]>
11
 *
12
 * @method SymfonyKernelBrowser inner()
13
 */
14
class KernelBrowser extends BrowserKitBrowser implements ProfileAware
15
{
16
    final public function __construct(SymfonyKernelBrowser $inner)
17
    {
18
        parent::__construct($inner);
19
    }
20
21
    /**
22
     * @see SymfonyKernelBrowser::disableReboot()
23
     *
24
     * @return static
25
     */
26
    public function disableReboot(): self
27
    {
28
        $this->inner()->disableReboot();
29
30
        return $this;
31
    }
32
33
    /**
34
     * @see SymfonyKernelBrowser::enableReboot()
35
     *
36
     * @return static
37
     */
38
    public function enableReboot(): self
39
    {
40
        $this->inner()->enableReboot();
41
42
        return $this;
43
    }
44
45
    /**
46
     * By default, exceptions made during a request are caught and converted
47
     * to responses by Symfony. This disables this behaviour and actually
48
     * throws the exception.
49
     *
50
     * @return static
51
     */
52
    final public function throwExceptions(): self
53
    {
54
        $this->inner()->catchExceptions(false);
55
56
        return $this;
57
    }
58
59
    /**
60
     * Re-enables catching exceptions.
61
     *
62
     * @return static
63
     */
64
    final public function catchExceptions(): self
65
    {
66
        $this->inner()->catchExceptions(true);
67
68
        return $this;
69
    }
70
71
    /**
72
     * Enable profiling for the next request. Not required if profiling is
73
     * globally enabled.
74
     *
75
     * @return static
76
     */
77
    final public function withProfiling(): self
78
    {
79
        $this->inner()->enableProfiler();
80
81
        return $this;
82
    }
83
84
    final public function actingAs(UserInterface $user, ?string $firewall = null): self
85
    {
86
        if (null === $firewall) {
87
            $this->inner()->loginUser($user);
88
        } else {
89
            $this->inner()->loginUser($user, $firewall);
90
        }
91
92
        return $this;
93
    }
94
95
    final public function profile(): Profile
96
    {
97
        if (!$profile = $this->inner()->getProfile()) {
98
            throw new \RuntimeException('Profiler not enabled for this request. Try calling ->withProfiling() before the request.');
99
        }
100
101
        return $profile;
102
    }
103
}
104