Completed
Push — master ( 34817a...00c020 )
by Alexander
03:41 queued 46s
created

MakesApiRequests::seeSuccess()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 7
Ratio 100 %

Importance

Changes 0
Metric Value
dl 7
loc 7
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 2
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
    protected function seeSuccess( $data = null, $status = 200 )
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
    protected function seeSuccessEquals( $data = null, $status = 200 )
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 is a valid success response.
50
     *
51
     * @param  mixed $data
52
     * @param  int   $status
53
     * @return JsonResponse
54
     */
55
    protected function seeSuccessResponse( $data = null, $status = 200 ):JsonResponse
56
    {
57
        $response = app( Responder::class )->success( $data, $status );
58
59
        $this->seeStatusCode( $response->getStatusCode() )->seeJson( [
60
            'success' => true,
61
            'status' => $response->getStatusCode()
62
        ] )->seeJsonStructure( [ 'data' ] );
63
64
        return $response;
65
    }
66
67
    /**
68
     * Assert that the response data contains given values.
69
     *
70
     * @param  mixed $data
71
     * @return $this
72
     */
73
    protected function seeSuccessData( $data = null )
74
    {
75
        collect( $data )->each( function ( $value, $key ) {
76
            if ( is_array( $value ) ) {
77
                $this->seeSuccessData( $value );
78
            } else {
79
                $this->seeJson( [ $key => $value ] );
80
            }
81
        } );
82
83
        return $this;
84
    }
85
86
    /**
87
     * Decodes JSON response and returns the data.
88
     *
89
     * @return array
90
     */
91
    protected function getSuccessData()
92
    {
93
        return $this->decodeResponseJson()[ 'data' ];
94
    }
95
96
    /**
97
     * Assert that the response is a valid error response.
98
     *
99
     * @param  string   $error
100
     * @param  int|null $status
101
     * @return $this
102
     */
103
    protected function seeError( string $error, int $status = null )
104
    {
105
        if ( ! is_null( $status ) ) {
106
            $this->seeStatusCode( $status );
107
        }
108
109
        if ( config( 'responder.status_code' ) ) {
110
            $this->seeJson( 'status' => $status );
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_DOUBLE_ARROW, expecting ',' or ')'
Loading history...
111
        }
112
113
        return $this->seeJson( [
114
            'success' => false
115
        ] )->seeJsonSubset( [
116
            'error' => [
117
                'code' => $error
118
            ]
119
        ] );
120
    }
121
122
    /**
123
     * Asserts that the status code of the response matches the given code.
124
     *
125
     * @param  int $status
126
     * @return $this
127
     */
128
    abstract protected function seeStatusCode( $status );
129
130
    /**
131
     * Assert that the response contains JSON.
132
     *
133
     * @param  array|null $data
134
     * @param  bool       $negate
135
     * @return $this
136
     */
137
    abstract public function seeJson( array $data = null, $negate = false );
138
139
    /**
140
     * Assert that the JSON response has a given structure.
141
     *
142
     * @param  array|null $structure
143
     * @param  array|null $responseData
144
     * @return $this
145
     */
146
    abstract public function seeJsonStructure( array $structure = null, $responseData = null );
147
148
    /**
149
     * Assert that the response is a superset of the given JSON.
150
     *
151
     * @param  array $data
152
     * @return $this
153
     */
154
    abstract protected function seeJsonSubset( array $data );
155
156
    /**
157
     * Assert that the response contains an exact JSON array.
158
     *
159
     * @param  array $data
160
     * @return $this
161
     */
162
    abstract public function seeJsonEquals( array $data );
163
164
    /**
165
     * Validate and return the decoded response JSON.
166
     *
167
     * @return array
168
     */
169
    abstract protected function decodeResponseJson();
170
}