Skip to content

Commit d9b3489

Browse files
committed
Additional Tests
And a fix
1 parent c4e9433 commit d9b3489

File tree

3 files changed

+155
-1
lines changed

3 files changed

+155
-1
lines changed

src/Support/RouteDocEntry.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public function rawArray(): array
6767
{
6868
return [
6969
'error' => $this->error ? 'X' : '',
70-
'method' => $this->httpMethod,
70+
'method' => $this->method,
7171
'path' => $this->path,
7272
'name' => $this->name ?? '',
7373
'class' => $this->class,
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
3+
use PHPUnit\Framework\TestCase;
4+
use RouteDocs\Support\RouteDocEntry;
5+
use RouteDocs\Support\RouteDocCollection;
6+
7+
class RouteDocCollectionTest extends TestCase
8+
{
9+
protected function makeEntry($class, $action, $method, $path, $name = null, $error = false)
10+
{
11+
return new RouteDocEntry($class, $action, $method, $path, $name, $error);
12+
}
13+
14+
public function testSortByKey()
15+
{
16+
$a = $this->makeEntry('A', 'foo', 'GET', '/a', 'a');
17+
$b = $this->makeEntry('B', 'bar', 'POST', '/b', 'b');
18+
$c = $this->makeEntry('C', 'baz', 'PUT', '/c', 'c');
19+
$collection = new RouteDocCollection([$b, $c, $a]);
20+
21+
$sorted = $collection->sortByKey('class');
22+
$this->assertEquals(['A', 'B', 'C'], $sorted->pluck('class')->all());
23+
}
24+
25+
public function testHasErrors()
26+
{
27+
$a = $this->makeEntry('A', 'foo', 'GET', '/a', 'a', false);
28+
$b = $this->makeEntry('B', 'bar', 'POST', '/b', 'b', true);
29+
$collection = new RouteDocCollection([$a, $b]);
30+
31+
$this->assertTrue($collection->hasErrors());
32+
$this->assertFalse((new RouteDocCollection([$a]))->hasErrors());
33+
}
34+
35+
public function testOnlyErrors()
36+
{
37+
$a = $this->makeEntry('A', 'foo', 'GET', '/a', 'a', false);
38+
$b = $this->makeEntry('B', 'bar', 'POST', '/b', 'b', true);
39+
$collection = new RouteDocCollection([$a, $b]);
40+
41+
$errors = $collection->onlyErrors();
42+
$this->assertCount(1, $errors);
43+
$this->assertEquals('B', $errors->first()->class);
44+
}
45+
46+
public function testToDisplayArrayWithAndWithoutErrorAndColor()
47+
{
48+
$a = $this->makeEntry('A', 'foo', 'GET', '/a', 'a', false);
49+
$b = $this->makeEntry('B', 'bar', 'POST', '/b', 'b', true);
50+
$collection = new RouteDocCollection([$a, $b]);
51+
52+
// With error column and color
53+
$withErrorColor = $collection->toDisplayArray(true, true);
54+
$this->assertEquals('<fg=red>X</>', $withErrorColor[1]['error']);
55+
$this->assertStringContainsString('<fg=blue>GET</>', $withErrorColor[0]['method']);
56+
57+
// Without error column, with color
58+
$noErrorColor = $collection->toDisplayArray(false, true);
59+
$this->assertArrayNotHasKey('error', $noErrorColor[0]);
60+
$this->assertStringContainsString('<fg=yellow>POST</>', $noErrorColor[1]['method']);
61+
62+
// Without error column, no color
63+
$noErrorNoColor = $collection->toDisplayArray(false, false);
64+
$this->assertEquals('GET', $noErrorNoColor[0]['method']);
65+
$this->assertArrayNotHasKey('error', $noErrorNoColor[0]);
66+
}
67+
}

tests/Unit/RouteDocEntryTest.php

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<?php
2+
3+
use PHPUnit\Framework\TestCase;
4+
use RouteDocs\Support\RouteDocEntry;
5+
6+
class RouteDocEntryTest extends TestCase
7+
{
8+
public function testRawArrayOutput()
9+
{
10+
$entry = new RouteDocEntry(
11+
class: 'App\\Http\\Controllers\\UserController',
12+
action: 'index',
13+
method: 'GET',
14+
path: '/users/{id}',
15+
name: 'users.show',
16+
error: false
17+
);
18+
19+
$expected = [
20+
'error' => '',
21+
'method' => 'GET',
22+
'path' => '/users/{id}',
23+
'name' => 'users.show',
24+
'class' => 'App\\Http\\Controllers\\UserController',
25+
'action' => 'index',
26+
];
27+
28+
$this->assertEquals($expected, $entry->toArray());
29+
}
30+
31+
public function testColorArrayOutput()
32+
{
33+
$entry = new RouteDocEntry(
34+
class: 'App\\Http\\Controllers\\UserController',
35+
action: 'store',
36+
method: 'POST',
37+
path: '/users/{id}',
38+
name: null,
39+
error: true
40+
);
41+
$entry->setOutputColor(true);
42+
43+
$expected = [
44+
'error' => '<fg=red>X</>',
45+
'method' => '<fg=yellow>POST</>',
46+
'path' => '/users/<fg=yellow>{id}</>',
47+
'name' => '',
48+
'class' => '<fg=gray>App/</fg=gray><fg=gray>Http/</fg=gray><fg=gray>Controllers/</fg=gray>UserController',
49+
'action' => 'store',
50+
];
51+
52+
$this->assertEquals($expected, $entry->toArray());
53+
}
54+
55+
public function testSetOutputColorSwitchesBackToRaw()
56+
{
57+
$entry = new RouteDocEntry(
58+
class: 'App\\Controller',
59+
action: 'edit',
60+
method: 'PATCH',
61+
path: '/edit/{item}',
62+
name: null,
63+
error: false
64+
);
65+
$entry->setOutputColor(true);
66+
$this->assertStringContainsString('<fg=cyan>PATCH</>', $entry->toArray()['method']);
67+
68+
$entry->setOutputColor(false);
69+
$this->assertEquals('PATCH', $entry->toArray()['method']);
70+
}
71+
72+
public function testHighlightPathVariables()
73+
{
74+
$entry = new RouteDocEntry('A', 'B', 'GET', '/foo/{bar}/baz/{id}');
75+
$entry->setOutputColor(true);
76+
$array = $entry->toArray();
77+
$this->assertEquals('/foo/<fg=yellow>{bar}</>/baz/<fg=yellow>{id}</>', $array['path']);
78+
}
79+
80+
public function testColorHttpMethodDefault()
81+
{
82+
$entry = new RouteDocEntry('A', 'B', 'OPTIONS', '/foo');
83+
$entry->setOutputColor(true);
84+
$array = $entry->toArray();
85+
$this->assertEquals('<fg=white>OPTIONS</>', $array['method']);
86+
}
87+
}

0 commit comments

Comments
 (0)