Skip to content

Commit

Permalink
fix: Getting an aggregate fails when the query has derived fields (de…
Browse files Browse the repository at this point in the history
…fined in the select section) in the order section
  • Loading branch information
Finesse committed Feb 15, 2019
1 parent 79a1847 commit d70e27c
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ php:
- nightly
- hhvm

services:
- mysql

env:
- DEPENDENCIES="high"
- DEPENDENCIES="low"
Expand All @@ -21,6 +24,7 @@ cache:
- $HOME/.composer/cache

install:
- mysql -e 'CREATE DATABASE IF NOT EXISTS test;'
- if [[ "$DEPENDENCIES" = 'high' ]]; then composer update; fi
- if [[ "$DEPENDENCIES" = 'low' ]]; then composer update --prefer-lowest; fi
- composer require php-coveralls/php-coveralls '^2.0' --dev
Expand Down
1 change: 1 addition & 0 deletions src/Parts/SelectTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ protected function getAggregate(callable $prepareAggregate)
{
$query = clone $this;
$query->select = [];
$query->order = [];
$query = $query
->offset(null)
->limit(null)
Expand Down
42 changes: 42 additions & 0 deletions tests/Parts/SelectTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Finesse\MiniDB\Tests\Parts;

use Finesse\MiniDB\Database;
use Finesse\MiniDB\Exceptions\DatabaseException;
use Finesse\MiniDB\Exceptions\IncorrectQueryException;
use Finesse\MiniDB\Exceptions\InvalidArgumentException;
use Finesse\MiniDB\Query;
Expand Down Expand Up @@ -133,6 +134,47 @@ public function testAggregates()
});
}

/**
* Tests the aggregate functions with a query with a sort by a derived field.
*
* SQLite doesn't emit an error when an unknown field is used in the order section so MySQL is used for the test.
*
* @link https://docs.travis-ci.com/user/database-setup/#MySQL Credentials for testing MySQL in Travis CI
*/
public function testAggregateWithDerivedField()
{
try {
$database = Database::create([
'driver' => 'mysql',
'dsn' => 'mysql:host=localhost;dbname=test;charset=UTF8',
'username' => 'travis',
'password' => '',
'prefix' => 'pre_'
]);
} catch (DatabaseException $exception) {
$this->markTestSkipped('MySQL is not available: '.$exception->getMessage());
return;
}

$database->statement('
CREATE TABLE pre_items(
id INT(11) NOT NULL AUTO_INCREMENT,
value INT(11) NOT NULL,
PRIMARY KEY (id)
)
');
for ($i = 0; $i < 30; ++$i) {
$database->insert('INSERT INTO pre_items (value) VALUES (?)', [$i + 100]);
}

$query = $database
->table('items')
->addSelect($database->raw('value * 2'), 'computed_value') // Select is discarded while getting an aggregate and the order gets an unknown column
->orderBy('computed_value', 'desc');

$this->assertEquals($query->count(), 30);
}

/**
* Tests the `chunk` method
*/
Expand Down

0 comments on commit d70e27c

Please sign in to comment.