-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SQLParserUtils::expandListParameters functionality extracted into new…
… SqlParametersFlattener - the original method is not available anymore (it has been moved to Connection and made private) in the new version of doctrine/dbal - see doctrine/dbal#4397
- Loading branch information
1 parent
c1061e6
commit d59742b
Showing
3 changed files
with
80 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Shopsys\FrameworkBundle\Component\Doctrine; | ||
|
||
use Doctrine\DBAL\ExpandArrayParameters; | ||
use Doctrine\DBAL\SQL\Parser; | ||
|
||
class SqlParametersFlattener | ||
{ | ||
/** | ||
* inspired by @see \Doctrine\DBAL\Connection::expandArrayParameters() | ||
* | ||
* @param string $dql | ||
* @param array<string, mixed> $parameters | ||
* @return array | ||
*/ | ||
public static function flattenArrayParameters(string $dql, array $parameters): array | ||
{ | ||
$parser = new Parser(false); | ||
$visitor = new ExpandArrayParameters($parameters, []); | ||
$parser->parse($dql, $visitor); | ||
|
||
return $visitor->getParameters(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
tests/Unit/Component/Doctrine/SqlParametersFlattenerTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tests\FrameworkBundle\Unit\Component\Doctrine; | ||
|
||
use Iterator; | ||
use PHPUnit\Framework\TestCase; | ||
use Shopsys\FrameworkBundle\Component\Doctrine\SqlParametersFlattener; | ||
|
||
class SqlParametersFlattenerTest extends TestCase | ||
{ | ||
/** | ||
* @dataProvider expandArrayParametersDataProvider | ||
* @param string $dql | ||
* @param array $parameters | ||
* @param array $expectedFlattenedParameters | ||
*/ | ||
public function testExpandArrayParameters(string $dql, array $parameters, array $expectedFlattenedParameters): void | ||
{ | ||
$sqlParametersFlattener = new SqlParametersFlattener(); | ||
$actualFlattenedParameters = $sqlParametersFlattener::flattenArrayParameters($dql, $parameters); | ||
|
||
$this->assertSame($expectedFlattenedParameters, $actualFlattenedParameters); | ||
} | ||
|
||
/** | ||
* @return \Iterator | ||
*/ | ||
public function expandArrayParametersDataProvider(): Iterator | ||
{ | ||
yield [ | ||
'dql' => 'SELECT a FROM Article WHERE id = :id AND name = :name', | ||
'parameters' => [ | ||
'id' => 1, | ||
'name' => 'name', | ||
], | ||
'expectedFlattenedParameters' => [1, 'name'], | ||
]; | ||
yield [ | ||
'dql' => 'SELECT a FROM Article WHERE id = :id AND name = :name', | ||
'parameters' => [ | ||
'name' => 'name', | ||
'id' => 1, | ||
], | ||
'expectedFlattenedParameters' => [1, 'name'], | ||
]; | ||
} | ||
} |