diff --git a/lib/Doctrine/KeyValueStore/InvalidArgumentException.php b/lib/Doctrine/KeyValueStore/InvalidArgumentException.php new file mode 100644 index 0000000..1d42f93 --- /dev/null +++ b/lib/Doctrine/KeyValueStore/InvalidArgumentException.php @@ -0,0 +1,48 @@ +. + */ + +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 + ); + } +} diff --git a/lib/Doctrine/KeyValueStore/Storage/AmazonDynamoDbStorage.php b/lib/Doctrine/KeyValueStore/Storage/AmazonDynamoDbStorage.php index fcf5e7f..a2bbcbc 100644 --- a/lib/Doctrine/KeyValueStore/Storage/AmazonDynamoDbStorage.php +++ b/lib/Doctrine/KeyValueStore/Storage/AmazonDynamoDbStorage.php @@ -22,6 +22,7 @@ use Aws\DynamoDb\DynamoDbClient; use Aws\DynamoDb\Marshaler; use Doctrine\KeyValueStore\NotFoundException; +use Doctrine\KeyValueStore\InvalidArgumentException; class AmazonDynamoDbStorage implements Storage { @@ -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); } } @@ -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); } } @@ -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); diff --git a/tests/Doctrine/Tests/KeyValueStore/Storage/AmazonDynamoDbTest.php b/tests/Doctrine/Tests/KeyValueStore/Storage/AmazonDynamoDbTest.php index 57d5c51..b8085bb 100644 --- a/tests/Doctrine/Tests/KeyValueStore/Storage/AmazonDynamoDbTest.php +++ b/tests/Doctrine/Tests/KeyValueStore/Storage/AmazonDynamoDbTest.php @@ -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() @@ -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() @@ -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() { @@ -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']); } /** @@ -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); } @@ -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); } @@ -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() @@ -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()