Skip to content

Commit

Permalink
Add DBAL 4.x compat
Browse files Browse the repository at this point in the history
  • Loading branch information
mbabker committed Oct 15, 2023
1 parent 2630234 commit 4611138
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 37 deletions.
5 changes: 5 additions & 0 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,11 @@ jobs:
dbal: ''
composer-flags: '--prefer-stable --prefer-dist --prefer-lowest'
next-php: false
- php: '8.2'
# Revert to @rc or stable after RC2/stable tag
dbal: '^4.0@dev'
composer-flags: '--prefer-stable --prefer-dist'
next-php: false
- php: '8.2'
composer-flags: '--prefer-stable --prefer-dist'
dbal: ''
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
},
"require-dev": {
"doctrine/collections": "^1.8 || ^2.0",
"doctrine/dbal": "^3.5",
"doctrine/dbal": "^3.5 || ^4.0",
"doctrine/mongodb-odm": "^2.4",
"doctrine/orm": "^2.14",
"doctrine/phpcr-odm": "^1.7",
Expand Down
25 changes: 11 additions & 14 deletions lib/Adapter/Doctrine/DBAL/SingleTableQueryAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,30 +17,27 @@ class SingleTableQueryAdapter extends QueryAdapter
/**
* @param string $countField Primary key for the table in query, used in the count expression. Must include table alias.
*
* @throws InvalidArgumentException if the query has JOIN statements or the count field does not have a table alias
* @throws InvalidArgumentException if the count field does not have a table alias
*/
public function __construct(QueryBuilder $queryBuilder, string $countField)
{
if ($this->hasQueryBuilderJoins($queryBuilder)) {
throw new InvalidArgumentException('The query builder cannot have joins.');
}

parent::__construct($queryBuilder, $this->createCountQueryModifier($countField));
}

private function hasQueryBuilderJoins(QueryBuilder $queryBuilder): bool
{
return !empty($queryBuilder->getQueryPart('join'));
}

private function createCountQueryModifier(string $countField): \Closure
{
$select = $this->createSelectForCountField($countField);

return function (QueryBuilder $queryBuilder) use ($select): void {
$queryBuilder->select($select)
->resetQueryPart('orderBy')
->setMaxResults(1);
return static function (QueryBuilder $queryBuilder) use ($select): void {
$queryBuilder->select($select);

if (method_exists($queryBuilder, 'resetOrderBy')) {
$queryBuilder->resetOrderBy();
} else {
$queryBuilder->resetQueryPart('orderBy');
}

$queryBuilder->setMaxResults(1);
};
}

Expand Down
15 changes: 6 additions & 9 deletions lib/Adapter/Doctrine/DBAL/Tests/DBALTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,16 @@ protected function setUp(): void

private function createConnection(): Connection
{
return DriverManager::getConnection(
[
'driver' => 'pdo_sqlite',
'memory' => true,
]
);
return DriverManager::getConnection([
'driver' => 'pdo_sqlite',
'memory' => true,
]);
}

private function createSchema(): void
{
$schema = new Schema();

$posts = $schema->createTable('posts');
$posts->addColumn('id', Types::INTEGER, ['unsigned' => true, 'autoincrement' => true]);
$posts->addColumn('username', Types::STRING, ['length' => 32]);
Expand All @@ -46,9 +45,7 @@ private function createSchema(): void
$comments->addColumn('content', Types::TEXT);
$comments->setPrimaryKey(['id']);

$queries = $schema->toSql($this->connection->getDatabasePlatform()); // get queries to create this schema.

foreach ($queries as $sql) {
foreach ($schema->toSql($this->connection->getDatabasePlatform()) as $sql) {
$this->connection->executeQuery($sql);
}
}
Expand Down
2 changes: 1 addition & 1 deletion lib/Adapter/Doctrine/DBAL/Tests/QueryAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ protected function setUp(): void
{
parent::setUp();

$this->qb = new QueryBuilder($this->connection);
$this->qb = $this->connection->createQueryBuilder();
$this->qb->select('p.*')->from('posts', 'p');
}

Expand Down
12 changes: 1 addition & 11 deletions lib/Adapter/Doctrine/DBAL/Tests/SingleTableQueryAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ protected function setUp(): void
{
parent::setUp();

$this->qb = new QueryBuilder($this->connection);
$this->qb = $this->connection->createQueryBuilder();
$this->qb->select('p.*')->from('posts', 'p');

$this->adapter = new SingleTableQueryAdapter($this->qb, 'p.id');
Expand All @@ -33,16 +33,6 @@ public function testACountFieldWithoutAnAliasIsRejected(): void
new SingleTableQueryAdapter($this->qb, 'id');
}

public function testAQueryWithJoinStatementsIsRejected(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('The query builder cannot have joins.');

$this->qb->innerJoin('p', 'comments', 'c', 'c.post_id = p.id');

new SingleTableQueryAdapter($this->qb, 'p.id');
}

public function testAdapterReturnsNumberOfResults(): void
{
self::assertSame(50, $this->adapter->getNbResults());
Expand Down
2 changes: 1 addition & 1 deletion lib/Adapter/Doctrine/DBAL/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"license": "MIT",
"require": {
"php": "^8.1",
"doctrine/dbal": "^3.5",
"doctrine/dbal": "^3.5 || ^4.0",
"pagerfanta/core": "^3.7 || ^4.0"
},
"require-dev": {
Expand Down

0 comments on commit 4611138

Please sign in to comment.