Completed
Push — master ( ff0ab0...1d9d68 )
by Alexander
03:39
created

MakesApiRequests::seeSuccessStructure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 2
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Flugg\Responder\Traits;
4
5
use Flugg\Responder\Contracts\Responder;
6
use Illuminate\Http\JsonResponse;
7
8
/**
9
 * Use this trait in your base test case to give you some helper methods for
10
 * integration testing the API responses generated by the package.
11
 *
12
 * @package Laravel Responder
13
 * @author  Alexander Tømmerås <[email protected]>
14
 * @license The MIT License
15
 */
16
trait MakesApiRequests
17
{
18
    /**
19
     * Assert that the response is a valid success response.
20
     *
21
     * @param  mixed $data
22
     * @param  int   $status
23
     * @return $this
24
     */
25 View Code Duplication
    protected function seeSuccess( $data = null, $status = 200 )
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
26
    {
27
        $response = $this->seeSuccessResponse( $data, $status );
28
        $this->seeSuccessData( $response->getData( true )[ 'data' ] );
29
30
        return $this;
31
    }
32
33
    /**
34
     * Assert that the response is a valid success response.
35
     *
36
     * @param  mixed $data
37
     * @param  int   $status
38
     * @return $this
39
     */
40 View Code Duplication
    protected function seeSuccessEquals( $data = null, $status = 200 )
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
41
    {
42
        $response = $this->seeSuccessResponse( $data, $status );
43
        $this->seeJsonEquals( $response->getData( true ) );
44
45
        return $this;
46
    }
47
48
    /**
49
     * Assert that the response data contains the given structure.
50
     *
51
     * @param  mixed $data
52
     * @return $this
53
     */
54
    protected function seeSuccessStructure( $data = null, $status = 200 )
55
    {
56
        $this->seeStatusCode( $status );
57
58
        $this->seeJsonStructure( [
59
            'data' => $data
60
        ] );
61
62
        return $this;
63
    }
64
65
    /**
66
     * Assert that the response is a valid success response.
67
     *
68
     * @param  mixed $data
69
     * @param  int   $status
70
     * @return JsonResponse
71
     */
72
    protected function seeSuccessResponse( $data = null, $status = 200 ):JsonResponse
73
    {
74
        $response = app( Responder::class )->success( $data, $status );
75
76
        $this->seeStatusCode( $response->getStatusCode() )->seeJson( [
77
            'success' => true,
78
            'status' => $response->getStatusCode()
79
        ] )->seeJsonStructure( [ 'data' ] );
80
81
        return $response;
82
    }
83
84
    /**
85
     * Assert that the response data contains given values.
86
     *
87
     * @param  mixed $data
88
     * @return $this
89
     */
90
    protected function seeSuccessData( $data = null )
91
    {
92
        collect( $data )->each( function ( $value, $key ) {
93
            if ( is_array( $value ) ) {
94
                $this->seeSuccessData( $value );
95
            } else {
96
                $this->seeJson( [ $key => $value ] );
97
            }
98
        } );
99
100
        return $this;
101
    }
102
103
    /**
104
     * Decodes JSON response and returns the data.
105
     *
106
     * @return array
107
     */
108
    protected function getSuccessData()
109
    {
110
        return $this->decodeResponseJson()[ 'data' ];
111
    }
112
113
    /**
114
     * Assert that the response is a valid error response.
115
     *
116
     * @param  string   $error
117
     * @param  int|null $status
118
     * @return $this
119
     */
120
    protected function seeError( string $error, int $status = null )
121
    {
122
        if ( ! is_null( $status ) ) {
123
            $this->seeStatusCode( $status );
124
        }
125
126
        if ( config( 'responder.status_code' ) ) {
127
            $this->seeJson( [
128
                'status' => $status
129
            ] );
130
        }
131
132
        return $this->seeJson( [
133
            'success' => false
134
        ] )->seeJsonSubset( [
135
            'error' => [
136
                'code' => $error
137
            ]
138
        ] );
139
    }
140
141
    /**
142
     * Asserts that the status code of the response matches the given code.
143
     *
144
     * @param  int $status
145
     * @return $this
146
     */
147
    abstract protected function seeStatusCode( $status );
148
149
    /**
150
     * Assert that the response contains JSON.
151
     *
152
     * @param  array|null $data
153
     * @param  bool       $negate
154
     * @return $this
155
     */
156
    abstract public function seeJson( array $data = null, $negate = false );
157
158
    /**
159
     * Assert that the JSON response has a given structure.
160
     *
161
     * @param  array|null $structure
162
     * @param  array|null $responseData
163
     * @return $this
164
     */
165
    abstract public function seeJsonStructure( array $structure = null, $responseData = null );
166
167
    /**
168
     * Assert that the response is a superset of the given JSON.
169
     *
170
     * @param  array $data
171
     * @return $this
172
     */
173
    abstract protected function seeJsonSubset( array $data );
174
175
    /**
176
     * Assert that the response contains an exact JSON array.
177
     *
178
     * @param  array $data
179
     * @return $this
180
     */
181
    abstract public function seeJsonEquals( array $data );
182
183
    /**
184
     * Validate and return the decoded response JSON.
185
     *
186
     * @return array
187
     */
188
    abstract protected function decodeResponseJson();
189
}