Skip to content

Commit 2a3cedc

Browse files
committed
Adjust tests, interfaces and traits related to stats / status
1 parent 4dea55d commit 2a3cedc

12 files changed

+103
-25
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
@props(['status', 'testsCount', 'passingTests', 'testsCount'])
2+
23
<span class="rounded-full text-xs text-{{ $status === 'passed' ? 'green' : ($status === 'warned' ? 'yellow' : 'red') }}-800 bg-{{ $status === 'passed' ? 'green' : ($status === 'warned' ? 'yellow' : 'red') }}-300 px-3 py-1 inline-flex">
34
@if ($status === 'passed')
45
{{ $testsCount }}
56
@else
67
{{ $passingTests }} / {{ $testsCount }}
78
@endif
8-
</span>
9+
</span>

resources/views/dashboard/_module-panel.blade.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<div class="rounded-lg bg-white overflow-hidden">
22
<div class="flex p-4 justify-between items-center w-full border-b border-gray-300 bg-gray-200">
33
<span class="font-semibold text-lg text-gray-700">{{ $module->name }}</span>
4-
<x-enlighten-stats-badge :status="$module->getStatus()" :tests-count="$module->getTestsCount()" :passing-tests="$module->getPassingTestsCount()"></x-enlighten-stats-badge>
4+
<x-enlighten-stats-badge :model="$module"></x-enlighten-stats-badge>
55
</div>
66
<ul class="py-4">
77
@foreach($module->groups as $group)

resources/views/dashboard/index.blade.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
</td>
2323
<td class=" px-6 py-4 ">{{ $run->created_at->toDateTimeString() }}</td>
2424
<td class=" px-6 py-4 ">
25-
<x-enlighten-stats-badge :status="$run->getStatus()" :tests-count="$run->getTestsCount()" :passing-tests="$run->getPassingTestsCount()"></x-enlighten-stats-badge>
25+
<x-enlighten-stats-badge :model="$run"></x-enlighten-stats-badge>
2626
</td>
2727
<td class=" px-6 py-4 ">
2828
<a href="{{ route('enlighten.run.show', $run->id) }}" class="text-teal-500 hover:text-teal-600">View</a>

src/GetsStatsFromGroups.php

+11-1
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,14 @@ public function getStatus(): string
2626

2727
return 'warned';
2828
}
29-
}
29+
30+
public function hasPassed(): bool
31+
{
32+
return $this->getStatus() === 'passed';
33+
}
34+
35+
public function hasFailed(): bool
36+
{
37+
return $this->getStatus() === 'failed';
38+
}
39+
}

src/Models/Run.php

+13-1
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44

55
use Illuminate\Database\Eloquent\Model;
66
use Styde\Enlighten\GetsStatsFromGroups;
7+
use Styde\Enlighten\Statable;
8+
use Styde\Enlighten\Statusable;
79

8-
class Run extends Model
10+
class Run extends Model implements Statable, Statusable
911
{
1012
use GetsStatsFromGroups;
1113

@@ -19,4 +21,14 @@ public function groups()
1921
{
2022
return $this->hasMany(ExampleGroup::class);
2123
}
24+
25+
public function hasPassed()
26+
{
27+
return $this->passed;
28+
}
29+
30+
public function hasFailed()
31+
{
32+
return $this->failed;
33+
}
2234
}

src/Module.php

+8-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use Illuminate\Support\Collection;
66

7-
class Module
7+
class Module implements Statusable, Statable
88
{
99
use ReadsDynamicAttributes, GetsStatsFromGroups;
1010

@@ -29,8 +29,13 @@ public function addGroups(Collection $groups): void
2929
$this->attributes['groups'] = $groups;
3030
}
3131

32-
public function getPassed(): bool
32+
public function hasPassed()
3333
{
34-
return $this->status === 'passed';
34+
return $this->passed;
35+
}
36+
37+
public function hasFailed()
38+
{
39+
return $this->failed;
3540
}
3641
}

src/Providers/EnlightenServiceProvider.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Styde\Enlighten\TestRun;
1717
use Styde\Enlighten\View\Components\AppLayoutComponent;
1818
use Styde\Enlighten\View\Components\ResponseInfoComponent;
19+
use Styde\Enlighten\View\Components\StatsBadgeComponent;
1920
use Styde\Enlighten\View\Components\StatusBadgeComponent;
2021

