Skip to content

Commit

Permalink
raise an error for pages < 1. #11230
Browse files Browse the repository at this point in the history
Pages should start at 1.
raise an error for pages < 1.
issue: #11230
  • Loading branch information
saeideng committed Sep 25, 2017
1 parent 8c41449 commit 825b454
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/Database/Query.php
Expand Up @@ -1259,9 +1259,14 @@ public function orHaving($conditions, $types = [])
* @param int|null $limit The number of rows you want in the page. If null
* the current limit clause will be used.
* @return $this
* @throws \InvalidArgumentException If page number < 1.
*/
public function page($num, $limit = null)
{
if ($num < 1) {
$msg = 'Pages should start at 1.';
throw new InvalidArgumentException($msg);
}
if ($limit !== null) {
$this->limit($limit);
}
Expand Down
1 change: 1 addition & 0 deletions src/Datasource/QueryInterface.php
Expand Up @@ -240,6 +240,7 @@ public function order($fields, $overwrite = false);
* @param int|null $limit The number of rows you want in the page. If null
* the current limit clause will be used.
* @return $this
* @throws \InvalidArgumentException If page number < 1.
*/
public function page($num, $limit = null);

Expand Down
15 changes: 15 additions & 0 deletions tests/TestCase/Database/QueryTest.php
Expand Up @@ -2206,6 +2206,21 @@ public function testSelectOffset()
$this->assertTrue($dirty);
}

/**
* Test Pages number.
*
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage Pages should start at 1.
* @return void
*/
public function testPageShouldStartAtOne()
{
$this->loadFixtures('Comments');
$query = new Query($this->connection);
$result = $query->from('comments')
->page(0);
}

/**
* Test selecting rows using the page() method.
*
Expand Down

0 comments on commit 825b454

Please sign in to comment.