Overview

Namespaces

  • mechanicious
    • Columnizer
    • Extensions
      • Bs3Table
    • Support
    • Tableman
    • TablemanExtension
    • Test
      • Tableman
  • PHP

Classes

  • Tableman
  • TablemanCollection
  • Overview
  • Namespace
  • Class
  • Tree
  1: <?php namespace mechanicious\Test\Tableman;
  2: 
  3: require_once __dir__ . '/../../../../vendor/autoload.php';
  4: 
  5: class TablemanCollection extends \PHPUnit_Framework_TestCase
  6: {
  7:   /**
  8:    *  Data to play with
  9:    */
 10:   protected $mockData = array(
 11:       array(
 12:           'id'    => 1,
 13:           'name'  => 'Joe',
 14:           'age'   => 25
 15:       ),
 16:       array(
 17:           'id'    => 2,
 18:           'name'  => 'Tony',
 19:           'age'   => 27,
 20:           'hobby' => 'sport',
 21:       ),
 22:   );
 23: 
 24:   /**
 25:    *  Useful with formatted string comparison  
 26:    * @param   string $string
 27:    * @return  string
 28:    */
 29:   public function cleanWhiteSpace($string, $replace = "")
 30:   {
 31:     // Make sure you don't use this method together with whitespace sensitive testing
 32:     return str_replace(array("\n", "\r", "\t", " "), $replace, $string);
 33:   }
 34: 
 35:   public function testMake()
 36:   {
 37:     $columnBag = with(new \mechanicious\Columnizer\Columnizer($this->mockData))->columnizeRowArray();
 38:     $tableman = new \mechanicious\Tableman\Tableman($columnBag);
 39:     $this->assertTrue($tableman->make(new \mechanicious\Columnizer\ColumnBag($this->mockData)) instanceof \mechanicious\Tableman\Tableman);
 40:   }
 41: 
 42:   public function testAll()
 43:   {
 44:     $columnBag = with(new \mechanicious\Columnizer\Columnizer($this->mockData))->columnizeRowArray();
 45:     $tableman = new \mechanicious\Tableman\Tableman($columnBag);
 46:     $this->assertTrue(is_array($tableman->all()));
 47:     $this->assertTrue($tableman->all()['id'] instanceof \mechanicious\Columnizer\Column);
 48:   }
 49: 
 50:   public function testCollapse()
 51:   {
 52:     $columnBag = with(new \mechanicious\Columnizer\Columnizer($this->mockData))->columnizeRowArray();
 53:     $tableman = new \mechanicious\Tableman\Tableman($columnBag);
 54:     $this->assertEquals(count($tableman->collapse()), 8);
 55:   }
 56: 
 57:   public function testDiff()
 58:   {
 59:     $columnBag = with(new \mechanicious\Columnizer\Columnizer($this->mockData))->columnizeRowArray();
 60:     $tableman = new \mechanicious\Tableman\Tableman($columnBag);
 61:     $deleted = array_pop($this->mockData);
 62:     // Note: $tableman get's prefilled with null, we deleted 2 entry from the testee.
 63:     $this->assertEquals($tableman->diff($this->mockData), array(array('hobby' => null), $deleted));
 64:   }
 65: 
 66:   public function testEach()
 67:   {
 68:     // Since each() is an alias of eachColumn, we borrow the test from TablemanMethods tests.
 69:     
 70:     // We'll try to rename columns while looping through the items and at the
 71:     // same time we'll try to replace columns.
 72:     $columnBag = with(new \mechanicious\Columnizer\Columnizer($this->mockData))->columnizeRowArray();
 73:     $tableman = new \mechanicious\Tableman\Tableman($columnBag);
 74:     $tableman->eachColumn(function(&$ref, &$column, $header) {
 75:       // If you actually want to make changes then make sure
 76:       // you **reference** items!
 77:       
 78:       // Replace the id column
 79:       if($header === 'id')
 80:       {
 81:         $column = new \mechanicious\Columnizer\Column(array(3,4), 'id');
 82:       }
 83: 
 84:       // Renaming columns while still in the loop
 85:       if(isset($ref['id'])) // should be true on the first iteration only 
 86:       {
 87:         $ref->renameColumns(array(
 88:         'id'    => 'identification',
 89:         'name'  => 'firstname',
 90:         'age'   => 'level',
 91:         'hobby' => 'likes',
 92:       ));
 93:       }
 94: 
 95:       // Again replace the column, note that the column has different name than the
 96:       // name of the column we're replacing, thus the name changes as well.
 97:       if(in_array('identification', $ref->getColumnHeaders()))
 98:       {
 99:         $ref->replaceColumn(new \mechanicious\Columnizer\Column(array(6,7), '#'), 'identification');
100:       }
101:     });
102: 
103:     $keyHeaders = $tableman->getColumnHeaders();
104:     $columnHeaders = array_map(function($column) {
105:       return $column->getHeader();
106:     }, $tableman->getColumns());
107: 
108:     $this->assertEquals($keyHeaders, array_values($columnHeaders));
109: 
110:     $this->assertEquals($this->cleanWhiteSpace($tableman->toJson()), $this->cleanWhiteSpace('
111:         {
112:           "#":[6,7],
113:           "firstname":["Joe","Tony"],
114:           "level":[25,27],
115:           "likes":[null,"sport"]
116:         }
117:       '));
118:   }
119: 
120:   public function testFetch()
121:   {
122:     $columnBag = with(new \mechanicious\Columnizer\Columnizer($this->mockData))->columnizeRowArray();
123:     $tableman = new \mechanicious\Tableman\Tableman($columnBag);
124:     $this->assertEquals($tableman->fetch('id'), new \Illuminate\Support\Collection(array(1, 2)));
125:   }
126: 
127:   public function testFilter()
128:   {
129:     $columnBag = with(new \mechanicious\Columnizer\Columnizer($this->mockData))->columnizeRowArray();
130:     $tableman = new \mechanicious\Tableman\Tableman($columnBag);
131: 
132:     $this->assertEquals($tableman->filter(function($value) { 
133:       if($value['name'] === "Tony")
134:         return $value;
135:     }), new \Illuminate\Support\Collection(array(1 => array(
136:           'id'    => 2,
137:           'name'  => 'Tony',
138:           'age'   => 27,
139:           'hobby' => 'sport',
140:       ))));
141:   }
142: 
143:   public function testFirst()
144:   {
145:     $columnBag = with(new \mechanicious\Columnizer\Columnizer($this->mockData))->columnizeRowArray();
146:     $tableman = new \mechanicious\Tableman\Tableman($columnBag);
147:     $this->assertTrue($tableman->first() instanceof \mechanicious\Columnizer\Column);
148:     // First column is the id column
149:     $this->assertEquals($tableman->first()[0], 1);
150:   }
151: 
152:   public function testFlatten()
153:   { 
154:     $columnBag = with(new \mechanicious\Columnizer\Columnizer($this->mockData))->columnizeRowArray();
155:     $tableman = new \mechanicious\Tableman\Tableman($columnBag);
156:     $this->assertEquals($tableman->flatten()->all(), $columnBag->flatten()->all());
157:   }
158: 
159:   public function testForget()
160:   {
161:     $columnBag = with(new \mechanicious\Columnizer\Columnizer($this->mockData))->columnizeRowArray();
162:     $tableman = new \mechanicious\Tableman\Tableman($columnBag);
163:     $tableman->forget('id');
164:     $this->assertEquals($tableman->get('id'), null);
165:   }
166: 
167:   public function testGet()
168:   {
169:     $columnBag = with(new \mechanicious\Columnizer\Columnizer($this->mockData))->columnizeRowArray();
170:     $tableman = new \mechanicious\Tableman\Tableman($columnBag);
171:     $this->assertEquals($tableman->get('id')->all(), array(1, 2));
172:   }
173: 
174:   public function testGroupBy()
175:   {
176:     $columnBag = with(new \mechanicious\Columnizer\Columnizer($this->mockData))->columnizeRowArray();
177:     $tableman = new \mechanicious\Tableman\Tableman($columnBag);
178:     // This function needs to be reimplemented.
179:     $this->assertEquals($tableman->groupBy('hobby'), $tableman->groupBy('hobby'));
180:   }
181: 
182:   public function testHas()
183:   {
184:     $columnBag = with(new \mechanicious\Columnizer\Columnizer($this->mockData))->columnizeRowArray();
185:     $tableman = new \mechanicious\Tableman\Tableman($columnBag);
186:     $this->assertTrue($tableman->has('id'));
187:   }
188: 
189:   public function testImplode()
190:   {
191:     $columnBag = with(new \mechanicious\Columnizer\Columnizer($this->mockData))->columnizeRowArray();
192:     $tableman = new \mechanicious\Tableman\Tableman($columnBag);
193:     $this->assertEquals($tableman->implode('name', ', '), 'Joe, Tony');
194:   }
195: }
API documentation generated by ApiGen 2.8.0