2122
class EnlightenServiceProvider extends ServiceProvider
@@ -103,6 +104,7 @@ private function registerViewComponents(): void
103104
$this->loadViewComponentsAs('enlighten', [
104105
'status-badge' => StatusBadgeComponent::class,
105106
'response-info' => ResponseInfoComponent::class,
107+
'stats-badge' => StatsBadgeComponent::class,
106108
'html-response' => 'enlighten::components.html-response',
107109
'json-response' => 'enlighten::components.json-response',
108110
'key-value' => 'enlighten::components.key-value',
@@ -112,7 +114,6 @@ private function registerViewComponents(): void
112114
'request-info' => 'enlighten::components.request-info',
113115
'parameters-table' => 'enlighten::components.parameters-table',
114116
'request-input-table' => 'enlighten::components.request-input-table',
115-
'stats-badge' => 'enlighten::components.stats-badge',
116117
]);
117118
}
118119

src/Statable.php

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace Styde\Enlighten;
4+
5+
interface Statable extends Statusable
6+
{
7+
public function getPassingTestsCount(): int;
8+
9+
public function getTestsCount(): int;
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
4+
namespace Styde\Enlighten\View\Components;
5+
6+
use Styde\Enlighten\Statusable;
7+
8+
trait RepresentsStatusAsColor
9+
{
10+
protected function getColor(Statusable $model)
11+
{
12+
if ($model->hasPassed()) {
13+
return 'green';
14+
} elseif ($model->hasFailed()) {
15+
return 'red';
16+
} else {
17+
return 'yellow';
18+
}
19+
}
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace Styde\Enlighten\View\Components;
4+
5+
use Illuminate\View\Component;
6+
use Styde\Enlighten\Statable;
7+
8+
class StatsBadgeComponent extends Component
9+
{
10+
use RepresentsStatusAsColor;
11+
12+
private Statable $model;
13+
14+
public function __construct(Statable $model)
15+
{
16+
$this->model = $model;
17+
}
18+
19+
public function render()
20+
{
21+
return view('enlighten::components.stats-badge', [
22+
'passingTests' => $this->model->getPassingTestsCount(),
23+
'testsCount' => $this->model->getTestsCount(),
24+
'status' => $this->model->getStatus(),
25+
'color' => $this->getColor($this->model),
26+
]);
27+
}
28+
}

src/View/Components/StatusBadgeComponent.php

+3-12
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
class StatusBadgeComponent extends Component
99
{
10+
use RepresentsStatusAsColor;
11+
1012
public Statusable $model;
1113
public string $size;
1214

@@ -19,18 +21,7 @@ public function __construct(Statusable $model, string $size = '8')
1921
public function render()
2022
{
2123
return view('enlighten::components.status-badge', [
22-
'color' => $this->color(),
24+
'color' => $this->getColor($this->model),
2325
]);
2426
}
25-
26-
private function color()
27-
{
28-
if ($this->model->hasPassed()) {
29-
return 'green';
30-
} elseif ($this->model->hasFailed()) {
31-
return 'red';
32-
} else {
33-
return 'yellow';
34-
}
35-
}
3627
}

tests/Unit/HasGroupsTest.php tests/Unit/GetsStatsFromGroupsTest.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
use Illuminate\Foundation\Testing\RefreshDatabase;
66
use Styde\Enlighten\GetsStatsFromGroups;
77
use Styde\Enlighten\Models\ExampleGroup;
8-
use Styde\Enlighten\Module;
8+
use Styde\Enlighten\ReadsDynamicAttributes;
99
use Tests\TestCase;
1010

11-
class HasGroupsTest extends TestCase
11+
class GetsStatsFromGroupsTest extends TestCase
1212
{
1313
use RefreshDatabase;
1414

@@ -23,10 +23,10 @@ public function get_the_stats_of_a_module(): void
2323
$this->createExample($group, 'third_test', 'passed');
2424
$this->createExample($group, 'fourth_test', 'passed');
2525

26-
// $module = new Module('All', ['*']);
27-
2826
$module = new class {
2927
use GetsStatsFromGroups;
28+
29+
public $groups;
3030
};
3131

3232
$module->groups = ExampleGroup::with('stats')->get();

0 commit comments

Comments
 (0)