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: use mechanicious\Columnizer\Column;
  3: 
  4: require_once __dir__ . '/../../../../vendor/autoload.php';
  5: 
  6: class Tableman extends \PHPUnit_Framework_TestCase
  7: {
  8:   /**
  9:    *  Data to play with
 10:    */
 11:   protected $mockData = array(
 12:       array(
 13:           'id'    => 1,
 14:           'name'  => 'Joe',
 15:           'age'   => 25
 16:       ),
 17:       array(
 18:           'id'    => 2,
 19:           'name'  => 'Tony',
 20:           'age'   => 27,
 21:           'hobby' => 'sport',
 22:       ),
 23:   );
 24: 
 25:   /**
 26:    *  Useful with formatted string comparison  
 27:    * @param   string $string
 28:    * @return  string
 29:    */
 30:   public function cleanWhiteSpace($string, $replace = "")
 31:   {
 32:     // Make sure you don't use this method together with whitespace sensitive testing
 33:     return str_replace(array("\n", "\r", "\t", " "), $replace, $string);
 34:   }
 35: 
 36:   public function testIlluminateCollectionInstatiation()
 37:   {
 38:     $collection = new \Illuminate\Support\Collection(array());
 39:     $this->assertEquals(get_class($collection), 'Illuminate\Support\Collection');
 40:   }
 41: 
 42:   public function testColumnizerColumnizeWithoutData()
 43:   {
 44:     $columnizer = new \mechanicious\Columnizer\Columnizer();
 45:     $this->assertEquals(get_class($columnizer->columnizeRowArray()), 'mechanicious\Columnizer\ColumnBag');
 46:   }
 47: 
 48:   public function testArrayAccessColumnizerColumnizeWithData()
 49:   {
 50:     $columnizer = new \mechanicious\Columnizer\Columnizer($this->mockData);
 51:     // ColumnBag
 52:     //      |--- Column (id)
 53:     //          |--- 1, 2
 54:     //    |--- Column (name)
 55:     //    |--- Column (age)
 56:     //    |--- Column (hobby)
 57:     $this->assertEquals(get_class($columnizer->columnizeRowArray()), 'mechanicious\Columnizer\ColumnBag');
 58:     $this->assertEquals(get_class($columnizer->columnizeRowArray()->get('name')), 'mechanicious\Columnizer\Column');
 59: 
 60:     // ArrayAccess in action
 61:     $this->assertEquals($columnizer->columnizeRowArray()['name'][1], 'Tony');
 62:   }
 63: 
 64:   public function testGetBS3Table()
 65:   {
 66:     $columnBag = with(new \mechanicious\Columnizer\Columnizer($this->mockData))->columnizeRowArray();
 67:     $tableman = new \mechanicious\Tableman\Tableman($columnBag);
 68:      $this->assertEquals(
 69:       $this->cleanWhiteSpace($tableman->Bs3Table(array(
 70:           'config' => array(),
 71:           'header' => array(),
 72:           'extra_classes' => array(),
 73:           'limit' => 10))),
 74:         $this->cleanWhiteSpace('
 75:         <table  class="table ">
 76:               <thead>
 77:                       <tr>
 78:                               <th></th>
 79:                               <th></th>
 80:                               <th></th>
 81:                               <th></th>
 82:                       </tr>
 83:               </thead>
 84:               <tbody>
 85:                       <tr>
 86:                               <td>1</td>
 87:                               <td>Joe</td>
 88:                               <td>25</td>
 89:                               <td></td>
 90:                       </tr>
 91:                       <tr>
 92:                               <td>2</td>
 93:                               <td>Tony</td>
 94:                               <td>27</td>
 95:                               <td>sport</td>
 96:                       </tr>
 97:               </tbody>
 98:         </table>'));
 99:   }
100: 
101:   public function testTablemanToJSON()
102:   {
103:     $columnBag = with(new \mechanicious\Columnizer\Columnizer($this->mockData))->columnizeRowArray();
104:     $tableman = new \mechanicious\Tableman\Tableman($columnBag);
105:     $this->assertEquals($this->cleanWhiteSpace($tableman->getJson()), 
106:       $this->cleanWhiteSpace('
107:       {
108:         "id":[1,2],
109:         "name":["Joe","Tony"],
110:         "age":[25,27],
111:         "hobby":[null,"sport"]
112:       }'
113:     ));
114:   }
115: 
116:   public function testEachRow()
117:   {
118:     $columnBag = with(new \mechanicious\Columnizer\Columnizer($this->mockData))->columnizeRowArray();
119:     $tableman = new \mechanicious\Tableman\Tableman($columnBag);
120:     $tableman->eachRow(function(&$ref, &$row, &$rowIndex) {
121:       // If you actually want to make changes then make sure
122:       // you **reference** items!
123:       foreach($row as $columnHeader => &$cell)
124:       {
125:         // Append an ellipsis at the very end of every cell.
126:         $cell .= "...";
127:       }
128:       if(in_array('id', $ref->getColumnHeaders()))
129:       {
130:         $ref->replaceColumn(new \mechanicious\Columnizer\Column(array(6,7), '#'), 'id');
131:       }
132: 
133:       $ref->referenceTest = 'ok';
134:     });
135:     
136:     // Test if the data changes get preserved.
137:     $this->assertEquals($this->cleanWhiteSpace($tableman->getJson('column')), 
138:       $this->cleanWhiteSpace('
139:       {
140:         "#":[6, 7],
141:         "name":["Joe...","Tony..."],
142:         "age":["25...", "27..."],
143:         "hobby":["...","sport..."]
144:       }'
145:     ));
146: 
147:     // Test if the main object get's affected. Wether the reference points to the right object.
148:     $this->assertEquals($tableman->referenceTest, 'ok');
149:   }
150: 
151:   public function testTablemanOrderColumns()
152:   {
153:     $columnBag = with(new \mechanicious\Columnizer\Columnizer($this->mockData))->columnizeRowArray();
154:     $tableman = new \mechanicious\Tableman\Tableman($columnBag);
155:     $tableman->orderColumns(array('hobby', 'age', 'id', 'name'));
156:     $this->assertEquals($this->cleanWhiteSpace($tableman->getJSON()), 
157:       $this->cleanWhiteSpace('
158:       {
159:         "hobby":[null,"sport"],
160:         "age":[25, 27],
161:         "id":[1,2],
162:         "name":["Joe","Tony"]
163:       }'
164:     ));
165:   }
166: 
167:   public function testTablemanRenameColumns()
168:   {
169:     $columnBag = with(new \mechanicious\Columnizer\Columnizer($this->mockData))->columnizeRowArray();
170:     $tableman = new \mechanicious\Tableman\Tableman($columnBag);
171:     $headers = array(
172:       'id'    => 'identification',
173:       'name'  => 'firstname',
174:       'age'   => 'level',
175:       'hobby' => 'likes',
176:       );
177: 
178:     $tableman->renameColumns($headers);
179:     $this->assertEquals($this->cleanWhiteSpace($tableman->toJson()), $this->cleanWhiteSpace('
180:         {
181:           "identification":[1,2],
182:           "firstname":["Joe","Tony"],
183:           "level":[25,27],
184:           "likes":[null,"sport"]
185:         }
186:       '));
187:   }
188: 
189:   public function testTablemanRenameColumnsEachRowCompatibility()
190:   {
191:     // We'll try to rename columns while looping through the items.
192:     $columnBag = with(new \mechanicious\Columnizer\Columnizer($this->mockData))->columnizeRowArray();
193:     $tableman = new \mechanicious\Tableman\Tableman($columnBag);
194:     $tableman->eachRow(function(&$ref, &$row, &$rowIndex) {
195:       // If you actually want to make changes then make sure
196:       // you **reference** items!
197:       foreach($row as $columnHeader => &$cell)
198:       {
199:         // Append an ellipsis at the very end of every cell.
200:         $cell .= "...";
201:       }
202: 
203:       // Renaming columns while still in the loop
204:       if(isset($ref['id'])) // should be true on the first iteration only 
205:       {
206:         $ref->renameColumns(array(
207:         'id'    => 'identification',
208:         'name'  => 'firstname',
209:         'age'   => 'level',
210:         'hobby' => 'likes',
211:       ));
212:       }
213: 
214:       if(in_array('id', $ref->getColumnHeaders()))
215:       {
216:         $ref->replaceColumn(new \mechanicious\Columnizer\Column(array(6,7), '#'), 'identification');
217:       }
218:     });
219: 
220:     $keyHeaders = $tableman->getColumnHeaders();
221:     $columnHeaders = array_map(function($column) {
222:       return $column->getHeader();
223:     }, $tableman->getColumns());
224: 
225:     $this->assertEquals($keyHeaders, array_values($columnHeaders));
226:   }
227: 
228:   public function testEachColumn()
229:   {
230:     // We'll try to rename columns while looping through the items and at the
231:     // same time we'll try to replace columns.
232:     $columnBag = with(new \mechanicious\Columnizer\Columnizer($this->mockData))->columnizeRowArray();
233:     $tableman = new \mechanicious\Tableman\Tableman($columnBag);
234:     $tableman->eachColumn(function(&$ref, &$column, $header) {
235:       // If you actually want to make changes then make sure
236:       // you **reference** items!
237:       
238:       // Replace the id column
239:       if($header === 'id')
240:       {
241:         $column = new \mechanicious\Columnizer\Column(array(3,4), 'id');
242:       }
243: 
244:       // Renaming columns while still in the loop
245:       if(isset($ref['id'])) // should be true on the first iteration only 
246:       {
247:         $ref->renameColumns(array(
248:         'id'    => 'identification',
249:         'name'  => 'firstname',
250:         'age'   => 'level',
251:         'hobby' => 'likes',
252:       ));
253:       }
254: 
255:       // Again replace the column, note that the column has different name than the
256:       // name of the column we're replacing, thus the name changes as well.
257:       if(in_array('identification', $ref->getColumnHeaders()))
258:       {
259:         $ref->replaceColumn(new \mechanicious\Columnizer\Column(array(6,7), '#'), 'identification');
260:       }
261:     });
262: 
263:     $keyHeaders = $tableman->getColumnHeaders();
264:     $columnHeaders = array_map(function($column) {
265:       return $column->getHeader();
266:     }, $tableman->getColumns());
267: 
268:     $this->assertEquals($keyHeaders, array_values($columnHeaders));
269: 
270:     $this->assertEquals($this->cleanWhiteSpace($tableman->toJson()), $this->cleanWhiteSpace('
271:         {
272:           "#":[6,7],
273:           "firstname":["Joe","Tony"],
274:           "level":[25,27],
275:           "likes":[null,"sport"]
276:         }
277:       '));
278:   }
279: 
280:   public function testColumnAdd()
281:   {
282:     // We'll try here:
283:     // 1. If data stays symmetric when adding an assymetric column (larger and smaller column in relation to the existent data)
284:     // 2. If the column gets the right offset
285:     // 3. If when adding an existing column the column gets replaced
286:     $columnBag = with(new \mechanicious\Columnizer\Columnizer($this->mockData))->columnizeRowArray();
287:     $tableman = new \mechanicious\Tableman\Tableman($columnBag);
288:     $tableman->addColumn(new \mechanicious\Columnizer\Column(array(true, false, true), 'registered'), 3);
289:     $this->assertEquals($this->cleanWhiteSpace($tableman->getJson()), 
290:       $this->cleanWhiteSpace('
291:       {
292:         "id":         [1, 2, null],
293:         "name":       ["Joe","Tony", null],
294:         "age":        [25, 27, null],
295:         "registered": [true, false, true],
296:         "hobby":      [null, "sport", null]
297:       }'
298:     ));
299: 
300:     $tableman->addColumn(new \mechanicious\Columnizer\Column(array(true), 'registered'), 2);
301:     $this->assertEquals($this->cleanWhiteSpace($tableman->getJson()), 
302:       $this->cleanWhiteSpace('
303:       {
304:         "id":         [1, 2, null],
305:         "name":       ["Joe","Tony", null],
306:         "registered": [true, null, null],
307:         "age":        [25, 27, null],
308:         "hobby":      [null, "sport", null]
309:       }'
310:     ));
311:   }
312: 
313:   public function testColumnChop()
314:   {
315:     $columnBag = with(new \mechanicious\Columnizer\Columnizer($this->mockData))->columnizeRowArray();
316:     $tableman = new \mechanicious\Tableman\Tableman($columnBag);
317:     $tableman->eachColumn(function(&$ref, &$column, $header) {
318:       $column->chop(1);
319:     });
320:     
321:     $this->assertEquals($this->cleanWhiteSpace($tableman->getJson()), 
322:       $this->cleanWhiteSpace('
323:       {
324:         "id":         [1],
325:         "name":       ["Joe"],
326:         "age":        [25],
327:         "hobby":      [null]
328:       }'
329:     ));
330:   }
331: 
332:   public function testTablemanBs3TableExtension()
333:   {
334:     $columnBag = with(new \mechanicious\Columnizer\Columnizer($this->mockData))->columnizeRowArray();
335:     $tableman = new \mechanicious\Tableman\Tableman($columnBag);
336:     $this->assertEquals(
337:       $this->cleanWhiteSpace($tableman->Bs3Table(array(
338:           'config' => array(),
339:           'header' => array(),
340:           'extra_classes' => array(),
341:           'limit' => 10))),
342:         $this->cleanWhiteSpace('
343:         <table  class="table ">
344:               <thead>
345:                       <tr>
346:                               <th></th>
347:                               <th></th>
348:                               <th></th>
349:                               <th></th>
350:                       </tr>
351:               </thead>
352:               <tbody>
353:                       <tr>
354:                               <td>1</td>
355:                               <td>Joe</td>
356:                               <td>25</td>
357:                               <td></td>
358:                       </tr>
359:                       <tr>
360:                               <td>2</td>
361:                               <td>Tony</td>
362:                               <td>27</td>
363:                               <td>sport</td>
364:                       </tr>
365:               </tbody>
366:         </table>'));
367:   } 
368: 
369:   public function testTablemanWithdraw()
370:   {
371:     $columnBag = with(new \mechanicious\Columnizer\Columnizer($this->mockData))->columnizeRowArray();
372:     $tableman = new \mechanicious\Tableman\Tableman($columnBag);
373:     $this->assertEquals($tableman->withdraw(function() {
374:       return 'withdraw string';
375:     }), 'withdraw string');
376:   }
377: 
378:   public function testSortColumns()
379:   {
380:     $columnBag = with(new \mechanicious\Columnizer\Columnizer($this->mockData))->columnizeRowArray();
381:     $tableman = new \mechanicious\Tableman\Tableman($columnBag);
382:     $tableman->sortColumns(function($current, $previous) { 
383:       if(strpos($current, 'a') !== false && strpos($previous, 'a') !== false) return 0;
384:       // Such that a is inferior
385:       return strpos($current, 'a') !== false && strpos($previous, 'a') === false ? 1 : -1; 
386:     });
387: 
388:     $this->assertEquals($tableman->getColumnHeaders(), array('hobby', 'age', 'name', 'id'));
389:   }
390: 
391:   public function testReverseColumnOrder()
392:   {
393:     $columnBag = with(new \mechanicious\Columnizer\Columnizer($this->mockData))->columnizeRowArray();
394:     $tableman = new \mechanicious\Tableman\Tableman($columnBag);
395:     $tableman->reverse();
396:     $this->assertEquals($tableman->getColumnHeaders(), array('hobby', 'age', 'name', 'id'));
397:   }
398: 
399:   public function testGetColumnHeaders()
400:   {
401:     $columnBag = with(new \mechanicious\Columnizer\Columnizer($this->mockData))->columnizeRowArray();
402:     $tableman = new \mechanicious\Tableman\Tableman($columnBag);
403:     $this->assertEquals($tableman->getColumnHeaders(), array('id', 'name', 'age', 'hobby'));
404:   }
405: 
406:   public function testColumnExists()
407:   {
408:     $columnBag = with(new \mechanicious\Columnizer\Columnizer($this->mockData))->columnizeRowArray();
409:     $tableman = new \mechanicious\Tableman\Tableman($columnBag);
410:     $this->assertEquals($tableman->columnExists('test'), false);
411:     $this->assertEquals($tableman->columnExists('id'), true);
412:   }  
413: 
414:   public function testColumnHas()
415:   {
416:     $columnBag = with(new \mechanicious\Columnizer\Columnizer($this->mockData))->columnizeRowArray();
417:     $tableman = new \mechanicious\Tableman\Tableman($columnBag);
418:     $this->assertEquals($tableman->columnHas('name', 'Tony'), true);
419:     $this->assertEquals($tableman->columnHas('age', '99'), false);
420:   }
421: 
422:   public function testPadData()
423:   {
424:     $columnBag = with(new \mechanicious\Columnizer\Columnizer($this->mockData))->columnizeRowArray();
425:     $tableman = new \mechanicious\Tableman\Tableman($columnBag);
426:     $tableman->padData(new Column(array(true, true, false), 'is_human'));
427:     // Since the mockData['names'] was only 2 row large and we aligned the columns with 3 row large column
428:     // now or name column should be prefilled to 3 rows with null
429:     $this->assertEquals($tableman->get('name')->toArray(), array('Joe', 'Tony', null));
430:   }
431: 
432:   public function testPrependColumn()
433:   {
434:     $columnBag = with(new \mechanicious\Columnizer\Columnizer($this->mockData))->columnizeRowArray();
435:     $tableman = new \mechanicious\Tableman\Tableman($columnBag);
436:     $tableman->prependColumn(new Column(array(true, true, false), 'is_human'));
437:     $this->assertEquals($tableman->getColumnHeaders()[0], 'is_human');
438:   }
439: 
440:   public function testAppendColumn()
441:   {
442:     $columnBag = with(new \mechanicious\Columnizer\Columnizer($this->mockData))->columnizeRowArray();
443:     $tableman = new \mechanicious\Tableman\Tableman($columnBag);
444:     $tableman->appendColumn(new Column(array(true, true, false), 'is_human'));
445:     $this->assertEquals(array_reverse($tableman->getColumnHeaders())[0], 'is_human');
446:   }
447: 
448:   public function testPopColumn()
449:   {
450:     $columnBag = with(new \mechanicious\Columnizer\Columnizer($this->mockData))->columnizeRowArray();
451:     $tableman = new \mechanicious\Tableman\Tableman($columnBag);
452:     $tableman->popColumn();
453:     $this->assertFalse(in_array('hobby', $tableman->getColumnHeaders()));
454:   }
455: 
456:   public function testShiftColumn()
457:   {
458:     $columnBag = with(new \mechanicious\Columnizer\Columnizer($this->mockData))->columnizeRowArray();
459:     $tableman = new \mechanicious\Tableman\Tableman($columnBag);
460:     $tableman->shiftColumn();
461:     $this->assertFalse(in_array('id', $tableman->getColumnHeaders()));
462:   }
463: 
464:   public function testRemoveColumn()
465:   {
466:     $columnBag = with(new \mechanicious\Columnizer\Columnizer($this->mockData))->columnizeRowArray();
467:     $tableman = new \mechanicious\Tableman\Tableman($columnBag);
468:     $tableman->removeColumn('name');
469:     $this->assertFalse(in_array('name', $tableman->getColumnHeaders()));
470:   }
471: 
472:   public function testCompareColumnContent()
473:   {
474:     $columnBag = with(new \mechanicious\Columnizer\Columnizer($this->mockData))->columnizeRowArray();
475:     $tableman = new \mechanicious\Tableman\Tableman($columnBag);
476:     $this->assertFalse($tableman->compareColumnContent($tableman->get('name'), $tableman->get('id')));
477:     // Although the column headers change the content should stay the same
478:     $this->assertTrue($tableman->compareColumnContent($tableman->get('name'), new Column(array('Joe', 'Tony'), 'coolnames')));
479:   }
480: 
481:   public function testReplaceColumn()
482:   {
483:     $columnBag = with(new \mechanicious\Columnizer\Columnizer($this->mockData))->columnizeRowArray();
484:     $tableman = new \mechanicious\Tableman\Tableman($columnBag);
485:     $tableman->replaceColumn(new Column(array('old', 'young'), 'approx_age'), 'age');
486:     $this->assertEquals($tableman->get('age'), null);
487:     $this->assertEquals($tableman->get('approx_age')->first(), 'old');
488:   }
489: 
490:   public function testEachRowOf()
491:   {
492:     $columnBag = with(new \mechanicious\Columnizer\Columnizer($this->mockData))->columnizeRowArray();
493:     $tableman = new \mechanicious\Tableman\Tableman($columnBag);
494:     $tableman->eachRowOf('age', function(&$ref, &$age, $rowIndex) {
495:       $age = 'oops?';
496:     });
497:     $this->assertEquals($tableman->get('age')->first(), 'oops?');
498:     $this->assertFalse($tableman->get('name')->first() === 'oops?');
499:   }
500: 
501:   public function testEachCell()
502:   {
503:     $columnBag = with(new \mechanicious\Columnizer\Columnizer($this->mockData))->columnizeRowArray();
504:     $tableman = new \mechanicious\Tableman\Tableman($columnBag);
505:     $tableman->eachCell(function(&$ref, &$cell, &$row, $rowIndex) {
506:       if($cell === $row['age']) return $cell = 100;
507:       $cell = 1;
508:     });
509:     $this->assertTrue($tableman->get('age')->first() === 100);
510:     $this->assertTrue($tableman->get('name')->first() === 1);
511:   }
512: 
513:   public function testGetColumn()
514:   {
515:     $columnBag = with(new \mechanicious\Columnizer\Columnizer($this->mockData))->columnizeRowArray();
516:     $tableman = new \mechanicious\Tableman\Tableman($columnBag);
517:     $this->assertEquals($tableman->getColumn('name')->first(), 'Joe');
518:   }
519: 
520:   public function testGetAllColumns()
521:   {
522:     $columnBag = with(new \mechanicious\Columnizer\Columnizer($this->mockData))->columnizeRowArray();
523:     $tableman = new \mechanicious\Tableman\Tableman($columnBag);
524:     $this->assertEquals($tableman->getAllColumns()['id']->last(), 2);
525:     $this->assertEquals(count($tableman->getAllColumns()), 4);
526:     $this->assertTrue($tableman->getAllColumns()['id'] instanceof \mechanicious\Columnizer\Column);
527:   }
528: 
529:   public function testGetRows()
530:   {
531:     $columnBag = with(new \mechanicious\Columnizer\Columnizer($this->mockData))->columnizeRowArray();
532:     $tableman = new \mechanicious\Tableman\Tableman($columnBag);
533:     $this->assertEquals($tableman->getRows()[1]['name'], 'Tony');
534:   }
535: }
API documentation generated by ApiGen 2.8.0