This repository has been archived by the owner on Nov 11, 2020. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 189
/
Copy pathArrayIteratorTest.php
113 lines (80 loc) · 3.16 KB
/
ArrayIteratorTest.php
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
<?php
namespace Doctrine\MongoDB\Tests;
use Doctrine\MongoDB\ArrayIterator;
class ArrayIteratorTest extends TestCase
{
public function testArrayAccess()
{
$arrayIterator = new ArrayIterator();
$this->assertInstanceOf('ArrayAccess', $arrayIterator);
$this->assertFalse($arrayIterator->offsetExists(0));
$this->assertNull($arrayIterator['undefinedOffset']);
$arrayIterator[0] = null;
$this->assertFalse($arrayIterator->offsetExists(0));
$this->assertArrayNotHasKey(0, $arrayIterator);
$this->assertNull($arrayIterator[0]);
$arrayIterator[] = true;
$this->assertTrue($arrayIterator->offsetExists(1));
$this->assertArrayHasKey(1, $arrayIterator);
$this->assertTrue($arrayIterator[1]);
unset($arrayIterator[0]);
$this->assertFalse($arrayIterator->offsetExists(0));
$this->assertEmpty($arrayIterator[0]);
}
public function testCount()
{
$this->assertInstanceOf('Countable', new ArrayIterator());
$this->assertCount(0, new ArrayIterator());
$this->assertCount(1, new ArrayIterator([1]));
$this->assertCount(2, new ArrayIterator([1, 2]));
}
public function testGetSingleResult()
{
$arrayIterator = new ArrayIterator([1, 2, 3]);
$this->assertSame(1, $arrayIterator->getSingleResult());
$arrayIterator->next();
$arrayIterator->next();
$arrayIterator->next();
$this->assertSame(1, $arrayIterator->getSingleResult());
}
public function testGetSingleResultWithEmptyArray()
{
$arrayIterator = new ArrayIterator();
$this->assertNull($arrayIterator->getSingleResult());
}
public function testIteration()
{
$arrayIterator = new ArrayIterator([1, 2, 3]);
$this->assertInstanceOf('Iterator', $arrayIterator);
$this->assertSame(0, $arrayIterator->key());
$this->assertSame(1, $arrayIterator->current());
$this->assertTrue($arrayIterator->valid());
$arrayIterator->next();
$this->assertSame(1, $arrayIterator->key());
$this->assertSame(2, $arrayIterator->current());
$this->assertTrue($arrayIterator->valid());
$arrayIterator->next();
$this->assertSame(2, $arrayIterator->key());
$this->assertSame(3, $arrayIterator->current());
$this->assertTrue($arrayIterator->valid());
$arrayIterator->next();
$this->assertNull($arrayIterator->key());
$this->assertFalse($arrayIterator->current());
$this->assertFalse($arrayIterator->valid());
$arrayIterator->rewind();
$this->assertSame(0, $arrayIterator->key());
$this->assertSame(1, $arrayIterator->current());
$this->assertTrue($arrayIterator->valid());
}
public function testToArray()
{
$arrayIterator = new ArrayIterator([1, 2, 3]);
$this->assertSame([1, 2, 3], $arrayIterator->toArray());
}
public function testFirstAndLast()
{
$arrayIterator = new ArrayIterator([1, 2, 3]);
$this->assertSame(1, $arrayIterator->first());
$this->assertSame(3, $arrayIterator->last());
}
}