Skip to content

Commit 87932a5

Browse files
committed
Merge pull request #8
2 parents 5b6f54e + dce08e0 commit 87932a5

11 files changed

+285
-19
lines changed

src/Client.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,9 @@
77
use MongoDB\Driver\Manager;
88
use MongoDB\Driver\ReadPreference;
99
use MongoDB\Driver\WriteConcern;
10+
use MongoDB\Exception\UnexpectedValueException;
1011
use MongoDB\Model\DatabaseInfoIterator;
1112
use MongoDB\Model\DatabaseInfoLegacyIterator;
12-
use stdClass;
13-
use UnexpectedValueException;
1413

1514
class Client
1615
{

src/Collection.php

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ public function bulkWrite(array $bulk, array $options = array())
163163
foreach ($bulk as $n => $op) {
164164
foreach ($op as $opname => $args) {
165165
if (!isset($args[0])) {
166-
throw new \InvalidArgumentException(sprintf("Missing argument#1 for '%s' (operation#%d)", $opname, $n));
166+
throw new InvalidArgumentException(sprintf("Missing argument#1 for '%s' (operation#%d)", $opname, $n));
167167
}
168168

169169
switch ($opname) {
@@ -173,7 +173,7 @@ public function bulkWrite(array $bulk, array $options = array())
173173

174174
case "updateMany":
175175
if (!isset($args[1])) {
176-
throw new \InvalidArgumentException(sprintf("Missing argument#2 for '%s' (operation#%d)", $opname, $n));
176+
throw new InvalidArgumentException(sprintf("Missing argument#2 for '%s' (operation#%d)", $opname, $n));
177177
}
178178
$options = array_merge($this->getWriteOptions(), isset($args[2]) ? $args[2] : array(), array("limit" => 0));
179179

@@ -182,25 +182,25 @@ public function bulkWrite(array $bulk, array $options = array())
182182

183183
case "updateOne":
184184
if (!isset($args[1])) {
185-
throw new \InvalidArgumentException(sprintf("Missing argument#2 for '%s' (operation#%d)", $opname, $n));
185+
throw new InvalidArgumentException(sprintf("Missing argument#2 for '%s' (operation#%d)", $opname, $n));
186186
}
187187
$options = array_merge($this->getWriteOptions(), isset($args[2]) ? $args[2] : array(), array("limit" => 1));
188188
$firstKey = key($args[1]);
189189
if (!isset($firstKey[0]) || $firstKey[0] != '$') {
190-
throw new \InvalidArgumentException("First key in \$update must be a \$operator");
190+
throw new InvalidArgumentException("First key in \$update must be a \$operator");
191191
}
192192

193193
$bulk->update($args[0], $args[1], $options);
194194
break;
195195

196196
case "replaceOne":
197197
if (!isset($args[1])) {
198-
throw new \InvalidArgumentException(sprintf("Missing argument#2 for '%s' (operation#%d)", $opname, $n));
198+
throw new InvalidArgumentException(sprintf("Missing argument#2 for '%s' (operation#%d)", $opname, $n));
199199
}
200200
$options = array_merge($this->getWriteOptions(), isset($args[2]) ? $args[2] : array(), array("limit" => 1));
201201
$firstKey = key($args[1]);
202202
if (isset($firstKey[0]) && $firstKey[0] == '$') {
203-
throw new \InvalidArgumentException("First key in \$update must NOT be a \$operator");
203+
throw new InvalidArgumentException("First key in \$update must NOT be a \$operator");
204204
}
205205

206206
$bulk->update($args[0], $args[1], $options);
@@ -217,7 +217,7 @@ public function bulkWrite(array $bulk, array $options = array())
217217
break;
218218

219219
default:
220-
throw new \InvalidArgumentException(sprintf("Unknown operation type called '%s' (operation#%d)", $opname, $n));
220+
throw new InvalidArgumentException(sprintf("Unknown operation type called '%s' (operation#%d)", $opname, $n));
221221
}
222222
}
223223
}
@@ -521,7 +521,7 @@ public function findOneAndReplace(array $filter, array $replacement, array $opti
521521
{
522522
$firstKey = key($replacement);
523523
if (isset($firstKey[0]) && $firstKey[0] == '$') {
524-
throw new \InvalidArgumentException("First key in \$replacement must NOT be a \$operator");
524+
throw new InvalidArgumentException("First key in \$replacement must NOT be a \$operator");
525525
}
526526

527527
$options = array_merge($this->getFindOneAndReplaceOptions(), $options);
@@ -559,7 +559,7 @@ public function findOneAndUpdate(array $filter, array $update, array $options =
559559
{
560560
$firstKey = key($update);
561561
if (!isset($firstKey[0]) || $firstKey[0] != '$') {
562-
throw new \InvalidArgumentException("First key in \$update must be a \$operator");
562+
throw new InvalidArgumentException("First key in \$update must be a \$operator");
563563
}
564564

565565
$options = array_merge($this->getFindOneAndUpdateOptions(), $options);
@@ -1036,7 +1036,7 @@ public function replaceOne(array $filter, array $update, array $options = array(
10361036
{
10371037
$firstKey = key($update);
10381038
if (isset($firstKey[0]) && $firstKey[0] == '$') {
1039-
throw new \InvalidArgumentException("First key in \$update must NOT be a \$operator");
1039+
throw new InvalidArgumentException("First key in \$update must NOT be a \$operator");
10401040
}
10411041
$wr = $this->_update($filter, $update, $options);
10421042

@@ -1078,7 +1078,7 @@ public function updateOne(array $filter, array $update, array $options = array()
10781078
{
10791079
$firstKey = key($update);
10801080
if (!isset($firstKey[0]) || $firstKey[0] != '$') {
1081-
throw new \InvalidArgumentException("First key in \$update must be a \$operator");
1081+
throw new InvalidArgumentException("First key in \$update must be a \$operator");
10821082
}
10831083
$wr = $this->_update($filter, $update, $options);
10841084

@@ -1134,10 +1134,10 @@ final protected function _delete($filter, $limit = 1)
11341134
final protected function _generateCommandException($doc)
11351135
{
11361136
if ($doc["errmsg"]) {
1137-
return new Exception($doc["errmsg"]);
1137+
return new RuntimeException($doc["errmsg"]);
11381138
}
11391139
var_dump($doc);
1140-
return new \Exception("FIXME: Unknown error");
1140+
return new RuntimeException("FIXME: Unknown error");
11411141
}
11421142

11431143
/**

src/Database.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@
1010
use MongoDB\Driver\ReadPreference;
1111
use MongoDB\Driver\Server;
1212
use MongoDB\Driver\WriteConcern;
13+
use MongoDB\Exception\InvalidArgumentException;
1314
use MongoDB\Model\CollectionInfoIterator;
1415
use MongoDB\Model\CollectionInfoCommandIterator;
1516
use MongoDB\Model\CollectionInfoLegacyIterator;
16-
use InvalidArgumentException;
1717

1818
class Database
1919
{

src/Exception/RuntimeException.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace MongoDB\Exception;
4+
5+
class RuntimeException extends \RuntimeException implements Exception
6+
{
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace MongoDB\Exception;
4+
5+
class UnexpectedValueException extends \UnexpectedValueException implements Exception
6+
{
7+
}

src/Model/IndexInfo.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,11 @@ public function __construct(array $info)
3535
}
3636

3737
/**
38-
* Return the index key(s).
38+
* Return the index key.
3939
*
4040
* @return array
4141
*/
42-
public function getKeys()
42+
public function getKey()
4343
{
4444
return (array) $this->info['key'];
4545
}
@@ -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
/**

src/Model/IndexInput.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
namespace MongoDB\Model;
44

55
use BSON\Serializable;
6+
use MongoDB\Exception\InvalidArgumentException;
7+
use MongoDB\Exception\UnexpectedTypeException;
68

79
/**
810
* Index input model class.

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+
}

0 commit comments

Comments
 (0)