Skip to content

Commit 6e38fef

Browse files
committed
Show 'see in enlighten' link when a test fails
1 parent acfe517 commit 6e38fef

File tree

7 files changed

+63
-2
lines changed

7 files changed

+63
-2
lines changed

phpunit.xml

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
bootstrap="vendor/autoload.php" colors="true" convertErrorsToExceptions="true"
44
convertNoticesToExceptions="true" convertWarningsToExceptions="true" processIsolation="false"
55
stopOnError="false" stopOnFailure="false" verbose="true"
6-
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
6+
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd"
7+
printerClass="Styde\Enlighten\Tests\ResultPrinter">
78
<coverage processUncoveredFiles="true">
89
<include>
910
<directory suffix=".php">./src</directory>

src/Models/ExampleGroup.php

+5
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,11 @@ public function scopeBySuite($query, Area $suite) : Builder
5151
}
5252

5353
// Accessors
54+
public function getSuiteAttribute()
55+
{
56+
return Str::slug(explode('\\', $this->class_name)[1]);
57+
}
58+
5459
public function getPassingTestsCountAttribute()
5560
{
5661
return $this->stats

src/TestMethodInfo.php

+18
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,24 @@ public function __construct(TestClassInfo $classInfo, string $methodName, array
3030
$this->line = null;
3131
}
3232

33+
public function getSignature()
34+
{
35+
return $this->classInfo->getClassName().'::'.$this->methodName;
36+
}
37+
38+
public function getLink()
39+
{
40+
if ($this->example->group == null) {
41+
return null;
42+
}
43+
44+
return route('enlighten.group.show', [
45+
'run' => $this->example->group->run_id,
46+
'suite' => $this->example->group->suite ?: 'feature', //@TODO: fix this link
47+
'group' => $this->example->group->id,
48+
]).'#'.$this->example->method_name;
49+
}
50+
3351
public function isIgnored(): bool
3452
{
3553
return false;

src/TestRun.php

+12
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,18 @@ class TestRun
1515

1616
private string $context = 'test';
1717

18+
private static $failedTestLinks = [];
19+
20+
public static function saveFailedTestLink(TestMethodInfo $testMethodInfo)
21+
{
22+
static::$failedTestLinks[$testMethodInfo->getSignature()] = $testMethodInfo->getLink();
23+
}
24+
25+
public static function getFailedTestLink(string $signature): string
26+
{
27+
return static::$failedTestLinks[$signature];
28+
}
29+
1830
public function __construct(GitInfo $gitInfo)
1931
{
2032
$this->gitInfo = $gitInfo;

src/Tests/EnlightenSetup.php

+5
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Illuminate\Contracts\Debug\ExceptionHandler;
66
use Illuminate\Support\Facades\DB;
77
use PHPUnit\TextUI\TestRunner;
8+
use Styde\Enlighten\Models\Status;
89
use Styde\Enlighten\TestInspector;
910
use Styde\Enlighten\TestRun;
1011

@@ -140,6 +141,10 @@ protected function saveTestExample()
140141
}
141142

142143
$test->saveTestStatus($this->getStatusAsText());
144+
145+
if ($this->getStatus() !== TestRunner::STATUS_PASSED) {
146+
TestRun::saveFailedTestLink($test);
147+
}
143148
}
144149

145150
private function getStatusAsText()

src/Tests/ResultPrinter.php

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace Styde\Enlighten\Tests;
4+
5+
use PHPUnit\Framework\TestFailure;
6+
use PHPUnit\TextUI\DefaultResultPrinter;
7+
use Styde\Enlighten\TestRun;
8+
9+
class ResultPrinter extends DefaultResultPrinter
10+
{
11+
protected function printDefectTrace(TestFailure $defect): void
12+
{
13+
parent::printDefectTrace($defect);
14+
15+
if ($link = TestRun::getFailedTestLink($defect->getTestName())) {
16+
$this->write("\n⚡See in Enlighten: ");
17+
$this->writeWithColor('fg-yellow, bold', "{$link}\n");
18+
}
19+
}
20+
}

tests/Integration/ApiRequestTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ function gets_the_list_of_users()
3232
'email' => '[email protected]',
3333
]);
3434

35-
$this->get('api/users')
35+
$this->get('api/user')
3636
->assertOk()
3737
->assertSimilarJson([
3838
'data' => [

0 commit comments

Comments
 (0)