Skip to content

Commit 271d2b4

Browse files
committed
started cleaning up tests to reflect the new changes
1 parent efe48cc commit 271d2b4

File tree

6 files changed

+79
-264
lines changed

6 files changed

+79
-264
lines changed

composer.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020
"autoload": {
2121
"psr-4": {
2222
"SparkPost\\": "lib/SparkPost/",
23-
"SparkPost\\SendGridCompatibility\\": "lib/SendGridCompatibility/"
23+
"SparkPost\\SendGridCompatibility\\": "lib/SendGridCompatibility/",
24+
"SparkPost\\Test\\TestUtils\\": "test/unit/TestUtils/"
2425
}
2526
}
2627
}

lib/SparkPost/APIResource.php

-22
Original file line numberDiff line numberDiff line change
@@ -110,17 +110,6 @@ public function setHttpAdapter($httpAdapter) {
110110
$this->httpAdapter->setConfiguration($this->getHttpConfig($this->config));
111111
}
112112

113-
/**
114-
* Retrieves the Http Adapter that was previously setup by the user
115-
* @throws \Exception
116-
*/
117-
public function getHttpAdapter() {
118-
if ($this->httpAdapter === null) {
119-
throw new \Exception('No Http Adapter has been provided');
120-
}
121-
return $this->httpAdapter;
122-
}
123-
124113
/**
125114
* Allows the user to pass in values to override the defaults and set their API key
126115
* @param Array $settingsConfig - Hashmap that contains config values for the SDK to connect to SparkPost
@@ -142,17 +131,6 @@ public function setConfig(Array $settingsConfig) {
142131
}
143132
}
144133

145-
/**
146-
* Retrieves the configuration that was previously setup by the user
147-
* @throws \Exception
148-
*/
149-
public function getConfig() {
150-
if ($this->config === null) {
151-
throw new \Exception('No configuration has been provided');
152-
}
153-
return $this->config;
154-
}
155-
156134
/**
157135
* @desc Private Method helper to reference parameter mappings and set the right value for the right parameter
158136
*/

test/unit/APIResourceTest.php

+17-127
Original file line numberDiff line numberDiff line change
@@ -1,143 +1,33 @@
11
<?php
22
namespace SparkPost\Test;
3-
43
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;
96

107
class APIResourceTest extends \PHPUnit_Framework_TestCase {
118

12-
private $client = null;
9+
private static $utils;
10+
private $adapterMock;
11+
private $resource;
1312

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+
}
2816

2917
/**
3018
* (non-PHPdoc)
3119
* @before
3220
* @see PHPUnit_Framework_TestCase::setUp()
3321
*/
3422
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+
}
14232

14333
}

test/unit/SparkPostTest.php

+3-113
Original file line numberDiff line numberDiff line change
@@ -6,122 +6,12 @@
66

