function it_should_set_provider_state(Interaction $interaction) { $this->interactionFactory->create("An alligator named Mary exists", "A request for an alligator", ["method" => "get", "path" => "/alligators/Mary", "query" => ["name" => "fred"], "headers" => ["Accept" => "application/json"], "body" => ["param" => 1]], ["status" => 200, "headers" => ["Content-Type" => "application/json"], "body" => ["name" => "Mary", "children" => Pact::eachLike(["name" => "Fred", "age" => 2])]])->willReturn($interaction); $consumerPactBuilder = $this->given("An alligator named Mary exists")->uponReceiving("A request for an alligator")->with(["method" => "get", "path" => "/alligators/Mary", "query" => ["name" => "fred"], "headers" => ["Accept" => "application/json"], "body" => ["param" => 1]])->willRespondWith(["status" => 200, "headers" => ["Content-Type" => "application/json"], "body" => ["name" => "Mary", "children" => Pact::eachLike(["name" => "Fred", "age" => 2])]]); $interaction = $consumerPactBuilder->interactionFromBuild(); $interaction->shouldHaveType(Interaction::class); }
function it_should_return_correct_array_for_nested_matchers() { $description = new Description('A request for foo'); $providerState = new ProviderState('foo exists'); $request = $this->createRequest(Method::GET, '/client', [], ["accept" => "application/json"]); $response = $this->createResponse(StatusCode::OK_CODE, ["match" => Pact::eachLike(Pact::eachLike(["size" => Pact::like(10), "colour" => Pact::term("red", "red|green|blue"), "tag" => Pact::eachLike([Pact::like("jumper"), Pact::like("shirt")], 2)]))], ["Content-Type" => "application/json"]); $this->beConstructedWith($providerState, $description, $request, $response); \PHPUnit_Framework_Assert::assertEquals(json_encode(["description" => 'A request for foo', "provider_state" => 'foo exists', "request" => ["method" => "get", "path" => "/client", "headers" => ["accept" => "application/json"]], "response" => ["status" => 200, "headers" => ["Content-Type" => "application/json"], "body" => ["match" => ["json_class" => "Pact::ArrayLike", "contents" => ["json_class" => "Pact::ArrayLike", "contents" => ["size" => ["json_class" => "Pact::SomethingLike", "contents" => 10], "colour" => ["json_class" => "Pact::Term", "data" => ["generate" => "red", "matcher" => ["json_class" => "Regexp", "o" => 0, "s" => "red|green|blue"]]], "tag" => ["json_class" => "Pact::ArrayLike", "contents" => [["json_class" => "Pact::SomethingLike", "contents" => "jumper"], ["json_class" => "Pact::SomethingLike", "contents" => "shirt"]], "min" => 2]], "min" => 1], "min" => 1]]]]), json_encode($this->jsonSerialize()->getWrappedObject())); }
<?php require __DIR__ . '/../vendor/autoload.php'; $host = "localhost:1234"; $client = new \Http\Adapter\Guzzle6\Client(); $httpServiceCollaborator = new \Madkom\PactClient\Http\HttpMockServiceCollaborator($client, $host, "consumer_service_c", "provider_service_d"); $consumerPactBuilder = new \Madkom\PactClient\Application\ConsumerPactBuilder(new \Madkom\PactClient\Domain\Interaction\InteractionFactory()); $interaction = $consumerPactBuilder->given("user exists")->uponReceiving('A request for user')->with(["method" => "get", "path" => "/user", "query" => ["id" => "2"], "headers" => ["Accept" => "application/json"], "data" => ["type" => "admin"]])->willRespondWith(["status" => 200, "headers" => ["Content-Type" => "application/json"], "body" => ["foo" => \Madkom\PactClient\Application\Pact::eachLike(["name" => \Madkom\PactClient\Application\Pact::like('Edward'), "favColor" => \Madkom\PactClient\Application\Pact::term("red", "red|green|yellow")])]])->interactionFromBuild(); $httpServiceCollaborator->setupInteraction($interaction); // Fake request from your application to provider $request = new \GuzzleHttp\Psr7\Request("GET", $host . "/user?id=2", ["Accept" => "application/json"], json_encode(["type" => "admin"])); $client->sendRequest($request); // Verifies if your pact you made was fulfilled by consumer (was this request ever send to mock_service) $httpServiceCollaborator->verify(); $httpServiceCollaborator->finishProviderVerificationProcess();
function it_should_next_next_multiple_matchers_correctly() { $this->beConstructedWith(["size" => Pact::like(10), "colour" => Pact::term("red", "red|green|blue"), "tag" => new EachLike([Pact::like("jumper"), Pact::like("shirt")], 2)], 1); \PHPUnit_Framework_Assert::assertEquals(json_encode(["json_class" => "Pact::ArrayLike", "contents" => ["size" => ["json_class" => "Pact::SomethingLike", "contents" => 10], "colour" => ["json_class" => "Pact::Term", "data" => ["generate" => "red", "matcher" => ["json_class" => "Regexp", "o" => 0, "s" => "red|green|blue"]]], "tag" => ["json_class" => "Pact::ArrayLike", "contents" => [["json_class" => "Pact::SomethingLike", "contents" => "jumper"], ["json_class" => "Pact::SomethingLike", "contents" => "shirt"]], "min" => 2]], "min" => 1]), json_encode($this->jsonSerialize()->getWrappedObject())); }
function it_should_throw_exception_if_no_status_code_passed() { $this->shouldThrow(PactException::class)->during('create', ["provider info", "request info", ["method" => "get", "path" => "/client", "query" => ["name" => "fred"], "headers" => ["Accept" => "application/json"], "body" => ["param" => 1]], ["headers" => ["Content-Type" => "application/json"], "body" => ["name" => "Mary", "children" => Pact::eachLike(["name" => "Fred", "age" => 2])]]]); }