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

JsonTests::can_save_formatted_json_source()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 21
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 11
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 21
rs 9.9
1
<?php
2
3
namespace Zenstruck\Browser\Tests\Extension;
4
5
use Symfony\Component\VarDumper\VarDumper;
6
7
/**
8
 * @author Kevin Bond <[email protected]>
9
 */
10
trait JsonTests
11
{
12
    /**
13
     * @test
14
     */
15
    public function can_assert_json_matches(): void
16
    {
17
        $this->browser()
0 ignored issues
show
Bug introduced by
It seems like browser() 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

17
        $this->/** @scrutinizer ignore-call */ 
18
               browser()
Loading history...
18
            ->post('/json', ['json' => [
19
                'foo' => [
20
                    'bar' => ['baz' => 1],
21
                    'bam' => ['baz' => 2],
22
                    'boo' => ['baz' => 3],
23
                ],
24
            ]])
25
            ->assertJsonMatches('foo.bar.baz', 1)
26
            ->assertJsonMatches('foo.*.baz', [1, 2, 3])
27
            ->assertJsonMatches('length(foo)', 3)
28
        ;
29
    }
30
31
    /**
32
     * @test
33
     */
34
    public function can_dump_json_response_as_array(): void
35
    {
36
        $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...
37
38
        VarDumper::setHandler(function($var) use (&$dumpedValues) {
39
            $dumpedValues[] = $var;
40
        });
41
42
        $this->browser()
43
            ->post('/json', ['json' => ['foo' => 'bar']])
44
            ->dump()
45
        ;
46
47
        VarDumper::setHandler();
48
49
        // a null value is added to the beginning
50
        $dumped = \array_values(\array_filter($dumpedValues))[0];
51
52
        $this->assertStringContainsString('    "foo": "bar"', $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

52
        $this->/** @scrutinizer ignore-call */ 
53
               assertStringContainsString('    "foo": "bar"', $dumped);
Loading history...
53
    }
54
55
    /**
56
     * @test
57
     */
58
    public function can_dump_json_array_key(): void
59
    {
60
        $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...
61
62
        VarDumper::setHandler(function($var) use (&$dumpedValues) {
63
            $dumpedValues[] = $var;
64
        });
65
66
        $this->browser()
67
            ->post('/json', ['json' => ['foo' => 'bar']])
68
            ->dump('foo')
69
        ;
70
71
        VarDumper::setHandler();
72
73
        // a null value is added to the beginning
74
        $dumped = \array_values(\array_filter($dumpedValues))[0];
75
76
        $this->assertSame('bar', $dumped);
0 ignored issues
show
Bug introduced by
It seems like assertSame() 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

76
        $this->/** @scrutinizer ignore-call */ 
77
               assertSame('bar', $dumped);
Loading history...
77
    }
78
79
    /**
80
     * @test
81
     */
82
    public function can_dump_json_path_expression(): void
83
    {
84
        $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...
85
86
        VarDumper::setHandler(function($var) use (&$dumpedValues) {
87
            $dumpedValues[] = $var;
88
        });
89
90
        $this->browser()
91
            ->post('/json', ['json' => [
92
                'foo' => [
93
                    'bar' => ['baz' => 1],
94
                    'bam' => ['baz' => 2],
95
                    'boo' => ['baz' => 3],
96
                ],
97
            ]])
98
            ->dump('foo.*.baz')
99
        ;
100
101
        VarDumper::setHandler();
102
103
        // a null value is added to the beginning
104
        $dumped = \array_values(\array_filter($dumpedValues))[0];
105
106
        $this->assertSame([1, 2, 3], $dumped);
107
    }
108
109
    /**
110
     * @test
111
     */
112
    public function can_save_formatted_json_source(): void
113
    {
114
        $file = __DIR__.'/../../var/browser/source/source.txt';
115
116
        if (\file_exists($file)) {
117
            \unlink($file);
118
        }
119
120
        $this->browser()
121
            ->visit('/http-method')
122
            ->saveSource('/source.txt')
123
        ;
124
125
        $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

125
        $this->/** @scrutinizer ignore-call */ 
126
               assertFileExists($file);
Loading history...
126
127
        $contents = \file_get_contents($file);
128
129
        $this->assertStringContainsString('/http-method', $contents);
130
        $this->assertStringContainsString('    "content": "",', $contents);
131
132
        \unlink($file);
133
    }
134
}
135