diff --git a/src/Support/RouteDocEntry.php b/src/Support/RouteDocEntry.php index bd1f1c7..43590ca 100644 --- a/src/Support/RouteDocEntry.php +++ b/src/Support/RouteDocEntry.php @@ -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, diff --git a/tests/Unit/RouteDocCollectionTest.php b/tests/Unit/RouteDocCollectionTest.php new file mode 100644 index 0000000..e7aeda5 --- /dev/null +++ b/tests/Unit/RouteDocCollectionTest.php @@ -0,0 +1,67 @@ +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('X', $withErrorColor[1]['error']); + $this->assertStringContainsString('GET', $withErrorColor[0]['method']); + + // Without error column, with color + $noErrorColor = $collection->toDisplayArray(false, true); + $this->assertArrayNotHasKey('error', $noErrorColor[0]); + $this->assertStringContainsString('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]); + } +} diff --git a/tests/Unit/RouteDocEntryTest.php b/tests/Unit/RouteDocEntryTest.php new file mode 100644 index 0000000..1aa0722 --- /dev/null +++ b/tests/Unit/RouteDocEntryTest.php @@ -0,0 +1,87 @@ + '', + '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' => 'X', + 'method' => 'POST', + 'path' => '/users/{id}', + 'name' => '', + 'class' => 'App/Http/Controllers/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('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/{bar}/baz/{id}', $array['path']); + } + + public function testColorHttpMethodDefault() + { + $entry = new RouteDocEntry('A', 'B', 'OPTIONS', '/foo'); + $entry->setOutputColor(true); + $array = $entry->toArray(); + $this->assertEquals('OPTIONS', $array['method']); + } +}