Skip to content

Commit 199546c

Browse files
committed
Refactor
1 parent a992bd4 commit 199546c

11 files changed

+124
-52
lines changed

src/Facades/GitInfo.php

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace Styde\Enlighten\Facades;
4+
5+
use Illuminate\Support\Facades\Facade;
6+
7+
/**
8+
* @method static string currentBranch
9+
* @method static string head
10+
* @method static string modified
11+
*/
12+
class GitInfo extends Facade
13+
{
14+
public static function getFacadeAccessor()
15+
{
16+
return \Styde\Enlighten\Utils\GitInfo::class;
17+
}
18+
}

src/Http/Middleware/HttpExampleCreatorMiddleware.php

-3
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,7 @@
33
namespace Styde\Enlighten\Http\Middleware;
44

55
use Closure;
6-
use Illuminate\Http\Response;
76
use Styde\Enlighten\HttpExampleCreator;
8-
use Styde\Enlighten\TestRun;
9-
use Throwable;
107

118
class HttpExampleCreatorMiddleware
129
{

src/Providers/EnlightenServiceProvider.php

+6-5
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
use Illuminate\Support\ServiceProvider;
88
use Illuminate\Support\Str;
99
use Styde\Enlighten\Utils\Annotations;
10-
use Styde\Enlighten\Utils\GitInfo;
1110
use Styde\Enlighten\Http\Middleware\HttpExampleCreatorMiddleware;
1211
use Styde\Enlighten\HttpExampleCreator;
1312
use Styde\Enlighten\RequestInspector;
@@ -23,7 +22,6 @@
2322
use Styde\Enlighten\View\Components\EditButtonComponent;
2423
use Styde\Enlighten\View\Components\ExceptionInfoComponent;
2524
use Styde\Enlighten\View\Components\HtmlResponseComponent;
26-
use Styde\Enlighten\View\Components\JsonResponseComponent;
2725
use Styde\Enlighten\View\Components\KeyValueComponent;
2826
use Styde\Enlighten\View\Components\RequestInputTableComponent;
2927
use Styde\Enlighten\View\Components\RouteParametersTableComponent;
@@ -38,7 +36,7 @@ public function boot()
3836
{
3937
$this->mergeConfigFrom($this->componentPath('config/enlighten.php'), 'enlighten');
4038

41-
if (!$this->app['config']->get('enlighten.enabled')) {
39+
if (! $this->app['config']->get('enlighten.enabled')) {
4240
return;
4341
}
4442

@@ -50,9 +48,12 @@ public function boot()
5048

5149
if ($this->app->runningInConsole()) {
5250
$this->loadMigrationsFrom($this->componentPath('database/migrations'));
53-
$this->registerMiddleware();
5451
$this->registerPublishing();
5552
}
53+
54+
if ($this->app->runningUnitTests()) {
55+
$this->registerMiddleware();
56+
}
5657
}
5758

5859
protected function addDatabaseConnection(Config $config)
@@ -100,7 +101,7 @@ private function registerMiddleware()
100101
private function registerTestRun()
101102
{
102103
$this->app->singleton(TestRun::class, function () {
103-
return new TestRun(new GitInfo);
104+
return TestRun::getInstance();
104105
});
105106
}
106107

src/TestClassInfo.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ class TestClassInfo
1212
private array $texts;
1313
protected ?ExampleGroup $exampleGroup = null;
1414

15-
public function __construct(TestRun $testRun, string $className, array $texts = [])
15+
public function __construct(string $className, array $texts = [])
1616
{
17-
$this->testRun = $testRun;
17+
$this->testRun = TestRun::getInstance();
1818
$this->className = $className;
1919
$this->texts = $texts;
2020
}

src/TestInspector.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ private function makeTestClassInfo($name): TestClassInfo
7272

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

75-
return new TestClassInfo($this->testRun, $name, $this->getTextsFrom($annotations));
75+
return new TestClassInfo($name, $this->getTextsFrom($annotations));
7676
}
7777

7878
protected function getTextsFrom(Collection $annotations): array

src/TestRun.php

+23-11
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33
namespace Styde\Enlighten;
44

55
use Styde\Enlighten\Models\Run;
6-
use Styde\Enlighten\Utils\GitInfo;
6+
use Styde\Enlighten\Facades\GitInfo;
77

88
class TestRun
99
{
10-
private static bool $hasBeenReset = false;
10+
private static ?self $instance = null;
1111

12-
private GitInfo $gitInfo;
12+
private Run $run;
1313

14-
private ?Run $run = null;
14+
private bool $hasBeenReset = false;
1515

1616
private string $context = 'test';
1717

@@ -27,14 +27,26 @@ public static function getFailedTestLink(string $signature): string
2727
return static::$failedTestLinks[$signature];
2828
}
2929

30-
public function __construct(GitInfo $gitInfo)
30+
public static function getInstance(): self
3131
{
32-
$this->gitInfo = $gitInfo;
32+
if (is_null(static::$instance)) {
33+
static::$instance = new self;
34+
}
35+
36+
return static::$instance;
37+
}
3338

39+
public static function resetInstance()
40+
{
41+
static::$instance = null;
42+
}
43+
44+
private function __construct()
45+
{
3446
$this->run = Run::firstOrNew([
35-
'branch' => $this->gitInfo->currentBranch(),
36-
'head' => $this->gitInfo->head(),
37-
'modified' => $this->gitInfo->modified(),
47+
'branch' => GitInfo::currentBranch(),
48+
'head' => GitInfo::head(),
49+
'modified' => GitInfo::modified(),
3850
]);
3951
}
4052

@@ -59,12 +71,12 @@ public function save(): Run
5971

6072
public function reset()
6173
{
62-
if (static::$hasBeenReset) {
74+
if ($this->hasBeenReset) {
6375
return;
6476
}
6577

6678
$this->run->delete();
6779

68-
static::$hasBeenReset = true;
80+
$this->hasBeenReset = true;
6981
}
7082
}

src/Tests/EnlightenSetup.php

+1-18
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@ public function setUpEnlighten()
2828
return;
2929
}
3030

