|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @author Antoine Hedgcock |
|
4
|
|
|
*/ |
|
5
|
|
|
|
|
6
|
|
|
namespace CrateTest\Stdlib; |
|
7
|
|
|
|
|
8
|
|
|
use Crate\Stdlib\Collection; |
|
9
|
|
|
use PHPUnit_Framework_TestCase; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Class CollectionTest |
|
13
|
|
|
* |
|
14
|
|
|
* @coversDefaultClass \Crate\Stdlib\Collection |
|
15
|
|
|
* @covers ::<!public> |
|
16
|
|
|
* |
|
17
|
|
|
* @group unit |
|
18
|
|
|
*/ |
|
19
|
|
|
class CollectionTest extends PHPUnit_Framework_TestCase |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* @var array |
|
23
|
|
|
*/ |
|
24
|
|
|
private $rows = [ |
|
25
|
|
|
[1, 'hello'], |
|
26
|
|
|
[2, 'world'] |
|
27
|
|
|
]; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var array |
|
31
|
|
|
*/ |
|
32
|
|
|
private $columns = ['id', 'name']; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var Collection |
|
36
|
|
|
*/ |
|
37
|
|
|
private $collection; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @covers ::__construct |
|
41
|
|
|
*/ |
|
42
|
|
|
protected function setUp() |
|
43
|
|
|
{ |
|
44
|
|
|
$this->collection = new Collection($this->rows, $this->columns, 0, count($this->rows)); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @covers ::map |
|
49
|
|
|
*/ |
|
50
|
|
|
public function testMap() |
|
51
|
|
|
{ |
|
52
|
|
|
$result = $this->collection->map(function(array $row) { |
|
53
|
|
|
return implode(':', $row); |
|
54
|
|
|
}); |
|
55
|
|
|
|
|
56
|
|
|
$this->assertEquals(['1:hello', '2:world'], $result); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @covers ::getColumnIndex |
|
61
|
|
|
*/ |
|
62
|
|
|
public function testGetColumnIndex() |
|
63
|
|
|
{ |
|
64
|
|
|
$this->assertNull($this->collection->getColumnIndex('helloWorld')); |
|
65
|
|
|
|
|
66
|
|
|
$this->assertEquals(0, $this->collection->getColumnIndex('id')); |
|
67
|
|
|
$this->assertEquals(1, $this->collection->getColumnIndex('name')); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @covers ::getColumns |
|
72
|
|
|
*/ |
|
73
|
|
|
public function testGetColumns() |
|
74
|
|
|
{ |
|
75
|
|
|
$this->assertEquals(['id' => 0, 'name' => 1], $this->collection->getColumns()); |
|
76
|
|
|
$this->assertEquals(['id', 'name'], $this->collection->getColumns(false)); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @covers ::getColumns |
|
81
|
|
|
*/ |
|
82
|
|
|
public function testGetColumnsSameColumnTwice() { |
|
83
|
|
|
$this->collection = new Collection([], ['id', 'id'], 0, 2); |
|
84
|
|
|
$this->assertEquals(['id' => 0, 'id' => 1], $this->collection->getColumns()); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @covers ::getRows |
|
89
|
|
|
*/ |
|
90
|
|
|
public function testGetRows() |
|
91
|
|
|
{ |
|
92
|
|
|
$this->assertEquals($this->rows, $this->collection->getRows()); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* @covers ::count |
|
97
|
|
|
*/ |
|
98
|
|
|
public function testCount() |
|
99
|
|
|
{ |
|
100
|
|
|
$this->assertEquals(count($this->rows), $this->collection->count()); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
public function testIterator() |
|
104
|
|
|
{ |
|
105
|
|
|
$this->assertInstanceOf('Iterator', $this->collection); |
|
106
|
|
|
|
|
107
|
|
|
foreach ($this->collection as $index => $row) { |
|
108
|
|
|
$this->assertEquals($this->rows[$index], $row); |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
|