diff --git a/src/Schema/Table.php b/src/Schema/Table.php index ed2b5dd..3ca7198 100644 --- a/src/Schema/Table.php +++ b/src/Schema/Table.php @@ -8,7 +8,6 @@ use Dynamite\Exception\SchemaException; use Dynamite\Validator\Constraints as Assert; use Symfony\Component\Serializer\Annotation\SerializedName; -use Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer; final class Table { @@ -121,14 +120,36 @@ public function addGlobalSecondaryIndex( $this->globalSecondaryIndexes = []; } - $this->globalSecondaryIndexes[] = $this->buildGlobalSecondaryIndex( - $name, - $projectionType, - $hashAttribute, - $rangeAttribute, - $writeCapacity, - $readCapacity - ); + $keySchema = [ + [ + 'AttributeName' => $hashAttribute, + 'KeyType' => KeyType::HASH, + ], + ]; + + if ($rangeAttribute !== null) { + $keySchema[] = [ + 'AttributeName' => $rangeAttribute, + 'KeyType' => KeyType::RANGE, + ]; + } + + $provisionedThroughput = null; + if ($readCapacity !== null && $writeCapacity !== null) { + $provisionedThroughput = [ + 'ReadCapacityUnits' => $readCapacity, + 'WriteCapacityUnits' => $writeCapacity, + ]; + } + + $this->globalSecondaryIndexes[] = [ + 'IndexName' => $name, + 'KeySchema' => $keySchema, + 'Projection' => [ + 'ProjectionType' => $projectionType, + ], + 'ProvisionedThroughput' => $provisionedThroughput, + ]; } public function getGlobalSecondaryIndexes(): ?array @@ -161,46 +182,6 @@ function (array $index): array { ); } - private function buildGlobalSecondaryIndex( - string $name, - string $projectionType, - string $hashAttribute, - ?string $rangeAttribute, - ?int $writeCapacity, - ?int $readCapacity - ): array { - $keySchema = [ - [ - 'AttributeName' => $hashAttribute, - 'KeyType' => KeyType::HASH, - ], - ]; - - if ($rangeAttribute !== null) { - $keySchema[] = [ - 'AttributeName' => $rangeAttribute, - 'KeyType' => KeyType::RANGE, - ]; - } - - $provisionedThroughput = null; - if ($readCapacity !== null && $writeCapacity !== null) { - $provisionedThroughput = [ - 'ReadCapacityUnits' => $readCapacity, - 'WriteCapacityUnits' => $writeCapacity, - ]; - } - - return [ - 'IndexName' => $name, - 'KeySchema' => $keySchema, - 'Projection' => [ - 'ProjectionType' => $projectionType, - ], - 'ProvisionedThroughput' => $provisionedThroughput, - ]; - } - public function addLocalSecondaryIndex(string $name, string $hashAttribute, ?string $rangeAttribute): void { if ($this->localSecondaryIndexes === null) { @@ -267,12 +248,4 @@ public function assertHashKeySet(): void throw SchemaException::hashKeyNotSet(); } } - - public function getSerializationContext(string $group): array - { - return [ - 'groups' => $group, - AbstractObjectNormalizer::SKIP_NULL_VALUES => true, - ]; - } } diff --git a/tests/Unit/Schema/TableTest.php b/tests/Unit/Schema/TableTest.php index a5f7df4..6096242 100644 --- a/tests/Unit/Schema/TableTest.php +++ b/tests/Unit/Schema/TableTest.php @@ -123,16 +123,4 @@ public function testAddAttribute(): void self::assertSame($expected, $schema->getAttributeDefinitions()); } - - public function testGetSerializationContext(): void - { - $schema = new Table(); - - $expected = [ - 'groups' => 'create', - 'skip_null_values' => true, - ]; - - self::assertSame($expected, $schema->getSerializationContext('create')); - } } diff --git a/tests/Unit/TableTest.php b/tests/Unit/TableTest.php index b1dd520..beffaef 100644 --- a/tests/Unit/TableTest.php +++ b/tests/Unit/TableTest.php @@ -13,11 +13,48 @@ use AsyncAws\DynamoDb\Result\CreateTableOutput; use Dynamite\AbstractTable; use Dynamite\Exception\SchemaException; +use Dynamite\Exception\ValidationException; use Dynamite\TableInterface; use Psr\Log\LogLevel; class TableTest extends UnitTestCase { + public function testLoadWithoutTableNameSet(): void + { + $validator = $this->createValidator(); + $serializer = $this->createSerializer(); + $dynamoDbClientMock = $this->createMock(DynamoDbClient::class); + $logger = $this->createTestLogger(); + + $table = new class() extends AbstractTable implements TableInterface { + public function configure(): void + { + $this->addAttributes([ + ['Id', ScalarAttributeType::S], + ['Email', ScalarAttributeType::S], + ]); + } + }; + + try { + $table->setValidator($validator); + $table->setNormalizer($serializer); + $table->create($dynamoDbClientMock, $logger); + + self::fail('Exception is not thrown'); + } catch (\Throwable $e) { + $expectedErrors = [ + 'tableName' => [ + 'Table name is not defined', + ], + ]; + + self::assertInstanceOf(ValidationException::class, $e); + self::assertSame('Validation failed', $e->getMessage()); + self::assertSame($expectedErrors, $e->getErrors()); + } + } + public function testAddAttributeWithUnexpectedType(): void { $this->expectException(SchemaException::class);