Passed
Push — 1.x ( 5e9553...a9c58b )
by Kevin
01:53
created

BrowserTests::assert_on()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 2
Metric Value
cc 1
eloc 11
nc 1
nop 0
dl 0
loc 13
rs 9.9
c 2
b 0
f 2
1
<?php
2
3
namespace Zenstruck\Browser\Tests;
4
5
use Symfony\Component\VarDumper\VarDumper;
6
use Zenstruck\Browser;
7
use Zenstruck\Browser\Tests\Extension\HtmlTests;
8
use Zenstruck\Browser\Tests\Fixture\TestComponent1;
9
use Zenstruck\Browser\Tests\Fixture\TestComponent2;
10
11
/**
12
 * @author Kevin Bond <[email protected]>
13
 */
14
trait BrowserTests
15
{
16
    use HtmlTests;
17
18
    /**
19
     * @test
20
     */
21
    public function multiple_browsers(): void
22
    {
23
        $browser1 = $this->browser()
0 ignored issues
show
Bug introduced by
The method browser() does not exist on Zenstruck\Browser\Tests\BrowserTests. Did you maybe mean browserClass()? ( Ignorable by Annotation )

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

23
        $browser1 = $this->/** @scrutinizer ignore-call */ 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...
24
            ->visit('/page1')
25
            ->assertOn('/page1')
26
        ;
27
28
        $browser2 = $this->browser()
0 ignored issues
show
Unused Code introduced by
The assignment to $browser2 is dead and can be removed.
Loading history...
29
            ->visit('/page2')
30
            ->assertOn('/page2')
31
        ;
32
33
        // this ensures a different browser is actually used
34
        $browser1->assertOn('/page1');
35
    }
36
37
    /**
38
     * @test
39
     */
40
    public function assert_on(): void
41
    {
42
        $this->browser()
43
            ->visit('/page1')
44
            ->assertOn('/page1')
45
            ->assertOn('http://www.example.com/page1')
46
            ->assertNotOn('/page2')
47
            ->assertNotOn('http://www.example.com/page1', ['path', 'host'])
48
            ->visit('/page1?foo=bar')
49
            ->assertOn('/page1?foo=bar')
50
            ->assertOn('/page1', ['path'])
51
            ->assertOn('/page1', ['path', 'fragment'])
52
            ->assertNotOn('/page1?foo=baz')
53
        ;
54
    }
55
56
    /**
57
     * @test
58
     * @dataProvider encodedUrlProvider
59
     */
60
    public function assert_on_encoded($url, $expected): void
61
    {
62
        $this->browser()
63
            ->visit($url)
64
            ->assertOn($expected)
65
        ;
66
    }
67
68
    public static function encodedUrlProvider(): iterable
69
    {
70
        yield ['/page1?filter[q]=value', '/page1?filter[q]=value'];
71
        yield ['/page1?filter%5Bq%5D=value', '/page1?filter[q]=value'];
72
        yield ['/page1?filter[q]=value', '/page1?filter%5Bq%5D=value'];
73
        yield ['/page1#foo bar', '/page1#foo bar'];
74
        yield ['/page1#foo%20bar', '/page1#foo bar'];
75
        yield ['/page1#foo bar', '/page1#foo%20bar'];
76
        yield ['/page1#foo+bar', '/page1#foo bar'];
77
        yield ['/page1#foo bar', '/page1#foo+bar'];
78
    }
79
80
    /**
81
     * @test
82
     */
83
    public function can_use_current_browser(): void
84
    {
85
        $browser = $this->browser();
86
87
        $browser
88
            ->use(function(Browser $b) use ($browser) {
89
                $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

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

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

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

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

150
        $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...
151
    }
152
153
    /**
154
     * @test
155
     */
156
    public function redirects_are_followed_by_default(): void
157
    {
158
        $this->browser()
159
            ->visit('/redirect1')
160
            ->assertOn('/page1')
161
        ;
162
    }
163
164
    /**
165
     * @test
166
     */
167
    public function content_assertions(): void
168
    {
169
        $this->browser()
170
            ->visit('/page1')
171
            ->assertContains('h1 title')
172
            ->assertNotContains('invalid text')
173
        ;
174
    }
175
176
    /**
177
     * @test
178
     */
179
    public function can_dump_response(): void
180
    {
181
        $dumpedValues[] = null;
0 ignored issues
show
Comprehensibility Best Practice introduced by
$dumpedValues was never initialized. Although not strictly required by PHP, it is generally a good practice to add $dumpedValues = array(); before regardless.
Loading history...
182
183
        VarDumper::setHandler(function($var) use (&$dumpedValues) {
184
            $dumpedValues[] = $var;
185
        });
186
187
        $this->browser()
188
            ->visit('/page1')
189
            ->dump()
190
        ;
191
192
        VarDumper::setHandler();
193
194
        // a null value is added to the beginning
195
        $dumped = \array_values(\array_filter($dumpedValues))[0];
196
197
        $this->assertStringContainsString('/page1', $dumped);
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

197
        $this->/** @scrutinizer ignore-call */ 
198
               assertStringContainsString('/page1', $dumped);
Loading history...
198
        $this->assertStringContainsString('<html', $dumped);
199
        $this->assertStringContainsString('<h1>h1 title</h1>', $dumped);
200
    }
201
202
    /**
203
     * @test
204
     */
205
    public function can_save_source(): void
206
    {
207
        $file = __DIR__.'/../var/browser/source/source.txt';
208
209
        if (\file_exists($file)) {
210
            \unlink($file);
211
        }
212
213
        $this->browser()
214
            ->visit('/page1')
215
            ->saveSource('source.txt')
216
        ;
217
218
        $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

218
        $this->/** @scrutinizer ignore-call */ 
219
               assertFileExists($file);
Loading history...
219
220
        $contents = \file_get_contents($file);
221
222
        $this->assertStringContainsString('/page1', $contents);
223
        $this->assertStringContainsString('<html', $contents);
224
        $this->assertStringContainsString('<h1>h1 title</h1>', $contents);
225
226
        \unlink($file);
227
    }
228
229
    abstract protected static function browserClass(): string;
230
}
231