Passed
Push — 1.x ( e3781f...255b78 )
by Kevin
02:10
created

BrowserTests::form_actions_by_field_name()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 16
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 18
rs 9.7333
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
            ->assertNotOn('/page2')
46
            ->visit('/page1?foo=bar')
47
            ->assertOn('/page1?foo=bar')
48
            ->assertNotOn('/page1?foo=baz')
49
        ;
50
    }
51
52
    /**
53
     * @test
54
     */
55
    public function can_use_current_browser(): void
56
    {
57
        $browser = $this->browser();
58
59
        $browser
60
            ->use(function(Browser $b) use ($browser) {
61
                $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

61
                $this->/** @scrutinizer ignore-call */ 
62
                       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...
62
63
                $browser->visit('/redirect1');
64
            })
65
            ->assertOn('/page1')
66
            ->use(function() {
67
                $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

67
                $this->/** @scrutinizer ignore-call */ 
68
                       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...
68
            })
69
        ;
70
    }
71
72
    /**
73
     * @test
74
     */
75
    public function can_use_components(): void
76
    {
77
        $this->browser()
78
            ->use(function(TestComponent1 $component) {
79
                $component->assertTitle('h1 title');
80
            })
81
            ->assertOn('/page1')
82
        ;
83
    }
84
85
    /**
86
     * @test
87
     */
88
    public function component_pre_assertions_and_actions_are_called(): void
89
    {
90
        $this->browser()
91
            ->use(function(TestComponent2 $component) {
92
                $this->assertTrue($component->preActionsCalled);
93
                $this->assertTrue($component->preAssertionsCalled);
94
            })
95
        ;
96
    }
97
98
    /**
99
     * @test
100
     */
101
    public function with_can_accept_multiple_browsers_and_components(): void
102
    {
103
        $this->browser()
104
            ->use(function(Browser $browser1, $browser2, TestComponent1 $component1, TestComponent2 $component2) {
105
                $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

105
                $this->/** @scrutinizer ignore-call */ 
106
                       assertInstanceOf(Browser::class, $browser1);
Loading history...
106
                $this->assertInstanceOf(Browser::class, $browser2);
107
                $this->assertInstanceOf($this->browserClass(), $browser1);
108
                $this->assertInstanceOf($this->browserClass(), $browser2);
109
                $this->assertInstanceOf(TestComponent1::class, $component1);
110
                $this->assertInstanceOf(TestComponent2::class, $component2);
111
            })
112
        ;
113
    }
114
115
    /**
116
     * @test
117
     */
118
    public function invalid_with_callback_parameter_throws_type_error(): void
119
    {
120
        $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

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

122
        $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...
123
    }
124
125
    /**
126
     * @test
127
     */
128
    public function redirects_are_followed_by_default(): void
129
    {
130
        $this->browser()
131
            ->visit('/redirect1')
132
            ->assertOn('/page1')
133
        ;
134
    }
135
136
    /**
137
     * @test
138
     */
139
    public function content_assertions(): void
140
    {
141
        $this->browser()
142
            ->visit('/page1')
143
            ->assertContains('h1 title')
144
            ->assertNotContains('invalid text')
145
        ;
146
    }
147
148
    /**
149
     * @test
150
     */
151
    public function can_dump_response(): void
152
    {
153
        $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...
154
155
        VarDumper::setHandler(function($var) use (&$dumpedValues) {
156
            $dumpedValues[] = $var;
157
        });
158
159
        $this->browser()
160
            ->visit('/page1')
161
            ->dump()
162
        ;
163
164
        VarDumper::setHandler();
165
166
        // a null value is added to the beginning
167
        $dumped = \array_values(\array_filter($dumpedValues))[0];
168
169
        $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

169
        $this->/** @scrutinizer ignore-call */ 
170
               assertStringContainsString('/page1', $dumped);
Loading history...
170
        $this->assertStringContainsString('<h1>h1 title</h1>', $dumped);
171
        $this->assertStringContainsString('/page1', $dumped);
172
    }
173
174
    /**
175
     * @test
176
     */
177
    public function can_save_source(): void
178
    {
179
        $file = __DIR__.'/../var/browser/source/source.txt';
180
181
        if (\file_exists($file)) {
182
            \unlink($file);
183
        }
184
185
        $this->browser()
186
            ->visit('/page1')
187
            ->saveSource('source.txt')
188
        ;
189
190
        $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

190
        $this->/** @scrutinizer ignore-call */ 
191
               assertFileExists($file);
Loading history...
191
192
        $contents = \file_get_contents($file);
193
194
        $this->assertStringContainsString('/page1', $contents);
195
        $this->assertStringContainsString('<h1>h1 title</h1>', $contents);
196
197
        \unlink($file);
198
    }
199
200
    abstract protected static function browserClass(): string;
201
}
202