31-
$this->restoreTestRun();
32-
3331
$this->resetRunData();
3432

3533
$this->captureExceptions();
@@ -45,8 +43,6 @@ public function setUpEnlighten()
4543
$this->stopCapturingQueries();
4644

4745
$this->saveTestExample();
48-
49-
$this->preserveTestRun();
5046
});
5147
}
5248

@@ -61,8 +57,7 @@ private function captureQueries()
6157
return;
6258
}
6359

64-
$test = $this->app->make(TestInspector::class)
65-
->getInfo(get_class($this), $this->getName());
60+
$test = $this->app->make(TestInspector::class)->getInfo(get_class($this), $this->getName());
6661

6762
if ($test->isIgnored()) {
6863
return;
@@ -118,18 +113,6 @@ protected function withExceptionHandling()
118113
return $this;
119114
}
120115

121-
private function preserveTestRun()
122-
{
123-
static::$testRun = $this->app->make(TestRun::class);
124-
}
125-
126-
private function restoreTestRun()
127-
{
128-
if (static::$testRun) {
129-
$this->app->instance(TestRun::class, static::$testRun);
130-
}
131-
}
132-
133116
private function resetRunData()
134117
{
135118
$this->app->make(TestRun::class)->reset();

tests/Integration/CaptureQueriesTest.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ function it_stores_the_queries_executed_during_the_test()
3030

3131
$example = Example::first();
3232

33-
$this->assertNotNull($example);
33+
$this->assertNotNull($example, 'The Example was not recorded as expected');
34+
35+
$this->assertNotNull($example->http_data->first(), 'The Example HTTP data was not recorded as expected');
3436

3537
tap($example->queries->shift(), function (ExampleQuery $exampleQuery) {
3638
$this->assertNotNull($exampleQuery);

tests/Unit/TestClassInfoTest.php

+4-10
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,16 @@ class TestClassInfoTest extends TestCase
1212
/** @test */
1313
function it_gets_a_default_title()
1414
{
15-
$testRun = new TestRun(new GitInfo);
16-
17-
$clasInfo = new TestClassInfo($testRun, 'ListUsersTest');
18-
15+
$clasInfo = new TestClassInfo('ListUsersTest');
1916
$this->assertSame('List Users', $clasInfo->getTitle());
2017

21-
$clasInfo = new TestClassInfo($testRun, 'ListTestsTest');
22-
18+
$clasInfo = new TestClassInfo('ListTestsTest');
2319
$this->assertSame('List Tests', $clasInfo->getTitle());
2420

25-
$clasInfo = new TestClassInfo($testRun, 'ShowUsers');
26-
21+
$clasInfo = new TestClassInfo('ShowUsers');
2722
$this->assertSame('Show Users', $clasInfo->getTitle());
2823

29-
$clasInfo = new TestClassInfo($testRun, 'CreateTestTest');
30-
24+
$clasInfo = new TestClassInfo('CreateTestTest');
3125
$this->assertSame('Create Test', $clasInfo->getTitle());
3226
}
3327
}

tests/Unit/TestMethodInfoTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class TestMethodInfoTest extends TestCase
1414
function generated_titles_do_not_include_the_test_prefix()
1515
{
1616
$testMethodInfo = new TestMethodInfo(
17-
new TestClassInfo(new TestRun(new GitInfo), 'TestClass'),
17+
new TestClassInfo('TestClass'),
1818
'test_it_removes_the_test_prefix'
1919
);
2020

tests/Unit/TestRunTest.php

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
namespace Tests\Unit;
4+
5+
use Illuminate\Foundation\Testing\RefreshDatabase;
6+
use Styde\Enlighten\Models\Run;
7+
use Styde\Enlighten\TestRun;
8+
use Tests\TestCase;
9+
10+
class TestRunTest extends TestCase
11+
{
12+
use RefreshDatabase;
13+
14+
protected function tearDown(): void
15+
{
16+
// Reset the instance to allow TestRun to be properly unit tested.
17+
// This is required in the tearDown() method to make sure this
18+
// test does not interfere with all the integration tests.
19+
TestRun::resetInstance();
20+
21+
parent::tearDown();
22+
}
23+
24+
/** @test */
25+
function can_only_create_an_instance_of_test_run()
26+
{
27+
$reflection = new \ReflectionClass(TestRun::class);
28+
29+
$this->assertTrue($reflection->getConstructor()->isPrivate());
30+
}
31+
32+
/** @test */
33+
function can_get_a_singleton_instance_of_test_run()
34+
{
35+
$this->assertInstanceOf(TestRun::class, TestRun::getInstance());
36+
37+
$this->assertSame(TestRun::getInstance(), TestRun::getInstance());
38+
}
39+
40+
/** @test */
41+
function can_reset_a_test_run()
42+
{
43+
TestRun::getInstance()->save();
44+
45+
$this->assertNotNull(Run::first());
46+
47+
TestRun::getInstance()->reset();
48+
49+
$this->assertNull(Run::first());
50+
}
51+
52+
/** @test */
53+
function a_test_run_can_only_be_reset_once()
54+
{
55+
TestRun::getInstance()->save();
56+
TestRun::getInstance()->reset();
57+
58+
$this->assertNull(Run::first());
59+
60+
TestRun::getInstance()->save();
61+
TestRun::getInstance()->reset();
62+
63+
$this->assertNotNull(Run::first());
64+
}
65+
}

0 commit comments

Comments
 (0)