Skip to content

Commit

Permalink
Add DebugRoutesCommand tests
Browse files Browse the repository at this point in the history
  • Loading branch information
vjik committed Nov 8, 2023
1 parent bbc70ef commit abc161d
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 15 deletions.
8 changes: 4 additions & 4 deletions src/Debug/DebugRoutesCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$data = $route->__debugInfo();
$action = '';
$middlewares = [];
if (!empty($data['middlewareDefinitions'])) {
$middlewareDefinitions = $data['middlewareDefinitions'];
if (!empty($data['enabledMiddlewares'])) {
$middlewareDefinitions = $data['enabledMiddlewares'];
$action = array_pop($middlewareDefinitions);
$middlewares = $middlewareDefinitions;
}
Expand Down Expand Up @@ -91,8 +91,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int
foreach ($this->routeCollection->getRoutes() as $route) {
$data = $route->__debugInfo();
$action = '';
if (!empty($data['middlewareDefinitions'])) {
$middlewareDefinitions = $data['middlewareDefinitions'];
if (!empty($data['enabledMiddlewares'])) {
$middlewareDefinitions = $data['enabledMiddlewares'];
$action = array_pop($middlewareDefinitions);
}
$rows[] = [
Expand Down
80 changes: 69 additions & 11 deletions tests/Debug/DebugRoutesCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,84 @@
use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Tester\CommandTester;
use Yiisoft\Router\Debug\DebugRoutesCommand;
use Yiisoft\Router\RouteCollectionInterface;
use Yiisoft\Router\Route;
use Yiisoft\Router\RouteCollection;
use Yiisoft\Router\RouteCollector;
use Yiisoft\Router\Tests\Support\TestController;
use Yiisoft\Router\Tests\Support\TestMiddleware1;
use Yiisoft\Yii\Debug\Debugger;
use Yiisoft\Yii\Debug\DebuggerIdGenerator;
use Yiisoft\Yii\Debug\Storage\StorageInterface;
use Yiisoft\Yii\Debug\Storage\MemoryStorage;

final class DebugRoutesCommandTest extends TestCase
{
public function testCommand()
public function testBase(): void
{
$routeCollection = $this->createMock(RouteCollectionInterface::class);
$routeCollection->method('getRoutes')->willReturn([]);
$idGenerator = new DebuggerIdGenerator();
$storage = $this->createMock(StorageInterface::class);
$storage->expects($this->never())->method('clear');
$debugger = new Debugger($idGenerator, $storage, []);
$debuggerIdGenerator = new DebuggerIdGenerator();

$command = new DebugRoutesCommand($routeCollection, $debugger);
$command = new DebugRoutesCommand(
new RouteCollection(
(new RouteCollector())->addRoute(
Route::get('/')
->host('example.com')
->defaults(['SpecialArg' => 1])
->action(fn () => 'Hello, XXXXXX!')
->name('site/index'),
Route::get('/about')
->action([TestController::class, 'index'])
->name('site/about'),
),
),
new Debugger(
$debuggerIdGenerator,
new MemoryStorage($debuggerIdGenerator),
[],
),
);

$commandTester = new CommandTester($command);

$commandTester->execute([]);
$output = $commandTester->getDisplay();

$this->assertStringContainsString('site/index', $output);
$this->assertStringContainsString('SpecialArg', $output);
$this->assertStringContainsString('example.com', $output);
$this->assertStringContainsString('XXXXXX', $output);
$this->assertStringContainsString('site/about', $output);
$this->assertStringContainsString(TestController::class . '::index', $output);
}

public function testSpecificRoute(): void
{
$debuggerIdGenerator = new DebuggerIdGenerator();

$command = new DebugRoutesCommand(
new RouteCollection(
(new RouteCollector())->addRoute(
Route::get('/')
->host('example.com')
->defaults(['SpecialArg' => 1])
->name('site/index')
->middleware(TestMiddleware1::class)
->action(fn () => 'Hello world!'),
Route::get('/about')->name('site/about'),
),
),
new Debugger(
$debuggerIdGenerator,
new MemoryStorage($debuggerIdGenerator),
[],
),
);

$commandTester = new CommandTester($command);
$commandTester->execute(['route' => ['site/index']]);
$output = $commandTester->getDisplay();

$this->assertStringContainsString('site/index', $output);
$this->assertStringContainsString('TestMiddleware1', $output);
$this->assertStringContainsString('SpecialArg', $output);
$this->assertStringContainsString('example.com', $output);
$this->assertStringNotContainsString('site/about', $output);
}
}

0 comments on commit abc161d

Please sign in to comment.