| [ Index ] |
PHP Cross Reference of Limb3 |
[Summary view] [Print] [Text view]
1 <?php 2 /* 3 * Limb PHP Framework 4 * 5 * @link http://limb-project.com 6 * @copyright Copyright © 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/lmbSet.class.php'); 11 12 class lmbCollectionTest extends UnitTestCase 13 { 14 function testEmptyIterator() 15 { 16 $iterator = new lmbCollection(array()); 17 $iterator->rewind(); 18 $this->assertFalse($iterator->valid()); 19 } 20 21 function testIterateArrayWithFalseValue() 22 { 23 $iterator = new lmbCollection(array(false)); 24 $iterator->rewind(); 25 $this->assertFalse($iterator->valid()); 26 } 27 28 function testIterate() 29 { 30 $data = array (array('x' => 1,'y' => 2), 31 array('x' => 3,'y' => 4), 32 array('x' => 5,'y' => 6)); 33 34 $iterator = new lmbCollection($data); 35 $iterator->rewind(); 36 $this->assertTrue($iterator->valid()); 37 38 $dataspace1 = $iterator->current(); 39 $this->assertEqual($dataspace1->export(), array('x' => 1,'y' => 2)); 40 41 $iterator->next(); 42 $dataspace2 = $iterator->current(); 43 $this->assertEqual($dataspace2->export(), array('x' => 3,'y' => 4)); 44 } 45 46 function testIterateOver() 47 { 48 $data = array (array('x' => 1,'y' => 2), 49 array('x' => 3,'y' => 4)); 50 $iterator = new lmbCollection($data); 51 $iterator->rewind(); 52 $iterator->next(); 53 $iterator->next(); 54 $this->assertFalse($iterator->valid()); 55 $dataspace = $iterator->current(); 56 $this->assertEqual($dataspace->export(), array()); 57 } 58 59 function testIterateWithForeach() 60 { 61 $data = array (array('x' => '1'), 62 array('x' => '2'), 63 array('x' => '3')); 64 65 $iterator = new lmbCollection($data); 66 67 $str = ''; 68 foreach($iterator as $record) 69 $str .= $record->get('x'); 70 71 $this->assertEqual($str, '123'); 72 } 73 74 function testWorksOkWithArrayOfSets() 75 { 76 $data = array(new lmbSet(array('x' => '1')), 77 new lmbSet(array('x' => '2')), 78 new lmbSet(array('x' => '3'))); 79 80 $iterator = new lmbCollection($data); 81 82 $str = ''; 83 foreach($iterator as $record) 84 $str .= $record->get('x'); 85 86 $this->assertEqual($str, '123'); 87 } 88 89 function testAdd() 90 { 91 $item1 = new lmbSet(array('x' => 1,'y' => 2)); 92 $item2 = new lmbSet(array('x' => 3,'y' => 4)); 93 94 $iterator = new lmbCollection(); 95 $this->assertTrue($iterator->isEmpty()); 96 $iterator->add($item1); 97 $this->assertFalse($iterator->isEmpty()); 98 $iterator->add($item2); 99 100 $iterator->rewind(); 101 $this->assertTrue($iterator->valid()); 102 103 $this->assertEqual($iterator->current(), $item1); 104 $iterator->next(); 105 $this->assertEqual($iterator->current(), $item2); 106 } 107 108 function testSort() 109 { 110 $data = array (array('x' => 'C'), 111 array('x' => 'A'), 112 array('x' => 'B')); 113 114 $iterator = new lmbCollection($data); 115 $iterator->sort(array('x' => 'DESC')); 116 $arr = $iterator->getArray(); 117 $this->assertEqual($arr[0]['x'], 'C'); 118 $this->assertEqual($arr[1]['x'], 'B'); 119 $this->assertEqual($arr[2]['x'], 'A'); 120 } 121 122 function testSortWorksOkWithSetsToo() 123 { 124 $item1 = new lmbSet(array('x' => 'C')); 125 $item2 = new lmbSet(array('x' => 'A')); 126 $item3 = new lmbSet(array('x' => 'B')); 127 128 $iterator = new lmbCollection(array($item1, $item2, $item3)); 129 $iterator->sort(array('x' => 'DESC')); 130 $arr = $iterator->getArray(); 131 $this->assertEqual($arr[0]->get('x'), 'C'); 132 $this->assertEqual($arr[1]->get('x'), 'B'); 133 $this->assertEqual($arr[2]->get('x'), 'A'); 134 } 135 136 function testDontSortEmptyCollection() 137 { 138 $iterator = new lmbCollection(); 139 $iterator->sort(array('x' => 'DESC')); 140 $this->assertEqual($iterator->getArray(), array()); 141 } 142 143 function testConcat() 144 { 145 $item1 = new lmbSet(array('x' => 'C')); 146 $item2 = new lmbSet(array('x' => 'A')); 147 $item3 = new lmbSet(array('x' => 'B')); 148 $item4 = new lmbSet(array('x' => 'D')); 149 150 $col1 = new lmbCollection(array($item1, $item2)); 151 $col2 = new lmbCollection(array($item3)); 152 $col3 = new lmbCollection(array($item4)); 153 154 $this->assertEqual(lmbCollection :: concat($col1, $col2, $col3), 155 new lmbCollection(array($item1, $item2, $item3, $item4))); 156 } 157 } 158 ?>
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Fri Dec 5 04:05:07 2008 | Cross-referenced by PHPXref 0.7 |