コード例 #1
0
 public function testEach()
 {
     // Since each() is an alias of eachColumn, we borrow the test from TablemanMethods tests.
     // We'll try to rename columns while looping through the items and at the
     // same time we'll try to replace columns.
     $columnBag = with(new \mechanicious\Columnizer\Columnizer($this->mockData))->columnizeRowArray();
     $tableman = new \mechanicious\Tableman\Tableman($columnBag);
     $tableman->eachColumn(function (&$ref, &$column, $header) {
         // If you actually want to make changes then make sure
         // you **reference** items!
         // Replace the id column
         if ($header === 'id') {
             $column = new \mechanicious\Columnizer\Column(array(3, 4), 'id');
         }
         // Renaming columns while still in the loop
         if (isset($ref['id'])) {
             $ref->renameColumns(array('id' => 'identification', 'name' => 'firstname', 'age' => 'level', 'hobby' => 'likes'));
         }
         // Again replace the column, note that the column has different name than the
         // name of the column we're replacing, thus the name changes as well.
         if (in_array('identification', $ref->getColumnHeaders())) {
             $ref->replaceColumn(new \mechanicious\Columnizer\Column(array(6, 7), '#'), 'identification');
         }
     });
     $keyHeaders = $tableman->getColumnHeaders();
     $columnHeaders = array_map(function ($column) {
         return $column->getHeader();
     }, $tableman->getColumns());
     $this->assertEquals($keyHeaders, array_values($columnHeaders));
     $this->assertEquals($this->cleanWhiteSpace($tableman->toJson()), $this->cleanWhiteSpace('
     {
       "#":[6,7],
       "firstname":["Joe","Tony"],
       "level":[25,27],
       "likes":[null,"sport"]
     }
   '));
 }