Passed
Push — 1.x ( d0ea8a...e8b990 )
by Kevin
01:55
created

BrowserTests::can_use_components()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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

91
                $this->/** @scrutinizer ignore-call */ 
92
                       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...
92
93
                $browser->visit('/redirect1');
94
            })
95
            ->assertOn('/page1')
96
            ->use(function() {
97
                $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

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

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

152
        $this->/** @scrutinizer ignore-call */ 
153
               expectException(\TypeError::class);
Loading history...
153
154
        $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

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

192
        $this->/** @scrutinizer ignore-call */ 
193
               assertStringContainsString('/page1', $output[0]);
Loading history...
193
        $this->assertStringContainsString('<html', $output[0]);
194
        $this->assertStringContainsString('<h1>h1 title</h1>', $output[0]);
195
    }
196
197
    /**
198
     * @test
199
     */
200
    public function can_save_source(): void
201
    {
202
        $contents = self::catchFileContents(__DIR__.'/../var/browser/source/source.txt', function() {
203
            $this->browser()
204
                ->visit('/page1')
205
                ->saveSource('source.txt')
206
            ;
207
        });
208
209
        $this->assertStringContainsString('/page1', $contents);
210
        $this->assertStringContainsString('<html', $contents);
211
        $this->assertStringContainsString('<h1>h1 title</h1>', $contents);
212
    }
213
214
    protected static function catchFileContents(string $expectedFile, callable $callback): string
215
    {
216
        (new Filesystem())->remove($expectedFile);
217
218
        $callback();
219
220
        self::assertFileExists($expectedFile);
221
222
        return \file_get_contents($expectedFile);
223
    }
224
225
    protected static function catchVarDumperOutput(callable $callback): array
226
    {
227
        $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...
228
229
        VarDumper::setHandler(function($var) use (&$output) {
230
            $output[] = $var;
231
        });
232
233
        $callback();
234
235
        // reset to default handler
236
        VarDumper::setHandler();
237
238
        // a null value is added to the beginning
239
        return \array_values(\array_filter($output));
240
    }
241
242
    abstract protected function browser(): Browser;
243
}
244