Commit 4f7349e9 authored by Jeremy Mikola's avatar Jeremy Mikola

PHPLIB-141: Static method for immutable BadMethodCallException

parent c02f1272
...@@ -4,6 +4,17 @@ namespace MongoDB\Exception; ...@@ -4,6 +4,17 @@ namespace MongoDB\Exception;
class BadMethodCallException extends \BadMethodCallException implements Exception class BadMethodCallException extends \BadMethodCallException implements Exception
{ {
/**
* Thrown when a mutable method is invoked on an immutable object.
*
* @param string $class Class name
* @return self
*/
public static function classIsImmutable($class)
{
return new static(sprintf('%s is immutable', $class));
}
/** /**
* Thrown when accessing a result field on an unacknowledged write result. * Thrown when accessing a result field on an unacknowledged write result.
* *
......
...@@ -151,21 +151,21 @@ class IndexInfo implements ArrayAccess ...@@ -151,21 +151,21 @@ class IndexInfo implements ArrayAccess
* Not supported. * Not supported.
* *
* @see http://php.net/arrayaccess.offsetset * @see http://php.net/arrayaccess.offsetset
* @throws BadMethodCallException IndexInfo is immutable * @throws BadMethodCallException
*/ */
public function offsetSet($key, $value) public function offsetSet($key, $value)
{ {
throw new BadMethodCallException('IndexInfo is immutable'); throw BadMethodCallException::classIsImmutable(__CLASS__);
} }
/** /**
* Not supported. * Not supported.
* *
* @see http://php.net/arrayaccess.offsetunset * @see http://php.net/arrayaccess.offsetunset
* @throws BadMethodCallException IndexInfo is immutable * @throws BadMethodCallException
*/ */
public function offsetUnset($key) public function offsetUnset($key)
{ {
throw new BadMethodCallException('IndexInfo is immutable'); throw BadMethodCallException::classIsImmutable(__CLASS__);
} }
} }
...@@ -96,4 +96,36 @@ class IndexInfoTest extends TestCase ...@@ -96,4 +96,36 @@ class IndexInfoTest extends TestCase
$info = new IndexInfo($expectedInfo); $info = new IndexInfo($expectedInfo);
$this->assertSame($expectedInfo, $info->__debugInfo()); $this->assertSame($expectedInfo, $info->__debugInfo());
} }
/**
* @expectedException MongoDB\Exception\BadMethodCallException
* @expectedExceptionMessage MongoDB\Model\IndexInfo is immutable
*/
public function testOffsetSetCannotBeCalled()
{
$info = new IndexInfo([
'v' => 1,
'key' => ['x' => 1],
'name' => 'x_1',
'ns' => 'foo.bar',
]);
$info['v'] = 2;
}
/**
* @expectedException MongoDB\Exception\BadMethodCallException
* @expectedExceptionMessage MongoDB\Model\IndexInfo is immutable
*/
public function testOffsetUnsetCannotBeCalled()
{
$info = new IndexInfo([
'v' => 1,
'key' => ['x' => 1],
'name' => 'x_1',
'ns' => 'foo.bar',
]);
unset($info['v']);
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment