Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Support/RouteDocEntry.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public function rawArray(): array
{
return [
'error' => $this->error ? 'X' : '',
'method' => $this->httpMethod,
'method' => $this->method,
'path' => $this->path,
'name' => $this->name ?? '',
'class' => $this->class,
Expand Down
67 changes: 67 additions & 0 deletions tests/Unit/RouteDocCollectionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

use PHPUnit\Framework\TestCase;
use RouteDocs\Support\RouteDocEntry;
use RouteDocs\Support\RouteDocCollection;

class RouteDocCollectionTest extends TestCase
{
protected function makeEntry($class, $action, $method, $path, $name = null, $error = false)
{
return new RouteDocEntry($class, $action, $method, $path, $name, $error);
}

public function testSortByKey()
{
$a = $this->makeEntry('A', 'foo', 'GET', '/a', 'a');
$b = $this->makeEntry('B', 'bar', 'POST', '/b', 'b');
$c = $this->makeEntry('C', 'baz', 'PUT', '/c', 'c');
$collection = new RouteDocCollection([$b, $c, $a]);

$sorted = $collection->sortByKey('class');
$this->assertEquals(['A', 'B', 'C'], $sorted->pluck('class')->all());
}

public function testHasErrors()
{
$a = $this->makeEntry('A', 'foo', 'GET', '/a', 'a', false);
$b = $this->makeEntry('B', 'bar', 'POST', '/b', 'b', true);
$collection = new RouteDocCollection([$a, $b]);

$this->assertTrue($collection->hasErrors());
$this->assertFalse((new RouteDocCollection([$a]))->hasErrors());
}

public function testOnlyErrors()
{
$a = $this->makeEntry('A', 'foo', 'GET', '/a', 'a', false);
$b = $this->makeEntry('B', 'bar', 'POST', '/b', 'b', true);
$collection = new RouteDocCollection([$a, $b]);

$errors = $collection->onlyErrors();
$this->assertCount(1, $errors);
$this->assertEquals('B', $errors->first()->class);
}

public function testToDisplayArrayWithAndWithoutErrorAndColor()
{
$a = $this->makeEntry('A', 'foo', 'GET', '/a', 'a', false);
$b = $this->makeEntry('B', 'bar', 'POST', '/b', 'b', true);
$collection = new RouteDocCollection([$a, $b]);

// With error column and color
$withErrorColor = $collection->toDisplayArray(true, true);
$this->assertEquals('<fg=red>X</>', $withErrorColor[1]['error']);
$this->assertStringContainsString('<fg=blue>GET</>', $withErrorColor[0]['method']);

// Without error column, with color
$noErrorColor = $collection->toDisplayArray(false, true);
$this->assertArrayNotHasKey('error', $noErrorColor[0]);
$this->assertStringContainsString('<fg=yellow>POST</>', $noErrorColor[1]['method']);

// Without error column, no color
$noErrorNoColor = $collection->toDisplayArray(false, false);
$this->assertEquals('GET', $noErrorNoColor[0]['method']);
$this->assertArrayNotHasKey('error', $noErrorNoColor[0]);
}
}
87 changes: 87 additions & 0 deletions tests/Unit/RouteDocEntryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php

use PHPUnit\Framework\TestCase;
use RouteDocs\Support\RouteDocEntry;

class RouteDocEntryTest extends TestCase
{
public function testRawArrayOutput()
{
$entry = new RouteDocEntry(
class: 'App\\Http\\Controllers\\UserController',
action: 'index',
method: 'GET',
path: '/users/{id}',
name: 'users.show',
error: false
);

$expected = [
'error' => '',
'method' => 'GET',
'path' => '/users/{id}',
'name' => 'users.show',
'class' => 'App\\Http\\Controllers\\UserController',
'action' => 'index',
];

$this->assertEquals($expected, $entry->toArray());
}

public function testColorArrayOutput()
{
$entry = new RouteDocEntry(
class: 'App\\Http\\Controllers\\UserController',
action: 'store',
method: 'POST',
path: '/users/{id}',
name: null,
error: true
);
$entry->setOutputColor(true);

$expected = [
'error' => '<fg=red>X</>',
'method' => '<fg=yellow>POST</>',
'path' => '/users/<fg=yellow>{id}</>',
'name' => '',
'class' => '<fg=gray>App/</fg=gray><fg=gray>Http/</fg=gray><fg=gray>Controllers/</fg=gray>UserController',
'action' => 'store',
];

$this->assertEquals($expected, $entry->toArray());
}

public function testSetOutputColorSwitchesBackToRaw()
{
$entry = new RouteDocEntry(
class: 'App\\Controller',
action: 'edit',
method: 'PATCH',
path: '/edit/{item}',
name: null,
error: false
);
$entry->setOutputColor(true);
$this->assertStringContainsString('<fg=cyan>PATCH</>', $entry->toArray()['method']);

$entry->setOutputColor(false);
$this->assertEquals('PATCH', $entry->toArray()['method']);
}

public function testHighlightPathVariables()
{
$entry = new RouteDocEntry('A', 'B', 'GET', '/foo/{bar}/baz/{id}');
$entry->setOutputColor(true);
$array = $entry->toArray();
$this->assertEquals('/foo/<fg=yellow>{bar}</>/baz/<fg=yellow>{id}</>', $array['path']);
}

public function testColorHttpMethodDefault()
{
$entry = new RouteDocEntry('A', 'B', 'OPTIONS', '/foo');
$entry->setOutputColor(true);
$array = $entry->toArray();
$this->assertEquals('<fg=white>OPTIONS</>', $array['method']);
}
}