|
1 | 1 | <?php
|
2 | 2 | namespace SparkPost\Test;
|
3 |
| - |
4 | 3 | use SparkPost\APIResource;
|
5 |
| -use SparkPost\SparkPost; |
6 |
| -use Guzzle\Plugin\Mock\MockPlugin; |
7 |
| -use Guzzle\Http\Message\Response; |
8 |
| - |
| 4 | +use SparkPost\Test\TestUtils\ClassUtils; |
| 5 | +use Ivory\HttpAdapter\CurlHttpAdapter; |
9 | 6 |
|
10 | 7 | class APIResourceTest extends \PHPUnit_Framework_TestCase {
|
11 | 8 |
|
12 |
| - private $client = null; |
| 9 | + private static $utils; |
| 10 | + private $adapterMock; |
| 11 | + private $resource; |
13 | 12 |
|
14 |
| - /** |
15 |
| - * Allows access to private methods |
16 |
| - * |
17 |
| - * This is needed to mock the GuzzleHttp\Client responses |
18 |
| - * |
19 |
| - * @param string $name |
20 |
| - * @return ReflectionMethod |
21 |
| - */ |
22 |
| - private static function getMethod($name) { |
23 |
| - $class = new \ReflectionClass('\SparkPost\APIResource'); |
24 |
| - $method = $class->getMethod($name); |
25 |
| - $method->setAccessible(true); |
26 |
| - return $method; |
27 |
| - } |
| 13 | + public static function setUpBeforeClass() { |
| 14 | + |
| 15 | + } |
28 | 16 |
|
29 | 17 | /**
|
30 | 18 | * (non-PHPdoc)
|
31 | 19 | * @before
|
32 | 20 | * @see PHPUnit_Framework_TestCase::setUp()
|
33 | 21 | */
|
34 | 22 | public function setUp() {
|
35 |
| - SparkPost::getHttpHeaders = function ($headers) {return ['what'=>'what']}; |
36 |
| - APIResource::$endpoint = 'someValidEndpoint'; // when using APIResource directly an endpoint needs to be set. |
37 |
| - } |
38 |
| - // |
39 |
| - // /** |
40 |
| - // * @desc Ensures that the configuration class is not instantiable. |
41 |
| - // */ |
42 |
| - // public function testConstructorCannotBeCalled() { |
43 |
| - // $class = new \ReflectionClass('\SparkPost\Transmission'); |
44 |
| - // $this->assertFalse($class->isInstantiable()); |
45 |
| - // } |
46 |
| - // |
47 |
| - // /** |
48 |
| - // * @desc tests happy path |
49 |
| - // */ |
50 |
| - // public function testFetchWithGoodResponse() { |
51 |
| - // $mock = new MockPlugin(); |
52 |
| - // $mock->addResponse(new Response(200, array(), '{"results":[{"test":"This is a test"}, {"test":"two"}]}')); |
53 |
| - // $this->client->addSubscriber($mock); |
54 |
| - // $this->assertEquals(array("results"=>array(array('test'=>'This is a test'), array('test'=>'two'))), APIResource::fetchResource()); |
55 |
| - // } |
56 |
| - // |
57 |
| - // /** |
58 |
| - // * @desc tests happy path |
59 |
| - // */ |
60 |
| - // public function testDeleteWithGoodResponse() { |
61 |
| - // $mock = new MockPlugin(); |
62 |
| - // $mock->addResponse(new Response(200, array(), '{"results":[{"test":"This is a test"}]}')); |
63 |
| - // $this->client->addSubscriber($mock); |
64 |
| - // |
65 |
| - // $this->assertEquals(array("results"=>array(array('test'=>'This is a test'))), APIResource::deleteResource('someId')); |
66 |
| - // } |
67 |
| - // |
68 |
| - // /** |
69 |
| - // * @desc tests 404 bad response |
70 |
| - // * @expectedException Exception |
71 |
| - // * @expectedExceptionMessage The specified resource does not exist |
72 |
| - // */ |
73 |
| - // public function testFetchWith404Response() { |
74 |
| - // $mock = new MockPlugin(); |
75 |
| - // $mock->addResponse(new Response(404, array())); |
76 |
| - // $this->client->addSubscriber($mock); |
77 |
| - // APIResource::fetchResource('someId'); |
78 |
| - // } |
79 |
| - // |
80 |
| - // /** |
81 |
| - // * @desc tests unknown bad response |
82 |
| - // * @expectedException Exception |
83 |
| - // * @expectedExceptionMessage Received bad response from SomeValidEndpoint API: 400 |
84 |
| - // */ |
85 |
| - // public function testFetchWithOtherBadResponse() { |
86 |
| - // $mock = new MockPlugin(); |
87 |
| - // $mock->addResponse(new Response(400, array())); |
88 |
| - // $this->client->addSubscriber($mock); |
89 |
| - // APIResource::fetchResource('someId'); |
90 |
| - // } |
91 |
| - // |
92 |
| - // /** |
93 |
| - // * @desc tests bad response |
94 |
| - // * @expectedException Exception |
95 |
| - // * @expectedExceptionMessageRegExp /Unable to contact SomeValidEndpoint API:.* / |
96 |
| - // */ |
97 |
| - // public function testFetchForCatchAllException() { |
98 |
| - // $mock = new MockPlugin(); |
99 |
| - // $mock->addResponse(new Response(500)); |
100 |
| - // $this->client->addSubscriber($mock); |
101 |
| - // APIResource::fetchResource('someId'); |
102 |
| - // } |
103 |
| - // |
104 |
| - // /** |
105 |
| - // * @desc tests happy path |
106 |
| - // */ |
107 |
| - // public function testSuccessfulSend() { |
108 |
| - // $body = array("result"=>array("transmission_id"=>"11668787484950529"), "status"=>array("message"=> "ok","code"=> "1000")); |
109 |
| - // $mock = new MockPlugin(); |
110 |
| - // $mock->addResponse(new Response(200, array(), json_encode($body))); |
111 |
| - // $this->client->addSubscriber($mock); |
112 |
| - // |
113 |
| - // |
114 |
| - // $this->assertEquals($body, APIResource::sendRequest(array('text'=>'awesome email'))); |
115 |
| - // } |
116 |
| - // |
117 |
| - // /** |
118 |
| - // * @desc tests bad response |
119 |
| - // * @expectedException Exception |
120 |
| - // * @expectedExceptionMessage ["This is a fake error"] |
121 |
| - // */ |
122 |
| - // public function testSendFor400Exception() { |
123 |
| - // $body = array('errors'=>array('This is a fake error')); |
124 |
| - // $mock = new MockPlugin(); |
125 |
| - // $mock->addResponse(new Response(400, array(), json_encode($body))); |
126 |
| - // $this->client->addSubscriber($mock); |
127 |
| - // APIResource::sendRequest(array('text'=>'awesome email')); |
128 |
| - // } |
129 |
| - // |
130 |
| - // |
131 |
| - // /** |
132 |
| - // * @desc tests bad response |
133 |
| - // * @expectedException Exception |
134 |
| - // * @expectedExceptionMessageRegExp /Unable to contact SomeValidEndpoint API:.* / |
135 |
| - // */ |
136 |
| - // public function testSendForCatchAllException() { |
137 |
| - // $mock = new MockPlugin(); |
138 |
| - // $mock->addResponse(new Response(500)); |
139 |
| - // $this->client->addSubscriber($mock); |
140 |
| - // APIResource::sendRequest(array('text'=>'awesome email')); |
141 |
| - // } |
| 23 | + $this->adapterMock = $this->getMockBuilder('CurlHttpAdapter')->getMock(); |
| 24 | + |
| 25 | + $this->resource = new APIResource(new CurlHttpAdapter(), ['key'=>'a key']); |
| 26 | + self::$utils = new ClassUtils($this->resource); |
| 27 | + } |
| 28 | + |
| 29 | + public function testConstructorSetsUpAdapterAndConfig() { |
| 30 | + $this->assertEquals('Ivory\HttpAdapter\CurlHttpAdapter', get_class(self::$utils->getProperty($this->resource, 'httpAdapter'))); |
| 31 | + } |
142 | 32 |
|
143 | 33 | }
|
0 commit comments