Skip to content

Commit 3affca3

Browse files
authored
Merge pull request #153 from thecodingmachine/feature/composite-pk-get-by-id
Composite Pk - getById: Add Tests
2 parents 906daf1 + e0004d9 commit 3affca3

File tree

3 files changed

+67
-26
lines changed

3 files changed

+67
-26
lines changed

src/Utils/BeanDescriptor.php

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,20 @@ class BeanDescriptor implements BeanDescriptorInterface
103103
*/
104104
private $configuration;
105105

106-
public function __construct(Table $table, string $beanNamespace, string $generatedBeanNamespace, string $daoNamespace, string $generatedDaoNamespace, SchemaAnalyzer $schemaAnalyzer, Schema $schema, TDBMSchemaAnalyzer $tdbmSchemaAnalyzer, NamingStrategyInterface $namingStrategy, AnnotationParser $annotationParser, CodeGeneratorListenerInterface $codeGeneratorListener, ConfigurationInterface $configuration)
107-
{
106+
public function __construct(
107+
Table $table,
108+
string $beanNamespace,
109+
string $generatedBeanNamespace,
110+
string $daoNamespace,
111+
string $generatedDaoNamespace,
112+
SchemaAnalyzer $schemaAnalyzer,
113+
Schema $schema,
114+
TDBMSchemaAnalyzer $tdbmSchemaAnalyzer,
115+
NamingStrategyInterface $namingStrategy,
116+
AnnotationParser $annotationParser,
117+
CodeGeneratorListenerInterface $codeGeneratorListener,
118+
ConfigurationInterface $configuration
119+
) {
108120
$this->table = $table;
109121
$this->beanNamespace = $beanNamespace;
110122
$this->generatedBeanNamespace = $generatedBeanNamespace;
@@ -779,27 +791,35 @@ public function generateDaoPhpCode(): ?FileGenerator
779791
$class->addMethodFromGenerator($findAllMethod);
780792
}
781793

782-
if (count($primaryKeyColumns) === 1) {
783-
$primaryKeyColumn = $primaryKeyColumns[0];
784-
$primaryKeyPhpType = TDBMDaoGenerator::dbalTypeToPhpType($this->table->getColumn($primaryKeyColumn)->getType());
794+
if (count($primaryKeyColumns) > 0) {
795+
$lazyLoadingParameterName = 'lazyLoading';
796+
$parameters = [];
797+
$parametersTag = [];
798+
$primaryKeyFilter = [];
799+
800+
foreach ($primaryKeyColumns as $primaryKeyColumn) {
801+
if ($primaryKeyColumn === $lazyLoadingParameterName) {
802+
throw new TDBMException('Primary Column name `' . $lazyLoadingParameterName . '` is not allowed.');
803+
}
804+
$phpType = TDBMDaoGenerator::dbalTypeToPhpType($this->table->getColumn($primaryKeyColumn)->getType());
805+
$parameters[] = new ParameterGenerator($primaryKeyColumn, $phpType);
806+
$parametersTag[] = new ParamTag($primaryKeyColumn, [$phpType]);
807+
$primaryKeyFilter[] = "'$primaryKeyColumn' => \$$primaryKeyColumn";
808+
}
809+
$parameters[] = new ParameterGenerator($lazyLoadingParameterName, 'bool', false);
810+
$parametersTag[] = new ParamTag($lazyLoadingParameterName, ['bool'], 'If set to true, the object will not be loaded right away. Instead, it will be loaded when you first try to access a method of the object.');
811+
$parametersTag[] = new ReturnTag(['\\'.$beanClassName]);
812+
$parametersTag[] = new ThrowsTag('\\'.TDBMException::class);
785813

786814
$getByIdMethod = new MethodGenerator(
787815
'getById',
788-
[
789-
new ParameterGenerator('id', $primaryKeyPhpType),
790-
new ParameterGenerator('lazyLoading', 'bool', false)
791-
],
816+
$parameters,
792817
MethodGenerator::FLAG_PUBLIC,
793-
"return \$this->tdbmService->findObjectByPk('$tableName', ['$primaryKeyColumn' => \$id], [], \$lazyLoading);",
818+
"return \$this->tdbmService->findObjectByPk('$tableName', [" . implode(', ', $primaryKeyFilter) . "], [], \$$lazyLoadingParameterName);",
794819
(new DocBlockGenerator(
795820
"Get $beanClassWithoutNameSpace specified by its ID (its primary key).",
796821
'If the primary key does not exist, an exception is thrown.',
797-
[
798-
new ParamTag('id', [$primaryKeyPhpType]),
799-
new ParamTag('lazyLoading', ['bool'], 'If set to true, the object will not be loaded right away. Instead, it will be loaded when you first try to access a method of the object.'),
800-
new ReturnTag(['\\'.$beanClassName]),
801-
new ThrowsTag('\\'.TDBMException::class)
802-
]
822+
$parametersTag
803823
))->setWordWrap(false)
804824
);
805825
$getByIdMethod->setReturnType($beanClassName);

tests/TDBMDaoGeneratorTest.php

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1799,15 +1799,6 @@ public function testDecimalIsMappedToString(): void
17991799
$this->assertSame('string', (string) $reflectionClass->getMethod('getLength')->getReturnType());
18001800
}
18011801

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-
18111802
/**
18121803
* @depends testDaoGeneration
18131804
*/
@@ -1836,6 +1827,18 @@ public function testDeleteMultiPrimaryKeysBean(): void
18361827
$this->assertCount(0, $stateDao->findAll());
18371828
}
18381829

1830+
/**
1831+
* @depends testDaoGeneration
1832+
*/
1833+
public function testCompositePrimaryKeyGetter(): void
1834+
{
1835+
$stateDao = new StateDao($this->tdbmService);
1836+
$country = new CountryBean('USA');
1837+
$stateBean = new StateBean($country, 'CA', 'California');
1838+
$stateDao->save($stateBean);
1839+
$this->assertSame($stateBean, $stateDao->getById($country->getId(), 'CA'));
1840+
}
1841+
18391842
/**
18401843
* @depends testDaoGeneration
18411844
*/
@@ -2091,7 +2094,7 @@ public function testOneToOneInverseRelationGetter(): void
20912094
$this->assertNull($objectBase->getObjectInherited());
20922095
$objectInherited = new ObjectInheritedBean($objectBase);
20932096
$objectInheritedDao->save($objectInherited);
2094-
$this->assertInstanceOf(ObjectInheritedBean::class, $objectBase->getObjectInherited());
2097+
$this->assertSame($objectInherited, $objectBase->getObjectInherited());
20952098
$this->assertEquals(1, $objectBase->jsonSerialize()['objectInherited']['id']);
20962099
}
20972100
}

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)