Passed
Push — 1.x ( e7706b...d0ea8a )
by Kevin
02:02
created

BrowserTests::can_save_source()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 12
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 22
rs 9.8666
1
<?php
2
3
namespace Zenstruck\Browser\Tests;
4
5
use Symfony\Component\VarDumper\VarDumper;
6
use Zenstruck\Browser;
7
use Zenstruck\Browser\Test\HasBrowser;
8
use Zenstruck\Browser\Tests\Extension\HtmlTests;
9
use Zenstruck\Browser\Tests\Fixture\TestComponent1;
10
use Zenstruck\Browser\Tests\Fixture\TestComponent2;
11
12
/**
13
 * @author Kevin Bond <[email protected]>
14
 */
15
trait BrowserTests
16
{
17
    use HasBrowser, HtmlTests;
18
19
    /**
20
     * @test
21
     */
22
    public function multiple_browsers(): void
23
    {
24
        $browser1 = $this->browser()
25
            ->visit('/page1')
26
            ->assertOn('/page1')
27
        ;
28
29
        $browser2 = $this->browser()
0 ignored issues
show
Unused Code introduced by
The assignment to $browser2 is dead and can be removed.
Loading history...
30
            ->visit('/page2')
31
            ->assertOn('/page2')
32
        ;
33
34
        // this ensures a different browser is actually used
35
        $browser1->assertOn('/page1');
36
    }
37
38
    /**
39
     * @test
40
     */
41
    public function assert_on(): void
42
    {
43
        $this->browser()
44
            ->visit('/page1')
45
            ->assertOn('/page1')
46
            ->assertOn('http://www.example.com/page1')
47
            ->assertNotOn('/page2')
48
            ->assertNotOn('http://www.example.com/page1', ['path', 'host'])
49
            ->visit('/page1?foo=bar')
50
            ->assertOn('/page1?foo=bar')
51
            ->assertOn('/page1', ['path'])
52
            ->assertOn('/page1', ['path', 'fragment'])
53
            ->assertNotOn('/page1?foo=baz')
54
        ;
55
    }
56
57
    /**
58
     * @test
59
     * @dataProvider encodedUrlProvider
60
     */
61
    public function assert_on_encoded($url, $expected): void
62
    {
63
        $this->browser()
64
            ->visit($url)
65
            ->assertOn($expected)
66
        ;
67
    }
68
69
    public static function encodedUrlProvider(): iterable
70
    {
71
        yield ['/page1?filter[q]=value', '/page1?filter[q]=value'];
72
        yield ['/page1?filter%5Bq%5D=value', '/page1?filter[q]=value'];
73
        yield ['/page1?filter[q]=value', '/page1?filter%5Bq%5D=value'];
74
        yield ['/page1#foo bar', '/page1#foo bar'];
75
        yield ['/page1#foo%20bar', '/page1#foo bar'];
76
        yield ['/page1#foo bar', '/page1#foo%20bar'];
77
        yield ['/page1#foo+bar', '/page1#foo bar'];
78
        yield ['/page1#foo bar', '/page1#foo+bar'];
79
    }
80
81
    /**
82
     * @test
83
     */
84
    public function can_use_current_browser(): void
85
    {
86
        $browser = $this->browser();
87
88
        $browser
89
            ->use(function(Browser $b) use ($browser) {
90
                $this->assertSame($b, $browser);
0 ignored issues
show
Bug introduced by
The method assertSame() does not exist on Zenstruck\Browser\Tests\BrowserTests. 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

90
                $this->/** @scrutinizer ignore-call */ 
91
                       assertSame($b, $browser);

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...
91
92
                $browser->visit('/redirect1');
93
            })
94
            ->assertOn('/page1')
95
            ->use(function() {
96
                $this->assertTrue(true);
0 ignored issues
show
Bug introduced by
The method assertTrue() does not exist on Zenstruck\Browser\Tests\BrowserTests. 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

96
                $this->/** @scrutinizer ignore-call */ 
97
                       assertTrue(true);

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...
97
            })
98
        ;
99
    }
100
101
    /**
102
     * @test
103
     */
104
    public function can_use_components(): void
105
    {
106
        $this->browser()
107
            ->use(function(TestComponent1 $component) {
108
                $component->assertTitle('h1 title');
109
            })
110
            ->assertOn('/page1')
111
        ;
112
    }
113
114
    /**
115
     * @test
116
     */
117
    public function component_pre_assertions_and_actions_are_called(): void
118
    {
119
        $this->browser()
120
            ->use(function(TestComponent2 $component) {
121
                $this->assertTrue($component->preActionsCalled);
122
                $this->assertTrue($component->preAssertionsCalled);
123
            })
124
        ;
125
    }
126
127
    /**
128
     * @test
129
     */
130
    public function with_can_accept_multiple_browsers_and_components(): void
131
    {
132
        $browser = $this->browser();
133
134
        $browser
135
            ->use(function(Browser $browser1, $browser2, TestComponent1 $component1, TestComponent2 $component2) use ($browser) {
136
                $this->assertInstanceOf(Browser::class, $browser1);
0 ignored issues
show
Bug introduced by
It seems like assertInstanceOf() 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

136
                $this->/** @scrutinizer ignore-call */ 
137
                       assertInstanceOf(Browser::class, $browser1);
Loading history...
137
                $this->assertInstanceOf(Browser::class, $browser2);
138
                $this->assertInstanceOf(\get_class($browser), $browser1);
139
                $this->assertInstanceOf(\get_class($browser), $browser2);
140
                $this->assertInstanceOf(TestComponent1::class, $component1);
141
                $this->assertInstanceOf(TestComponent2::class, $component2);
142
            })
143
        ;
144
    }
145
146
    /**
147
     * @test
148
     */
149
    public function invalid_with_callback_parameter_throws_type_error(): void
150
    {
151
        $this->expectException(\TypeError::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

151
        $this->/** @scrutinizer ignore-call */ 
152
               expectException(\TypeError::class);
Loading history...
152
153
        $this->browser()->use(function(string $invalidType) {});
0 ignored issues
show
Unused Code introduced by
The parameter $invalidType is not used and could be removed. ( Ignorable by Annotation )

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

153
        $this->browser()->use(function(/** @scrutinizer ignore-unused */ string $invalidType) {});

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
154
    }
155
156
    /**
157
     * @test
158
     */
159
    public function redirects_are_followed_by_default(): void
160
    {
161
        $this->browser()
162
            ->visit('/redirect1')
163
            ->assertOn('/page1')
164
        ;
165
    }
166
167
    /**
168
     * @test
169
     */
170
    public function content_assertions(): void
171
    {
172
        $this->browser()
173
            ->visit('/page1')
174
            ->assertContains('h1 title')
175
            ->assertNotContains('invalid text')
176
        ;
177
    }
178
179
    /**
180
     * @test
181
     */
182
    public function can_dump_response(): void
183
    {
184
        $output = self::catchVarDumperOutput(function() {
185
            $this->browser()
186
                ->visit('/page1')
187
                ->dump()
188
            ;
189
        });
190
191
        $this->assertStringContainsString('/page1', $output[0]);
0 ignored issues
show
Bug introduced by
It seems like assertStringContainsString() 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

191
        $this->/** @scrutinizer ignore-call */ 
192
               assertStringContainsString('/page1', $output[0]);
Loading history...
192
        $this->assertStringContainsString('<html', $output[0]);
193
        $this->assertStringContainsString('<h1>h1 title</h1>', $output[0]);
194
    }
195
196
    /**
197
     * @test
198
     */
199
    public function can_save_source(): void
200
    {
201
        $file = __DIR__.'/../var/browser/source/source.txt';
202
203
        if (\file_exists($file)) {
204
            \unlink($file);
205
        }
206
207
        $this->browser()
208
            ->visit('/page1')
209
            ->saveSource('source.txt')
210
        ;
211
212
        $this->assertFileExists($file);
0 ignored issues
show
Bug introduced by
It seems like assertFileExists() 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

212
        $this->/** @scrutinizer ignore-call */ 
213
               assertFileExists($file);
Loading history...
213
214
        $contents = \file_get_contents($file);
215
216
        $this->assertStringContainsString('/page1', $contents);
217
        $this->assertStringContainsString('<html', $contents);
218
        $this->assertStringContainsString('<h1>h1 title</h1>', $contents);
219
220
        \unlink($file);
221
    }
222
223
    protected static function catchVarDumperOutput(callable $callback): array
224
    {
225
        $output[] = null;
0 ignored issues
show
Comprehensibility Best Practice introduced by
$output was never initialized. Although not strictly required by PHP, it is generally a good practice to add $output = array(); before regardless.
Loading history...
226
227
        VarDumper::setHandler(function($var) use (&$output) {
228
            $output[] = $var;
229
        });
230
231
        $callback();
232
233
        // reset to default handler
234
        VarDumper::setHandler();
235
236
        // a null value is added to the beginning
237
        return \array_values(\array_filter($output));
238
    }
239
240
    abstract protected function browser(): Browser;
241
}
242