22
33namespace Matomo \ReportingApi \tests ;
44
5+ use Exception ;
56use GuzzleHttp \ClientInterface ;
67use GuzzleHttp \Psr7 \Response ;
7- use PHPUnit \ Framework \ TestCase ;
8+ use InvalidArgumentException ;
89use Matomo \ReportingApi \HttpClient ;
910use Matomo \ReportingApi \RequestFactoryInterface ;
10- use Prophecy \Argument ;
11+ use PHPUnit \Framework \TestCase ;
12+ use Prophecy \PhpUnit \ProphecyTrait ;
1113use Psr \Http \Message \RequestInterface ;
1214use Psr \Http \Message \ResponseInterface ;
1315
1921class HttpClientTest extends TestCase
2022{
2123
24+ use ProphecyTrait;
25+
2226 /**
2327 * @param array $parameters
2428 *
@@ -111,11 +115,12 @@ public function testSetUrl($url)
111115 * @param string $invalid_url
112116 * An invalid URL.
113117 *
114- * @expectedException \InvalidArgumentException
118+ *
115119 * @dataProvider invalidUrlProvider
116120 */
117121 public function testInvalidUrl ($ invalid_url )
118122 {
123+ $ this ->expectException (InvalidArgumentException::class);
119124 $ this ->getHttpClient ()->setUrl ($ invalid_url );
120125 }
121126
@@ -188,21 +193,11 @@ public function testGetMethod($method)
188193 */
189194 public function testSetMethod ($ method )
190195 {
191- // It is expected that the given HTTP method will be passed to the request factory.
192- $ request_factory = $ this ->prophesize (RequestFactoryInterface::class);
193- $ request_factory
194- ->getRequest ($ method , Argument::any ())
195- ->willReturn ($ this ->prophesize (RequestInterface::class)->reveal ())
196- ->shouldBeCalled ();
197-
198- $ http_client = $ this ->getHttpClient (null , $ request_factory ->reveal ());
199- $ http_client ->setUrl ('http://example.com ' );
200- $ result = $ http_client ->setMethod ($ method );
201- // Send the request so that we can check if the correct HTTP method is used.
202- $ http_client ->sendRequest ();
196+ $ http_client = $ this ->getHttpClient ();
197+ $ http_client ->setMethod ($ method );
203198
204- // Check that the client is returned for chaining.
205- $ this ->assertEquals ($ http_client , $ result );
199+ // Check if correct HTTP method is returned by client
200+ $ this ->assertEquals ($ http_client-> getMethod () , $ method );
206201 }
207202
208203 /**
@@ -212,11 +207,13 @@ public function testSetMethod($method)
212207 * An unsupported or invalid HTTP method.
213208 *
214209 * @covers ::setMethod
215- * @expectedException \InvalidArgumentException
210+ *
216211 * @dataProvider unsupportedHttpMethodsProvider
217212 */
218213 public function testUnsupportedHttpMethods ($ invalid_method )
219214 {
215+ $ this ->expectException (InvalidArgumentException::class);
216+
220217 $ http_client = $ this ->getHttpClient ();
221218 $ http_client ->setMethod ($ invalid_method );
222219 }
@@ -270,10 +267,11 @@ public function unsupportedHttpMethodsProvider()
270267 * Tests that an exception is thrown when sending a request without specifying a URL.
271268 *
272269 * @covers ::sendRequest
273- * @expectedException \Exception
270+ *
274271 */
275272 public function testSendRequestWithoutUrl ()
276273 {
274+ $ this ->expectException (Exception::class);
277275 $ this ->getHttpClient ()
278276 ->setRequestParameters (['foo ' => 'bar ' ])
279277 ->setMethod ('GET ' )
@@ -338,7 +336,7 @@ public function sendRequestProvider()
338336 'module ' => 'API ' ,
339337 'method ' => 'API.getMatomoVersion ' ,
340338 ],
341- ]
339+ ],
342340 ],
343341 [
344342 // The HTTP method to use.
@@ -358,7 +356,7 @@ public function sendRequestProvider()
358356 'module ' => 'API ' ,
359357 'method ' => 'API.getMatomoVersion ' ,
360358 ],
361- ]
359+ ],
362360 ],
363361 ];
364362 }
0 commit comments