Skip to content

Commit 9bc925f

Browse files
committed
OneToOne inverse relation: test singular getter name and serialize index
1 parent a0ccf06 commit 9bc925f

File tree

3 files changed

+37
-24
lines changed

3 files changed

+37
-24
lines changed

src/Utils/DirectForeignKeyMethodDescriptor.php

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -73,14 +73,10 @@ public function __construct(
7373
*/
7474
public function getName() : string
7575
{
76-
$name = $this->foreignKey->getLocalTableName();
77-
if ($this->hasLocalUniqueIndex()) {
78-
$name = TDBMDaoGenerator::toSingular($name);
79-
}
8076
if (!$this->useAlternateName) {
81-
return 'get'.TDBMDaoGenerator::toCamelCase($name);
77+
return 'get' . $this->getPropertyName();
8278
} else {
83-
$methodName = 'get'.TDBMDaoGenerator::toCamelCase($name).'By';
79+
$methodName = 'get' . $this->getPropertyName() . 'By';
8480

8581
$camelizedColumns = array_map([TDBMDaoGenerator::class, 'toCamelCase'], $this->foreignKey->getUnquotedLocalColumns());
8682

@@ -90,6 +86,20 @@ public function getName() : string
9086
}
9187
}
9288

89+
/**
90+
* Returns the property name in CamelCase taking into account singularization
91+
*
92+
* @return string
93+
*/
94+
private function getPropertyName() : string
95+
{
96+
$name = $this->foreignKey->getLocalTableName();
97+
if ($this->hasLocalUniqueIndex()) {
98+
$name = TDBMDaoGenerator::toSingular($name);
99+
}
100+
return TDBMDaoGenerator::toCamelCase($name);
101+
}
102+
93103
/**
94104
* Returns the name of the class that will be returned by the getter (short name).
95105
*
@@ -174,22 +184,28 @@ private function getFilters(ForeignKeyConstraint $fk) : string
174184
return $parametersCode;
175185
}
176186

187+
private $hasLocalUniqueIndex;
177188
/**
178189
* Check if the ForeignKey have an unique index
179190
*
180191
* @return bool
181192
*/
182193
private function hasLocalUniqueIndex(): bool
183194
{
195+
if ($this->hasLocalUniqueIndex !== null) {
196+
return $this->hasLocalUniqueIndex;
197+
}
184198
foreach ($this->getForeignKey()->getLocalTable()->getIndexes() as $index) {
185199
if (
186200
$index->isUnique()
187201
&& count($index->getUnquotedColumns()) === count($this->getForeignKey()->getUnquotedLocalColumns())
188202
&& !array_diff($index->getUnquotedColumns(), $this->getForeignKey()->getUnquotedLocalColumns()) // Check for permuted columns too
189203
) {
204+
$this->hasLocalUniqueIndex = true;
190205
return true;
191206
}
192207
}
208+
$this->hasLocalUniqueIndex = false;
193209
return false;
194210
}
195211

@@ -226,7 +242,7 @@ public function getJsonSerializeCode() : string
226242
$format = "jsonSerialize($stopRecursion)";
227243
}
228244
$isIncluded = $this->findAnnotation(Annotation\JsonInclude::class) !== null;
229-
$index = $jsonCollection->key ?: lcfirst(TDBMDaoGenerator::toCamelCase($this->foreignKey->getLocalTableName()));
245+
$index = $jsonCollection->key ?: lcfirst($this->getPropertyName());
230246
$class = $this->getBeanClassName();
231247
$variableName = '$' . TDBMDaoGenerator::toVariableName($class);
232248
$getter = $this->getName();

tests/TDBMAbstractServiceTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -437,12 +437,12 @@ private static function initSchema(Connection $connection): void
437437
->column('track_id')->references('tracks')
438438
->column('artist_id')->references('artists')->comment('@JsonKey("feat") @JsonInclude');
439439

440-
$db->table('object_base')
440+
$db->table('base_objects')
441441
->column('id')->integer()->primaryKey()->autoIncrement()
442442
->column('label')->string();
443-
$db->table('object_inherited')
443+
$db->table('inherited_objects')
444444
->column('id')->integer()->primaryKey()->autoIncrement()
445-
->column('object_base_id')->references('object_base')->unique()->comment('@JsonCollection');
445+
->column('base_object_id')->references('base_objects')->unique()->comment('@JsonCollection');
446446

447447
$sqlStmts = $toSchema->getMigrateFromSql($fromSchema, $connection->getDatabasePlatform());
448448

