Skip to content

Commit

Permalink
Remove broken assertion from DateAddFunction and DateSubFunction (#11243
Browse files Browse the repository at this point in the history
)

* Remove wrong asserts in DATE_ADD and DATE_SUB query AST function handlers

* Require DBAL 3.8.2
  • Loading branch information
ondrejmirtes authored Feb 20, 2024
1 parent d5ba106 commit b6b4cbc
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 25 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/static-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ jobs:
include:
- dbal-version: default
config: phpstan.neon
- dbal-version: 3.7
- dbal-version: 3.8.2
config: phpstan-dbal3.neon

steps:
Expand Down Expand Up @@ -65,7 +65,7 @@ jobs:
matrix:
dbal-version:
- default
- 3.7
- 3.8.2

steps:
- name: "Checkout code"
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"composer-runtime-api": "^2",
"ext-ctype": "*",
"doctrine/collections": "^2.1",
"doctrine/dbal": "^3.6 || ^4",
"doctrine/dbal": "^3.8.2 || ^4",
"doctrine/deprecations": "^0.5.3 || ^1",
"doctrine/event-manager": "^1.2 || ^2",
"doctrine/inflector": "^1.4 || ^2.0",
Expand Down
13 changes: 2 additions & 11 deletions src/Query/AST/Functions/DateAddFunction.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
use Doctrine\ORM\Query\SqlWalker;
use Doctrine\ORM\Query\TokenType;

use function assert;
use function is_numeric;
use function strtolower;

/**
Expand Down Expand Up @@ -63,17 +61,10 @@ public function getSql(SqlWalker $sqlWalker): string
};
}

/**
* @return numeric-string
*
* @throws ASTException
*/
/** @throws ASTException */
private function dispatchIntervalExpression(SqlWalker $sqlWalker): string
{
$sql = $this->intervalExpression->dispatch($sqlWalker);
assert(is_numeric($sql));

return $sql;
return $this->intervalExpression->dispatch($sqlWalker);
}

public function parse(Parser $parser): void
Expand Down
13 changes: 2 additions & 11 deletions src/Query/AST/Functions/DateSubFunction.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
use Doctrine\ORM\Query\QueryException;
use Doctrine\ORM\Query\SqlWalker;

use function assert;
use function is_numeric;
use function strtolower;

/**
Expand Down Expand Up @@ -56,16 +54,9 @@ public function getSql(SqlWalker $sqlWalker): string
};
}

/**
* @return numeric-string
*
* @throws ASTException
*/
/** @throws ASTException */
private function dispatchIntervalExpression(SqlWalker $sqlWalker): string
{
$sql = $this->intervalExpression->dispatch($sqlWalker);
assert(is_numeric($sql));

return $sql;
return $this->intervalExpression->dispatch($sqlWalker);
}
}
31 changes: 31 additions & 0 deletions tests/Tests/ORM/Functional/QueryDqlFunctionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use DateTimeImmutable;
use Doctrine\DBAL\Platforms\SQLitePlatform;
use Doctrine\ORM\AbstractQuery;
use Doctrine\Tests\Models\Company\CompanyEmployee;
use Doctrine\Tests\Models\Company\CompanyManager;
use Doctrine\Tests\OrmFunctionalTestCase;
use PHPUnit\Framework\Attributes\DataProvider;
Expand Down Expand Up @@ -487,4 +488,34 @@ protected function generateFixture(): void
$this->_em->flush();
$this->_em->clear();
}

#[Group('GH-11240')]
public function testDateAddWithColumnInterval(): void
{
$query = sprintf(
'SELECT DATE_ADD(CURRENT_TIMESTAMP(), m.salary, \'day\') AS add FROM %s m',
CompanyEmployee::class,
);

$result = $this->_em->createQuery($query)
->setMaxResults(1)
->getSingleResult(AbstractQuery::HYDRATE_ARRAY);

self::assertArrayHasKey('add', $result);
}

#[Group('GH-11240')]
public function testDateSubWithColumnInterval(): void
{
$query = sprintf(
'SELECT DATE_SUB(CURRENT_TIMESTAMP(), m.salary, \'day\') AS add FROM %s m',
CompanyEmployee::class,
);

$result = $this->_em->createQuery($query)
->setMaxResults(1)
->getSingleResult(AbstractQuery::HYDRATE_ARRAY);

self::assertArrayHasKey('add', $result);
}
}

0 comments on commit b6b4cbc

Please sign in to comment.