Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | <?php |
||
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 ); |
||
|
|||
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 | */ |
||
170 | } |