Repository navigation
Expand file tree
/
Copy pathFactoryTest.php
More file actions
187 lines (156 loc) · 7.64 KB
/
Copy pathFactoryTest.php
File metadata and controls
187 lines (156 loc) · 7.64 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
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
<?php
namespace Dingo\Api\Tests\Http\Response;
use Mockery;
use PHPUnit_Framework_TestCase;
use Illuminate\Support\Collection;
use Dingo\Api\Tests\Stubs\UserStub;
use Dingo\Api\Http\Response\Factory;
use Illuminate\Pagination\Paginator;
use Dingo\Api\Transformer\Factory as TransformerFactory;
class FactoryTest extends PHPUnit_Framework_TestCase
{
public function setUp()
{
$this->transformer = Mockery::mock(TransformerFactory::class);
$this->factory = new Factory($this->transformer);
}
public function tearDown()
{
Mockery::close();
}
public function testMakingACreatedResponse()
{
$response = $this->factory->created();
$responseWithLocation = $this->factory->created('test');
$this->assertSame($response->getStatusCode(), 201);
$this->assertFalse($response->headers->has('Location'));
$this->assertSame($responseWithLocation->getStatusCode(), 201);
$this->assertTrue($responseWithLocation->headers->has('Location'));
$this->assertSame($responseWithLocation->headers->get('Location'), 'test');
}
public function testMakingAnAcceptedResponse()
{
$response = $this->factory->accepted();
$responseWithLocation = $this->factory->accepted('testHeader');
$responseWithContent = $this->factory->accepted(null, 'testContent');
$responseWithBoth = $this->factory->accepted('testHeader', 'testContent');
$this->assertSame($response->getStatusCode(), 202);
$this->assertFalse($response->headers->has('Location'));
$this->assertSame('', $response->getContent());
$this->assertSame($responseWithLocation->getStatusCode(), 202);
$this->assertTrue($responseWithLocation->headers->has('Location'));
$this->assertSame($responseWithLocation->headers->get('Location'), 'testHeader');
$this->assertSame('', $responseWithLocation->getContent());
$this->assertSame($responseWithContent->getStatusCode(), 202);
$this->assertFalse($responseWithContent->headers->has('Location'));
$this->assertSame('testContent', $responseWithContent->getContent());
$this->assertSame($responseWithBoth->getStatusCode(), 202);
$this->assertTrue($responseWithBoth->headers->has('Location'));
$this->assertSame($responseWithBoth->headers->get('Location'), 'testHeader');
$this->assertSame('testContent', $responseWithBoth->getContent());
}
public function testMakingANoContentResponse()
{
$response = $this->factory->noContent();
$this->assertSame(204, $response->getStatusCode());
$this->assertSame('', $response->getContent());
}
public function testMakingCollectionRegistersUnderlyingClassWithTransformer()
{
$this->transformer->shouldReceive('register')->twice()->with(UserStub::class, 'test', [], null);
$this->assertInstanceOf(Collection::class, $this->factory->collection(new Collection([new UserStub('Jason')]), 'test')->getOriginalContent());
$this->assertInstanceOf(Collection::class, $this->factory->withCollection(new Collection([new UserStub('Jason')]), 'test')->getOriginalContent());
}
public function testMakingCollectionResponseWithThreeParameters()
{
$this->transformer->shouldReceive('register')->twice()->with('Dingo\Api\Tests\Stubs\UserStub', 'test', [], Mockery::on(function ($param) {
return $param instanceof \Closure;
}));
$this->assertInstanceOf('Illuminate\Support\Collection', $this->factory->collection(new Collection([new UserStub('Jason')]), 'test', function ($resource, $fractal) {
$this->assertInstanceOf('League\Fractal\Resource\Collection', $resource);
$this->assertInstanceOf('League\Fractal\Manager', $fractal);
})->getOriginalContent());
$this->assertInstanceOf('Illuminate\Support\Collection', $this->factory->withCollection(new Collection([new UserStub('Jason')]), 'test', function ($resource, $fractal) {
$this->assertInstanceOf('League\Fractal\Resource\Collection', $resource);
$this->assertInstanceOf('League\Fractal\Manager', $fractal);
})->getOriginalContent());
}
public function testMakingItemsRegistersClassWithTransformer()
{
$this->transformer->shouldReceive('register')->twice()->with(UserStub::class, 'test', [], null);
$this->assertInstanceOf(UserStub::class, $this->factory->item(new UserStub('Jason'), 'test')->getOriginalContent());
$this->assertInstanceOf(UserStub::class, $this->factory->withItem(new UserStub('Jason'), 'test')->getOriginalContent());
}
public function testMakingItemResponseWithThreeParameters()
{
$this->transformer->shouldReceive('register')->twice()->with('Dingo\Api\Tests\Stubs\UserStub', 'test', [], Mockery::on(function ($param) {
return $param instanceof \Closure;
}));
$this->assertInstanceOf('Dingo\Api\Tests\Stubs\UserStub', $this->factory->item(new UserStub('Jason'), 'test', function ($resource, $fractal) {
$this->assertInstanceOf('League\Fractal\Resource\Item', $resource);
$this->assertInstanceOf('League\Fractal\Manager', $fractal);
})->getOriginalContent());
$this->assertInstanceOf('Dingo\Api\Tests\Stubs\UserStub', $this->factory->withItem(new UserStub('Jason'), 'test', function ($resource, $fractal) {
$this->assertInstanceOf('League\Fractal\Resource\Item', $resource);
$this->assertInstanceOf('League\Fractal\Manager', $fractal);
})->getOriginalContent());
}
public function testMakingPaginatorRegistersUnderlyingClassWithTransformer()
{
$this->transformer->shouldReceive('register')->twice()->with(UserStub::class, 'test', [], null);
$this->assertInstanceOf(Paginator::class, $this->factory->paginator(new Paginator([new UserStub('Jason')], 1), 'test')->getOriginalContent());
$this->assertInstanceOf(Paginator::class, $this->factory->withPaginator(new Paginator([new UserStub('Jason')], 1), 'test')->getOriginalContent());
}
/**
* @expectedException \Symfony\Component\HttpKernel\Exception\HttpException
*/
public function testNotFoundThrowsHttpException()
{
$this->factory->errorNotFound();
}
/**
* @expectedException \Symfony\Component\HttpKernel\Exception\HttpException
*/
public function testBadRequestThrowsHttpException()
{
$this->factory->errorBadRequest();
}
/**
* @expectedException \Symfony\Component\HttpKernel\Exception\HttpException
*/
public function testForbiddenThrowsHttpException()
{
$this->factory->errorForbidden();
}
/**
* @expectedException \Symfony\Component\HttpKernel\Exception\HttpException
*/
public function testInternalThrowsHttpException()
{
$this->factory->errorInternal();
}
/**
* @expectedException \Symfony\Component\HttpKernel\Exception\HttpException
*/
public function testUnauthorizedThrowsHttpException()
{
$this->factory->errorUnauthorized();
}
/**
* @expectedException \Symfony\Component\HttpKernel\Exception\HttpException
*/
public function testMethodNotAllowedThrowsHttpException()
{
$this->factory->errorMethodNotAllowed();
}
public function testMakingArrayResponse()
{
$response = $this->factory->array(['foo' => 'bar']);
$this->assertSame('{"foo":"bar"}', $response->getContent());
}
public function testPrefixingWithCallsMethodsCorrectly()
{
$response = $this->factory->withArray(['foo' => 'bar']);
$this->assertSame('{"foo":"bar"}', $response->getContent());
}
}