Passed
Push — 1.x ( 885d6a...11c6d3 )
by Kevin
01:56
created

Browser::normalizeDumpVariable()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 9
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A Browser::dumpCurrentState() 0 3 1
1
<?php
2
3
namespace Zenstruck;
4
5
use Behat\Mink\Driver\DriverInterface;
6
use Behat\Mink\Element\DocumentElement;
7
use Behat\Mink\Exception\ExpectationException;
8
use Behat\Mink\Mink;
9
use Behat\Mink\Session;
10
use Behat\Mink\WebAssert;
11
use PHPUnit\Framework\Assert as PHPUnit;
0 ignored issues
show
Bug introduced by
The type PHPUnit\Framework\Assert was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
use Symfony\Component\Filesystem\Filesystem;
13
use Symfony\Component\VarDumper\VarDumper;
14
use Zenstruck\Browser\Component;
15
use Zenstruck\Browser\Response;
16
use Zenstruck\Browser\Test\Constraint\UrlMatches;
17
use Zenstruck\Browser\Util\FunctionExecutor;
18
19
/**
20
 * @author Kevin Bond <[email protected]>
21
 */
22
class Browser
23
{
24
    private const SESSION = 'app';
25
26
    private Mink $mink;
27
    private ?string $sourceDir = null;
28
29
    public function __construct(DriverInterface $driver)
30
    {
31
        $this->mink = new Mink([self::SESSION => new Session($driver)]);
32
    }
33
34
    /**
35
     * @return static
36
     */
37
    final public function setSourceDir(string $dir): self
38
    {
39
        $this->sourceDir = $dir;
40
41
        return $this;
42
    }
43
44
    final public function minkSession(): Session
45
    {
46
        return $this->mink->getSession(self::SESSION);
47
    }
48
49
    final public function webAssert(): WebAssert
50
    {
51
        return $this->mink->assertSession(self::SESSION);
52
    }
53
54
    final public function documentElement(): DocumentElement
55
    {
56
        return $this->minkSession()->getPage();
57
    }
58
59
    /**
60
     * @return static
61
     */
62
    final public function visit(string $uri): self
63
    {
64
        $this->minkSession()->visit($uri);
65
66
        return $this;
67
    }
68
69
    /**
70
     * @param array $parts The url parts to check (@see parse_url)
71
     *
72
     * @return static
73
     */
74
    final public function assertOn(string $expected, array $parts = ['path', 'query', 'fragment']): self
75
    {
76
        PHPUnit::assertThat($expected, new UrlMatches($this->minkSession()->getCurrentUrl(), $parts));
77
78
        return $this;
79
    }
80
81
    /**
82
     * @param array $parts The url parts to check (@see parse_url)
83
     *
84
     * @return static
85
     */
86
    final public function assertNotOn(string $expected, array $parts = ['path', 'query', 'fragment']): self
87
    {
88
        PHPUnit::assertThat(
89
            $expected,
90
            PHPUnit::logicalNot(new UrlMatches($this->minkSession()->getCurrentUrl(), $parts))
91
        );
92
93
        return $this;
94
    }
95
96
    /**
97
     * @return static
98
     */
99
    final public function assertContains(string $expected): self
100
    {
101
        return $this->wrapMinkExpectation(
102
            fn() => $this->webAssert()->responseContains($expected)
103
        );
104
    }
105
106
    /**
107
     * @return static
108
     */
109
    final public function assertNotContains(string $expected): self
110
    {
111
        return $this->wrapMinkExpectation(
112
            fn() => $this->webAssert()->responseNotContains($expected)
113
        );
114
    }
115
116
    /**
117
     * @return static
118
     */
119
    final public function use(callable $callback): self
120
    {
121
        FunctionExecutor::createFor($callback)
122
            ->replaceUntypedArgument($this)
123
            ->replaceTypedArgument(self::class, $this)
124
            ->replaceTypedArgument(Component::class, fn(string $class) => new $class($this))
125
            ->execute()
126
        ;
127
128
        return $this;
129
    }
130
131
    /**
132
     * @return static
133
     */
134
    final public function saveSource(string $filename): self
135
    {
136
        if ($this->sourceDir) {
137
            $filename = \sprintf('%s/%s', \rtrim($this->sourceDir, '/'), \ltrim($filename, '/'));
138
        }
139
140
        (new Filesystem())->dumpFile($filename, $this->response()->raw());
141
142
        return $this;
143
    }
144
145
    /**
146
     * @return static
147
     */
148
    final public function dump(?string $selector = null): self
149
    {
150
        VarDumper::dump($selector ? $this->response()->find($selector) : $this->response()->raw());
151
152
        return $this;
153
    }
154
155
    final public function dd(?string $selector = null): void
156
    {
157
        $this->dump($selector);
158
        exit(1);
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
159
    }
160
161
    public function dumpCurrentState(string $filename): void
162
    {
163
        $this->saveSource("{$filename}.txt");
164
    }
165
166
    protected function response(): Response
167
    {
168
        return Response::createFor($this->minkSession());
169
    }
170
171
    /**
172
     * @return static
173
     */
174
    final protected function wrapMinkExpectation(callable $callback): self
175
    {
176
        try {
177
            $callback();
178
            PHPUnit::assertTrue(true);
179
        } catch (ExpectationException $e) {
180
            PHPUnit::fail($e->getMessage());
181
        }
182
183
        return $this;
184
    }
185
}
186