DictionaryTest.php
4.98 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
<?php
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
namespace Easy\Tests\Collections;
use ArrayObject;
use Easy\Collections\Dictionary;
use InvalidArgumentException;
use OutOfBoundsException;
use stdClass;
/**
* Description of CollectionTest
*
* @author italo
*/
class DictionaryTest extends CollectionsTestCase
{
/**
* @var Dictionary
*/
private $coll;
protected function setUp()
{
$this->coll = new Dictionary();
}
public function testNewInstanceWithArray()
{
$this->assertNotNull(new Dictionary(array(
'key1' => 'value1',
'key2' => array(
'key21' => 'value21',
'key22' => array(
'key221' => 'value221',
)
),
'key3' => 'value3',
'key4' => 'value4'
)));
}
public function testNewInstanceWithTraversable()
{
$traversable = new ArrayObject(array(
'key1' => 'value1',
'key2' => 'value2',
'key3' => 'value3',
'key4' => 'value4'
));
$this->assertNotNull(new Dictionary($traversable));
}
public function testNewInstance()
{
$this->assertNotNull($this->coll);
}
/**
* @expectedException InvalidArgumentException
*/
public function testInvalidElementsToInstanciate()
{
$coll = new Dictionary();
$coll->addAll('string');
}
public function testAddAllWithSomeValues()
{
$arrayList = new Dictionary();
$arrayList->add('key1', 'value1')
->add('key2', 'value2');
$secoundArrayList = new Dictionary();
$secoundArrayList->add('key3', 'value3')
->add('key4', 'value4');
$arrayList->addAll($secoundArrayList);
$this->assertEquals(array(
'key1' => 'value1',
'key2' => 'value2',
'key3' => 'value3',
'key4' => 'value4'
), $arrayList->toArray());
}
public function testAddItem()
{
$this->coll->add('key', 'testing');
$this->assertTrue(is_string((string) $this->coll));
}
/**
* @expectedException InvalidArgumentException
*/
public function testAddDuplicateKey()
{
$this->coll->add('key', 'testing');
$this->coll->add('key', 'testing2');
}
public function testSetItem()
{
$this->coll->set('key', 'testing');
$this->assertTrue(is_string((string) $this->coll));
}
public function testGetItem()
{
$this->coll->set('keyOne', 'testing');
$this->assertEquals('testing', $this->coll->get('keyOne'));
}
/**
* @expectedException OutOfBoundsException
*/
public function testGetInvalidItem()
{
$this->coll->set('keyOne', 'testing');
$this->coll->get('keyTwo');
}
public function testGetKeys()
{
$this->coll->add('keyOne', 'testing1');
$this->coll->add('keyTwo', 'testing2');
$this->coll->add('keyThree', 'testing3');
$this->assertEquals(array('keyOne', 'keyTwo', 'keyThree'), $this->coll->toKeysArrays());
}
/**
* @expectedException InvalidArgumentException
*/
public function testSetNullKey()
{
$this->coll->set(null, 'testing');
}
public function testTryGetSuccess()
{
$this->coll->add('key', 'testing');
$value = $this->coll->tryGet('key');
$this->assertEquals('testing', $value);
}
public function testTryGetError()
{
$this->coll->add('key', 'testing');
$value = $this->coll->tryGet('key2');
$this->assertNull($value);
}
public function testTryGetDefaultValue()
{
$this->coll->add('key', 'testing');
$value = $this->coll->tryGet('key2', 'testingValue');
$this->assertEquals('testingValue', $value);
}
/**
* @expectedException InvalidArgumentException
*/
public function testRemovingNonExistentEntryReturnsNull()
{
$this->assertEquals(null, $this->coll->remove('testing_does_not_exist'));
}
public function testArrayAccess()
{
$this->coll['keyOne'] = 'one';
$this->coll['keyTwo'] = 'two';
$this->assertEquals($this->coll['keyOne'], 'one');
$this->assertEquals($this->coll['keyTwo'], 'two');
unset($this->coll['keyOne']);
$this->assertEquals($this->coll->count(), 1);
$this->assertTrue(isset($this->coll['keyTwo']));
}
public function testToList()
{
$this->coll->addAll(array(1, 2, 3, 4));
$map = $this->coll->toList();
$this->assertInstanceOf('Easy\\Collections\\ArrayList', $map);
}
public function testSetObjectKey()
{
$class = new stdClass();
$this->coll->set($class, 'testing');
$this->assertEquals($this->coll->get($class), 'testing');
}
}