Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[DoctrineBridge] Kill DBAL 2 support #50571

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@
"doctrine/annotations": "^1.13.1|^2",
"doctrine/collections": "^1.0|^2.0",
"doctrine/data-fixtures": "^1.1",
"doctrine/dbal": "^2.13.1|^3.0",
"doctrine/dbal": "^3.6",
"doctrine/orm": "^2.15",
"dragonmantank/cron-expression": "^3",
"egulias/email-validator": "^2.1.10|^3.1|^4",
Expand Down Expand Up @@ -161,7 +161,7 @@
"ext-psr": "<1.1|>=2",
"async-aws/core": "<1.5",
"doctrine/annotations": "<1.13.1",
"doctrine/dbal": "<2.13.1",
"doctrine/dbal": "<3.6",
"doctrine/orm": "<2.15",
"egulias/email-validator": "~3.0.0",
"masterminds/html5": "<2.6",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

namespace Symfony\Bridge\Doctrine\DataCollector;

use Doctrine\DBAL\Logging\DebugStack;
use Doctrine\DBAL\Types\ConversionException;
use Doctrine\DBAL\Types\Type;
use Doctrine\Persistence\ManagerRegistry;
Expand All @@ -32,11 +31,6 @@ class DoctrineDataCollector extends DataCollector
private array $connections;
private array $managers;

/**
* @var array<string, DebugStack>
*/
private array $loggers = [];

