<?php
namespace Core;

class RouterTest extends \PHPUnit\Framework\TestCase
{
    /**
     * Data provider for matchingRouteSuccess
     *
     * Contains: route, params, $_SERVER['REQUEST_URI'], expected_params
     *
     * @return array
     */
    public function routeProvider()
    {
        return [
            [
                '',
                ['controller' => 'posts', 'action' => 'index'],
                '',
                ['controller' => 'posts', 'action' => 'index']
            ],
            [
                '{controller}/{action}',
                [],
                '/posts/entry',
                ['controller' => 'posts', 'action' => 'entry']
            ],
            [
                '{controller}/{action}/{id:\d+}',
                [],
                '/posts/edit/3',
                ['controller' => 'posts', 'action' => 'edit', 'id' => '3']
            ],
            [
                '{action}',
                ['controller' => 'home'],
                '/login',
                ['controller' => 'home', 'action' => 'login']
            ],
            [
                '{?status:\d+}',
                ['controller' => 'posts', 'action' => 'index'],
                '/?status=1',
                ['controller' => 'posts', 'action' => 'index', 'status' => '1']
            ]
        ];
    }

    /**
     *
     * @test
     * @dataProvider routeProvider
     */
    public function matchingRouteSuccess($route, $params, $url, $expected_param)
    {
        $router = new Router();

        $router->add($route, $params);

        // echo "$route\n$url\n";
        // var_dump($router->getRoutes());

        $this->assertTrue($router->match($url));

        $this->assertEquals($expected_param, $router->getParams());
        var_dump($router->getParams());
    }

    /**
     *
     * @test
     * @dataProvider routeProvider
     */
    // public function dispatchingRouteSuccess($route, $params, $url)
    // {
    //     $router = new Router();
    //     $router->add($route, $params);
    //
    //     $this->assertTrue($router->dispatch($url));
    // }
}
