<commit_before><?php

namespace Test\Cli\Tasks;

use Fake\Kernels\Cli\StubKernelCli;
use Neutrino\Cli\Output\Decorate;
use Neutrino\Cli\Output\Writer;
use Neutrino\Constants\Services;
use Neutrino\Foundation\Cli\Tasks\RouteListTask;
use Neutrino\Support\Reflacker;
use Phalcon\Cli\Dispatcher;
use Phalcon\Cli\Router;
use Phalcon\Events\Manager;
use Test\TestCase\TestCase;

class RouteListTaskTest extends TestCase
{
    protected static function kernelClassInstance()
    {
        return StubKernelCli::class;
    }

    public function testGetHttpRoute()
    {
        $expectedRoutes = [
            ['pattern' => '/get', 'paths' => ['controller' => 'Stub', 'action' => 'index']]
        ];

        $eventManager = $this->createMock(Manager::class);

        $dispatcher = $this->mockService(Services::DISPATCHER, Dispatcher::class, true);

        $dispatcher->expects($this->any())->method('getEventsManager')->willReturn($eventManager);
        $dispatcher->expects($this->any())->method('getActionSuffix')->willReturn('Action');

        $task = new RouteListTask();

        /** @var Router\Route[] $routes */
        $routes = Reflacker::invoke($task, 'getHttpRoutesInfos');

        $this->assertInstanceOf(Router::class, $this->getDI()->getShared(Services::ROUTER));

        $this->assertEquals('Controller', $routes['controllerSuffix']);
        $this->assertEquals('Action', $routes['actionSuffix']);

        foreach ($expectedRoutes as $key => $expectedRoute) {
            $route = $routes['routes'][$key];

            $this->assertEquals($expectedRoute['pattern'], $route->getPattern());
            $this->assertEquals($expectedRoute['paths'], $route->getPaths());
        }
    }

    public function testMainAction()
    {
        $expected = [
            'write' => ['exactly' => 9, 'consecutive' => [
                ['+--------+------+----------+-----------------------------+----------------------------------------------------------------------+-------------------------------+'],
                ['| DOMAIN | NAME | METHOD   | PATTERN                     | ACTION                                                               | MIDDLEWARE                    |'],
                ['+--------+------+----------+-----------------------------+----------------------------------------------------------------------+-------------------------------+'],
                ['|        |      | GET      | /get                        | \StubController::indexAction                                         |                               |'],
                ['|        |      | POST     | /post                       | Fake\Kernels\Http\Controllers\StubController::indexAction            |                               |'],
                ['|        |      | GET      | /u/'.Decorate::notice('{user}').'                   | Fake\Kernels\Http\Controllers\StubController::indexAction            |                               |'],
                ['|        |      | GET|HEAD | /get-head                   | Fake\Kernels\Http\Controllers\StubController::indexAction            | '.\Neutrino\Http\Middleware\Csrf::class.' |'],
                ['|        |      | GET      | /back/'.Decorate::notice('{controller}').'/'.Decorate::notice('{action}').' | Fake\Kernels\Http\Controllers\\'.Decorate::notice('{controller}').'Controller::'.Decorate::notice('{action}').'Action |                               |'],
                ['+--------+------+----------+-----------------------------+----------------------------------------------------------------------+-------------------------------+'],
            ]]
        ];

        $eventManager = $this->createMock(Manager::class);

        $dispatcher = $this->mockService(Services::DISPATCHER, Dispatcher::class, true);

        $dispatcher->expects($this->any())->method('getEventsManager')->willReturn($eventManager);
        $dispatcher->expects($this->any())->method('getActionSuffix')->willReturn('Action');

        $mock = $this->mockService(Services\Cli::OUTPUT, Writer::class, true);

        foreach ($expected as $func => $params) {
            $method = $mock->expects($this->exactly($params['exactly']))
                ->method($func);

            if (!empty($params['consecutive'])) {
                $method->withConsecutive(...$params['consecutive']);
            }
        }

        $task = new RouteListTask();

        $task->mainAction();
    }

