Skip to content

Commit

Permalink
Add type parameter to getQueryBuilder()
Browse files Browse the repository at this point in the history
This aligns the interface we have in the 4.x branch, and also gives
a solution to #651 for applications that want to not have any
deprecations.
  • Loading branch information
markstory committed Jul 28, 2024
1 parent d1147b2 commit 813c344
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
24 changes: 22 additions & 2 deletions src/CakeAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
use Phinx\Db\Adapter\AdapterInterface;
use Phinx\Db\Adapter\AdapterWrapper;

use function Cake\Core\deprecationWarning;

Check failure on line 23 in src/CakeAdapter.php

View workflow job for this annotation

GitHub Actions / Coding Standard & Static Analysis

Expected 0 lines between different types of use statement, found 1.

/**
* Decorates an AdapterInterface in order to proxy some method to the actual
* connection object.
Expand Down Expand Up @@ -76,11 +78,29 @@ public function getCakeConnection()
/**
* Returns a new Query object
*
* @param string|null $type The query type to get. Defaults to null emitting a deprecation
* @return \Cake\Database\Query
*/
public function getQueryBuilder(): Query
public function getQueryBuilder(?string $type = null): Query
{
return $this->getCakeConnection()->newQuery();
switch ($type) {
case 'delete':
return $this->getCakeConnection()->deleteQuery();
case 'insert':
return $this->getCakeConnection()->insertQuery();
case 'select':
return $this->getCakeConnection()->selectQuery();
case 'update':
return $this->getCakeConnection()->updateQuery();
case null:
default:
deprecationWarning(
'Using getQueryBuilder() with no parameters is deprecated. ' .
"Please provide a query type parameter e.g `getQueryBuilder('select')`"
);

return $this->getCakeConnection()->newQuery();

Check failure on line 102 in src/CakeAdapter.php

View workflow job for this annotation

GitHub Actions / Coding Standard & Static Analysis

DeprecatedMethod

src/CakeAdapter.php:102:52: DeprecatedMethod: The method Cake\Database\Connection::newQuery has been marked as deprecated (see https://psalm.dev/001)
}
}

/**
Expand Down
20 changes: 20 additions & 0 deletions tests/TestCase/MigrationsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
namespace Migrations\Test\TestCase;

use Cake\Core\Plugin;
use Cake\Database\Query\InsertQuery;
use Cake\Database\Query\SelectQuery;
use Cake\Database\Query\UpdateQuery;
use Cake\Datasource\ConnectionManager;
use Cake\TestSuite\TestCase;
use Migrations\CakeAdapter;
Expand Down Expand Up @@ -1001,4 +1004,21 @@ public function migrationsProvider()
],
];
}

public function testQueryBuilder(): void
{
$adapter = $this->migrations
->getManager()
->getEnvironment('default')
->getAdapter();

$this->assertInstanceOf(CakeAdapter::class, $adapter);
$this->assertInstanceOf(DeleteQuery::class, $adapter->getQueryBuilder('delete'));
$this->assertInstanceOf(InsertQuery::class, $adapter->getQueryBuilder('insert'));
$this->assertInstanceOf(SelectQuery::class, $adapter->getQueryBuilder('select'));
$this->assertInstanceOf(UpdateQuery::class, $adapter->getQueryBuilder('update'));
$this->deprecated(function () use ($adapter) {
$this->assertInstanceOf(SelectQuery::class, $adapter->getQueryBuilder());
});
}
}

0 comments on commit 813c344

Please sign in to comment.