From f464f6a52195b4927dfdce632d9adae597078aa4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20N=C3=A9grier?= Date: Wed, 6 Dec 2017 09:44:11 +0100 Subject: [PATCH] Adding a meaningful error message if a table has no primary key --- src/Utils/BeanDescriptor.php | 5 +++++ tests/Utils/BeanDescriptorTest.php | 10 ++++++++++ 2 files changed, 15 insertions(+) diff --git a/src/Utils/BeanDescriptor.php b/src/Utils/BeanDescriptor.php index 1abb7b2d..de46d6a2 100644 --- a/src/Utils/BeanDescriptor.php +++ b/src/Utils/BeanDescriptor.php @@ -162,6 +162,11 @@ public function getExposedProperties(): array */ private function getProperties(Table $table) { + if ($table->getPrimaryKey() === null) { + // Security check: a table MUST have a primary key + throw new TDBMException(sprintf('Table "%s" does not have any primary key', $table->getName())); + } + $parentRelationship = $this->schemaAnalyzer->getParentRelationship($table->getName()); if ($parentRelationship) { $parentTable = $this->schema->getTable($parentRelationship->getForeignTableName()); diff --git a/tests/Utils/BeanDescriptorTest.php b/tests/Utils/BeanDescriptorTest.php index 888323a0..c36a6d14 100644 --- a/tests/Utils/BeanDescriptorTest.php +++ b/tests/Utils/BeanDescriptorTest.php @@ -22,8 +22,10 @@ use Doctrine\Common\Cache\VoidCache; use Doctrine\DBAL\Schema\Schema; +use Doctrine\DBAL\Schema\Table; use Mouf\Database\SchemaAnalyzer\SchemaAnalyzer; use TheCodingMachine\TDBM\TDBMAbstractServiceTest; +use TheCodingMachine\TDBM\TDBMException; use TheCodingMachine\TDBM\TDBMSchemaAnalyzer; class BeanDescriptorTest extends TDBMAbstractServiceTest @@ -86,6 +88,14 @@ public function testGetTable() $this->assertSame($usersTable, $beanDescriptor->getTable()); } + public function testTableWithNoPrimaryKey() + { + $table = new Table('no_primary_key'); + $this->expectException(TDBMException::class); + $this->expectExceptionMessage('Table "no_primary_key" does not have any primary key'); + new BeanDescriptor($table, 'Foo\\Bar', 'Foo\\Generated\\Bar', $this->schemaAnalyzer, $this->schema, $this->tdbmSchemaAnalyzer, new DefaultNamingStrategy()); + } + /*public function testGeneratePhpCode() { $usersTable = $this->schema->getTable("users"); $beanDescriptor = new BeanDescriptor($usersTable, $this->schemaAnalyzer, $this->schema);