    public function testMainActionNoSubstitution()
    {
        $expected = [
            'write' => ['exactly' => 9, 'consecutive' => [
                ['+--------+------+----------+---------------------------+----------------------------------------------------------------------+-------------------------------+'],
                ['| DOMAIN | NAME | METHOD   | PATTERN                   | ACTION                                                               | MIDDLEWARE                    |'],
                ['+--------+------+----------+---------------------------+----------------------------------------------------------------------+-------------------------------+'],
                ['|        |      | GET      | /get                      | \StubController::indexAction                                         |                               |'],
                ['|        |      | POST     | /post                     | Fake\Kernels\Http\Controllers\StubController::indexAction            |                               |'],
                ['|        |      | GET      | /u/:int                   | Fake\Kernels\Http\Controllers\StubController::indexAction            |                               |'],
                ['|        |      | GET|HEAD | /get-head                 | Fake\Kernels\Http\Controllers\StubController::indexAction            | '.\Neutrino\Http\Middleware\Csrf::class.' |'],
                ['|        |      | GET      | /back/:controller/:action | Fake\Kernels\Http\Controllers\\'.Decorate::notice('{controller}').'Controller::'.Decorate::notice('{action}').'Action |                               |'],
                ['+--------+------+----------+---------------------------+----------------------------------------------------------------------+-------------------------------+'],
            ]]
        ];

        $eventManager = $this->createMock(Manager::class);

        $dispatcher = $this->mockService(Services::DISPATCHER, Dispatcher::class, true);

        $dispatcher->expects($this->any())->method('getEventsManager')->willReturn($eventManager);
        $dispatcher->expects($this->any())->method('getActionSuffix')->willReturn('Action');
        $dispatcher->expects($this->any())->method('getOptions')->willReturn(['no-substitution' => true]);

        $mock = $this->mockService(Services\Cli::OUTPUT, Writer::class, true);

        foreach ($expected as $func => $params) {
            $method = $mock->expects($this->exactly($params['exactly']))
                ->method($func);

            if (!empty($params['consecutive'])) {
                $method->withConsecutive(...$params['consecutive']);
            }
        }

        $task = new RouteListTask();

        $task->mainAction();
    }
}<commit_msg>test(Tasks\RouteList): Fix Tasks\RouteList test<commit_after><?php

namespace Test\Cli\Tasks;

use Fake\Kernels\Cli\StubKernelCli;
use Neutrino\Cli\Output\Decorate;
use Neutrino\Cli\Output\Writer;
use Neutrino\Constants\Services;
use Neutrino\Foundation\Cli\Tasks\RouteListTask;
use Neutrino\Support\Reflacker;
use Phalcon\Cli\Dispatcher;
use Phalcon\Cli\Router;
use Phalcon\Events\Manager;
use Test\TestCase\TestCase;

class RouteListTaskTest extends TestCase
{
    protected static function kernelClassInstance()
    {
        return StubKernelCli::class;
    }

    public function testGetHttpRoute()
    {
        $expectedRoutes = [
            ['pattern' => '/get', 'paths' => ['controller' => 'Stub', 'action' => 'index']]
        ];

        $eventManager = $this->createMock(Manager::class);

        $dispatcher = $this->mockService(Services::DISPATCHER, Dispatcher::class, true);

        $dispatcher->expects($this->any())->method('getEventsManager')->willReturn($eventManager);
        $dispatcher->expects($this->any())->method('getActionSuffix')->willReturn('Action');

        $task = new RouteListTask();

        /** @var Router\Route[] $routes */
        $routes = Reflacker::invoke($task, 'getHttpRoutesInfos');

        $this->assertInstanceOf(Router::class, $this->getDI()->getShared(Services::ROUTER));

        $this->assertEquals('Controller', $routes['controllerSuffix']);
        $this->assertEquals('Action', $routes['actionSuffix']);

        foreach ($expectedRoutes as $key => $expectedRoute) {
            $route = $routes['routes'][$key];

            $this->assertEquals($expectedRoute['pattern'], $route->getPattern());
            $this->assertEquals($expectedRoute['paths'], $route->getPaths());
        }
    }

