Skip to content

Commit

Permalink
Added TabularCollection tests (started them)
Browse files Browse the repository at this point in the history
* references "feature"
  • Loading branch information
nozavroni committed Dec 5, 2016
1 parent a0edfd6 commit 5198c22
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/CSVelte/Collection/TabularCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,13 @@ protected function isConsistentDataStructure($data)

return static::isTabular($data);
}

public function map(callable $callback)
{
$ret = [];
foreach ($this->data as $key => $row) {
$ret[$key] = $callback(static::factory($row));
}
return static::factory($ret);
}
}
30 changes: 30 additions & 0 deletions tests/CSVelte/Collection/AbstractCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace CSVelteTest\Collection;

use CSVelte\Collection\MultiCollection;
use CSVelte\Collection\TabularCollection;
use CSVelteTest\UnitTestCase;
use Faker;

Expand Down Expand Up @@ -44,5 +45,34 @@ public function setUp()
$this->testdata[MultiCollection::class]['words'][] = $faker->words;
$this->testdata[MultiCollection::class]['userAgent'][] = $faker->userAgent;
}
$this->testdata[TabularCollection::class] = [
'user' => [],
'profile' => []
];
for($t = 1; $t <= 5; $t++) {
$created = $faker->dateTimeThisYear->format('YmdHis');
$profile_id = $t + 125;
$this->testdata[TabularCollection::class]['user'][] = [
'id' => $t,
'profile_id' => $profile_id,
'email' => $faker->email,
'password' => sha1($faker->asciify('**********')),
'role' => $faker->randomElement(['user','admin','user','user','user','user','user','moderator','moderator']),
'is_active' => $faker->boolean,
'created' => $created,
'modified' => $created
];
$this->testdata[TabularCollection::class]['profile'][] = [
'id' => $profile_id,
'address' => $faker->streetAddress,
'city' => $faker->city,
'state' => $faker->stateAbbr,
'zipcode' => $faker->postcode,
'phone' => $faker->phoneNumber,
'bio' => $faker->paragraph,
'created' => $created,
'modified' => $created
];
}
}
}
9 changes: 9 additions & 0 deletions tests/CSVelte/Collection/MultiCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,4 +121,13 @@ public function testMultiContainsSearchesThroughoutAllDimensions()
// $this->assertFalse($coll->contains($func), 'names');
}

public function testMapRunsCallbackAgainstEachItem()
{
$coll = Collection::factory($this->testdata[MultiCollection::class]['addresses']);
$coll2 = $coll->map(function($val) {
return strlen($val);
});
$this->assertEquals([18,22,19,21,20,30,27,14,32,24], $coll2->toArray());
}

}
37 changes: 37 additions & 0 deletions tests/CSVelte/Collection/TabularCollectionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php
namespace CSVelteTest\Collection;

use CSVelte\Collection\AbstractCollection;
use CSVelte\Collection\Collection;
use CSVelte\Collection\MultiCollection;

use CSVelte\Collection\TabularCollection;
use function CSVelte\is_traversable;

class TabularCollectionTest extends AbstractCollectionTest
{
public function testFactoryReturnsTabularCollection()
{
$coll = Collection::factory($this->testdata[TabularCollection::class]['user']);
$this->assertInstanceOf(TabularCollection::class, $coll);
$coll2 = Collection::factory($this->testdata[TabularCollection::class]['profile']);
$this->assertInstanceOf(TabularCollection::class, $coll2);
}

public function testMapTabularCollection()
{
$coll = Collection::factory($this->testdata[TabularCollection::class]['user']);
$func = function($row) {
$this->assertInstanceOf(AbstractCollection::class, $row);
return $row->get('email');
};
$newcoll = $coll->map($func->bindTo($this));
$this->assertEquals([
'ohauck@bahringer.info',
'larry.emard@pacocha.com',
'jaylin.mueller@yahoo.com',
'gfriesen@hotmail.com',
'verla.ohara@dibbert.com'
], $newcoll->toArray());
}
}

0 comments on commit 5198c22

Please sign in to comment.