Completed
Push — master ( e1d656...42899b )
by Alexander
03:35
created

MakesApiRequests::seeJsonEquals()

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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