tests/TDBMDaoGeneratorTest.php

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -40,27 +40,26 @@
4040
use TheCodingMachine\TDBM\Test\Dao\AllNullableDao;
4141
use TheCodingMachine\TDBM\Test\Dao\AnimalDao;
4242
use TheCodingMachine\TDBM\Test\Dao\ArtistDao;
43+
use TheCodingMachine\TDBM\Test\Dao\BaseObjectDao;
4344
use TheCodingMachine\TDBM\Test\Dao\Bean\AccountBean;
4445
use TheCodingMachine\TDBM\Test\Dao\Bean\AllNullableBean;
4546
use TheCodingMachine\TDBM\Test\Dao\Bean\AnimalBean;
46-
use TheCodingMachine\TDBM\Test\Dao\Bean\ArrayBean;
4747
use TheCodingMachine\TDBM\Test\Dao\Bean\Article2Bean;
4848
use TheCodingMachine\TDBM\Test\Dao\Bean\ArticleBean;
4949
use TheCodingMachine\TDBM\Test\Dao\Bean\ArtistBean;
50+
use TheCodingMachine\TDBM\Test\Dao\Bean\BaseObjectBean;
5051
use TheCodingMachine\TDBM\Test\Dao\Bean\BoatBean;
5152
use TheCodingMachine\TDBM\Test\Dao\Bean\CatBean;
5253
use TheCodingMachine\TDBM\Test\Dao\Bean\CategoryBean;
5354
use TheCodingMachine\TDBM\Test\Dao\Bean\CountryBean;
54-
use TheCodingMachine\TDBM\Test\Dao\Bean\DateArrayBean;
5555
use TheCodingMachine\TDBM\Test\Dao\Bean\DogBean;
5656
use TheCodingMachine\TDBM\Test\Dao\Bean\FileBean;
5757
use TheCodingMachine\TDBM\Test\Dao\Bean\Generated\ArticleBaseBean;
5858
use TheCodingMachine\TDBM\Test\Dao\Bean\Generated\BoatBaseBean;
5959
use TheCodingMachine\TDBM\Test\Dao\Bean\Generated\FileBaseBean;
6060
use TheCodingMachine\TDBM\Test\Dao\Bean\Generated\UserBaseBean;
61+
use TheCodingMachine\TDBM\Test\Dao\Bean\InheritedObjectBean;
6162
use TheCodingMachine\TDBM\Test\Dao\Bean\NodeBean;
62-
use TheCodingMachine\TDBM\Test\Dao\Bean\ObjectBaseBean;
63-
use TheCodingMachine\TDBM\Test\Dao\Bean\ObjectInheritedBean;
6463
use TheCodingMachine\TDBM\Test\Dao\Bean\PersonBean;
6564
use TheCodingMachine\TDBM\Test\Dao\Bean\RefNoPrimKeyBean;
6665
use TheCodingMachine\TDBM\Test\Dao\Bean\RoleBean;
@@ -73,11 +72,9 @@
7372
use TheCodingMachine\TDBM\Test\Dao\CountryDao;
7473
use TheCodingMachine\TDBM\Test\Dao\DogDao;
7574
use TheCodingMachine\TDBM\Test\Dao\FileDao;
76-
use TheCodingMachine\TDBM\Test\Dao\Generated\ContactBaseDao;
7775
use TheCodingMachine\TDBM\Test\Dao\Generated\UserBaseDao;
76+
use TheCodingMachine\TDBM\Test\Dao\InheritedObjectDao;
7877
use TheCodingMachine\TDBM\Test\Dao\NodeDao;
79-
use TheCodingMachine\TDBM\Test\Dao\ObjectBaseDao;
80-
use TheCodingMachine\TDBM\Test\Dao\ObjectInheritedDao;
8178
use TheCodingMachine\TDBM\Test\Dao\RefNoPrimKeyDao;
8279
use TheCodingMachine\TDBM\Test\Dao\RoleDao;
8380
use TheCodingMachine\TDBM\Test\Dao\StateDao;
@@ -2087,14 +2084,14 @@ public function testLazyLoad(): void
20872084
*/
20882085
public function testOneToOneInverseRelationGetter(): void
20892086
{
2090-
$objectBaseDao = new ObjectBaseDao($this->tdbmService);
2091-
$objectInheritedDao = new ObjectInheritedDao($this->tdbmService);
2092-
$objectBase = new ObjectBaseBean('label');
2087+
$objectBaseDao = new BaseObjectDao($this->tdbmService);
2088+
$objectInheritedDao = new InheritedObjectDao($this->tdbmService);
2089+
$objectBase = new BaseObjectBean('label');
20932090
$objectBaseDao->save($objectBase);
2094-
$this->assertNull($objectBase->getObjectInherited());
2095-
$objectInherited = new ObjectInheritedBean($objectBase);
2091+
$this->assertNull($objectBase->getInheritedObject());
2092+
$objectInherited = new InheritedObjectBean($objectBase);
20962093
$objectInheritedDao->save($objectInherited);
2097-
$this->assertSame($objectInherited, $objectBase->getObjectInherited());
2098-
$this->assertEquals(1, $objectBase->jsonSerialize()['objectInherited']['id']);
2094+
$this->assertSame($objectInherited, $objectBase->getInheritedObject());
2095+
$this->assertEquals(1, $objectBase->jsonSerialize()['inheritedObject']['id']);
20992096
}
21002097
}

0 commit comments

Comments
 (0)