[ Index ]

PHP Cross Reference of Limb3

title

Body

[close]

/web_app/tests/cases/plain/fetcher/ -> lmbFetcherTest.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  lmb_require('limb/core/src/lmbCollectionDecorator.class.php');
  11  lmb_require('limb/core/src/lmbClassPath.class.php');
  12  lmb_require('limb/web_app/src/fetcher/lmbFetcher.class.php');
  13  
  14  class TestingDatasetDecorator extends lmbCollectionDecorator
  15  {
  16    var $prefix1;
  17    var $prefix2;
  18    var $sort_params;
  19  
  20    function setPrefix1($prefix)
  21    {
  22      $this->prefix1 = $prefix;
  23    }
  24  
  25    function setPrefix2($prefix)
  26    {
  27      $this->prefix2 = $prefix;
  28    }
  29  
  30    function sort($sort_params)
  31    {
  32      $this->sort_params = $sort_params;
  33    }
  34  
  35    function current()
  36    {
  37      $record = parent :: current();
  38      $data = $record->export();
  39      $data['full'] = $this->prefix1 . $data['name'] . '-' . $data['job'] . $this->prefix2;
  40      $processed_record = new lmbSet();
  41      $processed_record->import($data);
  42      return $processed_record;
  43    }
  44  }
  45  
  46  class TestingFetcher extends lmbFetcher
  47  {
  48    var $use_dataset = array();
  49  
  50    protected function _createDataSet()
  51    {
  52      return $this->use_dataset;
  53    }
  54  }
  55  
  56  class lmbFetcherTest extends UnitTestCase
  57  {
  58    function testFetchCreateDatasetUsesScalarValue()
  59    {
  60      $fetcher = new TestingFetcher();
  61      $fetcher->use_dataset = 'blah';
  62      $dataset = $fetcher->fetch();
  63  
  64      $dataset->rewind();
  65      $this->assertFalse($dataset->valid());
  66    }
  67  
  68    function testFetchCreateDatasetUsesArray()
  69    {
  70      $fetcher = new TestingFetcher();
  71      $fetcher->use_dataset = array(array('name' => 'John', 'job' => 'Carpenter'),
  72                              array('name' => 'Mike', 'job' => 'Fisher'));
  73      $dataset = $fetcher->fetch();
  74  
  75      $dataset->rewind();
  76      $this->assertTrue($dataset->valid());
  77      $record = $dataset->current();
  78      $this->assertEqual($record->get('name'), 'John');
  79    }
  80  
  81    function testFetchCreateDatasetUsesObject()
  82    {
  83      $fetcher = new TestingFetcher();
  84      $fetcher->use_dataset = new lmbCollection(array(array('name' => 'John', 'job' => 'Carpenter'),
  85                                                       array('name' => 'Mike', 'job' => 'Fisher')));
  86      $dataset = $fetcher->fetch();
  87  
  88      $dataset->rewind();
  89      $this->assertTrue($dataset->valid());
  90      $record = $dataset->current();
  91      $this->assertEqual($record->get('name'), 'John');
  92    }
  93  
  94    function testAddDecoratorWithParams()
  95    {
  96      $fetcher = new TestingFetcher();
  97      $fetcher->use_dataset = new lmbCollection(array(array('name' => 'John', 'job' => 'Carpenter'),
  98                                                       array('name' => 'Mike', 'job' => 'Fisher')));
  99      $fetcher->addDecorator('TestingDatasetDecorator', array('prefix1' => 'PrefixA_',
 100                                                              'prefix2' => '_PrefixB'));
 101      $dataset = $fetcher->fetch();
 102  
 103      $dataset->rewind();
 104      $this->assertTrue($dataset->valid());
 105      $record = $dataset->current();
 106      $this->assertEqual($record->get('full'), 'PrefixA_John-Carpenter_PrefixB');
 107    }
 108  
 109    function testSetOrder()
 110    {
 111      $fetcher = new TestingFetcher();
 112      $fetcher->use_dataset = new lmbCollection(array(array('name' => 'John', 'job' => 'Carpenter'),
 113                                                       array('name' => 'Mike', 'job' => 'Fisher')));
 114      $fetcher->addDecorator('TestingDatasetDecorator');
 115      $fetcher->setOrder('title=ASC,name,last_name=DESC');
 116  
 117      $dataset = $fetcher->fetch();
 118  
 119      $this->assertEqual($dataset->sort_params, array('title' => 'ASC',
 120                                                      'name' => 'ASC',
 121                                                      'last_name' => 'DESC'));
 122    }
 123  
 124    function testExtractOrderPairsFromStringSimpleCase()
 125    {
 126      $order = lmbFetcher :: extractOrderPairsFromString('title=DESC,name=ASC');
 127      $this->assertEqual($order, array('title' => 'DESC',
 128                                       'name' => 'ASC'));
 129    }
 130  
 131    function testExtractOrderPairsFromStringSimpleRandom()
 132    {
 133      $order = lmbFetcher :: extractOrderPairsFromString('title=rand()');
 134      $this->assertEqual($order, array('title' => 'RAND()'));
 135    }
 136  
 137    function testExtractOrderPairsFromStringSimpleError()
 138    {
 139      try
 140      {
 141        lmbFetcher :: extractOrderPairsFromString('title=error');
 142        $this->assertTrue(false);
 143      }
 144      catch(lmbException $e)
 145      {
 146        $this->assertWantedPattern('/Wrong order type/', $e->getMessage());
 147      }
 148    }
 149  }
 150  ?>


Generated: Mon Dec 1 03:56:46 2008 Cross-referenced by PHPXref 0.7