Last active
March 5, 2020 14:57
-
-
Save bigfoot90/d4f146bacad359329d219a804f6cd12a to your computer and use it in GitHub Desktop.
PHPacto Symfony integration test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Bigfoot\PHPacto\PHPacto; | |
use Bigfoot\PHPacto\Guzzle\ProviderMock; | |
use Bigfoot\PHPacto\Guzzle\ProviderMockGuzzle6; | |
use Bigfoot\PHPacto\Loader\PactLoader; | |
use GuzzleHttp\Client as GuzzleClient; | |
use PHPUnit\Framework\TestCase; | |
class Consumer | |
{ | |
/** | |
* @var GuzzleClient | |
*/ | |
private $client; | |
/** | |
* @param GuzzleClient $client | |
*/ | |
public function __construct(GuzzleClient $client) | |
{ | |
$this->client = $client; | |
} | |
public function callProvider() | |
{ | |
// Do your Guzzle call | |
// $this->client->request(...); | |
} | |
} | |
class PHPactoConsumerTest extends TestCase | |
{ | |
use PHPactoTestTrait; | |
/** | |
* @var GuzzleClient | |
*/ | |
protected $client; | |
/** | |
* @var ProviderMock | |
*/ | |
protected $provider; | |
/** | |
* @var PHPacto | |
*/ | |
protected $phpacto; | |
public function setUp() | |
{ | |
$this->provider = new ProviderMockGuzzle6(); | |
$this->client = new GuzzleClient(['handler' => $this->provider->getHandler()]); | |
$this->phpacto = new PHPacto(__DIR__); | |
} | |
public function testApi() | |
{ | |
$pact = $this->phpacto->getPact('contract.yaml'); | |
// This will call PHPactoTestTrait::assertRequestMatchesPact() | |
// when Consumer makes a Guzzle request | |
$this->provider->handlePact($pact); | |
$consumer = new Consumer($this->client); | |
$consumer->callProvider(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Bigfoot\PHPacto\PHPacto; | |
use Bigfoot\PHPacto\PactInterface; | |
use Bigfoot\PHPacto\Test\PHPactoTestTrait; | |
use Psr\Http\Message\ResponseInterface; | |
use Symfony\Bridge\PsrHttpMessage\Factory\DiactorosFactory; | |
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; | |
use Symfony\Component\BrowserKit\Client; | |
class PHPactoWebTest extends WebTestCase | |
{ | |
use PHPactoTestTrait; | |
/** | |
* @var Client | |
*/ | |
protected $client; | |
/** | |
* @var PHPacto | |
*/ | |
protected $phpacto; | |
public function setUp() | |
{ | |
$this->client = $this->createClient(); | |
$this->phpacto = new PHPacto(__DIR__); | |
} | |
public function consumerMakesRequest(PactInterface $pact): ResponseInterface | |
{ | |
$psr7Request = $this->getLoadedPact()->getRequest()->getSample(); | |
$method = $psr7Request->getMethod(); | |
$uri = (string) $psr7Request->getUri(); | |
$parameters = $psr7Request->getParsedBody() ?? []; | |
$headers = $psr7Request->getHeaders() ?? []; | |
$body = $psr7Request->getBody(); | |
$body->rewind(); | |
$files = []; | |
$this->client->request($method, $uri, $parameters, $files, $headers, $body); | |
$sfResponse = $this->client->getResponse(); | |
$psr7Factory = new DiactorosFactory(); | |
$psr7Response = $psr7Factory->createResponse($sfResponse); | |
return $psr7Response; | |
} | |
public function testApi() | |
{ | |
$pact = $this->phpacto->getPact('contract.yaml'); | |
$response = $this->consumerMakesRequest($pact); | |
self::assertPactMatchesResponse($pact, $response); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment