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

KernelBrowser::actingAs()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 5
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
     * By default, exceptions made during a request are caught and converted
23
     * to responses by Symfony. This disables this behaviour and actually
24
     * throws the exception.
25
     *
26
     * @return static
27
     */
28
    final public function throwExceptions(): self
29
    {
30
        $this->inner()->catchExceptions(false);
31
32
        return $this;
33
    }
34
35
    /**
36
     * Re-enables catching exceptions.
37
     *
38
     * @return static
39
     */
40
    final public function catchExceptions(): self
41
    {
42
        $this->inner()->catchExceptions(true);
43
44
        return $this;
45
    }
46
47
    /**
48
     * Enable profiling for the next request. Not required if profiling is
49
     * globally enabled.
50
     *
51
     * @return static
52
     */
53
    final public function withProfiling(): self
54
    {
55
        $this->inner()->enableProfiler();
56
57
        return $this;
58
    }
59
60
    final public function actingAs(UserInterface $user): self
61
    {
62
        $this->inner()->loginUser($user);
63
64
        return $this;
65
    }
66
67
    final public function profile(): Profile
68
    {
69
        if (!$profile = $this->inner()->getProfile()) {
70
            throw new \RuntimeException('Profiler not enabled for this request. Try calling ->withProfiling() before the request.');
71
        }
72
73
        return $profile;
74
    }
75
}
76