Skip to content

Commit dce08e0

Browse files
committed
PHPLIB-75: Unit tests for database, collection, and index models
1 parent e90d518 commit dce08e0

File tree

5 files changed

+252
-1
lines changed

5 files changed

+252
-1
lines changed

src/Model/IndexInfo.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ public function offsetExists($key)
133133
*/
134134
public function offsetGet($key)
135135
{
136-
return $this->data[$key];
136+
return $this->info[$key];
137137
}
138138

139139
/**

tests/Model/CollectionInfoTest.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
namespace MongoDB\Tests;
4+
5+
use MongoDB\Model\CollectionInfo;
6+
use MongoDB\Tests\TestCase;
7+
8+
class CollectionInfoTest extends TestCase
9+
{
10+
public function testGetName()
11+
{
12+
$info = new CollectionInfo(array('name' => 'foo'));
13+
$this->assertSame('foo', $info->getName());
14+
}
15+
16+
public function testGetOptions()
17+
{
18+
$info = new CollectionInfo(array('name' => 'foo'));
19+
$this->assertSame(array(), $info->getOptions());
20+
21+
$info = new CollectionInfo(array('name' => 'foo', 'options' => array('capped' => true, 'size' => 1048576)));
22+
$this->assertSame(array('capped' => true, 'size' => 1048576), $info->getOptions());
23+
}
24+
25+
public function testCappedCollectionMethods()
26+
{
27+
$info = new CollectionInfo(array('name' => 'foo'));
28+
$this->assertFalse($info->isCapped());
29+
$this->assertNull($info->getCappedMax());
30+
$this->assertNull($info->getCappedSize());
31+
32+
$info = new CollectionInfo(array('name' => 'foo', 'options' => array('capped' => true, 'size' => 1048576)));
33+
$this->assertTrue($info->isCapped());
34+
$this->assertNull($info->getCappedMax());
35+
$this->assertSame(1048576, $info->getCappedSize());
36+
37+
$info = new CollectionInfo(array('name' => 'foo', 'options' => array('capped' => true, 'size' => 1048576, 'max' => 100)));
38+
$this->assertTrue($info->isCapped());
39+
$this->assertSame(100, $info->getCappedMax());
40+
$this->assertSame(1048576, $info->getCappedSize());
41+
}
42+
}

tests/Model/DatabaseInfoTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace MongoDB\Tests;
4+
5+
use MongoDB\Model\DatabaseInfo;
6+
use MongoDB\Tests\TestCase;
7+
8+
class DatabaseInfoTest extends TestCase
9+
{
10+
public function testGetName()
11+
{
12+
$info = new DatabaseInfo(array('name' => 'foo'));
13+
$this->assertSame('foo', $info->getName());
14+
}
15+
16+
public function testGetSizeOnDisk()
17+
{
18+
$info = new DatabaseInfo(array('sizeOnDisk' => '1048576'));
19+
$this->assertSame(1048576, $info->getSizeOnDisk());
20+
}
21+
22+
public function testIsEmpty()
23+
{
24+
$info = new DatabaseInfo(array('empty' => false));
25+
$this->assertFalse($info->isEmpty());
26+
27+
$info = new DatabaseInfo(array('empty' => true));
28+
$this->assertTrue($info->isEmpty());
29+
}
30+
}

tests/Model/IndexInfoTest.php

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<?php
2+
3+
namespace MongoDB\Tests;
4+
5+
use MongoDB\Model\IndexInfo;
6+
use MongoDB\Tests\TestCase;
7+
8+
class IndexInfoTest extends TestCase
9+
{
10+
public function testBasicIndex()
11+
{
12+
$info = new IndexInfo(array(
13+
'v' => 1,
14+
'key' => array('x' => 1),
15+
'name' => 'x_1',
16+
'ns' => 'foo.bar',
17+
));
18+
19+
$this->assertSame(1, $info->getVersion());
20+
$this->assertSame(array('x' => 1), $info->getKey());
21+
$this->assertSame('x_1', $info->getName());
22+
$this->assertSame('foo.bar', $info->getNamespace());
23+
$this->assertFalse($info->isSparse());
24+
$this->assertFalse($info->isTtl());
25+
$this->assertFalse($info->isUnique());
26+
}
27+
28+
public function testSparseIndex()
29+
{
30+
$info = new IndexInfo(array(
31+
'v' => 1,
32+
'key' => array('y' => 1),
33+
'name' => 'y_sparse',
34+
'ns' => 'foo.bar',
35+
'sparse' => true,
36+
));
37+
38+
$this->assertSame(1, $info->getVersion());
39+
$this->assertSame(array('y' => 1), $info->getKey());
40+
$this->assertSame('y_sparse', $info->getName());
41+
$this->assertSame('foo.bar', $info->getNamespace());
42+
$this->assertTrue($info->isSparse());
43+
$this->assertFalse($info->isTtl());
44+
$this->assertFalse($info->isUnique());
45+
}
46+
47+
public function testUniqueIndex()
48+
{
49+
$info = new IndexInfo(array(
50+
'v' => 1,
51+
'key' => array('z' => 1),
52+
'name' => 'z_unique',
53+
'ns' => 'foo.bar',
54+
'unique' => true,
55+
));
56+
57+
$this->assertSame(1, $info->getVersion());
58+
$this->assertSame(array('z' => 1), $info->getKey());
59+
$this->assertSame('z_unique', $info->getName());
60+
$this->assertSame('foo.bar', $info->getNamespace());
61+
$this->assertFalse($info->isSparse());
62+
$this->assertFalse($info->isTtl());
63+
$this->assertTrue($info->isUnique());
64+
}
65+
66+
public function testTtlIndex()
67+
{
68+
$info = new IndexInfo(array(
69+
'v' => 1,
70+
'key' => array('z' => 1),
71+
'name' => 'z_unique',
72+
'ns' => 'foo.bar',
73+
'expireAfterSeconds' => 100,
74+
));
75+
76+
$this->assertSame(1, $info->getVersion());
77+
$this->assertSame(array('z' => 1), $info->getKey());
78+
$this->assertSame('z_unique', $info->getName());
79+
$this->assertSame('foo.bar', $info->getNamespace());
80+
$this->assertFalse($info->isSparse());
81+
$this->assertTrue($info->isTtl());
82+
$this->assertFalse($info->isUnique());
83+
$this->assertTrue(isset($info['expireAfterSeconds']));
84+
$this->assertSame(100, $info['expireAfterSeconds']);
85+
}
86+
}

tests/Model/IndexInputTest.php

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
<?php
2+
3+
namespace MongoDB\Tests;
4+
5+
use MongoDB\Model\IndexInput;
6+
use MongoDB\Tests\TestCase;
7+
8+
class IndexInputTest extends TestCase
9+
{
10+
/**
11+
* @expectedException MongoDB\Exception\InvalidArgumentException
12+
*/
13+
public function testConstructorShouldRequireKey()
14+
{
15+
new IndexInput(array());
16+
}
17+
18+
/**
19+
* @expectedException MongoDB\Exception\UnexpectedTypeException
20+
*/
21+
public function testConstructorShouldRequireKeyToBeArrayOrObject()
22+
{
23+
new IndexInput(array('key' => 'foo'));
24+
}
25+
26+
/**
27+
* @expectedException MongoDB\Exception\UnexpectedTypeException
28+
*/
29+
public function testConstructorShouldRequireKeyOrderToBeScalar()
30+
{
31+
new IndexInput(array('key' => array('x' => array())));
32+
}
33+
34+
/**
35+
* @expectedException MongoDB\Exception\InvalidArgumentException
36+
*/
37+
public function testConstructorShouldRequireNamespace()
38+
{
39+
new IndexInput(array('key' => array('x' => 1)));
40+
}
41+
42+
/**
43+
* @expectedException MongoDB\Exception\UnexpectedTypeException
44+
*/
45+
public function testConstructorShouldRequireNamespaceToBeString()
46+
{
47+
new IndexInput(array('key' => array('x' => 1), 'ns' => 1));
48+
}
49+
50+
/**
51+
* @expectedException MongoDB\Exception\UnexpectedTypeException
52+
*/
53+
public function testConstructorShouldRequireNameToBeString()
54+
{
55+
new IndexInput(array('key' => array('x' => 1), 'ns' => 'foo.bar', 'name' => 1));
56+
}
57+
58+
/**
59+
* @dataProvider provideExpectedNameAndKey
60+
*/
61+
public function testNameGeneration($expectedName, array $key)
62+
{
63+
$this->assertSame($expectedName, (string) new IndexInput(array('key' => $key, 'ns' => 'foo.bar')));
64+
}
65+
66+
public function provideExpectedNameAndKey()
67+
{
68+
return array(
69+
array('x_1', array('x' => 1)),
70+
array('x_1_y_-1', array('x' => 1, 'y' => -1)),
71+
array('loc_2dsphere', array('loc' => '2dsphere')),
72+
array('loc_2dsphere_x_1', array('loc' => '2dsphere', 'x' => 1)),
73+
array('doc_text', array('doc' => 'text')),
74+
);
75+
}
76+
77+
public function testBsonSerialization()
78+
{
79+
$expected = array(
80+
'key' => array('x' => 1),
81+
'ns' => 'foo.bar',
82+
'name' => 'x_1',
83+
);
84+
85+
$indexInput = new IndexInput(array(
86+
'key' => array('x' => 1),
87+
'ns' => 'foo.bar',
88+
));
89+
90+
$this->assertInstanceOf('BSON\Serializable', $indexInput);
91+
$this->assertEquals($expected, $indexInput->bsonSerialize());
92+
}
93+
}

0 commit comments

Comments
 (0)