Skip to content

Commit 496779f

Browse files
committed
PHPLIB-46, PHPLIB-63, PHPLIB-69: Functional tests for index methods
1 parent 48f6d1e commit 496779f

File tree

1 file changed

+163
-0
lines changed

1 file changed

+163
-0
lines changed

tests/CollectionFunctionalTest.php

Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
use MongoDB\Collection;
66
use MongoDB\Driver\Manager;
7+
use MongoDB\Model\IndexInfo;
8+
use InvalidArgumentException;
79

810
class CollectionFunctionalTest extends FunctionalTestCase
911
{
@@ -56,4 +58,165 @@ function testInsertAndRetrieve()
5658
}
5759
$this->assertEquals(0, $n);
5860
}
61+
62+
public function testCreateIndex()
63+
{
64+
$that = $this;
65+
66+
$this->assertSame('x_1', $this->collection->createIndex(array('x' => 1), array('sparse' => true, 'unique' => true)));
67+
$this->assertIndexExists('x_1', function(IndexInfo $info) use ($that) {
68+
$that->assertTrue($info->isSparse());
69+
$that->assertTrue($info->isUnique());
70+
$that->assertFalse($info->isTtl());
71+
});
72+
73+
$this->assertSame('y_-1_z_1', $this->collection->createIndex(array('y' => -1, 'z' => 1)));
74+
$this->assertIndexExists('y_-1_z_1', function(IndexInfo $info) use ($that) {
75+
$that->assertFalse($info->isSparse());
76+
$that->assertFalse($info->isUnique());
77+
$that->assertFalse($info->isTtl());
78+
});
79+
80+
$this->assertSame('g_2dsphere_z_1', $this->collection->createIndex(array('g' => '2dsphere', 'z' => 1)));
81+
$this->assertIndexExists('g_2dsphere_z_1', function(IndexInfo $info) use ($that) {
82+
$that->assertFalse($info->isSparse());
83+
$that->assertFalse($info->isUnique());
84+
$that->assertFalse($info->isTtl());
85+
});
86+
87+
$this->assertSame('t_1', $this->collection->createIndex(array('t' => 1), array('expireAfterSeconds' => 0)));
88+
$this->assertIndexExists('t_1', function(IndexInfo $info) use ($that) {
89+
$that->assertFalse($info->isSparse());
90+
$that->assertFalse($info->isUnique());
91+
$that->assertTrue($info->isTtl());
92+
});
93+
}
94+
95+
public function testCreateIndexes()
96+
{
97+
$that = $this;
98+
99+
$expectedNames = array('x_1', 'y_-1_z_1', 'g_2dsphere_z_1', 't_1');
100+
101+
$indexes = array(
102+
array('key' => array('x' => 1), 'sparse' => true, 'unique' => true),
103+
array('key' => array('y' => -1, 'z' => 1)),
104+
array('key' => array('g' => '2dsphere', 'z' => 1)),
105+
array('key' => array('t' => 1), 'expireAfterSeconds' => 0),
106+
);
107+
108+
$this->assertSame($expectedNames, $this->collection->createIndexes($indexes));
109+
110+
$this->assertIndexExists('x_1', function(IndexInfo $info) use ($that) {
111+
$that->assertTrue($info->isSparse());
112+
$that->assertTrue($info->isUnique());
113+
$that->assertFalse($info->isTtl());
114+
});
115+
116+
$this->assertIndexExists('y_-1_z_1', function(IndexInfo $info) use ($that) {
117+
$that->assertFalse($info->isSparse());
118+
$that->assertFalse($info->isUnique());
119+
$that->assertFalse($info->isTtl());
120+
});
121+
122+
$this->assertIndexExists('g_2dsphere_z_1', function(IndexInfo $info) use ($that) {
123+
$that->assertFalse($info->isSparse());
124+
$that->assertFalse($info->isUnique());
125+
$that->assertFalse($info->isTtl());
126+
});
127+
128+
$this->assertIndexExists('t_1', function(IndexInfo $info) use ($that) {
129+
$that->assertFalse($info->isSparse());
130+
$that->assertFalse($info->isUnique());
131+
$that->assertTrue($info->isTtl());
132+
});
133+
}
134+
135+
public function testDropIndex()
136+
{
137+
$this->assertSame('x_1', $this->collection->createIndex(array('x' => 1)));
138+
$this->assertIndexExists('x_1');
139+
$this->assertCommandSucceeded($this->collection->dropIndex('x_1'));
140+
141+
foreach ($this->collection->listIndexes() as $index) {
142+
if ($index->getName() === 'x_1') {
143+
$this->fail('The "x_1" index should have been deleted');
144+
}
145+
}
146+
}
147+
148+
/**
149+
* @expectedException MongoDB\Exception\InvalidArgumentException
150+
*/
151+
public function testDropIndexShouldNotAllowWildcardCharacter()
152+
{
153+
$this->assertSame('x_1', $this->collection->createIndex(array('x' => 1)));
154+
$this->assertIndexExists('x_1');
155+
$this->collection->dropIndex('*');
156+
}
157+
158+
public function testDropIndexes()
159+
{
160+
$this->assertSame('x_1', $this->collection->createIndex(array('x' => 1)));
161+
$this->assertSame('y_1', $this->collection->createIndex(array('y' => 1)));
162+
$this->assertIndexExists('x_1');
163+
$this->assertIndexExists('y_1');
164+
$this->assertCommandSucceeded($this->collection->dropIndexes());
165+
166+
foreach ($this->collection->listIndexes() as $index) {
167+
if ($index->getName() === 'x_1') {
168+
$this->fail('The "x_1" index should have been deleted');
169+
}
170+
171+
if ($index->getName() === 'y_1') {
172+
$this->fail('The "y_1" index should have been deleted');
173+
}
174+
}
175+
}
176+
177+
public function testListIndexes()
178+
{
179+
$this->assertSame('x_1', $this->collection->createIndex(array('x' => 1)));
180+
181+
$indexes = $this->collection->listIndexes();
182+
$this->assertInstanceOf('MongoDB\Model\IndexInfoIterator', $indexes);
183+
184+
foreach ($indexes as $index) {
185+
$this->assertInstanceOf('MongoDB\Model\IndexInfo', $index);
186+
}
187+
}
188+
189+
/**
190+
* Asserts that an index with the given name exists for the collection.
191+
*
192+
* An optional $callback may be provided, which should take an IndexInfo
193+
* argument as its first and only parameter. If an IndexInfo matching the
194+
* given name is found, it will be passed to the callback, which may perform
195+
* additional assertions.
196+
*
197+
* @param callable $callback
198+
*/
199+
private function assertIndexExists($indexName, $callback = null)
200+
{
201+
if ($callback !== null && ! is_callable($callback)) {
202+
throw new InvalidArgumentException('$callback is not a callable');
203+
}
204+
205+
$indexes = $this->collection->listIndexes();
206+
207+
$foundIndex = null;
208+
209+
foreach ($indexes as $index) {
210+
if ($index->getName() === $indexName) {
211+
$foundIndex = $index;
212+
break;
213+
}
214+
}
215+
216+
$this->assertNotNull($foundIndex, sprintf('Found %s index for the collection', $indexName));
217+
218+
if ($callback !== null) {
219+
call_user_func($callback, $foundIndex);
220+
}
221+
}
59222
}

0 commit comments

Comments
 (0)