|
| 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 | +} |
0 commit comments