Skip to content

Commit

Permalink
Fixed return type for CriteriaInterface::getLimit
Browse files Browse the repository at this point in the history
See: #15004
  • Loading branch information
sergeyklay committed May 4, 2020
1 parent 930fc08 commit 3271782
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 81 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG-4.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
- Changed the way `Phalcon\Http\Response::__construct` checks `content` data type. Now a `TypeError` will be thrown if incompatible data type was passed [#14983](https://github.com/phalcon/cphalcon/issues/14983)
- Changed return type hints of the following `Phalcon\Flash\FlashInterface`'s methods: `error`, `message`, `notice`, `success` and `warning` [#14994](https://github.com/phalcon/cphalcon/issues/14994)
- Changed return type hint for `Phalcon\Mvc\ModelInterface::sum` [#15000](https://github.com/phalcon/cphalcon/issues/15000)
- Changed return type for `Phalcon\Mvc\Model\Criteria::getLimit` so that integer, NULL or array will be returned [#15004](https://github.com/phalcon/cphalcon/issues/15004)

## Fixed
- Fixed `Phalcon\Mvc\Model\Query\Builder::getPhql` to add single quote between string value on a simple condition [#14874](https://github.com/phalcon/cphalcon/issues/14874)
Expand Down Expand Up @@ -43,6 +44,7 @@
- Fixed return type hints of the following `Phalcon\Flash\AbstractFlash`'s methods: `error`, `notice`, `success` and `warning` [#14994](https://github.com/phalcon/cphalcon/issues/14994)
- Fixed return type hint for `Phalcon\Translate\InterpolatorFactory::newInstance` [#14996](https://github.com/phalcon/cphalcon/issues/14996)
- Fixed return type hint for `Phalcon\Mvc\Model::sum` [#15000](https://github.com/phalcon/cphalcon/issues/15000)
- Fixed return type hint for `Phalcon\Mvc\Model\CriteriaInterface::getLimit` and `Phalcon\Mvc\Model\Criteria::getLimit` to follow documentation and original purpose [#15004](https://github.com/phalcon/cphalcon/issues/15004)

[#14987](https://github.com/phalcon/cphalcon/issues/14987)

Expand Down
60 changes: 35 additions & 25 deletions ext/phalcon/mvc/model/criteria.zep.c

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 1 addition & 8 deletions ext/phalcon/mvc/model/criteria.zep.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 7 additions & 5 deletions ext/phalcon/mvc/model/criteriainterface.zep.c

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 1 addition & 8 deletions ext/phalcon/mvc/model/criteriainterface.zep.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 14 additions & 10 deletions phalcon/Mvc/Model/Criteria.zep
Original file line number Diff line number Diff line change
Expand Up @@ -393,15 +393,15 @@ class Criteria implements CriteriaInterface, InjectionAwareInterface
}

/**
* Returns the limit parameter in the criteria, which will be an integer if
* limit was set without an offset, an array with 'number' and 'offset' keys
* if an offset was set with the limit, or null if limit has not been set.
* Returns the limit parameter in the criteria, which will be
*
* @return string|null
* - An integer if 'limit' was set without an 'offset'
* - An array with 'number' and 'offset' keys if an offset was set with the limit
* - NULL if limit has not been set
*/
public function getLimit() -> string | null
public function getLimit() -> int | array | null
{
var limit;
var limit, offset;

if !fetch limit, this->params["limit"] {
return null;
Expand Down Expand Up @@ -634,10 +634,14 @@ class Criteria implements CriteriaInterface, InjectionAwareInterface
return this;
}

let this->params["limit"] = [
"number": limit,
"offset": offset
];
if offset == 0 {
let this->params["limit"] = limit;
} else {
let this->params["limit"] = [
"number": limit,
"offset": offset
];
}

return this;
}
Expand Down
14 changes: 8 additions & 6 deletions phalcon/Mvc/Model/CriteriaInterface.zep
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
*
* (c) Phalcon Team <[email protected]>
*
* For the full copyright and license information, please view the LICENSE.txt
* file that was distributed with this source code.
* For the full copyright and license information, please view the
* LICENSE.txt file that was distributed with this source code.
*/

namespace Phalcon\Mvc\Model;
Expand Down Expand Up @@ -98,11 +98,13 @@ interface CriteriaInterface
public function getHaving();

/**
* Returns the limit parameter in the criteria, which will be an integer if
* limit was set without an offset, an array with 'number' and 'offset' keys
* if an offset was set with the limit, or null if limit has not been set.
* Returns the limit parameter in the criteria, which will be
*
* - An integer if 'limit' was set without an 'offset'
* - An array with 'number' and 'offset' keys if an offset was set with the limit
* - NULL if limit has not been set
*/
public function getLimit() -> string | null;
public function getLimit() -> int | array | null;

/**
* Returns an internal model name on which the criteria will be applied
Expand Down
44 changes: 25 additions & 19 deletions tests/database/Mvc/Model/Criteria/LimitCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
*
* (c) Phalcon Team <[email protected]>
*
* For the full copyright and license information, please view the LICENSE.txt
* file that was distributed with this source code.
* For the full copyright and license information, please view the
* LICENSE.txt file that was distributed with this source code.
*/

declare(strict_types=1);
Expand All @@ -16,24 +16,34 @@
use DatabaseTester;
use Phalcon\Mvc\Model\Criteria;
use Phalcon\Mvc\Model\Query\Builder;
use Phalcon\Storage\Exception;
use Phalcon\Test\Fixtures\Traits\DiTrait;
use Phalcon\Test\Models\Invoices;

/**
* Class LimitCest
*/
class LimitCest
{
use DiTrait;

public function _before(DatabaseTester $I)
/**
* Executed before each test
*
* @param DatabaseTester $I
* @return void
*/
public function _before(DatabaseTester $I): void
{
$this->setNewFactoryDefault();
try {
$this->setNewFactoryDefault();
} catch (Exception $e) {
$I->fail($e->getMessage());
}
}

/**
* Tests Phalcon\Mvc\Model\Criteria :: limit()
*
* @param DatabaseTester $I
*
* @author Phalcon Team <[email protected]>
* @since 2020-02-01
*
Expand All @@ -58,20 +68,16 @@ public function mvcModelCriteriaLimit(DatabaseTester $I)
$expected = 'SELECT [Phalcon\Test\Models\Invoices].* '
. 'FROM [Phalcon\Test\Models\Invoices] '
. 'LIMIT :APL0:';
$actual = $builder->getPhql();
$I->assertEquals($expected, $actual);

$expected = [
'number' => 10,
'offset' => 0,
];
$actual = $criteria->getLimit();
$I->assertEquals($expected, $actual);
$I->assertEquals($expected, $builder->getPhql());
$I->assertEquals(10, $criteria->getLimit());
}

/**
* Tests Phalcon\Mvc\Model\Criteria :: limit() - offset
*
* @param DatabaseTester $I
*
* @author Phalcon Team <[email protected]>
* @since 2020-02-01
*
Expand All @@ -96,14 +102,14 @@ public function mvcModelCriteriaLimitOffset(DatabaseTester $I)
$expected = 'SELECT [Phalcon\Test\Models\Invoices].* '
. 'FROM [Phalcon\Test\Models\Invoices] '
. 'LIMIT :APL0: OFFSET :APL1:';
$actual = $builder->getPhql();
$I->assertEquals($expected, $actual);

$I->assertEquals($expected, $builder->getPhql());

$expected = [
'number' => 10,
'offset' => 15,
];
$actual = $criteria->getLimit();
$I->assertEquals($expected, $actual);

$I->assertEquals($expected, $criteria->getLimit());
}
}

0 comments on commit 3271782

Please sign in to comment.