    public function testMainAction()
    {
        $expected = [
            'notice' => ['exactly' => 8, 'consecutive' => [
                ['                '],
                ['  MODULE    :   '],
                ['  NAMESPACE :   '],
                ['                '],
                ['                                             '],
                ['  MODULE    :                                '],
                ['  NAMESPACE : Fake\Kernels\Http\Controllers  '],
                ['                                             '],
            ]],
            'write' => ['exactly' => 15, 'consecutive' => [
                ['+--------+------+--------+---------+-----------------------------+------------+'],
                ['| DOMAIN | NAME | METHOD | PATTERN | ACTION                      | MIDDLEWARE |'],
                ['+--------+------+--------+---------+-----------------------------+------------+'],
                ['|        |      | GET    | /get    | StubController::indexAction |            |'],
                ['+--------+------+--------+---------+-----------------------------+------------+'],
                [''],
                ['+--------+------+----------+-----------------------------+----------------------------------------+-------------------------------+'],
                ['| DOMAIN | NAME | METHOD   | PATTERN                     | ACTION                                 | MIDDLEWARE                    |'],
                ['+--------+------+----------+-----------------------------+----------------------------------------+-------------------------------+'],
                ['|        |      | POST     | /post                       | StubController::indexAction            |                               |'],
                ['|        |      | GET      | /u/'.Decorate::notice('{user}').'                   | StubController::indexAction            |                               |'],
                ['|        |      | GET|HEAD | /get-head                   | StubController::indexAction            | Neutrino\Http\Middleware\Csrf |'],
                ['|        |      | GET      | /back/'.Decorate::notice('{controller}').'/'.Decorate::notice('{action}').' | '.Decorate::notice('{controller}').'Controller::'.Decorate::notice('{action}').'Action |                               |'],
                ['+--------+------+----------+-----------------------------+----------------------------------------+-------------------------------+'],
            ]]
        ];

        $eventManager = $this->createMock(Manager::class);

        $dispatcher = $this->mockService(Services::DISPATCHER, Dispatcher::class, true);

        $dispatcher->expects($this->any())->method('getEventsManager')->willReturn($eventManager);
        $dispatcher->expects($this->any())->method('getActionSuffix')->willReturn('Action');

        $mock = $this->mockService(Services\Cli::OUTPUT, Writer::class, true);

        foreach ($expected as $func => $params) {
            $method = $mock->expects($this->exactly($params['exactly']))->method($func);

            if (!empty($params['consecutive'])) {
                $method->withConsecutive(...$params['consecutive']);
            }
        }

        $task = new RouteListTask();

        $task->mainAction();
    }

    public function testMainActionNoSubstitution()
    {
        $expected = [
            'notice' => ['exactly' => 8, 'consecutive' => [
                ['                '],
                ['  MODULE    :   '],
                ['  NAMESPACE :   '],
                ['                '],
                ['                                             '],
                ['  MODULE    :                                '],
                ['  NAMESPACE : Fake\Kernels\Http\Controllers  '],
                ['                                             '],
            ]],
            'write' => ['exactly' => 15, 'consecutive' => [
                ['+--------+------+--------+---------+-----------------------------+------------+'],
                ['| DOMAIN | NAME | METHOD | PATTERN | ACTION                      | MIDDLEWARE |'],
                ['+--------+------+--------+---------+-----------------------------+------------+'],
                ['|        |      | GET    | /get    | StubController::indexAction |            |'],
                ['+--------+------+--------+---------+-----------------------------+------------+'],
                [''],
                ['+--------+------+----------+---------------------------+----------------------------------------+-------------------------------+'],
                ['| DOMAIN | NAME | METHOD   | PATTERN                   | ACTION                                 | MIDDLEWARE                    |'],
                ['+--------+------+----------+---------------------------+----------------------------------------+-------------------------------+'],
                ['|        |      | POST     | /post                     | StubController::indexAction            |                               |'],
                ['|        |      | GET      | /u/:int                   | StubController::indexAction            |                               |'],
                ['|        |      | GET|HEAD | /get-head                 | StubController::indexAction            | Neutrino\Http\Middleware\Csrf |'],
                ['|        |      | GET      | /back/:controller/:action | '.Decorate::notice('{controller}').'Controller::'.Decorate::notice('{action}').'Action |                               |'],
                ['+--------+------+----------+---------------------------+----------------------------------------+-------------------------------+'],
            ]]
        ];

        $eventManager = $this->createMock(Manager::class);

        $dispatcher = $this->mockService(Services::DISPATCHER, Dispatcher::class, true);

        $dispatcher->expects($this->any())->method('getEventsManager')->willReturn($eventManager);
        $dispatcher->expects($this->any())->method('getActionSuffix')->willReturn('Action');
        $dispatcher->expects($this->any())->method('getOptions')->willReturn(['no-substitution' => true]);

        $mock = $this->mockService(Services\Cli::OUTPUT, Writer::class, true);

        foreach ($expected as $func => $params) {
            $method = $mock->expects($this->exactly($params['exactly']))->method($func);

            if (!empty($params['consecutive'])) {
                $method->withConsecutive(...$params['consecutive']);
            }
        }

        $task = new RouteListTask();

        $task->mainAction();
    }
}
{'fraction_non_alphanumeric': 0.2837439055683859, 'fraction_numerical': 0.0005132152938157557, 'mean_word_length': 1.107191132738578, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 147, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 58, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '663044', 'n_tokens_mistral': 3802, 'n_tokens_neox': 3534, 'n_words': 419}