Skip to content

Commit c00d68d

Browse files
committed
Move table generation logic into a helper
1 parent 1f2de51 commit c00d68d

File tree

4 files changed

+113
-24
lines changed

4 files changed

+113
-24
lines changed

src/Console/ListCommand.php

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Illuminate\Support\Collection;
66
use Jaspaul\LaravelRollout\FeaturePresenter;
7+
use Jaspaul\LaravelRollout\Helpers\FeatureTable;
78

89
class ListCommand extends RolloutCommand
910
{
@@ -22,31 +23,19 @@ class ListCommand extends RolloutCommand
2223
protected $description = 'Returns a list of all the features that have been created.';
2324

2425
/**
25-
* Returns the feature rows.
26+
* Outputs a table containing the features configured in rollout.
2627
*
27-
* @return \Illuminate\Support\Collection
28-
* A list of features.
28+
* @return void
2929
*/
30-
protected function getRows() : Collection
30+
public function handle()
3131
{
32-
return (new Collection($this->rollout->features()))
32+
$presenters = (new Collection($this->rollout->features()))
3333
->map(function ($feature) {
3434
return new FeaturePresenter($this->rollout->get($feature));
35-
})->map(function (FeaturePresenter $feature) {
36-
return $feature->toArray();
3735
});
38-
}
3936

40-
/**
41-
* Outputs a table containing the features configured in rollout.
42-
*
43-
* @return void
44-
*/
45-
public function handle()
46-
{
47-
$headers = ['name', 'status', 'request-parameter', 'percentage', 'users'];
48-
$rows = $this->getRows();
37+
$table = new FeatureTable($presenters);
4938

50-
$this->table($headers, $rows);
39+
$this->table($table->getHeaders()->toArray(), $table->getRows());
5140
}
5241
}

src/Helpers/FeatureTable.php

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
namespace Jaspaul\LaravelRollout\Helpers;
4+
5+
use Opensoft\Rollout\Feature;
6+
use Illuminate\Support\Collection;
7+
use Jaspaul\LaravelRollout\FeaturePresenter;
8+
9+
class FeatureTable
10+
{
11+
/**
12+
* The presenters to render into a table.
13+
*
14+
* @var \Illuminate\Support\Collection
15+
*/
16+
protected $presenters;
17+
18+
/**
19+
* Filters the provided collection into presenters. It also grabs the first
20+
* presenter and generates the headers from it's properties.
21+
*
22+
* @param \Illuminate\Support\Collection $presenters
23+
* A list of presenters to render into a table.
24+
*/
25+
public function __construct(Collection $presenters) {
26+
$this->presenters = $presenters->filter(function ($presenter) {
27+
return $presenter instanceof FeaturePresenter;
28+
});
29+
}
30+
31+
/**
32+
* Returns the key's for the feature array.
33+
*
34+
* @return \Illuminate\Support\Collection
35+
*/
36+
public function getHeaders() : Collection
37+
{
38+
$presenter = new FeaturePresenter(new Feature(''));
39+
return new Collection(array_keys($presenter->toArray()));
40+
}
41+
42+
/**
43+
* Returns each presenter as it's array representation.
44+
*
45+
* @return \Illuminate\Support\Collection
46+
*/
47+
public function getRows() : Collection
48+
{
49+
return $this->presenters->map(function (FeaturePresenter $feature) {
50+
return $feature->toArray();
51+
});
52+
}
53+
}

tests/Console/ListCommandTest.php

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Tests\Drivers;
44

55
use Tests\TestCase;
6+
use Opensoft\Rollout\Feature;
67
use Opensoft\Rollout\Rollout;
78
use Illuminate\Support\Facades\Artisan;
89
use Illuminate\Contracts\Console\Kernel;
@@ -15,16 +16,24 @@ class ListCommandTest extends TestCase
1516
*/
1617
function it_returns_an_empty_table_if_there_are_no_stored_features()
1718
{
18-
$expected = "+------+--------+-------------------+------------+-------+
19-
| name | status | request-parameter | percentage | users |
20-
+------+--------+-------------------+------------+-------+
21-
";
22-
2319
Artisan::call('rollout:list', []);
2420

2521
$output = $this->app[Kernel::class]->output();
22+
$output = explode("\n", $output);
23+
24+
$this->assertEquals(4, count($output));
25+
26+
// The first row is a header row +----+----+ ...
27+
$this->assertTrue((bool) preg_match('/[+-]+/', $output[0]));
28+
29+
// The second row contains each of the keys for a feature presenter
30+
$presenter = new FeaturePresenter(new Feature(''));
31+
foreach (array_keys($presenter->toArray()) as $key) {
32+
$this->assertContains($key, $output[1]);
33+
}
2634

27-
$this->assertSame($expected, $output);
35+
// The first row is a seperator row +----+----+ ...
36+
$this->assertTrue((bool) preg_match('/[+-]+/', $output[2]));
2837
}
2938

3039
/**

tests/Helpers/FeatureTableTest.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace Tests\Helpers;
4+
5+
use Tests\TestCase;
6+
use Opensoft\Rollout\Feature;
7+
use Illuminate\Support\Collection;
8+
use Jaspaul\LaravelRollout\FeaturePresenter;
9+
use Jaspaul\LaravelRollout\Helpers\FeatureTable;
10+
11+
class FeatureTableTest extends TestCase
12+
{
13+
/**
14+
* @test
15+
*/
16+
function get_headers_should_return_same_keys_as_the_feature_presenter_to_array_method()
17+
{
18+
$presenter = new FeaturePresenter(new Feature('test'));
19+
20+
$table = new FeatureTable(new Collection());
21+
22+
$this->assertSame(array_keys($presenter->toArray()), $table->getHeaders()->toArray());
23+
}
24+
25+
/**
26+
* @test
27+
*/
28+
function get_rows_will_only_return_rows_for_feature_presenters_in_the_collection()
29+
{
30+
$presenter = new FeaturePresenter(new Feature('test'));
31+
$collection = new Collection([$presenter, [], 'hi', 'alpha']);
32+
33+
$table = new FeatureTable($collection);
34+
35+
$this->assertEquals(1, $table->getRows()->count());
36+
$this->assertSame($presenter->toArray(), $table->getRows()->first());
37+
}
38+
}

0 commit comments

Comments
 (0)