[ Index ]

PHP Cross Reference of Limb3

title

Body

[close]

/wact/tests/cases/tags/fetch/ -> WactFetchTagTest.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  require_once('limb/wact/src/components/fetch/WactFetcher.interface.php');
  10  require_once('limb/wact/src/components/WactArrayIteratorDecorator.class.php');
  11  
  12  class TestingFetchTagsDataset extends WactArrayIterator
  13  {
  14    function sort($sort_params)
  15    {
  16      throw new WactException('Sorting is not implemented', array('sort_params' => $sort_params));
  17    }
  18  }
  19  
  20  class TestingTemplateDatasetDecorator extends WactArrayIteratorDecorator
  21  {
  22    var $prefix1;
  23    var $prefix2;
  24    var $sort_params;
  25  
  26    function setPrefix1($prefix)
  27    {
  28      $this->prefix1 = $prefix;
  29    }
  30  
  31    function setPrefix2($prefix)
  32    {
  33      $this->prefix2 = $prefix;
  34    }
  35  
  36    function sort($sort_params)
  37    {
  38      $this->sort_params = $sort_params;
  39    }
  40  
  41    function current()
  42    {
  43      $record = parent :: current();
  44      $data = $record->export();
  45      $data['full'] = $this->prefix1 . $data['title'] . '-' . $data['description'] . $this->prefix2;
  46      return new WactArrayObject($data);
  47    }
  48  }
  49  
  50  class TestingFetchTagsDatasetFetcher implements WactFetcher
  51  {
  52    static $stub_dataset;
  53    protected $extra_param;
  54  
  55    function fetch()
  56    {
  57      $result = array();
  58      foreach(self :: $stub_dataset as $key => $value)
  59      {
  60        $result[$key] = $value;
  61        if($this->extra_param)
  62          $result[$key]['param'] = $this->extra_param;
  63      }
  64  
  65      return new TestingFetchTagsDataset($result);
  66    }
  67  
  68    function setExtraParam($value)
  69    {
  70      $this->extra_param = $value;
  71    }
  72  
  73    static function setStubDataset($dataset)
  74    {
  75      self :: $stub_dataset = $dataset;
  76    }
  77  }
  78  
  79  class WactFetchTagTest extends WactTemplateTestCase
  80  {
  81    function setUp()
  82    {
  83      parent :: setUp();
  84  
  85      $dataset =  array(array('title' => 'joe', 'description' => 'fisher'),
  86                        array('title' => 'ivan', 'description' => 'gamer'));
  87  
  88      TestingFetchTagsDatasetFetcher :: setStubDataset($dataset);
  89    }
  90  
  91    function testSaveDatasetToVar()
  92    {
  93      $template = '<fetch using="TestingFetchTagsDatasetFetcher" to="data"/>' .
  94                  '<list:LIST from="data"><list:ITEM>{$title}|</list:ITEM></list:LIST>';
  95  
  96      $this->registerTestingTemplate('/tags/fetch/save_dataset_to_var.html', $template);
  97  
  98      $page = $this->initTemplate('/tags/fetch/save_dataset_to_var.html');
  99  
 100      $this->assertEqual(trim($page->capture()), 'joe|ivan|');
 101    }
 102  
 103    function testPathBasedTargetVarsAreNotSupported()
 104    {
 105      $template = '<fetch using="TestingFetchTagsDatasetFetcher" to="path.to.var"/>' .
 106                  '<list:LIST from="data"><list:ITEM>{$title}|</list:ITEM></list:LIST>';
 107  
 108      $this->registerTestingTemplate('/tags/fetch/buffer_path_based_vars_are_not_supported.html', $template);
 109  
 110      try
 111      {
 112        $page = $this->initTemplate('/tags/fetch/buffer_path_based_vars_are_not_supported.html');
 113        $this->assertTrue(false);
 114      }
 115      catch(WactException $e)
 116      {
 117        $this->assertWantedPattern('/Path based variable name is not supported in buffer attribute/', $e->getMessage());
 118        $this->assertEqual($e->getParam('expression'), 'path.to.var');
 119      }
 120    }
 121  
 122    function testSaveDatasetToVarInDatasource()
 123    {
 124      $template = '<core:datasource id="data"/><fetch using="TestingFetchTagsDatasetFetcher" to="[data]var1"/>' .
 125                  '<list:LIST from="{$#[data]var1}"><list:ITEM>{$title}|</list:ITEM></list:LIST>';
 126  
 127      $this->registerTestingTemplate('/tags/fetch/save_dataset_to_datasource_buffer.html', $template);
 128  
 129      $page = $this->initTemplate('/tags/fetch/save_dataset_to_datasource_buffer.html');
 130  
 131      $this->assertEqual(trim($page->capture()), 'joe|ivan|');
 132    }
 133  
 134    function testSaveDatasetToVarInWrongDatasourceThrowException()
 135    {
 136      $template = '<list:list id="data"/><fetch using="TestingFetchTagsDatasetFetcher" to="[data]var1"/>' .
 137                  '<list:LIST from="{$[data]var1}"><list:ITEM>{$title}|</list:ITEM></list:LIST>';
 138  
 139      $this->registerTestingTemplate('/tags/fetch/save_dataset_to_var_in_wrong_datasource.html', $template);
 140  
 141      try
 142      {
 143        $page = $this->initTemplate('/tags/fetch/save_dataset_to_var_in_wrong_datasource.html');
 144        $this->assertTrue(false);
 145      }
 146      catch(WactException $e)
 147      {
 148        $this->assertWantedPattern('/None existing expression datasource context/', $e->getMessage());
 149        $this->assertEqual($e->getParam('expression'), '[data]var1');
 150      }
 151    }
 152  
 153    function testSaveDatasetToListList()
 154    {
 155      $template = '<list:list id="data"/><fetch using="TestingFetchTagsDatasetFetcher" to="[data]"/>' .
 156                   '<list:LIST from="{$#[data]}"><list:ITEM>{$title}|</list:ITEM></list:LIST>';
 157  
 158      $this->registerTestingTemplate('/tags/fetch/save_dataset_to_list_list.html', $template);
 159  
 160      $page = $this->initTemplate('/tags/fetch/save_dataset_to_list_list.html');
 161  
 162      $this->assertEqual(trim($page->capture()), 'joe|ivan|');
 163    }
 164  
 165    function testSaveDatasetToNonExistingTargetDatasourceThrowException()
 166    {
 167      $template = '<fetch using="TestingFetchTagsDatasetFetcher" to="[no_such_datasource]"/>' .
 168                  '<list:LIST from="{$[data]}"><list:ITEM>{$title}|</list:ITEM></list:LIST>';
 169  
 170      $this->registerTestingTemplate('/tags/fetch/save_dataset_to_none_existing_target_datasource.html', $template);
 171  
 172      try
 173      {
 174        $page = $this->initTemplate('/tags/fetch/save_dataset_to_none_existing_target_datasource.html');
 175        $this->assertTrue(false);
 176      }
 177      catch(WactException $e)
 178      {
 179        $this->assertWantedPattern('/None existing expression datasource context/', $e->getMessage());
 180      }
 181    }
 182  
 183    function testSaveRecordToDatasource()
 184    {
 185      $template = '<core:datasource id="data"/>'.
 186                  '<fetch using="TestingFetchTagsDatasetFetcher" to="[data]" first="true"/>' .
 187                  '<core:datasource from="[data]">{$title}</core:datasource>';
 188  
 189      $this->registerTestingTemplate('/tags/fetch/save_record_to_datasource.html', $template);
 190  
 191      $page = $this->initTemplate('/tags/fetch/save_record_to_datasource.html');
 192  
 193      $this->assertEqual(trim($page->capture()), 'joe');
 194    }
 195  
 196    function testSaveRecordToDatasourceVar()
 197    {
 198      $template = '<core:datasource id="data"/>'.
 199                  '<fetch using="TestingFetchTagsDatasetFetcher" to="[data]var" first="true"/>' .
 200                  '<core:datasource from="[data]var">{$title}</core:datasource>';
 201  
 202      $this->registerTestingTemplate('/tags/fetch/save_record_to_datasource_var.html', $template);
 203  
 204      $page = $this->initTemplate('/tags/fetch/save_record_to_datasource_var.html');
 205  
 206      $this->assertEqual(trim($page->capture()), 'joe');
 207    }
 208  
 209    function testMultipleTargets()
 210    {
 211      $template = '<fetch using="TestingFetchTagsDatasetFetcher" to="[testTarget1],[testTarget2]" />' .
 212                  '<list:LIST id="testTarget1"><list:ITEM>{$title}-</list:ITEM></list:LIST>' .
 213                  '<list:LIST id="testTarget2"><list:ITEM>{$description}|</list:ITEM></list:LIST>';
 214  
 215      $this->registerTestingTemplate('/tags/fetch/dataset_multiple_targets.html', $template);
 216  
 217      $page = $this->initTemplate('/tags/fetch/dataset_multiple_targets.html');
 218  
 219      $this->assertEqual($page->capture(), 'joe-ivan-fisher|gamer|');
 220    }
 221  
 222    function testWithNavigator()
 223    {
 224      $template = '<fetch using="TestingFetchTagsDatasetFetcher" to="[testTarget]"  navigator="pagenav" />' .
 225                  '<list:LIST id="testTarget"><list:ITEM>{$title}|</list:ITEM></list:LIST>'.
 226                  '<pager:NAVIGATOR id="pagenav" items="10"></pager:NAVIGATOR>';
 227  
 228      $this->registerTestingTemplate('/tags/fetch/dataset_with_navigator.html', $template);
 229  
 230      $page = $this->initTemplate('/tags/fetch/dataset_with_navigator.html');
 231  
 232      $this->assertEqual($page->capture(), 'joe|ivan|');
 233  
 234      $pager = $page->findChild('pagenav');
 235      $this->assertEqual($pager->getTotalItems(), 2);
 236    }
 237  
 238    function testOnlyRecord()
 239    {
 240      $template = '<fetch using="TestingFetchTagsDatasetFetcher" to="[testTarget]" first="true" />' .
 241                  '<core:datasource id="testTarget">{$title}</core:datasource>';
 242  
 243      $this->registerTestingTemplate('/tags/fetch/dataset_only_record.html', $template);
 244  
 245      $page = $this->initTemplate('/tags/fetch/dataset_only_record.html');
 246  
 247      $this->assertEqual($page->capture(), 'joe');
 248    }
 249  
 250    function testApplyDecorators()
 251    {
 252      $template = '<fetch using="TestingFetchTagsDatasetFetcher" to="[testTarget]" >' .
 253                  '<fetch:decorate using="TestingTemplateDatasetDecorator"/>' .
 254                  '</fetch>' .
 255                  '<list:LIST id="testTarget"><list:ITEM>{$full}|</list:ITEM></list:LIST>';
 256  
 257      $this->registerTestingTemplate('/tags/fetch/dataset_with_decorators.html', $template);
 258  
 259      $page = $this->initTemplate('/tags/fetch/dataset_with_decorators.html');
 260  
 261      $this->assertEqual(trim($page->capture()), 'joe-fisher|ivan-gamer|');
 262    }
 263  
 264    function testSingleTargetWithDBEParam()
 265    {
 266      $template = '<fetch using="TestingFetchTagsDatasetFetcher" to="[testTarget]">' .
 267                  '<fetch:param extra_param="{$#genger}"/>' .
 268                  '</fetch>' .
 269                  '<list:LIST id="testTarget"><list:ITEM>{$title}-{$param}|</list:ITEM></list:LIST>';
 270  
 271      $this->registerTestingTemplate('/tags/fetch/dataset_single_target_with_dbe.html', $template);
 272  
 273      $page = $this->initTemplate('/tags/fetch/dataset_single_target_with_dbe.html');
 274      $page->set('genger', 'Man');
 275  
 276      $this->assertEqual(trim($page->capture()), 'joe-Man|ivan-Man|');
 277    }
 278  
 279    function testSingleTargetWithComplexDBEParam()
 280    {
 281      $template = '<fetch using="TestingFetchTagsDatasetFetcher" to="[testTarget]">' .
 282                  '<fetch:param extra_param="{$#request.gender}"/>' .
 283                  '</fetch>' .
 284                  '<list:LIST id="testTarget"><list:ITEM>{$title}-{$param}|</list:ITEM></list:LIST>';
 285  
 286      $this->registerTestingTemplate('/tags/fetch/dataset_single_target_with_complex_dbe.html', $template);
 287  
 288      $page = $this->initTemplate('/tags/fetch/dataset_single_target_with_complex_dbe.html');
 289  
 290      $page->set('request', array('gender' => 'Man'));
 291  
 292      $this->assertEqual(trim($page->capture()), 'joe-Man|ivan-Man|');
 293    }
 294  
 295    function testApplyDecoratorsWithExtraParams()
 296    {
 297      $template = '<fetch using="TestingFetchTagsDatasetFetcher" to="[testTarget]">' .
 298                  '<fetch:decorate using="TestingTemplateDatasetDecorator" prefix1="Hi-" prefix2="-!!"/>' .
 299                  '</fetch>' .
 300                  '<list:LIST id="testTarget"><list:ITEM>{$full}|</list:ITEM></list:LIST>';
 301  
 302      $this->registerTestingTemplate('/tags/fetch/dataset_with_decorators_with_params.html', $template);
 303  
 304      $page = $this->initTemplate('/tags/fetch/dataset_with_decorators_with_params.html');
 305  
 306      $this->assertEqual(trim($page->capture()), 'Hi-joe-fisher-!!|Hi-ivan-gamer-!!|');
 307    }
 308  
 309    function testOrderParamIsPassedFromParamTag()
 310    {
 311      $template = '<fetch using="TestingFetchTagsDatasetFetcher" to="[testTarget]" >' .
 312                  '<fetch:param order="title=ASC"/>' .
 313                  '</fetch>' .
 314                  '<list:LIST id="testTarget"><list:ITEM>{$title}-{$description}|</list:ITEM></list:LIST>';
 315  
 316      $this->registerTestingTemplate('/tags/fetch/dataset_with_sort_params_by_param_tag.html', $template);
 317  
 318      $page = $this->initTemplate('/tags/fetch/dataset_with_sort_params_by_param_tag.html');
 319  
 320      try
 321      {
 322        $page->capture();
 323      }
 324      catch(WactException $e)
 325      {
 326        $this->assertWantedPattern('/Sorting is not implemented/', $e->getMessage()); // see TestingFetchTagsDataset class at the top

 327        $this->assertEqual($e->getParam('sort_params'), array('title' => 'ASC'));
 328      }
 329    }
 330  
 331    function testOrderParamIsPassedFromFetchTagAttribute()
 332    {
 333      $template = '<fetch using="TestingFetchTagsDatasetFetcher" to="[testTarget]" order="title"/>' .
 334                  '<list:LIST id="testTarget"><list:ITEM>{$title}-{$description}|</list:ITEM></list:LIST>';
 335  
 336      $this->registerTestingTemplate('/tags/fetch/dataset_with_sort_params_by_tag_attribute.html', $template);
 337  
 338      $page = $this->initTemplate('/tags/fetch/dataset_with_sort_params_by_tag_attribute.html');
 339  
 340      try
 341      {
 342        $page->capture();
 343      }
 344      catch(WactException $e)
 345      {
 346        $this->assertWantedPattern('/Sorting is not implemented/', $e->getMessage()); // see TestingFetchTagsDataset class at the top

 347        $this->assertEqual($e->getParam('sort_params'), array('title' => 'ASC'));
 348      }
 349    }
 350  
 351    function testOffsetAndLimitFromParamTag()
 352    {
 353      $template = '<fetch using="TestingFetchTagsDatasetFetcher" to="[testTarget]">' .
 354                  '<fetch:param offset="1" limit="1"/></fetch>'.
 355                  '<list:LIST id="testTarget"><list:ITEM>{$title}|</list:ITEM></list:LIST>';
 356  
 357      $this->registerTestingTemplate('/tags/fetch/dataset_with_offset_limit_by_param_tag.html', $template);
 358  
 359      $page = $this->initTemplate('/tags/fetch/dataset_with_offset_limit_by_param_tag.html');
 360  
 361      $this->assertEqual($page->capture(), 'ivan|');
 362    }
 363  
 364    function testLimitNoOffsetFromParamTag()
 365    {
 366      $template = '<fetch using="TestingFetchTagsDatasetFetcher" to="[testTarget]">' .
 367                  '<fetch:param limit="1"/></fetch>'.
 368                  '<list:LIST id="testTarget"><list:ITEM>{$title}|</list:ITEM></list:LIST>';
 369  
 370      $this->registerTestingTemplate('/tags/fetch/dataset_with_limit_no_offset_by_param_tag.html', $template);
 371  
 372      $page = $this->initTemplate('/tags/fetch/dataset_with_limit_no_offset_by_param_tag.html');
 373  
 374      $this->assertEqual($page->capture(), 'joe|');
 375    }
 376  
 377    function testOffsetAndLimitFromFetchTagAttributes()
 378    {
 379      $template = '<fetch using="TestingFetchTagsDatasetFetcher" to="[testTarget]" offset="1" limit="1" />' .
 380                  '<list:LIST id="testTarget"><list:ITEM>{$title}|</list:ITEM></list:LIST>'.
 381                  '<pager:NAVIGATOR id="pagenav" items="10"></pager:NAVIGATOR>';
 382  
 383      $this->registerTestingTemplate('/tags/fetch/dataset_with_offset_limit_by_attributes.html', $template);
 384  
 385      $page = $this->initTemplate('/tags/fetch/dataset_with_offset_limit_by_attributes.html');
 386  
 387      $this->assertEqual($page->capture(), 'ivan|');
 388    }
 389  
 390    function testDatasetIsCachedByDetault()
 391    {
 392      $template = '<fetch using="TestingFetchTagsDatasetFetcher" to="[testTarget]" />' .
 393                  '<list:LIST id="testTarget"><list:ITEM>{$title}|</list:ITEM></list:LIST>';
 394  
 395      $this->registerTestingTemplate('/tags/fetch/fetched_dataset_is_cached.html', $template);
 396  
 397      $page = $this->initTemplate('/tags/fetch/fetched_dataset_is_cached.html');
 398  
 399      $this->assertEqual(trim($page->capture()), 'joe|ivan|');
 400  
 401      // let's change dataset and see what is does not have any affect

 402      $dataset = array(array('title' => 'vika', 'description' => 'dancer'),
 403                        array('title' => 'loly', 'description' => 'stripper'));
 404  
 405      TestingFetchTagsDatasetFetcher :: setStubDataset($dataset);
 406  
 407      $this->assertEqual(trim($page->capture()), 'joe|ivan|');
 408    }
 409  
 410    function testDatasetCachingCanBeDisabled()
 411    {
 412      $template = '<fetch using="TestingFetchTagsDatasetFetcher" to="[testTarget]" cache_dataset="false"/>' .
 413                  '<list:LIST id="testTarget"><list:ITEM>{$title}|</list:ITEM></list:LIST>';
 414  
 415      $this->registerTestingTemplate('/tags/fetch/feched_dataset_caching_disabled.html', $template);
 416  
 417      $page = $this->initTemplate('/tags/fetch/feched_dataset_caching_disabled.html');
 418  
 419      $this->assertEqual(trim($page->capture()), 'joe|ivan|');
 420  
 421      // let's change dataset and see what is does not have any affect

 422      $dataset = array(array('title' => 'vika', 'description' => 'dancer'),
 423                       array('title' => 'loly', 'description' => 'stripper'));
 424  
 425      TestingFetchTagsDatasetFetcher :: setStubDataset($dataset);
 426  
 427      $this->assertEqual(trim($page->capture()), 'vika|loly|');
 428    }
 429  
 430    function testMultipleTargetsForBC()
 431    {
 432      $template = '<fetch using="TestingFetchTagsDatasetFetcher" target="testTarget1,testTarget2" />' .
 433                  '<list:LIST id="testTarget1"><list:ITEM>{$title}-</list:ITEM></list:LIST>' .
 434                  '<list:LIST id="testTarget2"><list:ITEM>{$description}|</list:ITEM></list:LIST>';
 435  
 436      $this->registerTestingTemplate('/tags/fetch/dataset_multiple_targets_in_target_for_bc.html', $template);
 437  
 438      $page = $this->initTemplate('/tags/fetch/dataset_multiple_targets_in_target_for_bc.html');
 439  
 440      $this->assertEqual($page->capture(), 'joe-ivan-fisher|gamer|');
 441    }
 442  
 443    function testTargetAttributeForBC()
 444    {
 445      $template = '<fetch using="TestingFetchTagsDatasetFetcher" target="testTarget" />' .
 446                  '<list:LIST id="testTarget"><list:ITEM>{$title}|</list:ITEM></list:LIST>';
 447  
 448      $this->registerTestingTemplate('/tags/fetch/dataset_single_target_for_bc.html', $template);
 449  
 450      $page = $this->initTemplate('/tags/fetch/dataset_single_target_for_bc.html');
 451  
 452      $this->assertEqual(trim($page->capture()), 'joe|ivan|');
 453    }
 454  }
 455  ?>