Repository navigation
Expand file tree
/
Copy pathFactoryTest.php
More file actions
154 lines (127 loc) · 5.81 KB
/
Copy pathFactoryTest.php
File metadata and controls
154 lines (127 loc) · 5.81 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
<?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;
class FactoryTest extends PHPUnit_Framework_TestCase
{
public function setUp()
{
$this->transformer = Mockery::mock('Dingo\Api\Transformer\Factory');
$this->factory = new Factory($this->transformer);
}
public function tearDown()
{
Mockery::close();
}
public function testMakingACreatedResponse()
{
$response = $this->factory->created();
$responseWithLocation = $this->factory->created('test');
$this->assertEquals($response->getStatusCode(), 201);
$this->assertFalse($response->headers->has('Location'));
$this->assertEquals($responseWithLocation->getStatusCode(), 201);
$this->assertTrue($responseWithLocation->headers->has('Location'));
$this->assertEquals($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->assertEquals($response->getStatusCode(), 202);
$this->assertFalse($response->headers->has('Location'));
$this->assertEquals('', $response->getContent());
$this->assertEquals($responseWithLocation->getStatusCode(), 202);
$this->assertTrue($responseWithLocation->headers->has('Location'));
$this->assertEquals($responseWithLocation->headers->get('Location'), 'testHeader');
$this->assertEquals('', $responseWithLocation->getContent());
$this->assertEquals($responseWithContent->getStatusCode(), 202);
$this->assertFalse($responseWithContent->headers->has('Location'));
$this->assertEquals('testContent', $responseWithContent->getContent());
$this->assertEquals($responseWithBoth->getStatusCode(), 202);
$this->assertTrue($responseWithBoth->headers->has('Location'));
$this->assertEquals($responseWithBoth->headers->get('Location'), 'testHeader');
$this->assertEquals('testContent', $responseWithBoth->getContent());
}
public function testMakingANoContentResponse()
{
$response = $this->factory->noContent();
$this->assertEquals(204, $response->getStatusCode());
$this->assertEquals('', $response->getContent());
}
public function testMakingCollectionRegistersUnderlyingClassWithTransformer()
{
$this->transformer->shouldReceive('register')->twice()->with('Dingo\Api\Tests\Stubs\UserStub', 'test', [], null);
$this->assertInstanceOf('Illuminate\Support\Collection', $this->factory->collection(new Collection([new UserStub('Jason')]), 'test')->getOriginalContent());
$this->assertInstanceOf('Illuminate\Support\Collection', $this->factory->withCollection(new Collection([new UserStub('Jason')]), 'test')->getOriginalContent());
}
public function testMakingItemsRegistersClassWithTransformer()
{
$this->transformer->shouldReceive('register')->twice()->with('Dingo\Api\Tests\Stubs\UserStub', 'test', [], null);
$this->assertInstanceOf('Dingo\Api\Tests\Stubs\UserStub', $this->factory->item(new UserStub('Jason'), 'test')->getOriginalContent());
$this->assertInstanceOf('Dingo\Api\Tests\Stubs\UserStub', $this->factory->withItem(new UserStub('Jason'), 'test')->getOriginalContent());
}
public function testMakingPaginatorRegistersUnderlyingClassWithTransformer()
{
$this->transformer->shouldReceive('register')->twice()->with('Dingo\Api\Tests\Stubs\UserStub', 'test', [], null);
$this->assertInstanceOf('Illuminate\Pagination\Paginator', $this->factory->paginator(new Paginator([new UserStub('Jason')], 1), 'test')->getOriginalContent());
$this->assertInstanceOf('Illuminate\Pagination\Paginator', $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->assertEquals('{"foo":"bar"}', $response->getContent());
}
public function testPrefixingWithCallsMethodsCorrectly()
{
$response = $this->factory->withArray(['foo' => 'bar']);
$this->assertEquals('{"foo":"bar"}', $response->getContent());
}
}