-
Notifications
You must be signed in to change notification settings - Fork 2
/
ExampleOneMeetupAPIClientTest.php
80 lines (61 loc) · 2.44 KB
/
ExampleOneMeetupAPIClientTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
<?php
require_once ('../src/ExampleOneMeetupApiClient.php');
use PhpPact\Consumer\InteractionBuilder;
use PhpPact\Consumer\Model\ConsumerRequest;
use PhpPact\Consumer\Model\ProviderResponse;
use PhpPact\Consumer\Matcher\Matcher;
use PhpPact\Standalone\MockService\MockServerEnvConfig;
use PHPUnit\Framework\TestCase;
class ExampleOneMeetupAPIClientTest extends TestCase
{
const version = '2';
/**
* @test
*/
public function testCategories()
{
// build the request
$path = '/' . self::version . '/categories';
// build the request
$request = new ConsumerRequest();
$request
->setMethod('GET')
->setPath($path)
->addHeader('Content-Type', 'application/json');
// build the response
$matcher = new Matcher();
$category1 = new \stdClass();
$category1->name = $matcher->regex('Games','[gbBG]');
$category1->sort_name = 'Games';
$category1->id = 11;
$category1->shortname = 'Games';
$body = new \stdClass();
$body->results= $matcher->eachLike($category1);
$response = new ProviderResponse();
$response
->setStatus(200)
->addHeader('Content-Type', 'application/json')
->setBody($body);
// build up the expected results and appropriate responses
$config = new MockServerEnvConfig();
$mockService = new InteractionBuilder($config);
$mockService->given("General Meetup Categories")
->uponReceiving("A GET request to return JSON using Meetups category api under version 2")
->with($request)
->willRespondWith($response);
$service = new \ExampleOneMeetupApiClient($config->getBaseUri()); // Pass in the URL to the Mock Server.
$serviceResponse = $service->categories();
// do some asserts on the return
$this->assertEquals('200', $serviceResponse->getStatusCode(), "Let's make sure we have an OK response");
// do something with the body returned
$body = (string) $serviceResponse->getBody();
$this->assertTrue((json_decode($body) ? true : false), "Expect the JSON to be decoded without error");
$hasException = false;
try {
$mockService->verify();
} catch(\Exception $e) {
$hasException = true;
}
$this->assertFalse($hasException, "We expect the pacts to validate");
}
}