-
Notifications
You must be signed in to change notification settings - Fork 418
Expand file tree
/
Copy pathCollectionTest.php
More file actions
102 lines (84 loc) · 2.44 KB
/
CollectionTest.php
File metadata and controls
102 lines (84 loc) · 2.44 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
<?php
declare(strict_types=1);
namespace tests;
use flight\util\Collection;
use PHPUnit\Framework\TestCase;
class CollectionTest extends TestCase
{
private Collection $collection;
protected function setUp(): void
{
$this->collection = new Collection(['a' => 1, 'b' => 2]);
}
// Get an item
public function testGet(): void
{
$this->assertEquals(1, $this->collection->a);
}
// Set an item
public function testSet(): void
{
$this->collection->c = 3;
$this->assertEquals(3, $this->collection->c);
}
// Check if an item exists
public function testExists(): void
{
$this->assertTrue(isset($this->collection->a));
}
// Unset an item
public function testUnset(): void
{
unset($this->collection->a);
$this->assertFalse(isset($this->collection->a));
}
// Count items
public function testCount(): void
{
$this->assertEquals(2, count($this->collection));
}
// Iterate through items
public function testIterate(): void
{
$items = [];
foreach ($this->collection as $key => $value) {
$items[$key] = $value;
}
$this->assertEquals(['a' => 1, 'b' => 2], $items);
}
public function testJsonSerialize(): void
{
$this->assertEquals(['a' => 1, 'b' => 2], $this->collection->jsonSerialize());
}
public function testOffsetSetWithNullOffset(): void
{
$this->collection->offsetSet(null, 3);
$this->assertEquals(3, $this->collection->offsetGet(0));
}
public function testOffsetExists(): void
{
$this->collection->a = 1;
$this->assertTrue($this->collection->offsetExists('a'));
}
public function testOffsetUnset(): void
{
$this->collection->a = 1;
$this->assertTrue($this->collection->offsetExists('a'));
$this->collection->offsetUnset('a');
$this->assertFalse($this->collection->offsetExists('a'));
}
public function testKeys(): void
{
$this->collection->a = 1;
$this->collection->b = 2;
$this->assertEquals(['a', 'b'], $this->collection->keys());
}
public function testClear(): void
{
$this->collection->a = 1;
$this->collection->b = 2;
$this->assertEquals(['a', 'b'], $this->collection->keys());
$this->collection->clear();
$this->assertEquals(0, $this->collection->count());
}
}