Skip to content
This repository has been archived by the owner on Feb 5, 2024. It is now read-only.

Commit

Permalink
Exception refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
nicktacular committed Feb 3, 2016
1 parent f61ce21 commit 79bee99
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 27 deletions.
48 changes: 48 additions & 0 deletions lib/Doctrine/KeyValueStore/InvalidArgumentException.php
@@ -0,0 +1,48 @@
<?php
/*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the MIT license. For more information, see
* <http://www.doctrine-project.org>.
*/

namespace Doctrine\KeyValueStore;


class InvalidArgumentException extends KeyValueStoreException
{
public static function invalidType($name, $expectedType, &$actual)
{
return new static(
sprintf('The %s must be a %s, got "%s" instead.', $name, $expectedType, gettype($actual)),
0
);
}

public static function invalidLength($name, $min, $max)
{
return new static(
sprintf('The %s must be at least %d but no more than %d chars.', $name, $min, $max),
0
);
}

public static function invalidTableName($name)
{
return new static(
sprintf('Invalid table name: %s', $name),
0
);
}
}
15 changes: 6 additions & 9 deletions lib/Doctrine/KeyValueStore/Storage/AmazonDynamoDbStorage.php
Expand Up @@ -22,6 +22,7 @@
use Aws\DynamoDb\DynamoDbClient;
use Aws\DynamoDb\Marshaler;
use Doctrine\KeyValueStore\NotFoundException;
use Doctrine\KeyValueStore\InvalidArgumentException;

class AmazonDynamoDbStorage implements Storage
{
Expand Down Expand Up @@ -82,14 +83,12 @@ public function __construct(
private function validateKeyName($name)
{
if (!is_string($name)) {
throw new \InvalidArgumentException(
sprintf('The key must be a string, got "%s" instead.', gettype($name))
);
throw InvalidArgumentException::invalidType('key', 'string', $name);
}

$len = strlen($name);
if ($len > 255 || $len < 1) {
throw new \InvalidArgumentException('The name must not exceed 255 bytes.');
throw InvalidArgumentException::invalidLength('name', 1, 255);
}
}

Expand All @@ -105,13 +104,11 @@ private function validateKeyName($name)
private function validateTableName($name)
{
if (!is_string($name)) {
throw new \InvalidArgumentException(
sprintf('The key must be a string, got "%s" instead.', gettype($name))
);
throw InvalidArgumentException::invalidType('key', 'string', $name);
}

if (!preg_match('/^[a-z0-9_.-]{3,255}$/i', $name)) {
throw new \InvalidArgumentException('Invalid DynamoDB table name.');
throw InvalidArgumentException::invalidTableName($name);
}
}

Expand Down Expand Up @@ -146,7 +143,7 @@ public function getDefaultKeyName()
*
* @throws \InvalidArgumentException When the key or table name is invalid.
*/
public function setKeyForTable($table, $key)
private function setKeyForTable($table, $key)
{
$this->validateTableName($table);
$this->validateKeyName($key);
Expand Down
42 changes: 24 additions & 18 deletions tests/Doctrine/Tests/KeyValueStore/Storage/AmazonDynamoDbTest.php
Expand Up @@ -51,7 +51,7 @@ public function testThatTableKeysInitiallyEmpty()
}

/**
* @expectedException \InvalidArgumentException
* @expectedException \Doctrine\KeyValueStore\KeyValueStoreException
* @expectedExceptionMessage The key must be a string, got "array" instead.
*/
public function testDefaultKeyCannotBeSomethingOtherThanString()
Expand All @@ -61,7 +61,7 @@ public function testDefaultKeyCannotBeSomethingOtherThanString()
}

/**
* @expectedException \InvalidArgumentException
* @expectedException \Doctrine\KeyValueStore\KeyValueStoreException
* @expectedExceptionMessage The key must be a string, got "object" instead.
*/
public function testTableKeysMustAllBeStringsOrElse()
Expand All @@ -71,8 +71,8 @@ public function testTableKeysMustAllBeStringsOrElse()
}

/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage The name must not exceed 255 bytes.
* @expectedException \Doctrine\KeyValueStore\KeyValueStoreException
* @expectedExceptionMessage The name must be at least 1 but no more than 255 chars.
*/
public function testKeyNameMustBeUnder255Bytes()
{
Expand Down Expand Up @@ -101,16 +101,28 @@ public function validTableNames()
];
}

private function invokeMethod($methodName, $obj, array $args = null)
{
$relf = new \ReflectionObject($obj);
$method = $relf->getMethod($methodName);
$method->setAccessible(true);

if ($args) {
return $method->invokeArgs($obj, $args);
}

return $method->invoke($obj);
}

/**
* @dataProvider invalidTableNames
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage Invalid DynamoDB table name.
* @expectedException \Doctrine\KeyValueStore\KeyValueStoreException
*/
public function testTableNameValidatesAgainstInvalidTableNames($tableName)
{
$client = $this->getDynamoDbMock();
$storage = new AmazonDynamoDbStorage($client);
$storage->setKeyForTable($tableName, 'Id');
$this->invokeMethod('setKeyForTable', $storage, [$tableName, 'Id']);
}

/**
Expand All @@ -120,7 +132,7 @@ public function testTableNameValidatesAgainstValidTableNames($tableName)
{
$client = $this->getDynamoDbMock();
$storage = new AmazonDynamoDbStorage($client);
$storage->setKeyForTable($tableName, 'Id');
$this->invokeMethod('setKeyForTable', $storage, [$tableName, 'Id']);

$this->assertAttributeSame([$tableName => 'Id'], 'tableKeys', $storage);
}
Expand All @@ -129,8 +141,8 @@ public function testThatYouCanHaveMultipleTablesWithOverrides()
{
$client = $this->getDynamoDbMock();
$storage = new AmazonDynamoDbStorage($client);
$storage->setKeyForTable('Aaa', '2');
$storage->setKeyForTable('Bbb', '1');
$this->invokeMethod('setKeyForTable', $storage, ['Aaa', '2']);
$this->invokeMethod('setKeyForTable', $storage, ['Bbb', '1']);

$this->assertAttributeSame(['Aaa' => '2', 'Bbb' => '1'], 'tableKeys', $storage);
}
Expand Down Expand Up @@ -162,10 +174,7 @@ public function testThatSomeStorageHasDifferentKey()

$storage = new AmazonDynamoDbStorage($client, null, 'sauce', ['this' => 'that', 'yolo' => 'now']);

$r = new \ReflectionObject($storage);
$method = $r->getMethod('prepareKey');
$method->setAccessible(true);
$this->assertSame(['that' => ['N' => '111']], $method->invoke($storage, 'this', 111));
$this->assertSame(['that' => ['N' => '111']], $this->invokeMethod('prepareKey', $storage, ['this', 111]));
}

public function testThatSomeStorageUsesDefaultKey()
Expand All @@ -174,10 +183,7 @@ public function testThatSomeStorageUsesDefaultKey()

$storage = new AmazonDynamoDbStorage($client, null, 'sauce', ['this' => 'that', 'yolo' => 'now']);

$r = new \ReflectionObject($storage);
$method = $r->getMethod('prepareKey');
$method->setAccessible(true);
$this->assertSame(['sauce' => ['S' => 'hello']], $method->invoke($storage, 'MyTable', "hello"));
$this->assertSame(['sauce' => ['S' => 'hello']], $this->invokeMethod('prepareKey', $storage, ['MyTable', "hello"]));
}

public function testInsertingCallsAPutItem()
Expand Down

0 comments on commit 79bee99

Please sign in to comment.