|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* File ArrayAccessTraitTest.php |
|
4
|
|
|
* |
|
5
|
|
|
* @author Edward Pfremmer <[email protected]> |
|
6
|
|
|
*/ |
|
7
|
|
|
namespace Epfremme\Tests\Collection\Traits; |
|
8
|
|
|
|
|
9
|
|
|
use ArrayAccess; |
|
10
|
|
|
use Epfremme\Collection\BaseCollection; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Class ArrayAccessTraitTest |
|
14
|
|
|
* |
|
15
|
|
|
* @package Epfremme\Tests\Collection\Traits |
|
16
|
|
|
*/ |
|
17
|
|
|
class ArrayAccessTraitTest extends \PHPUnit_Framework_TestCase |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* @var BaseCollection |
|
21
|
|
|
*/ |
|
22
|
|
|
private $collection; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* {@inheritdoc} |
|
26
|
|
|
*/ |
|
27
|
|
|
public function setUp() |
|
28
|
|
|
{ |
|
29
|
|
|
parent::setUp(); |
|
30
|
|
|
|
|
31
|
|
|
$this->collection = new BaseCollection([1, 2, 3, 'a' => 4, 'b' => 5]); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Verify collection implements ArrayAccess interface |
|
36
|
|
|
* |
|
37
|
|
|
* @return void |
|
38
|
|
|
*/ |
|
39
|
|
|
public function testImplementsInterface() |
|
40
|
|
|
{ |
|
41
|
|
|
$this->assertInstanceOf(ArrayAccess::class, $this->collection); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Test offset exists method |
|
46
|
|
|
* |
|
47
|
|
|
* @reutrn void |
|
48
|
|
|
*/ |
|
49
|
|
|
public function testOffsetExists() |
|
50
|
|
|
{ |
|
51
|
|
|
$this->assertTrue($this->collection->offsetExists(0)); |
|
52
|
|
|
$this->assertTrue($this->collection->offsetExists('a')); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Test offset get returns correct value |
|
57
|
|
|
* |
|
58
|
|
|
* @return void |
|
59
|
|
|
*/ |
|
60
|
|
|
public function testOffsetGet() |
|
61
|
|
|
{ |
|
62
|
|
|
$this->assertEquals(1, $this->collection->offsetGet(0)); |
|
63
|
|
|
$this->assertEquals(4, $this->collection->offsetGet('a')); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Test invalid offset get exception |
|
68
|
|
|
* |
|
69
|
|
|
* @expectedException \InvalidArgumentException |
|
70
|
|
|
* @return void |
|
71
|
|
|
*/ |
|
72
|
|
|
public function testOffsetGetException() |
|
73
|
|
|
{ |
|
74
|
|
|
$this->collection->offsetGet('noop'); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Test offset set sets both new & existing values |
|
79
|
|
|
* |
|
80
|
|
|
* @return void |
|
81
|
|
|
*/ |
|
82
|
|
|
public function testOffsetSet() |
|
83
|
|
|
{ |
|
84
|
|
|
$this->collection->offsetSet('new', 7); |
|
85
|
|
|
$this->assertEquals(7, $this->collection->offsetGet('new')); |
|
86
|
|
|
|
|
87
|
|
|
$this->assertEquals(2, $this->collection->offsetGet(1)); |
|
88
|
|
|
$this->collection->offsetSet(1, 9); |
|
89
|
|
|
$this->assertEquals(9, $this->collection->offsetGet(1)); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Test offset unset method removes elements from collection |
|
94
|
|
|
* |
|
95
|
|
|
* @return void |
|
96
|
|
|
*/ |
|
97
|
|
|
public function testOffsetUnset() |
|
98
|
|
|
{ |
|
99
|
|
|
$this->assertTrue($this->collection->offsetExists(2)); |
|
100
|
|
|
$this->assertAttributeCount(5, 'elements', $this->collection); |
|
101
|
|
|
$this->collection->offsetUnset(2); |
|
102
|
|
|
$this->assertFalse($this->collection->offsetExists(2)); |
|
103
|
|
|
$this->assertAttributeCount(4, 'elements', $this->collection); |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|