Skip to content

Commit 082cdf0

Browse files
committed
Refactor static dependencies
1 parent 6d93811 commit 082cdf0

File tree

5 files changed

+34
-32
lines changed

5 files changed

+34
-32
lines changed

src/Providers/EnlightenServiceProvider.php

+16-8
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Styde\Enlighten\SessionInspector;
1616
use Styde\Enlighten\TestInspector;
1717
use Styde\Enlighten\TestRun;
18+
use Styde\Enlighten\Utils\TestTrace;
1819
use Styde\Enlighten\View\Components\AppLayoutComponent;
1920
use Styde\Enlighten\View\Components\ResponseInfoComponent;
2021
use Styde\Enlighten\View\Components\StatsBadgeComponent;
@@ -26,7 +27,7 @@ public function boot()
2627
{
2728
$this->mergeConfigFrom($this->componentPath('config/enlighten.php'), 'enlighten');
2829

29-
if (! $this->app['config']->get('enlighten.enabled')) {
30+
if (!$this->app['config']->get('enlighten.enabled')) {
3031
return;
3132
}
3233

@@ -43,12 +44,6 @@ public function boot()
4344
$this->registerViewComponents();
4445

4546
$this->registerPublishing();
46-
47-
Annotations::addCast('enlighten', function ($value) {
48-
$options = json_decode($value, JSON_OBJECT_AS_ARRAY);
49-
50-
return array_merge(['include' => true], $options ?: []);
51-
});
5247
}
5348

5449
protected function addDatabaseConnection(Config $config)
@@ -88,7 +83,20 @@ private function registerTestRun()
8883
private function registerTestInspector()
8984
{
9085
$this->app->singleton(TestInspector::class, function () {
91-
return new TestInspector($this->app[TestRun::class], $this->app['config']->get('enlighten.tests'));
86+
$annotations = new Annotations;
87+
88+
$annotations->addCast('enlighten', function ($value) {
89+
$options = json_decode($value, JSON_OBJECT_AS_ARRAY);
90+
91+
return array_merge(['include' => true], $options ?: []);
92+
});
93+
94+
return new TestInspector(
95+
$this->app[TestRun::class],
96+
new TestTrace,
97+
$annotations,
98+
$this->app['config']->get('enlighten.tests')
99+
);
92100
});
93101
}
94102

src/TestInspector.php

+11-7
Original file line numberDiff line numberDiff line change
@@ -15,26 +15,30 @@ class TestInspector
1515
protected array $classOptions = [];
1616

1717
private TestRun $testRun;
18+
private TestTrace $testTrace;
19+
private Annotations $annotations;
1820

1921
protected array $ignore;
2022

21-
public function __construct(TestRun $testRun, array $config)
23+
public function __construct(TestRun $testRun, TestTrace $testTrace, Annotations $annotations, array $config)
2224
{
2325
$this->testRun = $testRun;
26+
$this->testTrace = $testTrace;
27+
$this->annotations = $annotations;
2428
$this->ignore = $config['ignore'];
2529
}
2630

2731
public function getCurrentTestInfo(): TestInfo
2832
{
29-
$trace = TestTrace::get();
33+
$trace = $this->testTrace->get();
3034

31-
$info = $this->getInfo($trace->className, $trace->methodName);
35+
$info = $this->getInfo($trace['class'], $trace['function']);
3236

3337
if ($info->isIgnored()) {
3438
return $info;
3539
}
3640

37-
return $info->addLine($trace->line);
41+
return $info->addLine($trace['line']);
3842
}
3943

4044
public function getInfo($className, $methodName): TestInfo
@@ -50,7 +54,7 @@ protected function makeTestMethodInfo(string $className, string $methodName): Te
5054
{
5155
$testClassInfo = $this->getClassInfo($className);
5256

53-
$annotations = Annotations::fromMethod($className, $methodName);
57+
$annotations = $this->annotations->getFromMethod($className, $methodName);
5458

5559
if ($this->ignoreTest($className, $methodName, $annotations->get('enlighten', []))) {
5660
return new IgnoredTest($className, $methodName);
@@ -70,7 +74,7 @@ private function getClassInfo($className): TestClassInfo
7074

7175
private function makeTestClassInfo($name): TestClassInfo
7276
{
73-
$annotations = Annotations::fromClass($name);
77+
$annotations = $this->annotations->getFromClass($name);
7478

7579
$this->classOptions = $annotations->get('enlighten', []);
7680

@@ -80,7 +84,7 @@ private function makeTestClassInfo($name): TestClassInfo
8084
protected function getTextsFrom(Collection $annotations): array
8185
{
8286
return [
83-
'title' => $annotations->get('testdox'),
87+
'title' => $annotations->get('title') ?: $annotations->get('testdox'),
8488
'description' => $annotations->get('description'),
8589
];
8690
}

src/Utils/Annotations.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@ public static function addCast(string $key, Closure $callback)
1616
static::$casts[$key] = $callback;
1717
}
1818

19-
public static function fromClass($class): Collection
19+
public function getFromClass($class): Collection
2020
{
2121
$reflectionClass = new ReflectionClass($class);
2222

2323
return static::fromDocComment($reflectionClass->getDocComment());
2424
}
2525

26-
public static function fromMethod($class, $method): Collection
26+
public function getFromMethod($class, $method): Collection
2727
{
2828
$reflectionMethod = new ReflectionMethod($class, $method);
2929

src/Utils/TestTrace.php

+3-13
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,11 @@
66

77
class TestTrace
88
{
9-
public string $className;
10-
public string $methodName;
11-
12-
public static function get(): self
9+
public function get(): array
1310
{
14-
return new static(collect(debug_backtrace())->first(function ($trace) {
11+
return collect(debug_backtrace())->first(function ($trace) {
1512
return Str::contains($trace['file'], '/phpunit/')
1613
&& Str::endsWith($trace['file'], '/Framework/TestCase.php');
17-
}));
18-
}
19-
20-
public function __construct(array $trace)
21-
{
22-
$this->className = $trace['class'];
23-
$this->methodName = $trace['function'];
24-
$this->line = $trace['line'];
14+
});
2515
}
2616
}

tests/Unit/AnnotationsTest.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class AnnotationsTest extends TestCase
1616
/** @test */
1717
function gets_annotations_from_class()
1818
{
19-
$annotations = Annotations::fromClass(AnnotationsTest::class);
19+
$annotations = (new Annotations)->getFromClass(AnnotationsTest::class);
2020

2121
$this->assertInstanceOf(Collection::class, $annotations);
2222
$this->assertCount(3, $annotations);
@@ -32,7 +32,7 @@ function gets_annotations_from_class()
3232
*/
3333
function gets_annotations_from_methods()
3434
{
35-
$annotations = Annotations::fromMethod(AnnotationsTest::class, 'gets_annotations_from_methods');
35+
$annotations = (new Annotations)->getFromMethod(AnnotationsTest::class, 'gets_annotations_from_methods');
3636

3737
$this->assertInstanceOf(Collection::class, $annotations);
3838
$this->assertCount(3, $annotations);

0 commit comments

Comments
 (0)