Skip to content

Commit

Permalink
adjusting the TestSuite to the changed library
Browse files Browse the repository at this point in the history
  • Loading branch information
lluz committed Jul 19, 2017
1 parent 2476d67 commit 36a7eff
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 68 deletions.
116 changes: 49 additions & 67 deletions test/GuzzleHttpMockTest/MockTest.php
Original file line number Diff line number Diff line change
@@ -1,57 +1,46 @@
<?php


namespace Aeris\GuzzleHttpMockTest;


use GuzzleHttp\Client as GuzzleClient;
use GuzzleHttp\Message\MessageFactory;
use GuzzleHttp\Message\Response;
use GuzzleHttp\Message\ResponseInterface;
use GuzzleHttp\Stream\Stream;
use Aeris\GuzzleHttpMock\Mock as GuzzleHttpMock;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Response;
use Aeris\GuzzleHttpMock\Mock;
use Aeris\GuzzleHttpMock\Expect;

class MockTest extends \PHPUnit_Framework_TestCase {

/** @var GuzzleClient */
protected $guzzleClient;

/** @var GuzzleHttpMock */
/** @var Mock */
protected $httpMock;

/** @var MessageFactory */
protected $messageFactory;

public function setUp() {
$this->messageFactory = new MessageFactory();
$this->guzzleClient = new GuzzleClient();
$this->httpMock = new GuzzleHttpMock();
$this->httpMock = new Mock();

$this->httpMock->attachToClient($this->guzzleClient);
$this->guzzleClient = new GuzzleClient(['handler' => $this->httpMock->getHandlerStackWithMiddleware()]);
}

/** @test */
public function shouldReturnAResponseForARequestObject() {
$mockResponse = $this->createResponse(
$mockResponse =
new Response(200,
['Content-Type' => 'application/json'],
Stream::factory(json_encode([
json_encode([
'hello' => 'world',
'howareyou' => 'today'
]))
));
])
);

$request = $this->guzzleClient->createRequest(
$request = new Request(
'PUT',
'http://example.com/foo',
[
'query' => ['faz' => 'baz'],
'body' => json_encode(['shakeyo' => 'body']),
'headers' => [
'Content-Type' => 'application/json'
]
]
'http://example.com/foo?faz=baz',
[
'Content-Type' => 'application/json'
],
json_encode(['shakeyo' => 'body'])
);

$this->httpMock
Expand All @@ -73,13 +62,13 @@ public function shouldReturnAResponseForARequestObject() {

/** @test */
public function shouldReturnAResponseForARequestWithConfiguration() {
$mockResponse = $this->createResponse(new Response(200,
$mockResponse = new Response(200,
['Content-Type' => 'application/json'],
Stream::factory(json_encode([
json_encode([
'hello' => 'world',
'howareyou' => 'today'
]))
));
])
);

$this->httpMock
->shouldReceiveRequest()
Expand All @@ -97,7 +86,7 @@ public function shouldReturnAResponseForARequestWithConfiguration() {
$actualResponse = $this->guzzleClient
->put('http://example.com/foo', [
'query' => ['faz' => 'baz'],
'body' => ['shakeyo' => 'body'],
'form_params' => ['shakeyo' => 'body'],
]);

$this->httpMock->verify();
Expand All @@ -106,13 +95,13 @@ public function shouldReturnAResponseForARequestWithConfiguration() {

/** @test */
public function shouldReturnAResponseForAJsonBodyParamsExpectation() {
$mockResponse = $this->createResponse(new Response(200,
$mockResponse = new Response(200,
['Content-Type' => 'application/json'],
Stream::factory(json_encode([
json_encode([
'hello' => 'world',
'howareyou' => 'today'
]))
));
])
);

$this->httpMock
->shouldReceiveRequest()
Expand Down Expand Up @@ -140,13 +129,13 @@ public function shouldReturnAResponseForAJsonBodyParamsExpectation() {

/** @test */
public function shouldRespondToMultipleRequestsWithTheSameResponse() {
$mockResponse = $this->createResponse(new Response(200,
$mockResponse = new Response(200,
['Content-Type' => 'application/json'],
Stream::factory(json_encode([
json_encode([
'hello' => 'world',
'howareyou' => 'today'
]))
));
])
);

$this->httpMock
->shouldReceiveRequest()
Expand Down Expand Up @@ -178,14 +167,14 @@ public function shouldRespondToMultipleRequestsWithTheSameResponse() {
$actualResponse = $this->guzzleClient
->put('http://example.com/foo', [
'query' => ['faz' => 'baz'],
'body' => json_encode(['shakeyo' => 'body']),
'json' => ['shakeyo' => 'body'],
'headers' => ['Content-Type' => 'application/json']
]);

$actualResponse2 = $this->guzzleClient
->put('http://example.com/foo', [
'query' => ['faz' => 'baz'],
'body' => json_encode(['shakeyo' => 'hands in the air like you just don\'t care']),
'json' => ['shakeyo' => 'hands in the air like you just don\'t care'],
'headers' => ['Content-Type' => 'application/json']
]);

Expand Down Expand Up @@ -228,7 +217,7 @@ public function shouldRespondWithJson() {
$this->assertEquals([
'foo' => 'bar',
'faz' => ['baz', 'shnaz'],
], $response->json());
], json_decode((string)$response->getBody(), true));
}

/** @test */
Expand All @@ -249,7 +238,7 @@ public function shouldRespondWithJsonAndStatusCode() {
$this->assertEquals([
'foo' => 'bar',
'faz' => ['baz', 'shnaz'],
], $response->json());
], json_decode((string)$response->getBody(), true));
$this->assertEquals(234, $response->getStatusCode());
}

Expand All @@ -275,8 +264,8 @@ public function shouldCheckMultipleExpectations() {

$this->httpMock->verify();

$this->assertEquals(['foo' => 'bar'], $responseA->json());
$this->assertEquals(['shazaam' => 'kabloom'], $responseB->json());
$this->assertEquals(['foo' => 'bar'], json_decode((string)$responseA->getBody(), true));
$this->assertEquals(['shazaam' => 'kabloom'], json_decode((string)$responseB->getBody(), true));
}

/** @test */
Expand Down Expand Up @@ -307,9 +296,10 @@ public function shouldUseTheNextAvailableExpectationIfTheFirstIsUsedUp() {
$responseC = $this->guzzleClient
->get('http://www.example.com/users');

$this->assertEquals(['foo' => 'bar'], $responseA->json());
$this->assertEquals(['foo' => 'bar'], $responseB->json());
$this->assertEquals(['shazaam' => 'bologna'], $responseC->json());
$this->assertEquals(['foo' => 'bar'], json_decode((string)$responseA->getBody(), true));
$this->assertEquals(['foo' => 'bar'], json_decode((string)$responseB->getBody(), true));
$this->assertEquals(['shazaam' => 'bologna'], json_decode((string)$responseC->getBody(), true));


$this->httpMock->verify();
}
Expand Down Expand Up @@ -490,7 +480,7 @@ public function bodyParams_notMatch_shouldFail() {

$this->guzzleClient
->get('http://www.example.com/foo', [
'body' => ['not' => 'what I expected']
'form_params' => ['not' => 'what I expected']
]);

$this->httpMock->verify();
Expand All @@ -506,7 +496,7 @@ public function bodyParams_match_shouldPass() {

$this->guzzleClient
->get('http://www.example.com/foo', [
'body' => ['foo' => 'bar']
'form_params' => ['foo' => 'bar']
]);

$this->httpMock->verify();
Expand All @@ -524,7 +514,7 @@ public function body_customLogic_match_shouldPass() {

$this->guzzleClient
->get('http://www.example.com/foo', [
'body' => ['foo' => 'bar']
'form_params' => ['foo' => 'bar']
]);

$this->httpMock->verify();
Expand All @@ -545,7 +535,7 @@ public function body_customLogic_notMatch_shouldFail() {

$this->guzzleClient
->get('http://www.example.com/foo', [
'body' => ['foo' => 'shablooey']
'form_params' => ['foo' => 'shablooey']
]);

$this->httpMock->verify();
Expand All @@ -560,7 +550,7 @@ public function body_anyParams_shouldPass() {

$this->guzzleClient
->get('http://www.example.com/foo', [
'body' => ['foo' => 'bar']
'form_params' => ['foo' => 'bar']
]);

$this->httpMock->verify();
Expand All @@ -576,7 +566,7 @@ public function body_arrayContains_match_shouldPass() {

$this->guzzleClient
->get('http://www.example.com/foo', [
'body' => ['foo' => 'bar', 'faz' => 'baz']
'form_params' => ['foo' => 'bar', 'faz' => 'baz']
]);

$this->httpMock->verify();
Expand All @@ -595,7 +585,7 @@ public function body_arrayContains_notMatch_shouldFail() {

$this->guzzleClient
->get('http://www.example.com/foo', [
'body' => ['foo' => 'shablooey', 'faz' => 'baz']
'form_params' => ['foo' => 'shablooey', 'faz' => 'baz']
]);

$this->httpMock->verify();
Expand All @@ -614,7 +604,7 @@ public function body_outOfOrder_match_shouldPass() {

$this->guzzleClient
->get('http://www.example.com/foo', [
'body' => [
'form_params' => [
'foo' => 'bar',
'faz' => 'baz',
]
Expand All @@ -638,7 +628,7 @@ public function body_outOfOrder_nullValues_match_shouldPass() {

$this->guzzleClient
->get('http://www.example.com/foo', [
'body' => [
'json' => [
'faz' => 'baz',
'foo' => 'bar',
'nullB' => null,
Expand Down Expand Up @@ -676,7 +666,7 @@ public function jsonBody_notMatch_shouldFail() {
public function shouldFailIfNoRequestIsConfigured() {
$this->guzzleClient
->get('http://www.example.com/shazlooey', [
'body' => ['not' => 'what I expected']
'form_params' => ['not' => 'what I expected']
]);

$this->httpMock->verify();
Expand Down Expand Up @@ -807,12 +797,4 @@ public function shouldPassIfTheRequestIsMadeTheNumberOfSetTimes() {
$this->httpMock->verify();
}

/**
* @param ResponseInterface|string $response
* @return \GuzzleHttp\Message\RequestInterface|ResponseInterface
*/
protected function createResponse($response) {
return $this->messageFactory->fromMessage($response);
}

}
2 changes: 1 addition & 1 deletion test/phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
syntaxCheck="false">
<testsuites>
<testsuite name="GuzzleHttpMockTest">
<directory>../GuzzleHttpMockTest</directory>
<directory>./GuzzleHttpMockTest</directory>
</testsuite>
</testsuites>
</phpunit>

0 comments on commit 36a7eff

Please sign in to comment.