/
TestCase.php
77 lines (71 loc) · 2.32 KB
/
TestCase.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
<?php
/**
* PEAR2\Services\NYTimes\TestCase
*
* PHP version 5
*
* @category Services
* @package PEAR2_Services_NYTimes
* @author Till Klampaeckel <till@php.net>
* @copyright 2011 Till Klampaeckel
* @license http://www.opensource.org/licenses/bsd-license.php New BSD License
* @version SVN: $Id$
* @link https://github.com/pear2/Services_NYTimes
*/
/**
* An abstract TestCase class for test cases.
*
* @category Services
* @package PEAR2_Services_NYTimes
* @author Till Klampaeckel <till@php.net>
* @copyright 2011 Till Klampaeckel
* @license http://www.opensource.org/licenses/bsd-license.php New BSD License
* @link https://github.com/pear2/Services_NYTimes
*/
namespace PEAR2\Services\NYTimes;
abstract class TestCase extends \PHPUnit_Framework_TestCase
{
/**
* Helper function to setup the object to be injected into the mock.
*
* @param string $apiName The name of the API.
* @param string $apiVersion The version of the API.
* @param string $fixture The name of the fixture file.
*
* @return \HTTP_Request2_Response
*/
protected function setUpResponseObject($apiName, $apiVersion, $fixture)
{
$data = include __DIR__ . "/../../fixtures/{$apiName}/{$apiVersion}/{$fixture}";
$response = new \HTTP_Request2_Response(
$data['statusLine'],
true,
$data['effectiveUrl']
);
$response->appendBody($data['body']);
return $response;
}
/**
* Create a mock object.
*
* @param string $apiName The name of the API, e.g.
'newswire'.
* @param \HTTP_Request2_Response $responseObject A response object to inject.
*
* @return mixed
* @see self::setUpResponseObject()
*/
protected function getApiMocked($apiName, \HTTP_Request2_Response $responseObject)
{
$className = ucfirst(strtolower($apiName));
$mockedClass = $this->getMock(
"PEAR2\Services\NYTimes\\" . $className,
array('makeRequest',),
array('fooBar',) // fake api key
);
$mockedClass->expects($this->once())
->method('makeRequest')
->will($this->returnValue($responseObject));
return $mockedClass;
}
}