summaryrefslogtreecommitdiffstats
path: root/test/unit/SparkPostTest.php
blob: 26563c3094aa2877d565562574aca062ac6d15fd (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
<?php

namespace SparkPost\Test;

use Ivory\HttpAdapter\CurlHttpAdapter;
use Mockery;
use SparkPost\SparkPost;
use SparkPost\Test\TestUtils\ClassUtils;

class SparkPostTest extends \PHPUnit_Framework_TestCase
{
    private static $utils;
    private $adapterMock;
    /** @var SparkPost */
    private $resource;

    /**
     * (non-PHPdoc).
     *
     * @before
     *
     * @see PHPUnit_Framework_TestCase::setUp()
     */
    public function setUp()
    {
        //setup mock for the adapter
        $this->adapterMock = Mockery::mock('Ivory\HttpAdapter\HttpAdapterInterface', function ($mock) {
            $mock->shouldReceive('setConfiguration');
            $mock->shouldReceive('getConfiguration->getUserAgent')->andReturn('php-sparkpost/0.2.0');
        });

        $this->resource = new SparkPost($this->adapterMock, ['key' => 'a key']);
        self::$utils = new ClassUtils($this->resource);
        self::$utils->setProperty($this->resource, 'httpAdapter', $this->adapterMock);
    }

    public function tearDown()
    {
        Mockery::close();
    }

    /**
     * @desc Ensures that the configuration class is not instantiable.
     */
    public function testConstructorSetsUpTransmissions()
    {
        $sparky = new SparkPost(new CurlHttpAdapter(), ['key' => 'a key']);
        $this->assertEquals('SparkPost\Transmission', get_class($sparky->transmission));
        $adapter = self::$utils->getProperty($this->resource, 'httpAdapter');
        $this->assertRegExp('/php-sparkpost.*/', $adapter->getConfiguration()->getUserAgent());
    }

    public function testSetConfigStringKey()
    {
        $this->resource->setConfig('a key');
        $config = self::$utils->getProperty($this->resource, 'config');
        $this->assertEquals('a key', $config['key']);
    }

    /**
     * @expectedException Exception
     * @expectedExceptionMessageRegExp /API key/
     */
    public function testSetBadConfig()
    {
        $this->resource->setConfig(['not' => 'a key']);
    }

    public function testGetHeaders()
    {
        $results = $this->resource->getHttpHeaders();
        $this->assertEquals('a key', $results['Authorization']);
        $this->assertEquals('application/json', $results['Content-Type']);
    }

    public function testSetUnwrapped()
    {
        $results = $this->resource->setupUnwrapped('ASweetEndpoint');
        $this->assertEquals($this->resource->ASweetEndpoint, $results);
        $this->assertInstanceOf('SparkPost\APIResource', $results);
        $this->assertEquals('ASweetEndpoint', $results->endpoint);
    }
}