Repository navigation
Expand file tree
/
Copy pathResponseTest.php
More file actions
95 lines (72 loc) · 2.76 KB
/
Copy pathResponseTest.php
File metadata and controls
95 lines (72 loc) · 2.76 KB
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
<?php
namespace Dingo\Api\Tests\Http;
use StdClass;
use Mockery as m;
use Dingo\Api\Http\Response;
use PHPUnit_Framework_TestCase;
use Dingo\Api\Transformer\Binding;
use Dingo\Api\Http\Response\Format\Json;
use Illuminate\Events\Dispatcher as EventDispatcher;
class ResponseTest extends PHPUnit_Framework_TestCase
{
public function setUp()
{
Response::setEventDispatcher($this->events = new EventDispatcher);
}
public function tearDown()
{
m::close();
}
/**
* @expectedException \Symfony\Component\HttpKernel\Exception\NotAcceptableHttpException
* @expectedExceptionMessage Unable to format response according to Accept header.
*/
public function testGettingInvalidFormatterThrowsException()
{
Response::getFormatter('json');
}
public function testNonCastableObjectsSetAsOriginalContent()
{
$object = new StdClass;
$object->id = 'test';
$response = new Response($object);
$this->assertNull($response->getContent());
$this->assertSame($object, $response->getOriginalContent());
}
public function testAddingAndSettingMetaCallsUnderlyingTransformerBinding()
{
$binding = new Binding(m::mock('Illuminate\Container\Container'), 'foo');
$response = new Response('test', 200, [], $binding);
$response->setMeta(['foo' => 'bar']);
$response->meta('bing', 'bang');
$this->assertEquals(['foo' => 'bar', 'bing' => 'bang'], $response->getMeta());
}
public function testBuildingWithCustomStatusCodeAndHeaders()
{
$response = new Response('test');
$response->statusCode(302);
$response->header('Foo', 'Bar');
$this->assertEquals('Bar', $response->headers->get('Foo'));
$this->assertEquals(302, $response->getStatusCode());
}
public function testChangingContentWithEvents()
{
$this->events->listen('Dingo\Api\Event\ResponseWasMorphed', function ($event) {
$event->content['foo'] = 'bam!';
});
Response::addFormatter('json', new Json);
$response = new Response(['foo' => 'bar']);
$this->assertEquals('{"foo":"bam!"}', $response->morph('json')->getContent());
$this->events->forget('Dingo\Api\Event\ResponseWasMorphed');
}
public function testChangingResponseHeadersWithEvents()
{
$this->events->listen('Dingo\Api\Event\ResponseIsMorphing', function ($event) {
$event->response->headers->set('x-foo', 'bar');
});
Response::addFormatter('json', new Json);
$response = new Response(['foo' => 'bar']);
$this->assertEquals('bar', $response->morph('json')->headers->get('x-foo'));
$this->events->forget('Dingo\Api\Event\ResponseIsMorphing');
}
}