1
|
|
|
<?php namespace League\Fractal\Test; |
2
|
|
|
|
3
|
|
|
use League\Fractal\Pagination\Cursor; |
4
|
|
|
use League\Fractal\Resource\Collection; |
5
|
|
|
use Mockery; |
6
|
|
|
|
7
|
|
|
class CollectionTest extends \PHPUnit_Framework_TestCase |
8
|
|
|
{ |
9
|
|
|
protected $simpleCollection = [ |
10
|
|
|
['foo' => 'bar'], |
11
|
|
|
['baz' => 'ban'], |
12
|
|
|
]; |
13
|
|
|
|
14
|
|
|
public function testGetData() |
15
|
|
|
{ |
16
|
|
|
$resource = new Collection($this->simpleCollection, function (array $data) { |
17
|
|
|
return $data; |
18
|
|
|
}); |
19
|
|
|
|
20
|
|
|
$this->assertSame($resource->getData(), $this->simpleCollection); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @covers League\Fractal\Resource\Collection::setData |
25
|
|
|
*/ |
26
|
|
|
public function testSetData() |
27
|
|
|
{ |
28
|
|
|
$collection = Mockery::mock('League\Fractal\Resource\Collection')->makePartial(); |
29
|
|
|
$collection->setData('foo'); |
30
|
|
|
$this->assertSame('foo', $collection->getData()); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function testGetTransformer() |
34
|
|
|
{ |
35
|
|
|
$resource = new Collection($this->simpleCollection, function () { |
36
|
|
|
}); |
37
|
|
|
$this->assertTrue(is_callable($resource->getTransformer())); |
38
|
|
|
|
39
|
|
|
$resource = new Collection($this->simpleCollection, 'SomeClass'); |
40
|
|
|
$this->assertSame($resource->getTransformer(), 'SomeClass'); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @covers League\Fractal\Resource\Collection::setTransformer |
45
|
|
|
*/ |
46
|
|
|
public function testSetTransformer() |
47
|
|
|
{ |
48
|
|
|
$collection = Mockery::mock('League\Fractal\Resource\Collection')->makePartial(); |
49
|
|
|
$collection->setTransformer('foo'); |
50
|
|
|
$this->assertSame('foo', $collection->getTransformer()); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @covers League\Fractal\Resource\Collection::setCursor |
55
|
|
|
*/ |
56
|
|
|
public function testSetCursor() |
57
|
|
|
{ |
58
|
|
|
$cursor = Mockery::mock('League\Fractal\Pagination\Cursor'); |
59
|
|
|
$collection = Mockery::mock('League\Fractal\Resource\Collection')->makePartial(); |
60
|
|
|
$this->assertInstanceOf('League\Fractal\Resource\Collection', $collection->setCursor($cursor)); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @covers League\Fractal\Resource\Collection::getCursor |
65
|
|
|
*/ |
66
|
|
|
public function testGetCursor() |
67
|
|
|
{ |
68
|
|
|
$cursor = new Cursor(); |
69
|
|
|
$collection = Mockery::mock('League\Fractal\Resource\Collection')->makePartial(); |
70
|
|
|
$collection->setCursor($cursor); |
71
|
|
|
$this->assertInstanceOf('League\Fractal\Pagination\Cursor', $collection->getCursor()); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @covers League\Fractal\Resource\Collection::setPaginator |
76
|
|
|
* @covers League\Fractal\Resource\Collection::getPaginator |
77
|
|
|
*/ |
78
|
|
|
public function testGetSetPaginator() |
79
|
|
|
{ |
80
|
|
|
$paginator = Mockery::mock('League\Fractal\Pagination\IlluminatePaginatorAdapter'); |
81
|
|
|
$collection = Mockery::mock('League\Fractal\Resource\Collection')->makePartial(); |
82
|
|
|
$this->assertInstanceOf('League\Fractal\Resource\Collection', $collection->setPaginator($paginator)); |
83
|
|
|
$this->assertInstanceOf('League\Fractal\Pagination\PaginatorInterface', $collection->getPaginator()); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @covers League\Fractal\Resource\Collection::setMetaValue |
88
|
|
|
* @covers League\Fractal\Resource\Collection::getMetaValue |
89
|
|
|
*/ |
90
|
|
|
public function testGetSetMeta() |
91
|
|
|
{ |
92
|
|
|
$collection = Mockery::mock('League\Fractal\Resource\Collection')->makePartial(); |
93
|
|
|
$this->assertInstanceOf('League\Fractal\Resource\Collection', $collection->setMetaValue('foo', 'bar')); |
94
|
|
|
|
95
|
|
|
$this->assertSame(['foo' => 'bar'], $collection->getMeta()); |
96
|
|
|
$this->assertSame('bar', $collection->getMetaValue('foo')); |
97
|
|
|
$collection->setMeta(['baz' => 'bat']); |
98
|
|
|
$this->assertSame(['baz' => 'bat'], $collection->getMeta()); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @covers League\Fractal\Resource\Collection::setResourceKey |
103
|
|
|
*/ |
104
|
|
|
public function testSetResourceKey() |
105
|
|
|
{ |
106
|
|
|
$collection = Mockery::mock('League\Fractal\Resource\Collection')->makePartial(); |
107
|
|
|
$this->assertInstanceOf('League\Fractal\Resource\Collection', $collection->setResourceKey('foo')); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @covers League\Fractal\Resource\Collection::getResourceKey |
112
|
|
|
*/ |
113
|
|
|
public function testGetResourceKey() |
114
|
|
|
{ |
115
|
|
|
$collection = Mockery::mock('League\Fractal\Resource\Collection')->makePartial(); |
116
|
|
|
$collection->setResourceKey('foo'); |
117
|
|
|
$this->assertSame('foo', $collection->getResourceKey()); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
public function tearDown() |
121
|
|
|
{ |
122
|
|
|
Mockery::close(); |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|