77
class SparkPostTest extends \PHPUnit_Framework_TestCase {
88

9-
/**
10-
* Allows access to private properties in the Transmission class
11-
*
12-
* @param string $name
13-
* @param {*}
14-
* @return ReflectionMethod
15-
*/
16-
private static function setPrivateProperty($name, $value) {
17-
$class = new \ReflectionClass('\SparkPost\SparkPost');
18-
$prop = $class->getProperty($name);
19-
$prop->setAccessible(true);
20-
$prop->setValue($value);
21-
}
22-
23-
24-
public function setUp() {
25-
$this->setPrivateProperty('config', null);
26-
$this->setPrivateProperty('httpAdapter', null);
27-
}
28-
299
/**
3010
* @desc Ensures that the configuration class is not instantiable.
3111
*/
32-
public function testConstructorCannotBeCalled() {
33-
$class = new \ReflectionClass('\SparkPost\SparkPost');
34-
$this->assertFalse($class->isInstantiable());
35-
}
36-
37-
/**
38-
* @desc Tests that an exception is thrown when a library tries to recieve the config and it has not yet been set.
39-
* Since its a singleton this test must come before any setConfig tests.
40-
* @expectedException Exception
41-
* @expectedExceptionMessage No configuration has been provided
42-
*/
43-
public function testGetConfigEmptyException() {
44-
SparkPost::unsetConfig();
45-
SparkPost::getConfig();
46-
}
47-
48-
/**
49-
* @desc Tests that the api key is set when setting the config
50-
* @expectedException Exception
51-
* @expectedExceptionMessage You must provide an API key
52-
*/
53-
public function testSetConfigAPIKeyNotSetException() {
54-
SparkPost::setConfig(['something'=>'other than an API Key']);
55-
}
56-
57-
/**
58-
* @desc Tests that the api key is set when setting the config and that its not empty
59-
* @expectedException Exception
60-
* @expectedExceptionMessage You must provide an API key
61-
*/
62-
public function testSetConfigAPIKeyEmptyException() {
63-
SparkPost::setConfig(['key'=>'']);
64-
}
65-
66-
/**
67-
* @desc Tests overridable values are set while invalid values are ignored
68-
*/
69-
public function testSetConfigMultipleValuesAndGetConfig() {
70-
SparkPost::setConfig(['key'=>'lala', 'version'=>'v8', 'port'=>1024, 'someOtherValue'=>'fakeValue']);
71-
72-
$testConfig = SparkPost::getConfig();
73-
$this->assertEquals('lala', $testConfig['key']);
74-
$this->assertEquals('v8', $testConfig['version']);
75-
$this->assertEquals(1024, $testConfig['port']);
76-
$this->assertNotContains('someOtherValue', array_keys($testConfig));
77-
$this->assertEquals('https', $testConfig['protocol']);
78-
$this->assertEquals('api.sparkpost.com', $testConfig['host']);
79-
$this->assertEquals(true, $testConfig['strictSSL']);
12+
public function testConstructorSetsUpTransmissions() {
13+
$sparky = new SparkPost(new CurlHttpAdapter(), ['key'=>'a key']);
14+
$this->assertEquals('SparkPost\Transmission', get_class($sparky->transmission));
8015
}
81-
82-
/**
83-
* @desc tests getting an unset
84-
* @expectedException Exception
85-
* @expectedExceptionMessageRegExp /No Http Adapter/
86-
*/
87-
public function testGetHttpAdapterForIsset() {
88-
SparkPost::getHttpAdapter();
89-
}
90-
91-
/**
92-
* @desc tests failing validation for http adapters
93-
* @expectedException Exception
94-
* @expectedExceptionMessageRegExp /must be a valid Ivory\\HttpAdapter/
95-
*/
96-
public function testSetInvalidHttpAdapter() {
97-
SparkPost::setHttpAdapter(new \stdClass());
98-
}
99-
100-
public function testSetAndGetValidHttpAdapter() {
101-
SparkPost::setConfig(['key'=>'lala']);
102-
SparkPost::setHttpAdapter(new CurlHttpAdapter());
103-
$this->assertEquals('Ivory\HttpAdapter\CurlHttpAdapter', get_class(Sparkpost::getHttpAdapter()));
104-
}
105-
106-
public function testConfigure() {
107-
SparkPost::configure(new CurlHttpAdapter(), ['key'=>'lala']);
108-
$this->assertEquals('Ivory\HttpAdapter\CurlHttpAdapter', get_class(Sparkpost::getHttpAdapter()));
109-
}
110-
111-
public function testDefaultHeaders() {
112-
$key = 'lala';
113-
SparkPost::setConfig(['key'=>$key]);
114-
$this->assertEquals($key, Sparkpost::getHttpHeaders()['Authorization']);
115-
$this->assertEquals('application/json', Sparkpost::getHttpHeaders()['Content-Type']);
116-
}
117-
118-
public function testOverrideDefaultHeaders() {
119-
$key = 'lala';
120-
$headers=['Content-Type'=>'my/type'];
121-
SparkPost::setConfig(['key'=>$key]);
122-
$this->assertEquals($key, Sparkpost::getHttpHeaders($headers)['Authorization']);
123-
$this->assertEquals('my/type', Sparkpost::getHttpHeaders($headers)['Content-Type']);
124-
}
125-
12616
}
12717
?>

test/unit/TestUtils/ClassUtils.php

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
namespace SparkPost\Test\TestUtils;
3+
4+
5+
class ClassUtils {
6+
7+
private $class;
8+
9+
public function __construct($fqClassName) {
10+
$this->class = new \ReflectionClass($fqClassName);
11+
}
12+
13+
/**
14+
* Allows access to private methods
15+
*
16+
* This is needed to mock the GuzzleHttp\Client responses
17+
*
18+
* @param string $name
19+
* @return ReflectionMethod
20+
*/
21+
public function getMethod($method) {
22+
$method = $this->class->getMethod($name);
23+
$method->setAccessible(true);
24+
return $method;
25+
}
26+
27+
/**
28+
* Allows access to private properties in the Transmission class
29+
*
30+
* This is needed to mock the GuzzleHttp\Client responses
31+
*
32+
* @param string $name
33+
* @param {*}
34+
* @return ReflectionMethod
35+
*/
36+
public function getProperty($instance, $property) {
37+
$prop = $this->class->getProperty($property);
38+
$prop->setAccessible(true);
39+
return $prop->getValue($instance);
40+
}
41+
/**
42+
* Allows access to private properties in the Transmission class
43+
*
44+
* This is needed to mock the GuzzleHttp\Client responses
45+
*
46+
* @param string $name
47+
* @param {*}
48+
* @return ReflectionMethod
49+
*/
50+
public function setProperty($instance, $property, $value) {
51+
$prop = $this->class->getProperty($property);
52+
$prop->setAccessible(true);
53+
$prop->setValue($instance, $value);
54+
}
55+
}
56+
?>

test/unit/bootstrap.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
<?php
22
require_once dirname(__FILE__).'/../../vendor/autoload.php';
3-
?>
3+
?>

0 commit comments

Comments
 (0)