Skip to content

Commit 97e0d26

Browse files
committed
Updated tests for new structure
1 parent c753d55 commit 97e0d26

File tree

6 files changed

+114
-67
lines changed

6 files changed

+114
-67
lines changed

examples/unwrapped/create_template.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22
namespace Examples\Unwrapped;
33
require_once (dirname(__FILE__).'/../bootstrap.php');
4+
use SparkPost\SparkPost;
45
use GuzzleHttp\Client;
56
use Ivory\HttpAdapter\Guzzle6HttpAdapter;
67

@@ -14,7 +15,7 @@
1415

1516
$templateConfig = [
1617
'name' => 'Summer Sale!',
17-
'id'=>'summer-sale',
18+
'id'=>'jordan-test-summer-sale',
1819
'content'=> [
1920
'from' => 'from@sparkpostbox.com',
2021
'subject' => 'Summer deals',

lib/SparkPost/APIResource.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ class APIResource {
2828
*/
2929
protected static $structure = [];
3030

31+
/**
32+
* @desc SparkPost reference for httpAdapters and configs
33+
*/
34+
protected $sparkpost;
35+
3136
/**
3237
* @desc Initializes config and httpAdapter for use later.
3338
* @param $sparkpost SparkPost\SparkPost provides api configuration information

lib/SparkPost/SparkPost.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public function __construct($httpAdapter, $settingsConfig) {
5252
* The new resource is attached to this object as well as returned
5353
* @return SparkPost\APIResource - the unwrapped resource
5454
*/
55-
public function setupUnwrapped (string $endpoint) {
55+
public function setupUnwrapped ($endpoint) {
5656
$this->{$endpoint} = new APIResource($this);
5757
$this->{$endpoint}->endpoint = $endpoint;
5858

test/unit/APIResourceTest.php

Lines changed: 16 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -24,60 +24,44 @@ private function getExceptionMock($statusCode) {
2424
* @see PHPUnit_Framework_TestCase::setUp()
2525
*/
2626
public function setUp() {
27-
$this->adapterMock = Mockery::mock('Ivory\HttpAdapter\HttpAdapterInterface', function($mock) {
28-
$mock->shouldReceive('setConfiguration');
29-
$mock->shouldReceive('getConfiguration->getUserAgent')->andReturn('php-sparkpost/0.2.0');
27+
$this->sparkPostMock = Mockery::mock('SparkPost\SparkPost', function($mock) {
28+
$mock->shouldReceive('getHttpHeaders')->andReturn([]);
3029
});
31-
$this->resource = new APIResource($this->adapterMock, ['key'=>'a key']);
30+
$this->sparkPostMock->httpAdapter = Mockery::mock();
31+
$this->resource = new APIResource($this->sparkPostMock);
3232
self::$utils = new ClassUtils($this->resource);
33-
34-
self::$utils->setProperty($this->resource, 'httpAdapter', $this->adapterMock);
33+
self::$utils->setProperty($this->resource, 'sparkpost', $this->sparkPostMock);
3534
}
3635

3736
public function tearDown()
3837
{
3938
Mockery::close();
4039
}
4140

42-
public function testConstructorSetsUpAdapterAndConfig() {
43-
$adapter = self::$utils->getProperty($this->resource, 'httpAdapter');
44-
$this->assertRegExp('/php-sparkpost.*/', $adapter->getConfiguration()->getUserAgent());
45-
}
46-
47-
/**
48-
* @expectedException Exception
49-
* @expectedExceptionMessageRegExp /valid Ivory\\HttpAdapter/
50-
*/
51-
public function testSetBadHTTPAdapter() {
52-
$this->resource->setHttpAdapter(new \stdClass());
53-
}
54-
55-
/**
56-
* @expectedException Exception
57-
* @expectedExceptionMessageRegExp /API key/
58-
*/
59-
public function testSetBadConfig() {
60-
$this->resource->setConfig(['not'=>'a key']);
41+
public function testConstructorSetsUpSparkPostObject() {
42+
$this->sparkPostMock->newProp = 'new value';
43+
$this->assertEquals($this->sparkPostMock, self::$utils->getProperty($this->resource, 'sparkpost'));
6144
}
6245

6346
public function testCreate() {
6447
$testInput = ['test'=>'body'];
6548
$testBody = ["results"=>["my"=>"test"]];
6649
$responseMock = Mockery::mock();
67-
$this->adapterMock->shouldReceive('send')->
50+
$this->sparkPostMock->httpAdapter->shouldReceive('send')->
6851
once()->
6952
with(Mockery::type('string'), 'POST', Mockery::type('array'), json_encode($testInput))->
7053
andReturn($responseMock);
7154
$responseMock->shouldReceive('getBody->getContents')->andReturn(json_encode($testBody));
7255

56+
7357
$this->assertEquals($testBody, $this->resource->create($testInput));
7458
}
7559

7660
public function testUpdate() {
7761
$testInput = ['test'=>'body'];
7862
$testBody = ["results"=>["my"=>"test"]];
7963
$responseMock = Mockery::mock();
80-
$this->adapterMock->shouldReceive('send')->
64+
$this->sparkPostMock->httpAdapter->shouldReceive('send')->
8165
once()->
8266
with('/.*\/test/', 'PUT', Mockery::type('array'), json_encode($testInput))->
8367
andReturn($responseMock);
@@ -89,7 +73,7 @@ public function testUpdate() {
8973
public function testGet() {
9074
$testBody = ["results"=>["my"=>"test"]];
9175
$responseMock = Mockery::mock();
92-
$this->adapterMock->shouldReceive('send')->
76+
$this->sparkPostMock->httpAdapter->shouldReceive('send')->
9377
once()->
9478
with('/.*\/test/', 'GET', Mockery::type('array'), null)->
9579
andReturn($responseMock);
@@ -100,7 +84,7 @@ public function testGet() {
10084

10185
public function testDelete() {
10286
$responseMock = Mockery::mock();
103-
$this->adapterMock->shouldReceive('send')->
87+
$this->sparkPostMock->httpAdapter->shouldReceive('send')->
10488
once()->
10589
with('/.*\/test/', 'DELETE', Mockery::type('array'), null)->
10690
andReturn($responseMock);
@@ -111,7 +95,7 @@ public function testDelete() {
11195

11296
public function testAdapter404Exception() {
11397
try {
114-
$this->adapterMock->shouldReceive('send')->
98+
$this->sparkPostMock->httpAdapter->shouldReceive('send')->
11599
once()->
116100
andThrow($this->getExceptionMock(404));
117101

@@ -124,7 +108,7 @@ public function testAdapter404Exception() {
124108

125109
public function testAdapter4XXException() {
126110
try {
127-
$this->adapterMock->shouldReceive('send')->
111+
$this->sparkPostMock->httpAdapter->shouldReceive('send')->
128112
once()->
129113
andThrow($this->getExceptionMock(400));
130114

@@ -137,7 +121,7 @@ public function testAdapter4XXException() {
137121

138122
public function testAdapter5XXException() {
139123
try {
140-
$this->adapterMock->shouldReceive('send')->
124+
$this->sparkPostMock->httpAdapter->shouldReceive('send')->
141125
once()->
142126
andThrow(new \Exception('Something went wrong.'));
143127

test/unit/SparkPostTest.php

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,81 @@
33

44
use SparkPost\SparkPost;
55
use Ivory\HttpAdapter\CurlHttpAdapter;
6+
use SparkPost\Test\TestUtils\ClassUtils;
7+
use \Mockery;
68

79
class SparkPostTest extends \PHPUnit_Framework_TestCase {
810

11+
private static $utils;
12+
private $adapterMock;
13+
private $resource;
14+
15+
/**
16+
* (non-PHPdoc)
17+
* @before
18+
* @see PHPUnit_Framework_TestCase::setUp()
19+
*/
20+
public function setUp() {
21+
//setup mock for the adapter
22+
$this->adapterMock = Mockery::mock('Ivory\HttpAdapter\HttpAdapterInterface', function($mock) {
23+
$mock->shouldReceive('setConfiguration');
24+
$mock->shouldReceive('getConfiguration->getUserAgent')->andReturn('php-sparkpost/0.2.0');
25+
});
26+
27+
$this->resource = new SparkPost($this->adapterMock, ['key'=>'a key']);
28+
self::$utils = new ClassUtils($this->resource);
29+
self::$utils->setProperty($this->resource, 'httpAdapter', $this->adapterMock);
30+
}
31+
32+
public function tearDown()
33+
{
34+
Mockery::close();
35+
}
36+
937
/**
1038
* @desc Ensures that the configuration class is not instantiable.
1139
*/
1240
public function testConstructorSetsUpTransmissions() {
1341
$sparky = new SparkPost(new CurlHttpAdapter(), ['key'=>'a key']);
1442
$this->assertEquals('SparkPost\Transmission', get_class($sparky->transmission));
43+
$adapter = self::$utils->getProperty($this->resource, 'httpAdapter');
44+
$this->assertRegExp('/php-sparkpost.*/', $adapter->getConfiguration()->getUserAgent());
1545
}
46+
47+
/**
48+
* @expectedException Exception
49+
* @expectedExceptionMessageRegExp /valid Ivory\\HttpAdapter/
50+
*/
51+
public function testSetBadHTTPAdapter() {
52+
$this->resource->setHttpAdapter(new \stdClass());
53+
}
54+
55+
/**
56+
* @expectedException Exception
57+
* @expectedExceptionMessageRegExp /API key/
58+
*/
59+
public function testSetBadConfig() {
60+
$this->resource->setConfig(['not'=>'a key']);
61+
}
62+
63+
64+
public function testGetHeaders() {
65+
$results = $this->resource->getHttpHeaders();
66+
$this->assertEquals('a key', $results['Authorization']);
67+
$this->assertEquals('application/json', $results['Content-Type']);
68+
}
69+
70+
public function testGetHeadersOverride() {
71+
$results = $this->resource->getHttpHeaders(['Content-Type'=>'application/xml']);
72+
$this->assertEquals('application/xml', $results['Content-Type']);
73+
}
74+
75+
public function testSetUnwrapped() {
76+
$results = $this->resource->setupUnwrapped('ASweetEndpoint');
77+
$this->assertEquals($this->resource->ASweetEndpoint, $results);
78+
$this->assertInstanceOf('SparkPost\APIResource', $results);
79+
$this->assertEquals('ASweetEndpoint', $results->endpoint);
80+
}
81+
1682
}
1783
?>

test/unit/TransmissionTest.php

Lines changed: 24 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
<?php
22
namespace SparkPost\Test;
3-
43
use SparkPost\Transmission;
54
use SparkPost\Test\TestUtils\ClassUtils;
65
use \Mockery;
76

87
class TransmissionTest extends \PHPUnit_Framework_TestCase {
98

109
private static $utils;
11-
private $adapterMock;
10+
private $sparkPostMock;
1211
private $resource;
1312

1413
/**
@@ -17,35 +16,27 @@ class TransmissionTest extends \PHPUnit_Framework_TestCase {
1716
* @see PHPUnit_Framework_TestCase::setUp()
1817
*/
1918
public function setUp() {
20-
$this->adapterMock = Mockery::mock('Ivory\HttpAdapter\HttpAdapterInterface', function($mock) {
21-
$mock->shouldReceive('setConfiguration');
22-
$mock->shouldReceive('getConfiguration->getUserAgent')->andReturn('php-sparkpost/0.2.0');
19+
$this->sparkPostMock = Mockery::mock('SparkPost\SparkPost', function($mock) {
20+
$mock->shouldReceive('getHttpHeaders')->andReturn([]);
2321
});
24-
$this->resource = new Transmission($this->adapterMock, ['key'=>'a key']);
22+
$this->sparkPostMock->httpAdapter = Mockery::mock();
23+
$this->resource = new Transmission($this->sparkPostMock);
2524
self::$utils = new ClassUtils($this->resource);
26-
27-
self::$utils->setProperty($this->resource, 'httpAdapter', $this->adapterMock);
2825
}
2926

30-
public function tearDown()
31-
{
32-
Mockery::close();
33-
}
34-
35-
public function testConstructorSetsUpAdapterAndConfig() {
36-
$adapter = self::$utils->getProperty($this->resource, 'httpAdapter');
37-
$this->assertRegExp('/php-sparkpost.*/', $adapter->getConfiguration()->getUserAgent());
27+
public function tearDown(){
28+
Mockery::close();
3829
}
3930

40-
4131
public function testSend() {
4232
$responseMock = Mockery::mock();
4333
$body = ['text'=>'awesomesauce', 'content'=>['subject'=>'awesomeness']];
4434
$responseBody = ["results"=>"yay"];
45-
$this->adapterMock->shouldReceive('send')->
46-
once()->
47-
with('/.*\/transmissions/', 'POST', Mockery::type('array'), Mockery::type('string'))->
48-
andReturn($responseMock);
35+
36+
$this->sparkPostMock->httpAdapter->shouldReceive('send')->
37+
once()->
38+
with('/.*\/transmissions/', 'POST', Mockery::type('array'), Mockery::type('string'))->
39+
andReturn($responseMock);
4940

5041
$responseMock->shouldReceive('getBody->getContents')->andReturn(json_encode($responseBody));
5142

@@ -55,10 +46,10 @@ public function testSend() {
5546
public function testAllWithFilter() {
5647
$responseMock = Mockery::mock();
5748
$responseBody = ["results"=>"yay"];
58-
$this->adapterMock->shouldReceive('send')->
59-
once()->
60-
with('/.*transmissions.*?campaign_id=campaign&template_id=template/', 'GET', Mockery::type('array'), null)->
61-
andReturn($responseMock);
49+
$this->sparkPostMock->httpAdapter->shouldReceive('send')->
50+
once()->
51+
with('/.*transmissions.*?campaign_id=campaign&template_id=template/', 'GET', Mockery::type('array'), null)->
52+
andReturn($responseMock);
6253

6354
$responseMock->shouldReceive('getBody->getContents')->andReturn(json_encode($responseBody));
6455

@@ -68,10 +59,10 @@ public function testAllWithFilter() {
6859
public function testAllWithOutFilter() {
6960
$responseMock = Mockery::mock();
7061
$responseBody = ["results"=>"yay"];
71-
$this->adapterMock->shouldReceive('send')->
72-
once()->
73-
with('/.*\/transmissions/', 'GET', Mockery::type('array'), null)->
74-
andReturn($responseMock);
62+
$this->sparkPostMock->httpAdapter->shouldReceive('send')->
63+
once()->
64+
with('/.*\/transmissions/', 'GET', Mockery::type('array'), null)->
65+
andReturn($responseMock);
7566

7667
$responseMock->shouldReceive('getBody->getContents')->andReturn(json_encode($responseBody));
7768

@@ -81,10 +72,10 @@ public function testAllWithOutFilter() {
8172
public function testFind() {
8273
$responseMock = Mockery::mock();
8374
$responseBody = ["results"=>"yay"];
84-
$this->adapterMock->shouldReceive('send')->
85-
once()->
86-
with('/.*\/transmissions.*\/test/', 'GET', Mockery::type('array'), null)->
87-
andReturn($responseMock);
75+
$this->sparkPostMock->httpAdapter->shouldReceive('send')->
76+
once()->
77+
with('/.*\/transmissions.*\/test/', 'GET', Mockery::type('array'), null)->
78+
andReturn($responseMock);
8879

8980
$responseMock->shouldReceive('getBody->getContents')->andReturn(json_encode($responseBody));
9081

0 commit comments

Comments
 (0)