[ Index ]

PHP Cross Reference of Limb3

title

Body

[close]

/core/tests/cases/ -> lmbCollectionPaginationTest.class.php (source)

   1  <?php
   2  /*
   3   * Limb PHP Framework
   4   *
   5   * @link http://limb-project.com 
   6   * @copyright  Copyright &copy; 2004-2007 BIT(http://bit-creative.com)
   7   * @license    LGPL http://www.gnu.org/copyleft/lesser.html 
   8   */
   9  lmb_require('limb/core/src/lmbCollection.class.php');
  10  
  11  class lmbCollectionPaginationTest extends UnitTestCase
  12  {
  13    function testIterateWithPagination()
  14    {
  15      $data = array(array('x' => 'a'), array('x' => 'b'), array('x' => 'c'), array('x' => 'd'), array('x' => 'e'));
  16      $iterator = new lmbCollection($data);
  17      $iterator->paginate($offset = 0, $limit = 2);
  18      $this->assertEqual($iterator->count(), 5);
  19      $this->assertEqual($iterator->countPaginated(), $limit);
  20  
  21      $iterator->rewind();
  22      $dataspace1 = $iterator->current();
  23      $this->assertEqual($dataspace1->export(), array('x' => 'a'));
  24      $iterator->next();
  25      $dataspace2 = $iterator->current();
  26      $this->assertEqual($dataspace2->export(), array('x' => 'b'));
  27    }
  28  
  29    function testIterateWithPaginationNonZeroOffset()
  30    {
  31      $data = array(array('x' => 'a'), array('x' => 'b'), array('x' => 'c'), array('x' => 'd'), array('x' => 'e'));
  32      $iterator = new lmbCollection($data);
  33      $iterator->paginate($offset = 2, $limit = 2);
  34  
  35      $iterator->rewind();
  36      $dataspace1 = $iterator->current();
  37      $this->assertEqual($dataspace1->export(), array('x' => 'c'));
  38      $iterator->next();
  39      $dataspace2 = $iterator->current();
  40      $this->assertEqual($dataspace2->export(), array('x' => 'd'));
  41    }
  42  
  43    function testPaginateWithOutOfBounds()
  44    {
  45      $data = array(array('x' => 'a'), array('x' => 'b'), array('x' => 'c'), array('x' => 'd'), array('x' => 'e'));
  46      $iterator = new lmbCollection($data);
  47      $iterator->paginate($offset = 5, $limit = 2);
  48  
  49      $this->assertEqual($iterator->count(), 5);
  50      $this->assertEqual($iterator->countPaginated(), 0);
  51  
  52      $iterator->rewind();
  53      $this->assertFalse($iterator->valid());
  54    }
  55  
  56    function testPaginateWithOffsetLessThanZero()
  57    {
  58      $data = array(array('x' => 'a'), array('x' => 'b'), array('x' => 'c'), array('x' => 'd'), array('x' => 'e'));
  59      $iterator = new lmbCollection($data);
  60      $iterator->paginate($offset = -1, $limit = 2);
  61  
  62      $this->assertEqual($iterator->count(), 5);
  63      $this->assertEqual($iterator->countPaginated(), 0);
  64  
  65      $iterator->rewind();
  66      $this->assertFalse($iterator->valid());
  67    }
  68  
  69    function testWorksOkWithArrayOfSets()
  70    {
  71      $data = array(new lmbSet(array('x' => '1')),
  72                    new lmbSet(array('x' => '2')),
  73                    new lmbSet(array('x' => '3')));
  74  
  75      $iterator = new lmbCollection($data);
  76      $iterator->paginate($offset = 1, $limit = 2);
  77  
  78      $str = '';
  79      foreach($iterator as $record)
  80        $str .= $record->get('x');
  81  
  82      $this->assertEqual($str, '23');
  83    }
  84  
  85    function testResetInternalIteratorIfPrimaryDatasetChanged()
  86    {
  87      $data = array(new lmbSet(array('x' => '1')),
  88                    new lmbSet(array('x' => '2')),
  89                    new lmbSet(array('x' => '3')));
  90  
  91      $iterator = new lmbCollection($data);
  92      $iterator->paginate($offset = 1, $limit = 3);
  93  
  94      $str = '';
  95      foreach($iterator as $record)
  96        $str .= $record->get('x');
  97  
  98      $this->assertEqual($str, '23');
  99  
 100      $iterator->add(new lmbSet(array('x' => '4')));
 101  
 102      $str = '';
 103      foreach($iterator as $record)
 104        $str .= $record->get('x');
 105  
 106      $this->assertEqual($str, '234');
 107    }
 108  
 109    function testResetInternalIteratorOnSortToo()
 110    {
 111      $data = array(new lmbSet(array('x' => 'C')),
 112                    new lmbSet(array('x' => 'A')),
 113                    new lmbSet(array('x' => 'B')));
 114  
 115      $iterator = new lmbCollection($data);
 116      $iterator->paginate($offset = 1, $limit = 2);
 117  
 118      $str = '';
 119      foreach($iterator as $record)
 120        $str .= $record->get('x');
 121  
 122      $this->assertEqual($str, 'AB');
 123  
 124      $iterator->sort(array('x' => 'DESC'));
 125  
 126      $str = '';
 127      foreach($iterator as $record)
 128        $str .= $record->get('x');
 129  
 130      $this->assertEqual($str, 'BA');
 131    }
 132  
 133  }
 134  ?>


Generated: Fri Dec 5 04:05:07 2008 Cross-referenced by PHPXref 0.7