Skip to content

Commit

Permalink
Merge pull request #94 from oroinc/master-dependencies
Browse files Browse the repository at this point in the history
BAP-22252: Update doctrine-extensions to support latest dependencies
  • Loading branch information
x86demon authored Sep 22, 2023
2 parents d937c79 + 4d0c576 commit b6b8120
Show file tree
Hide file tree
Showing 24 changed files with 128 additions and 99 deletions.
1 change: 1 addition & 0 deletions .php-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
8.1
10 changes: 6 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,17 @@
}
],
"require": {
"php": ">=7.3",
"doctrine/orm": ">=2.6"
"php": ">=8.1",
"doctrine/orm": ">=2.6",
"doctrine/dbal": ">=2.6"
},
"require-dev": {
"phpunit/phpunit": "9.*",
"phpunit/phpunit": "~10",
"doctrine/data-fixtures": "^1.3",
"symfony/yaml": "5.*",
"symfony/cache": "5.*",
"squizlabs/php_codesniffer": "3.5.*"
"squizlabs/php_codesniffer": "3.5.*",
"doctrine/annotations": ">1.0, <2.0"
},
"autoload": {
"psr-4": {
Expand Down
18 changes: 17 additions & 1 deletion src/Oro/ORM/Query/AST/FunctionFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@

namespace Oro\ORM\Query\AST;

use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Platforms\MySQLPlatform;
use Doctrine\DBAL\Platforms\PostgreSQLPlatform;
use Doctrine\ORM\Query\QueryException;
use Oro\ORM\Query\AST\Platform\Functions\PlatformFunctionNode;

Expand All @@ -13,8 +16,21 @@ class FunctionFactory
*
* @throws QueryException
*/
public static function create(string $platformName, string $functionName, array $parameters): PlatformFunctionNode
public static function create(AbstractPlatform $platform, string $functionName, array $parameters): PlatformFunctionNode
{
if ($platform instanceof PostgreSQLPlatform) {
$platformName = 'postgresql';
} elseif ($platform instanceof MySQLPlatform) {
$platformName = 'mysql';
} else {
throw QueryException::syntaxError(
\sprintf(
'Not supported platform "%s"',
$platform::class
)
);
}

$className = __NAMESPACE__
. '\\Platform\\Functions\\'
. static::classify(\strtolower($platformName))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@

abstract class AbstractPlatformAwareFunctionNode extends FunctionNode
{
/** @var array */
public $parameters = [];
public array $parameters = [];

/**
* @noinspection PhpMissingReturnTypeInspection
* @noinspection ReturnTypeCanBeDeclaredInspection
* {@inheritdoc}
*/
public function getSql(SqlWalker $sqlWalker)
{
$function = FunctionFactory::create(
$sqlWalker->getConnection()->getDatabasePlatform()->getName(),
$sqlWalker->getConnection()->getDatabasePlatform(),
$this->name,
$this->parameters
);
Expand Down
7 changes: 5 additions & 2 deletions src/Oro/ORM/Query/AST/Functions/Cast.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class Cast extends AbstractPlatformAwareFunctionNode
public const TYPE_KEY = 'type';

/** @var array */
protected $supportedTypes = [
protected array $supportedTypes = [
'char',
'string',
'text',
Expand All @@ -30,6 +30,9 @@ class Cast extends AbstractPlatformAwareFunctionNode
'uuid'
];

/**
* {@inheritdoc}
*/
public function parse(Parser $parser)
{
$parser->match(Lexer::T_IDENTIFIER);
Expand Down Expand Up @@ -79,7 +82,7 @@ protected function isSupportedType(string $type): bool
{
$type = \strtolower(\trim($type));
foreach ($this->supportedTypes as $supportedType) {
if (0 === \strpos($type, $supportedType)) {
if (str_starts_with($type, $supportedType)) {
return true;
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/Oro/ORM/Query/AST/Functions/DateTime/ConvertTz.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ class ConvertTz extends AbstractPlatformAwareFunctionNode
public const FROM_TZ_KEY = 'from_tz';
public const TO_TZ_KEY = 'to_tz';

/**
* {@inheritdoc}
*/
public function parse(Parser $parser)
{
$parser->match(Lexer::T_IDENTIFIER);
Expand Down
3 changes: 3 additions & 0 deletions src/Oro/ORM/Query/AST/Functions/Numeric/Pow.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ class Pow extends AbstractPlatformAwareFunctionNode
public const VALUE_KEY = 'value';
public const POWER_KEY = 'power';

/**
* {@inheritdoc}
*/
public function parse(Parser $parser)
{
$parser->match(Lexer::T_IDENTIFIER);
Expand Down
3 changes: 3 additions & 0 deletions src/Oro/ORM/Query/AST/Functions/Numeric/Round.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ class Round extends AbstractPlatformAwareFunctionNode
public const VALUE = 'value';
public const PRECISION = 'precision';

/**
* {@inheritdoc}
*/
public function parse(Parser $parser)
{
$lexer = $parser->getLexer();
Expand Down
3 changes: 3 additions & 0 deletions src/Oro/ORM/Query/AST/Functions/Numeric/Sign.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ class Sign extends AbstractPlatformAwareFunctionNode
{
public const PARAMETER_KEY = 'expression';

/**
* {@inheritdoc}
*/
public function parse(Parser $parser)
{
$parser->match(Lexer::T_IDENTIFIER);
Expand Down
5 changes: 4 additions & 1 deletion src/Oro/ORM/Query/AST/Functions/Numeric/TimestampDiff.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class TimestampDiff extends AbstractPlatformAwareFunctionNode
/**
* @var array
*/
protected $supportedUnits = [
protected array $supportedUnits = [
'MICROSECOND',
'SECOND',
'MINUTE',
Expand All @@ -28,6 +28,9 @@ class TimestampDiff extends AbstractPlatformAwareFunctionNode
'YEAR'
];

/**
* {@inheritdoc}
*/
public function parse(Parser $parser)
{
$parser->match(Lexer::T_IDENTIFIER);
Expand Down
3 changes: 3 additions & 0 deletions src/Oro/ORM/Query/AST/Functions/SimpleFunction.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ class SimpleFunction extends AbstractPlatformAwareFunctionNode
{
public const PARAMETER_KEY = 'expression';

/**
* {@inheritdoc}
*/
public function parse(Parser $parser)
{
$parser->match(Lexer::T_IDENTIFIER);
Expand Down
3 changes: 3 additions & 0 deletions src/Oro/ORM/Query/AST/Functions/String/ConcatWs.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ class ConcatWs extends AbstractPlatformAwareFunctionNode
public const STRINGS_KEY = 'strings';
public const SEPARATOR_KEY = 'separator';

/**
* {@inheritdoc}
*/
public function parse(Parser $parser)
{
$parser->match(Lexer::T_IDENTIFIER);
Expand Down
9 changes: 6 additions & 3 deletions src/Oro/ORM/Query/AST/Functions/String/DateFormat.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class DateFormat extends AbstractPlatformAwareFunctionNode
public const FORMAT_KEY = 'format';

/** @var array */
private static $knownFormats = [
private static array $knownFormats = [
'%a',
'%b',
'%c',
Expand Down Expand Up @@ -49,7 +49,7 @@ class DateFormat extends AbstractPlatformAwareFunctionNode
];

/** @var array */
private static $supportedFormats = [
private static array $supportedFormats = [
'%a',
'%b',
'%c',
Expand All @@ -76,6 +76,9 @@ class DateFormat extends AbstractPlatformAwareFunctionNode
'%%',
];

/**
* {@inheritdoc}
*/
public function parse(Parser $parser)
{
$parser->match(Lexer::T_IDENTIFIER);
Expand All @@ -96,7 +99,7 @@ private function validateFormat(Parser $parser): void
$format = \str_replace('%%', '', (string)$this->parameters[self::FORMAT_KEY]);
$unsupportedFormats = \array_diff(self::$knownFormats, self::$supportedFormats);
foreach ($unsupportedFormats as $unsupportedFormat) {
if (false !== \strpos($format, $unsupportedFormat)) {
if (str_contains($format, $unsupportedFormat)) {
$parser->syntaxError(
\sprintf(
'Format string contains unsupported specifier %s. The supported specifiers are: "%s"',
Expand Down
1 change: 1 addition & 0 deletions src/Oro/ORM/Query/AST/Functions/String/GroupConcat.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class GroupConcat extends AbstractPlatformAwareFunctionNode

/**
* @url http://sysmagazine.com/posts/181666/
* {@inheritdoc}
*/
public function parse(Parser $parser)
{
Expand Down
3 changes: 3 additions & 0 deletions src/Oro/ORM/Query/AST/Functions/String/Replace.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ class Replace extends AbstractPlatformAwareFunctionNode
public const FROM_KEY = 'from';
public const TO_KEY = 'to';

/**
* {@inheritdoc}
*/
public function parse(Parser $parser)
{
$parser->match(Lexer::T_IDENTIFIER);
Expand Down
4 changes: 0 additions & 4 deletions src/Oro/ORM/Query/AST/Platform/Functions/Mysql/Cast.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,6 @@ public function getSql(SqlWalker $sqlWalker): string
$value = $this->parameters[DqlFunction::PARAMETER_KEY];
$type = $this->parameters[DqlFunction::TYPE_KEY];

if ($type === 'json' && !$sqlWalker->getConnection()->getDatabasePlatform()->hasNativeJsonType()) {
$type = 'text';
}

$type = \strtolower($type);
$isBoolean = $type === 'bool' || $type === 'boolean';
if ($type === 'char') {
Expand Down
4 changes: 2 additions & 2 deletions src/Oro/ORM/Query/AST/Platform/Functions/Postgresql/Cast.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ public function getSql(SqlWalker $sqlWalker): string
return $timestampFunction->getSql($sqlWalker);
}

if ($type === 'json' && !$sqlWalker->getConnection()->getDatabasePlatform()->hasNativeJsonType()) {
$type = 'text';
if ($type === 'json') {
$type = 'jsonb';
}

if ($type === 'bool') {
Expand Down
2 changes: 1 addition & 1 deletion tests/Oro/Tests/DBAL/Types/ArrayTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ protected function getType(): Type
return Type::getType(Types::ARRAY);
}

public function serializationDataProvider(): array
public static function serializationDataProvider(): array
{
return [
[['a' => 'b']],
Expand Down
2 changes: 1 addition & 1 deletion tests/Oro/Tests/DBAL/Types/ObjectTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ protected function getType(): Type
return Type::getType(Types::OBJECT);
}

public function serializationDataProvider(): array
public static function serializationDataProvider(): array
{
$object = new \stdClass();
$object->a = 'test1';
Expand Down
38 changes: 25 additions & 13 deletions tests/Oro/Tests/ORM/AST/FunctionFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,29 @@

namespace Oro\Tests\ORM\AST;

use Doctrine\DBAL\Platforms\MySQLPlatform;
use Doctrine\DBAL\Platforms\OraclePlatform;
use Doctrine\DBAL\Platforms\PostgreSQLPlatform;
use Doctrine\ORM\Query\QueryException;
use Oro\ORM\Query\AST\FunctionFactory;
use PHPUnit\Framework\TestCase;

class FunctionFactoryTest extends TestCase
{
public function testCreateException(): void
public function testCreateExceptionNotSupportedPlatform(): void
{
$platform = $this->createMock(OraclePlatform::class);
$this->expectException(QueryException::class);
$this->expectExceptionMessage('[Syntax Error] Function "TestF" does not supported for platform "Test"');
FunctionFactory::create('Test', 'TestF', []);
$this->expectExceptionMessage(sprintf('[Syntax Error] Not supported platform "%s"', $platform::class));
FunctionFactory::create($platform, 'date', []);
}

public function testCreateExceptionNotSupportedFunction(): void
{
$platform = $this->createMock(PostgreSQLPlatform::class);
$this->expectException(QueryException::class);
$this->expectExceptionMessage('[Syntax Error] Function "testF" does not supported for platform "postgresql"');
FunctionFactory::create($platform, 'testF', []);
}

/**
Expand All @@ -23,20 +35,20 @@ public function testCreateException(): void
public function testCreate(string $platform, string $function): void
{
$this->expectNotToPerformAssertions();
FunctionFactory::create($platform, $function, []);
FunctionFactory::create($this->createMock($platform), $function, []);
}

public function platformFunctionsDataProvider(): array
public static function platformFunctionsDataProvider(): array
{
return [
['mysql', 'date'],
['Mysql', 'Date'],
['MySql', 'DATE'],
['postgresql', 'group_concat'],
['Mysql', 'Group_Concat'],
['postgresql', 'GROUP_CONCAT'],
['Mysql', 'TimestampDiff'],
['postgresql', 'TIMESTAMPDIFF'],
[MySQLPlatform::class, 'date'],
[MySQLPlatform::class, 'Date'],
[MySQLPlatform::class, 'DATE'],
[PostgreSQLPlatform::class, 'group_concat'],
[MySQLPlatform::class, 'Group_Concat'],
[PostgreSQLPlatform::class, 'GROUP_CONCAT'],
[MySQLPlatform::class, 'TimestampDiff'],
[PostgreSQLPlatform::class, 'TIMESTAMPDIFF'],
];
}
}
13 changes: 3 additions & 10 deletions tests/Oro/Tests/ORM/AST/Query/Functions/FunctionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ public function testDqlFunction(array $functions, string $dql, $sql, array $expe
foreach ($sql as $sqlVariant) {
$constraints[] = static::equalTo($sqlVariant);
}
$constraint = new LogicalOr();
$constraint->setConstraints($constraints);
$constraint = LogicalOr::fromConstraints($constraints);
static::assertThat($query->getSQL(), $constraint);
} else {
static::assertEquals($sql, $query->getSQL(), \sprintf('Unexpected SQL for "%s"', $dql));
Expand All @@ -50,10 +49,9 @@ public function testDqlFunction(array $functions, string $dql, $sql, array $expe
/**
* @throws \Exception
*/
public function functionsDataProvider(): array
public static function functionsDataProvider(): \Generator
{
$platform = TestUtil::getPlatformName();
$data = [];
$files = new \FilesystemIterator(
__DIR__ . '/fixtures/' . $platform,
\FilesystemIterator::SKIP_DOTS | \FilesystemIterator::CURRENT_AS_PATHNAME
Expand All @@ -63,14 +61,9 @@ public function functionsDataProvider(): array
if (!\is_array($fileData)) {
throw new \RuntimeException(\sprintf('Could not parse file %s', $file));
}
$data[] = $fileData;
}

if (!$data) {
return [];
yield from $fileData;
}

return array_merge(...$data);
}

protected function registerDqlFunction(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,9 @@
- functions:
- { name: "cast", className: "Oro\\ORM\\Query\\AST\\Functions\\Cast", type: "numeric" }
dql: "SELECT CAST(CONCAT('{\"a\":', f.id, '}') as json) FROM Oro\\Entities\\Foo f WHERE f.id = 1"
sql:
- "SELECT CAST('{\"a\":' || t0_.id || '}' AS json) AS sclr_0 FROM test_foo t0_ WHERE t0_.id = 1"
- "SELECT CAST('{\"a\":' || t0_.id || '}' AS text) AS sclr_0 FROM test_foo t0_ WHERE t0_.id = 1"
sql: "SELECT CAST('{\"a\":' || t0_.id || '}' AS jsonb) AS sclr_0 FROM test_foo t0_ WHERE t0_.id = 1"
expectedResult:
- '{"a":1}'
- '{"a": 1}'

#DECIMAL
- functions:
Expand Down
Loading

0 comments on commit b6b8120

Please sign in to comment.