Skip to content

Namespaced exceptions and model tests #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 26, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@
use MongoDB\Driver\Manager;
use MongoDB\Driver\ReadPreference;
use MongoDB\Driver\WriteConcern;
use MongoDB\Exception\UnexpectedValueException;
use MongoDB\Model\DatabaseInfoIterator;
use MongoDB\Model\DatabaseInfoLegacyIterator;
use stdClass;
use UnexpectedValueException;

class Client
{
Expand Down
26 changes: 13 additions & 13 deletions src/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ public function bulkWrite(array $bulk, array $options = array())
foreach ($bulk as $n => $op) {
foreach ($op as $opname => $args) {
if (!isset($args[0])) {
throw new \InvalidArgumentException(sprintf("Missing argument#1 for '%s' (operation#%d)", $opname, $n));
throw new InvalidArgumentException(sprintf("Missing argument#1 for '%s' (operation#%d)", $opname, $n));
}

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

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

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

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

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

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

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

default:
throw new \InvalidArgumentException(sprintf("Unknown operation type called '%s' (operation#%d)", $opname, $n));
throw new InvalidArgumentException(sprintf("Unknown operation type called '%s' (operation#%d)", $opname, $n));
}
}
}
Expand Down Expand Up @@ -521,7 +521,7 @@ public function findOneAndReplace(array $filter, array $replacement, array $opti
{
$firstKey = key($replacement);
if (isset($firstKey[0]) && $firstKey[0] == '$') {
throw new \InvalidArgumentException("First key in \$replacement must NOT be a \$operator");
throw new InvalidArgumentException("First key in \$replacement must NOT be a \$operator");
}

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

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

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

Expand Down Expand Up @@ -1134,10 +1134,10 @@ final protected function _delete($filter, $limit = 1)
final protected function _generateCommandException($doc)
{
if ($doc["errmsg"]) {
return new Exception($doc["errmsg"]);
return new RuntimeException($doc["errmsg"]);
}
var_dump($doc);
return new \Exception("FIXME: Unknown error");
return new RuntimeException("FIXME: Unknown error");
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
use MongoDB\Driver\ReadPreference;
use MongoDB\Driver\Server;
use MongoDB\Driver\WriteConcern;
use MongoDB\Exception\InvalidArgumentException;
use MongoDB\Model\CollectionInfoIterator;
use MongoDB\Model\CollectionInfoCommandIterator;
use MongoDB\Model\CollectionInfoLegacyIterator;
use InvalidArgumentException;

class Database
{
Expand Down
7 changes: 7 additions & 0 deletions src/Exception/RuntimeException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace MongoDB\Exception;

class RuntimeException extends \RuntimeException implements Exception
{
}
7 changes: 7 additions & 0 deletions src/Exception/UnexpectedValueException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace MongoDB\Exception;

class UnexpectedValueException extends \UnexpectedValueException implements Exception
{
}
6 changes: 3 additions & 3 deletions src/Model/IndexInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ public function __construct(array $info)
}

/**
* Return the index key(s).
* Return the index key.
*
* @return array
*/
public function getKeys()
public function getKey()
{
return (array) $this->info['key'];
}
Expand Down Expand Up @@ -133,7 +133,7 @@ public function offsetExists($key)
*/
public function offsetGet($key)
{
return $this->data[$key];
return $this->info[$key];
}

/**
Expand Down
2 changes: 2 additions & 0 deletions src/Model/IndexInput.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace MongoDB\Model;

use BSON\Serializable;
use MongoDB\Exception\InvalidArgumentException;
use MongoDB\Exception\UnexpectedTypeException;

/**
* Index input model class.
Expand Down
42 changes: 42 additions & 0 deletions tests/Model/CollectionInfoTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace MongoDB\Tests;

use MongoDB\Model\CollectionInfo;
use MongoDB\Tests\TestCase;

class CollectionInfoTest extends TestCase
{
public function testGetName()
{
$info = new CollectionInfo(array('name' => 'foo'));
$this->assertSame('foo', $info->getName());
}

public function testGetOptions()
{
$info = new CollectionInfo(array('name' => 'foo'));
$this->assertSame(array(), $info->getOptions());

$info = new CollectionInfo(array('name' => 'foo', 'options' => array('capped' => true, 'size' => 1048576)));
$this->assertSame(array('capped' => true, 'size' => 1048576), $info->getOptions());
}

public function testCappedCollectionMethods()
{
$info = new CollectionInfo(array('name' => 'foo'));
$this->assertFalse($info->isCapped());
$this->assertNull($info->getCappedMax());
$this->assertNull($info->getCappedSize());

$info = new CollectionInfo(array('name' => 'foo', 'options' => array('capped' => true, 'size' => 1048576)));
$this->assertTrue($info->isCapped());
$this->assertNull($info->getCappedMax());
$this->assertSame(1048576, $info->getCappedSize());

$info = new CollectionInfo(array('name' => 'foo', 'options' => array('capped' => true, 'size' => 1048576, 'max' => 100)));
$this->assertTrue($info->isCapped());
$this->assertSame(100, $info->getCappedMax());
$this->assertSame(1048576, $info->getCappedSize());
}
}
30 changes: 30 additions & 0 deletions tests/Model/DatabaseInfoTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace MongoDB\Tests;

use MongoDB\Model\DatabaseInfo;
use MongoDB\Tests\TestCase;

class DatabaseInfoTest extends TestCase
{
public function testGetName()
{
$info = new DatabaseInfo(array('name' => 'foo'));
$this->assertSame('foo', $info->getName());
}

public function testGetSizeOnDisk()
{
$info = new DatabaseInfo(array('sizeOnDisk' => '1048576'));
$this->assertSame(1048576, $info->getSizeOnDisk());
}

public function testIsEmpty()
{
$info = new DatabaseInfo(array('empty' => false));
$this->assertFalse($info->isEmpty());

$info = new DatabaseInfo(array('empty' => true));
$this->assertTrue($info->isEmpty());
}
}
86 changes: 86 additions & 0 deletions tests/Model/IndexInfoTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php

namespace MongoDB\Tests;

use MongoDB\Model\IndexInfo;
use MongoDB\Tests\TestCase;

class IndexInfoTest extends TestCase
{
public function testBasicIndex()
{
$info = new IndexInfo(array(
'v' => 1,
'key' => array('x' => 1),
'name' => 'x_1',
'ns' => 'foo.bar',
));

$this->assertSame(1, $info->getVersion());
$this->assertSame(array('x' => 1), $info->getKey());
$this->assertSame('x_1', $info->getName());
$this->assertSame('foo.bar', $info->getNamespace());
$this->assertFalse($info->isSparse());
$this->assertFalse($info->isTtl());
$this->assertFalse($info->isUnique());
}

public function testSparseIndex()
{
$info = new IndexInfo(array(
'v' => 1,
'key' => array('y' => 1),
'name' => 'y_sparse',
'ns' => 'foo.bar',
'sparse' => true,
));

$this->assertSame(1, $info->getVersion());
$this->assertSame(array('y' => 1), $info->getKey());
$this->assertSame('y_sparse', $info->getName());
$this->assertSame('foo.bar', $info->getNamespace());
$this->assertTrue($info->isSparse());
$this->assertFalse($info->isTtl());
$this->assertFalse($info->isUnique());
}

public function testUniqueIndex()
{
$info = new IndexInfo(array(
'v' => 1,
'key' => array('z' => 1),
'name' => 'z_unique',
'ns' => 'foo.bar',
'unique' => true,
));

$this->assertSame(1, $info->getVersion());
$this->assertSame(array('z' => 1), $info->getKey());
$this->assertSame('z_unique', $info->getName());
$this->assertSame('foo.bar', $info->getNamespace());
$this->assertFalse($info->isSparse());
$this->assertFalse($info->isTtl());
$this->assertTrue($info->isUnique());
}

public function testTtlIndex()
{
$info = new IndexInfo(array(
'v' => 1,
'key' => array('z' => 1),
'name' => 'z_unique',
'ns' => 'foo.bar',
'expireAfterSeconds' => 100,
));

$this->assertSame(1, $info->getVersion());
$this->assertSame(array('z' => 1), $info->getKey());
$this->assertSame('z_unique', $info->getName());
$this->assertSame('foo.bar', $info->getNamespace());
$this->assertFalse($info->isSparse());
$this->assertTrue($info->isTtl());
$this->assertFalse($info->isUnique());
$this->assertTrue(isset($info['expireAfterSeconds']));
$this->assertSame(100, $info['expireAfterSeconds']);
}
}
Loading