Browse Source

raise an error for pages < 1. #11230

Pages should start at 1.
raise an error for pages < 1.
issue: #11230
saeid 8 years ago
parent
commit
825b454b65
3 changed files with 21 additions and 0 deletions
  1. 5 0
      src/Database/Query.php
  2. 1 0
      src/Datasource/QueryInterface.php
  3. 15 0
      tests/TestCase/Database/QueryTest.php

+ 5 - 0
src/Database/Query.php

@@ -1259,9 +1259,14 @@ class Query implements ExpressionInterface, IteratorAggregate
      * @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);
         }

+ 1 - 0
src/Datasource/QueryInterface.php

@@ -240,6 +240,7 @@ interface QueryInterface
      * @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);
 

+ 15 - 0
tests/TestCase/Database/QueryTest.php

@@ -2207,6 +2207,21 @@ class QueryTest extends TestCase
     }
 
     /**
+     * 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.
      *
      * @return void