summaryrefslogtreecommitdiffstats
path: root/test/unit/SparkPostTest.php
blob: 43e499fd4ecc0020cfe2d1e0c59a30c773ee9422 (plain)
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<?php
namespace SparkPost\Test;

use SparkPost\SparkPost;
use Ivory\HttpAdapter\CurlHttpAdapter;

class SparkPostTest extends \PHPUnit_Framework_TestCase {

  /**
	 * Allows access to private properties in the Transmission class
	 *
	 * @param string $name
	 * @param {*}
	 * @return ReflectionMethod
	 */
	private static function setPrivateProperty($name, $value) {
		$class = new \ReflectionClass('\SparkPost\SparkPost');
		$prop = $class->getProperty($name);
		$prop->setAccessible(true);
		$prop->setValue($value);
	}


  public function setUp() {
    $this->setPrivateProperty('config', null);
    $this->setPrivateProperty('httpAdapter', null);
	}

	/**
	 * @desc Ensures that the configuration class is not instantiable.
	 */
	public function testConstructorCannotBeCalled() {
		$class = new \ReflectionClass('\SparkPost\SparkPost');
		$this->assertFalse($class->isInstantiable());
	}

	/**
	 * @desc Tests that an exception is thrown when a library tries to recieve the config and it has not yet been set.
	 * 		Since its a singleton this test must come before any setConfig tests.
	 * @expectedException Exception
	 * @expectedExceptionMessage No configuration has been provided
	 */
	public function testGetConfigEmptyException() {
		SparkPost::unsetConfig();
		SparkPost::getConfig();
	}

	/**
	 * @desc Tests that the api key is set when setting the config
	 * @expectedException Exception
	 * @expectedExceptionMessage You must provide an API key
	 */
	public function testSetConfigAPIKeyNotSetException() {
		SparkPost::setConfig(['something'=>'other than an API Key']);
	}

	/**
	 * @desc Tests that the api key is set when setting the config and that its not empty
	 * @expectedException Exception
	 * @expectedExceptionMessage You must provide an API key
	 */
	public function testSetConfigAPIKeyEmptyException() {
		SparkPost::setConfig(['key'=>'']);
	}

	/**
	 * @desc Tests overridable values are set while invalid values are ignored
	 */
	public function testSetConfigMultipleValuesAndGetConfig() {
		SparkPost::setConfig(['key'=>'lala', 'version'=>'v8', 'port'=>1024, 'someOtherValue'=>'fakeValue']);

		$testConfig = SparkPost::getConfig();
		$this->assertEquals('lala', $testConfig['key']);
		$this->assertEquals('v8', $testConfig['version']);
		$this->assertEquals(1024, $testConfig['port']);
		$this->assertNotContains('someOtherValue', array_keys($testConfig));
		$this->assertEquals('https', $testConfig['protocol']);
		$this->assertEquals('api.sparkpost.com', $testConfig['host']);
		$this->assertEquals(true, $testConfig['strictSSL']);
	}

  /**
   * @desc tests getting an unset
   * @expectedException Exception
   * @expectedExceptionMessageRegExp /No Http Adapter/
   */
  public function testGetHttpAdapterForIsset() {
    SparkPost::getHttpAdapter();
  }

  /**
   * @desc tests failing validation for http adapters
   * @expectedException Exception
   * @expectedExceptionMessageRegExp /must be a valid Ivory\\HttpAdapter/
   */
  public function testSetInvalidHttpAdapter() {
    SparkPost::setHttpAdapter(new \stdClass());
  }

  public function testSetAndGetValidHttpAdapter() {
    SparkPost::setConfig(['key'=>'lala']);
    SparkPost::setHttpAdapter(new CurlHttpAdapter());
    $this->assertEquals('Ivory\HttpAdapter\CurlHttpAdapter', get_class(Sparkpost::getHttpAdapter()));
  }

  public function testConfigure() {
    SparkPost::configure(new CurlHttpAdapter(), ['key'=>'lala']);
    $this->assertEquals('Ivory\HttpAdapter\CurlHttpAdapter', get_class(Sparkpost::getHttpAdapter()));
  }

  public function testDefaultHeaders() {
    $key = 'lala';
    SparkPost::setConfig(['key'=>$key]);
    $this->assertEquals($key, Sparkpost::getHttpHeaders()['Authorization']);
    $this->assertEquals('application/json', Sparkpost::getHttpHeaders()['Content-Type']);
  }

  public function testOverrideDefaultHeaders() {
    $key = 'lala';
    $headers=['Content-Type'=>'my/type'];
    SparkPost::setConfig(['key'=>$key]);
    $this->assertEquals($key, Sparkpost::getHttpHeaders($headers)['Authorization']);
    $this->assertEquals('my/type', Sparkpost::getHttpHeaders($headers)['Content-Type']);
  }

}
?>