summaryrefslogtreecommitdiffstats
path: root/test/unit/APIResourceTest.php
blob: 1d29c556554072c88a72f73ccf54623a772112b1 (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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
<?php
namespace SparkPost\Test;
use SparkPost\APIResource;
use SparkPost\Test\TestUtils\ClassUtils;
use \Mockery;

class APIResourceTest extends \PHPUnit_Framework_TestCase {

  private static $utils;
  private $adapterMock;
  private $resource;

  private function getExceptionMock($statusCode) {
    $exception = new \Ivory\HttpAdapter\HttpAdapterException();
    $response = Mockery::mock('Ivory\HttpAdapter\Message\ResponseInterface');
    $response->shouldReceive('getStatusCode')->andReturn($statusCode);
    $exception->setResponse($response);
    return $exception;
  }

  /**
   * (non-PHPdoc)
   * @before
   * @see PHPUnit_Framework_TestCase::setUp()
   */
  public function setUp() {
    $this->sparkPostMock = Mockery::mock('SparkPost\SparkPost', function($mock) {
      $mock->shouldReceive('getHttpHeaders')->andReturn([]);
    });
    $this->sparkPostMock->httpAdapter = Mockery::mock();
    $this->resource = new APIResource($this->sparkPostMock);
    self::$utils = new ClassUtils($this->resource);
    self::$utils->setProperty($this->resource, 'sparkpost', $this->sparkPostMock);
  }

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

  public function testConstructorSetsUpSparkPostObject() {
    $this->sparkPostMock->newProp = 'new value';
    $this->assertEquals($this->sparkPostMock, self::$utils->getProperty($this->resource, 'sparkpost'));
  }

  public function testCreate() {
    $testInput = ['test'=>'body'];
    $testBody = ['results'=>['my'=>'test']];
    $responseMock = Mockery::mock();
    $this->sparkPostMock->httpAdapter->shouldReceive('send')->
      once()->
      with(Mockery::type('string'), 'POST', Mockery::type('array'), json_encode($testInput))->
      andReturn($responseMock);
    $responseMock->shouldReceive('getStatusCode')->andReturn(200);
    $responseMock->shouldReceive('getBody->getContents')->andReturn(json_encode($testBody));

    $this->assertEquals($testBody, $this->resource->create($testInput));
  }

  public function testUpdate() {
    $testInput = ['test'=>'body'];
    $testBody = ['results'=>['my'=>'test']];
    $responseMock = Mockery::mock();
    $this->sparkPostMock->httpAdapter->shouldReceive('send')->
      once()->
      with('/.*\/test/', 'PUT', Mockery::type('array'), json_encode($testInput))->
      andReturn($responseMock);
    $responseMock->shouldReceive('getStatusCode')->andReturn(200);
    $responseMock->shouldReceive('getBody->getContents')->andReturn(json_encode($testBody));

    $this->assertEquals($testBody, $this->resource->update('test', $testInput));
  }

  public function testGet() {
    $testBody = ['results'=>['my'=>'test']];
    $responseMock = Mockery::mock();
    $this->sparkPostMock->httpAdapter->shouldReceive('send')->
      once()->
      with('/.*\/test/', 'GET', Mockery::type('array'), null)->
      andReturn($responseMock);
    $responseMock->shouldReceive('getStatusCode')->andReturn(200);
    $responseMock->shouldReceive('getBody->getContents')->andReturn(json_encode($testBody));

    $this->assertEquals($testBody, $this->resource->get('test'));
  }

  public function testDelete() {
    $responseMock = Mockery::mock();
    $this->sparkPostMock->httpAdapter->shouldReceive('send')->
      once()->
      with('/.*\/test/', 'DELETE', Mockery::type('array'), null)->
      andReturn($responseMock);
    $responseMock->shouldReceive('getStatusCode')->andReturn(200);
    $responseMock->shouldReceive('getBody->getContents')->andReturn('');

    $this->assertEquals(null, $this->resource->delete('test'));
  }

  public function testAdapter404Exception() {
    try {
      $responseMock = Mockery::mock();
      $this->sparkPostMock->httpAdapter->shouldReceive('send')->
        once()->
        andReturn($responseMock);
      $responseMock->shouldReceive('getStatusCode')->andReturn(404);

      $this->resource->get('test');
    }
    catch(\Exception $e) {
      $this->assertRegExp('/.*resource does not exist.*/', $e->getMessage());
    }
  }

  public function testAdapter403Exception() {
    try {
      $responseMock = Mockery::mock();
      $this->sparkPostMock->httpAdapter->shouldReceive('send')->
      once()->
      andReturn($responseMock);
      $responseMock->shouldReceive('getStatusCode')->andReturn(403);

      $this->resource->get('test');
    }
    catch(\Exception $e) {
      $this->assertRegExp('/Request forbidden/', $e->getMessage());
    }
  }

  public function testAdapter4XXException() {
    try {
      $testBody = ['errors'=>['my'=>'test']];
      $responseMock = Mockery::mock();
      $this->sparkPostMock->httpAdapter->shouldReceive('send')->
        once()->
        andReturn($responseMock);
      $responseMock->shouldReceive('getStatusCode')->andReturn(400);
      $responseMock->shouldReceive('getBody')->andReturn(json_encode($testBody));

      $this->resource->get('test');
    }
    catch(\Exception $e) {
      $this->assertRegExp('/Received bad response.*/', $e->getMessage());
    }
  }

  public function testAdapter5XXException() {
    try {
      $this->sparkPostMock->httpAdapter->shouldReceive('send')->
        once()->
        andThrow(new \Exception('Something went wrong.'));

      $this->resource->get('test');
    }
    catch(\Exception $e) {
      $this->assertRegExp('/Unable to contact.*API.*/', $e->getMessage());
    }
  }

}