@@ -42,16 +42,11 @@ abstract class ApiClientTest extends \PHPUnit_Framework_TestCase
42
42
43
43
protected function setUp ()
44
44
{
45
- $ this ->requestHandler = $ this ->createRequestHandlerMock ();
46
- $ this ->serializer = $ this ->createSerializerMock ();
45
+ $ this ->requestHandler = $ this ->getMockBuilder ( ' \Xabbuh\XApi\Client\Request\HandlerInterface ' )-> getMock ();
46
+ $ this ->serializer = $ this ->getMockBuilder ( ' \Symfony\Component\Serializer\SerializerInterface ' )-> getMock ();
47
47
$ this ->serializerRegistry = $ this ->createSerializerRegistry ();
48
48
}
49
49
50
- protected function createRequestHandlerMock ()
51
- {
52
- return $ this ->getMock ('\Xabbuh\XApi\Client\Request\HandlerInterface ' );
53
- }
54
-
55
50
protected function createSerializerRegistry ()
56
51
{
57
52
$ registry = new SerializerRegistry ();
@@ -63,76 +58,54 @@ protected function createSerializerRegistry()
63
58
return $ registry ;
64
59
}
65
60
66
- protected function createSerializerMock ()
67
- {
68
- return $ this ->getMock ('\Symfony\Component\Serializer\SerializerInterface ' );
69
- }
70
-
71
- protected function validateDeserializer ($ data , $ type , $ returnValue )
72
- {
73
- $ this ->serializer
74
- ->expects ($ this ->once ())
75
- ->method ('deserialize ' )
76
- ->with ($ data , 'Xabbuh\XApi\Model \\' .$ type , 'json ' )
77
- ->will ($ this ->returnValue ($ returnValue ));
78
- }
79
-
80
61
protected function validateSerializer (array $ serializerMap )
81
62
{
82
63
$ this
83
64
->serializer
84
65
->expects ($ this ->any ())
85
66
->method ('serialize ' )
86
- ->will ( $ this -> returnCallback (function ($ data ) use ($ serializerMap ) {
67
+ ->willReturnCallback (function ($ data ) use ($ serializerMap ) {
87
68
foreach ($ serializerMap as $ entry ) {
88
69
if ($ data == $ entry ['data ' ]) {
89
70
return $ entry ['result ' ];
90
71
}
91
72
}
92
73
93
74
return '' ;
94
- }));
95
- }
96
-
97
- protected function createRequestMock ($ response = null )
98
- {
99
- $ request = $ this ->getMock ('\Guzzle\Http\Message\RequestInterface ' );
100
-
101
- if (null !== $ response ) {
102
- $ request ->expects ($ this ->any ())
103
- ->method ('send ' )
104
- ->will ($ this ->returnValue ($ response ));
105
- }
106
-
107
- return $ request ;
75
+ });
108
76
}
109
77
110
78
protected function createResponseMock ($ statusCode , $ body )
111
79
{
112
- $ response = $ this ->getMock (
113
- '\Guzzle\Http\Message\Response ' ,
114
- array (),
115
- array ($ statusCode )
116
- );
80
+ $ response = $ this ->getMockBuilder ('\Guzzle\Http\Message\Response ' )
81
+ ->disableOriginalConstructor ()
82
+ ->getMock ();
117
83
$ response ->expects ($ this ->any ())
118
84
->method ('getStatusCode ' )
119
- ->will ( $ this -> returnValue ( $ statusCode) );
85
+ ->willReturn ( $ statusCode );
120
86
$ response ->expects ($ this ->any ())
121
87
->method ('getBody ' )
122
- ->will ( $ this -> returnValue ( $ body) );
88
+ ->willReturn ( $ body );
123
89
124
90
return $ response ;
125
91
}
126
92
127
93
protected function validateRequest ($ method , $ uri , array $ urlParameters , $ body = null , $ response = null )
128
94
{
129
- $ request = $ this ->createRequestMock ($ response );
95
+ $ request = $ this ->getMockBuilder ('\Guzzle\Http\Message\RequestInterface ' )->getMock ();
96
+
97
+ if (null !== $ response ) {
98
+ $ request ->expects ($ this ->any ())
99
+ ->method ('send ' )
100
+ ->willReturn ($ response );
101
+ }
102
+
130
103
$ this
131
104
->requestHandler
132
105
->expects ($ this ->once ())
133
106
->method ('createRequest ' )
134
107
->with ($ method , $ uri , $ urlParameters , $ body )
135
- ->will ( $ this -> returnValue ( $ request) );
108
+ ->willReturn ( $ request );
136
109
137
110
return $ request ;
138
111
}
@@ -149,20 +122,24 @@ protected function validateRetrieveApiCall($method, $uri, array $urlParameters,
149
122
->expects ($ this ->once ())
150
123
->method ('executeRequest ' )
151
124
->with ($ request )
152
- ->will ( $ this -> throwException ( new NotFoundException ('Not found ' ) ));
125
+ ->willThrowException ( new NotFoundException ('Not found ' ));
153
126
} else {
154
127
$ this
155
128
->requestHandler
156
129
->expects ($ this ->once ())
157
130
->method ('executeRequest ' )
158
131
->with ($ request )
159
- ->will ( $ this -> returnValue ( $ response) );
132
+ ->willReturn ( $ response );
160
133
}
161
134
162
135
$ this ->validateSerializer ($ serializerMap );
163
136
164
137
if ($ statusCode < 400 ) {
165
- $ this ->validateDeserializer ($ rawResponse , $ type , $ transformedResult );
138
+ $ this ->serializer
139
+ ->expects ($ this ->once ())
140
+ ->method ('deserialize ' )
141
+ ->with ($ rawResponse , 'Xabbuh\XApi\Model \\' .$ type , 'json ' )
142
+ ->willReturn ($ transformedResult );
166
143
}
167
144
}
168
145
@@ -176,15 +153,8 @@ protected function validateStoreApiCall($method, $uri, array $urlParameters, $st
176
153
->expects ($ this ->once ())
177
154
->method ('executeRequest ' )
178
155
->with ($ request , array ($ statusCode ))
179
- ->will ( $ this -> returnValue ( $ response) );
156
+ ->willReturn ( $ response );
180
157
$ serializerMap [] = array ('data ' => $ object , 'result ' => $ rawRequest );
181
158
$ this ->validateSerializer ($ serializerMap );
182
159
}
183
-
184
- protected function validateDeleteDocumentCall ($ uri , array $ urlParameters , array $ serializerMap = array ())
185
- {
186
- $ response = $ this ->createResponseMock (204 , '' );
187
- $ this ->validateRequest ('delete ' , $ uri , $ urlParameters , '' , $ response );
188
- $ this ->validateSerializer ($ serializerMap );
189
- }
190
160
}
0 commit comments