Skip to content

Commit 50aad9f

Browse files
committed
Composite Pk - getById: Add Tests
1 parent 906daf1 commit 50aad9f

File tree

2 files changed

+33
-10
lines changed

2 files changed

+33
-10
lines changed

tests/TDBMDaoGeneratorTest.php

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
use TheCodingMachine\TDBM\Test\Dao\Bean\BoatBean;
5151
use TheCodingMachine\TDBM\Test\Dao\Bean\CatBean;
5252
use TheCodingMachine\TDBM\Test\Dao\Bean\CategoryBean;
53+
use TheCodingMachine\TDBM\Test\Dao\Bean\CompositePkBean;
5354
use TheCodingMachine\TDBM\Test\Dao\Bean\CountryBean;
5455
use TheCodingMachine\TDBM\Test\Dao\Bean\DateArrayBean;
5556
use TheCodingMachine\TDBM\Test\Dao\Bean\DogBean;
@@ -69,6 +70,7 @@
6970
use TheCodingMachine\TDBM\Test\Dao\BoatDao;
7071
use TheCodingMachine\TDBM\Test\Dao\CatDao;
7172
use TheCodingMachine\TDBM\Test\Dao\CategoryDao;
73+
use TheCodingMachine\TDBM\Test\Dao\CompositePkDao;
7274
use TheCodingMachine\TDBM\Test\Dao\ContactDao;
7375
use TheCodingMachine\TDBM\Test\Dao\CountryDao;
7476
use TheCodingMachine\TDBM\Test\Dao\DogDao;
@@ -1799,15 +1801,6 @@ public function testDecimalIsMappedToString(): void
17991801
$this->assertSame('string', (string) $reflectionClass->getMethod('getLength')->getReturnType());
18001802
}
18011803

1802-
/**
1803-
* @depends testDaoGeneration
1804-
*/
1805-
public function testNoGetByIdOnMultiPrimaryKeys(): void
1806-
{
1807-
$reflectionClass = new \ReflectionClass(StateDao::class);
1808-
$this->assertFalse($reflectionClass->hasMethod('getById'));
1809-
}
1810-
18111804
/**
18121805
* @depends testDaoGeneration
18131806
*/
@@ -1836,6 +1829,18 @@ public function testDeleteMultiPrimaryKeysBean(): void
18361829
$this->assertCount(0, $stateDao->findAll());
18371830
}
18381831

1832+
/**
1833+
* @depends testDaoGeneration
1834+
*/
1835+
public function testCompositePrimaryKeyGetter(): void
1836+
{
1837+
$stateDao = new StateDao($this->tdbmService);
1838+
$country = new CountryBean('USA');
1839+
$stateBean = new StateBean($country, 'CA', 'California');
1840+
$stateDao->save($stateBean);
1841+
$this->assertSame($stateBean, $stateDao->getById($country->getId(), 'CA'));
1842+
}
1843+
18391844
/**
18401845
* @depends testDaoGeneration
18411846
*/
@@ -2091,7 +2096,7 @@ public function testOneToOneInverseRelationGetter(): void
20912096
$this->assertNull($objectBase->getObjectInherited());
20922097
$objectInherited = new ObjectInheritedBean($objectBase);
20932098
$objectInheritedDao->save($objectInherited);
2094-
$this->assertInstanceOf(ObjectInheritedBean::class, $objectBase->getObjectInherited());
2099+
$this->assertSame($objectInherited, $objectBase->getObjectInherited());
20952100
$this->assertEquals(1, $objectBase->jsonSerialize()['objectInherited']['id']);
20962101
}
20972102
}

tests/Utils/BeanDescriptorTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
use Doctrine\Common\Cache\VoidCache;
2525
use Doctrine\DBAL\Schema\Schema;
2626
use Doctrine\DBAL\Schema\Table;
27+
use Doctrine\DBAL\Types\Type;
2728
use Mouf\Database\SchemaAnalyzer\SchemaAnalyzer;
2829
use TheCodingMachine\TDBM\TDBMAbstractServiceTest;
2930
use TheCodingMachine\TDBM\TDBMException;
@@ -98,6 +99,23 @@ public function testTableWithNoPrimaryKey(): void
9899
new BeanDescriptor($table, 'Foo\\Bar', 'Foo\\Generated\\Bar', 'Tdbm\\Test\\Daos', 'Tdbm\\Test\\Daos\\Generated', $this->schemaAnalyzer, $this->schema, $this->tdbmSchemaAnalyzer, $this->getNamingStrategy(), AnnotationParser::buildWithDefaultAnnotations([]), new BaseCodeGeneratorListener(), $this->getConfiguration());
99100
}
100101

102+
public function testTableWithLazyLoadingColumn(): void
103+
{
104+
$table = $this->schema->createTable('lazy_loading');
105+
$table->addColumn('lazyLoading', Type::BOOLEAN);
106+
$table->setPrimaryKey(['lazyLoading']);
107+
$sqlStmts = $this->schema->getMigrateFromSql($this->getConnection()->getSchemaManager()->createSchema(), $this->getConnection()->getDatabasePlatform());
108+
109+
foreach ($sqlStmts as $sqlStmt) {
110+
$this->getConnection()->exec($sqlStmt);
111+
}
112+
113+
$this->expectException(TDBMException::class);
114+
$this->expectExceptionMessage('Primary Column name `lazyLoading` is not allowed.');
115+
$beanDescriptor = new BeanDescriptor($table, 'Foo\\Bar', 'Foo\\Generated\\Bar', 'Tdbm\\Test\\Daos', 'Tdbm\\Test\\Daos\\Generated', $this->schemaAnalyzer, $this->schema, $this->tdbmSchemaAnalyzer, $this->getNamingStrategy(), AnnotationParser::buildWithDefaultAnnotations([]), new BaseCodeGeneratorListener(), $this->getConfiguration());
116+
$beanDescriptor->generateDaoPhpCode();
117+
}
118+
101119
/*public function testGeneratePhpCode() {
102120
$usersTable = $this->schema->getTable("users");
103121
$beanDescriptor = new BeanDescriptor($usersTable, $this->schemaAnalyzer, $this->schema);

0 commit comments

Comments
 (0)