Skip to content

Commit 75f1cc1

Browse files
committed
✅ Adding response trait:
- current method of referencing mock responses relative to the test adds duplicate code - using a trait means all the responses can live in the same directory - this should also work well with an API spec that included example requests / responses
1 parent 0bb7c81 commit 75f1cc1

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

test/Fixture/ResponseTrait.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
/**
3+
* Nexmo Client Library for PHP
4+
*
5+
* @copyright Copyright (c) 2017 Nexmo, Inc. (http://nexmo.com)
6+
* @license https://github.com/Nexmo/nexmo-php/blob/master/LICENSE.txt MIT License
7+
*/
8+
9+
namespace NexmoTest\Fixture;
10+
11+
use Zend\Diactoros\Response;
12+
13+
/**
14+
* Creates mock response objects.
15+
* TODO: use example responses from API spec
16+
*/
17+
trait ResponseTrait
18+
{
19+
protected function getResponse($type, $status = 200)
20+
{
21+
if(is_array($type)){
22+
$type = implode('/', $type);
23+
}
24+
25+
return new Response(fopen(__DIR__ . '/../responses/' . $type . '.json', 'r'), $status);
26+
}
27+
28+
protected function getResponseBody($type)
29+
{
30+
$response = $this->getResponse($type);
31+
return $response->getBody()->getContents();
32+
}
33+
34+
protected function getResponseData($type)
35+
{
36+
$body = $this->getResponseBody($type);
37+
return json_decode($body, true);
38+
}
39+
}

test/responses/calls/event.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"message": "Stream stopped",
3+
"uuid": "5dd627ff-caff-46a8-99ed-891e5ffebc55"
4+
}

0 commit comments

Comments
 (0)