Skip to content

Commit

Permalink
Merge pull request #191 from homersimpsons/fix/find-by-datetime
Browse files Browse the repository at this point in the history
DateTime filter (#189): Add test case
  • Loading branch information
moufmouf authored Feb 7, 2020
2 parents 347a348 + 443e451 commit 6442a4e
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 4 deletions.
13 changes: 12 additions & 1 deletion src/Utils/BeanDescriptor.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Doctrine\DBAL\Schema\Schema;
use Doctrine\DBAL\Schema\Table;
use Doctrine\DBAL\Schema\ForeignKeyConstraint;
use Doctrine\DBAL\Types\Type;
use JsonSerializable;
use Mouf\Database\SchemaAnalyzer\SchemaAnalyzer;
use PhpParser\Comment\Doc;
Expand Down Expand Up @@ -1291,7 +1292,17 @@ private function generateFindByDaoCodeForIndex(Index $index, string $beanNamespa
foreach ($elements as $element) {
$params[] = $element->getParamAnnotation();
if ($element instanceof ScalarBeanPropertyDescriptor) {
$filterArrayCode .= ' '.var_export($element->getColumnName(), true).' => '.$element->getSafeVariableName().",\n";
$typeName = $element->getDatabaseType()->getName();
if ($typeName === Type::DATETIME_IMMUTABLE) {
$filterArrayCode .= sprintf(
" %s => \$this->tdbmService->getConnection()->convertToDatabaseValue(%s, %s),\n",
var_export($element->getColumnName(), true),
$element->getSafeVariableName(),
var_export($typeName, true)
);
} else {
$filterArrayCode .= ' '.var_export($element->getColumnName(), true).' => '.$element->getSafeVariableName().",\n";
}
} elseif ($element instanceof ObjectBeanPropertyDescriptor) {
$foreignKey = $element->getForeignKey();
$columns = SafeFunctions::arrayCombine($foreignKey->getLocalColumns(), $foreignKey->getForeignColumns());
Expand Down
10 changes: 10 additions & 0 deletions src/Utils/ScalarBeanPropertyDescriptor.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,16 @@ public function getPhpType(): string
return TDBMDaoGenerator::dbalTypeToPhpType($type);
}

/**
* Returns the Database type for the property
*
* @return Type
*/
public function getDatabaseType(): Type
{
return $this->column->getType();
}

/**
* Returns true if the property is compulsory (and therefore should be fetched in the constructor).
*
Expand Down
2 changes: 1 addition & 1 deletion tests/TDBMAbstractServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ private static function initSchema(Connection $connection): void
}

$db->table('person')
->column('modified_at')->datetime()->null()
->column('modified_at')->datetime()->null()->index()
->column('order')->integer()->null();


Expand Down
13 changes: 11 additions & 2 deletions tests/TDBMDaoGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
use TheCodingMachine\TDBM\Test\Dao\Generated\UserBaseDao;
use TheCodingMachine\TDBM\Test\Dao\InheritedObjectDao;
use TheCodingMachine\TDBM\Test\Dao\NodeDao;
use TheCodingMachine\TDBM\Test\Dao\PersonDao;
use TheCodingMachine\TDBM\Test\Dao\RefNoPrimKeyDao;
use TheCodingMachine\TDBM\Test\Dao\RoleDao;
use TheCodingMachine\TDBM\Test\Dao\StateDao;
Expand Down Expand Up @@ -747,8 +748,9 @@ public function testFindFromRawSqlOrderByUserCount(): void
$countryDao = new TestCountryDao($this->tdbmService);
$countries = $countryDao->getCountriesByUserCount();

$this->assertCount(4, $countries);
for ($i = 1; $i < count($countries); $i++) {
$nbCountries = 4;
$this->assertCount($nbCountries, $countries);
for ($i = 1; $i < $nbCountries; $i++) {
$this->assertLessThanOrEqual($countries[$i - 1]->getUsers()->count(), $countries[$i]->getUsers()->count());
}
}
Expand Down Expand Up @@ -2169,4 +2171,11 @@ public function testMethodNameConflictsBetweenRegularAndAutoPivotProperties(): v
$artist->getChildrenByArtistsRelations(); // auto-pivot relationship
$this->assertEquals(1, 1);
}

public function testFindByDateTime(): void
{
$personDao = new PersonDao($this->tdbmService);
$personDao->findByModifiedAt(new \DateTimeImmutable())->count();
$this->assertTrue(true);
}
}

0 comments on commit 6442a4e

Please sign in to comment.