public function __construct(
private ManagerRegistry $registry,
private DebugDataHolder $debugDataHolder,
Expand All @@ -61,16 +55,8 @@ private function collectQueries(): array
{
$queries = [];

if (null !== $this->debugDataHolder) {
foreach ($this->debugDataHolder->getData() as $name => $data) {
$queries[$name] = $this->sanitizeQueries($name, $data);
}

return $queries;
}

foreach ($this->loggers as $name => $logger) {
$queries[$name] = $this->sanitizeQueries($name, $logger->queries);
foreach ($this->debugDataHolder->getData() as $name => $data) {
$queries[$name] = $this->sanitizeQueries($name, $data);
}

return $queries;
Expand All @@ -82,17 +68,7 @@ private function collectQueries(): array
public function reset()
{
$this->data = [];

if (null !== $this->debugDataHolder) {
$this->debugDataHolder->reset();

return;
}

foreach ($this->loggers as $logger) {
$logger->queries = [];
$logger->currentQuery = 0;
}
$this->debugDataHolder->reset();
}

public function getManagers()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
namespace Symfony\Bridge\Doctrine\Form\ChoiceList;

use Doctrine\DBAL\ArrayParameterType;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Types\ConversionException;
use Doctrine\DBAL\Types\Type;
use Doctrine\ORM\QueryBuilder;
Expand Down Expand Up @@ -71,13 +70,13 @@ public function getEntitiesByIds(string $identifier, array $values): array
$entity = current($qb->getRootEntities());
$metadata = $qb->getEntityManager()->getClassMetadata($entity);
if (\in_array($type = $metadata->getTypeOfField($identifier), ['integer', 'bigint', 'smallint'])) {
$parameterType = class_exists(ArrayParameterType::class) ? ArrayParameterType::INTEGER : Connection::PARAM_INT_ARRAY;
$parameterType = ArrayParameterType::INTEGER;

// Filter out non-integer values (e.g. ""). If we don't, some
// databases such as PostgreSQL fail.
$values = array_values(array_filter($values, fn ($v) => (string) $v === (string) (int) $v || ctype_digit($v)));
} elseif (\in_array($type, ['ulid', 'uuid', 'guid'])) {
$parameterType = class_exists(ArrayParameterType::class) ? ArrayParameterType::STRING : Connection::PARAM_STR_ARRAY;
$parameterType = ArrayParameterType::STRING;

// Like above, but we just filter out empty strings.
$values = array_values(array_filter($values, fn ($v) => '' !== (string) $v));
Expand All @@ -96,7 +95,7 @@ public function getEntitiesByIds(string $identifier, array $values): array
unset($value);
}
} else {
$parameterType = class_exists(ArrayParameterType::class) ? ArrayParameterType::STRING : Connection::PARAM_STR_ARRAY;
$parameterType = ArrayParameterType::STRING;
}
if (!$values) {
return [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@
namespace Symfony\Bridge\Doctrine\Security\RememberMe;

use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Driver\Result as DriverResult;
use Doctrine\DBAL\ParameterType;
use Doctrine\DBAL\Result;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\DBAL\Types\Types;
use Symfony\Component\Security\Core\Authentication\RememberMe\PersistentToken;
Expand Down Expand Up @@ -43,11 +41,9 @@
*/
class DoctrineTokenProvider implements TokenProviderInterface, TokenVerifierInterface
{
private Connection $conn;

public function __construct(Connection $conn)
{
$this->conn = $conn;
public function __construct(
private Connection $conn,
) {
}

public function loadTokenBySeries(string $series): PersistentTokenInterface
Expand All @@ -57,13 +53,9 @@ public function loadTokenBySeries(string $series): PersistentTokenInterface
$paramValues = ['series' => $series];
$paramTypes = ['series' => ParameterType::STRING];
$stmt = $this->conn->executeQuery($sql, $paramValues, $paramTypes);
$row = $stmt instanceof Result || $stmt instanceof DriverResult ? $stmt->fetchAssociative() : $stmt->fetch(\PDO::FETCH_ASSOC);

if ($row) {
return new PersistentToken($row['class'], $row['username'], $series, $row['value'], new \DateTime($row['last_used']));
}
$row = $stmt->fetchAssociative() ?: throw new TokenNotFoundException('No token found.');

throw new TokenNotFoundException('No token found.');
return new PersistentToken($row['class'], $row['username'], $series, $row['value'], new \DateTime($row['last_used']));
}

/**
Expand All @@ -74,11 +66,7 @@ public function deleteTokenBySeries(string $series)
$sql = 'DELETE FROM rememberme_token WHERE series=:series';
$paramValues = ['series' => $series];
$paramTypes = ['series' => ParameterType::STRING];
if (method_exists($this->conn, 'executeStatement')) {
$this->conn->executeStatement($sql, $paramValues, $paramTypes);
} else {
$this->conn->executeUpdate($sql, $paramValues, $paramTypes);
}
$this->conn->executeStatement($sql, $paramValues, $paramTypes);
}

/**
Expand All @@ -97,11 +85,8 @@ public function updateToken(string $series, #[\SensitiveParameter] string $token
'lastUsed' => Types::DATETIME_IMMUTABLE,
'series' => ParameterType::STRING,
];
if (method_exists($this->conn, 'executeStatement')) {
$updated = $this->conn->executeStatement($sql, $paramValues, $paramTypes);
} else {
$updated = $this->conn->executeUpdate($sql, $paramValues, $paramTypes);
}
$updated = $this->conn->executeStatement($sql, $paramValues, $paramTypes);

if ($updated < 1) {
throw new TokenNotFoundException('No token found.');
}
Expand All @@ -127,11 +112,7 @@ public function createNewToken(PersistentTokenInterface $token)
'value' => ParameterType::STRING,
'lastUsed' => Types::DATETIME_IMMUTABLE,
];
if (method_exists($this->conn, 'executeStatement')) {
$this->conn->executeStatement($sql, $paramValues, $paramTypes);
} else {
$this->conn->executeUpdate($sql, $paramValues, $paramTypes);
}
$this->conn->executeStatement($sql, $paramValues, $paramTypes);
}

public function verifyToken(PersistentTokenInterface $token, #[\SensitiveParameter] string $tokenValue): bool
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,84 @@
use Symfony\Component\VarDumper\Cloner\Data;
use Symfony\Component\VarDumper\Dumper\CliDumper;

// Doctrine DBAL 2 compatibility
class_exists(\Doctrine\DBAL\Platforms\MySqlPlatform::class);

class DoctrineDataCollectorTest extends TestCase
{
use DoctrineDataCollectorTestTrait;

protected function setUp(): void
{
ClockMock::register(self::class);
ClockMock::withClockMock(1500000000);
}

public function testCollectConnections()
{
$c = $this->createCollector([]);
$c->collect(new Request(), new Response());
$c = unserialize(serialize($c));
$this->assertEquals(['default' => 'doctrine.dbal.default_connection'], $c->getConnections());
}

public function testCollectManagers()
{
$c = $this->createCollector([]);
$c->collect(new Request(), new Response());
$c = unserialize(serialize($c));
$this->assertEquals(['default' => 'doctrine.orm.default_entity_manager'], $c->getManagers());
}

public function testCollectQueryCount()
{
$c = $this->createCollector([]);
$c->collect(new Request(), new Response());
$c = unserialize(serialize($c));
$this->assertEquals(0, $c->getQueryCount());

$queries = [
['sql' => 'SELECT * FROM table1', 'params' => [], 'types' => [], 'executionMS' => 0],
];
$c = $this->createCollector($queries);
$c->collect(new Request(), new Response());
$c = unserialize(serialize($c));
$this->assertEquals(1, $c->getQueryCount());
}

public function testCollectTime()
{
$c = $this->createCollector([]);
$c->collect(new Request(), new Response());
$c = unserialize(serialize($c));
$this->assertEquals(0, $c->getTime());

$queries = [
['sql' => 'SELECT * FROM table1', 'params' => [], 'types' => [], 'executionMS' => 1],
];
$c = $this->createCollector($queries);
$c->collect(new Request(), new Response());
$c = unserialize(serialize($c));
$this->assertEquals(1, $c->getTime());

$queries = [
['sql' => 'SELECT * FROM table1', 'params' => [], 'types' => [], 'executionMS' => 1],
['sql' => 'SELECT * FROM table2', 'params' => [], 'types' => [], 'executionMS' => 2],
];
$c = $this->createCollector($queries);
$c->collect(new Request(), new Response());
$c = unserialize(serialize($c));
$this->assertEquals(3, $c->getTime());
}

public function testCollectQueryWithNoTypes()
{
$queries = [
['sql' => 'SET sql_mode=(SELECT REPLACE(@@sql_mode, \'ONLY_FULL_GROUP_BY\', \'\'))', 'params' => [], 'types' => null, 'executionMS' => 1],
];
$c = $this->createCollector($queries);
$c->collect(new Request(), new Response());
$c = unserialize(serialize($c));

$collectedQueries = $c->getQueries();
$this->assertSame([], $collectedQueries['default'][0]['types']);
}

public function testReset()
{
$queries = [
Expand Down

This file was deleted.

4 changes: 1 addition & 3 deletions src/Symfony/Bridge/Doctrine/Tests/DoctrineTestHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,7 @@ public static function createTestConfiguration(): Configuration
$config->setProxyDir(sys_get_temp_dir());
$config->setProxyNamespace('SymfonyTests\Doctrine');
$config->setMetadataDriverImpl(new AttributeDriver([__DIR__.'/../Tests/Fixtures' => 'Symfony\Bridge\Doctrine\Tests\Fixtures'], true));
if (class_exists(DefaultSchemaManagerFactory::class)) {
$config->setSchemaManagerFactory(new DefaultSchemaManagerFactory());
}
$config->setSchemaManagerFactory(new DefaultSchemaManagerFactory());

return $config;
}
Expand Down
Loading