<commit_before><?php
namespace Tonis\Router;

use Tonis\TestAsset\NewRequestTrait;
use Zend\Diactoros\Response;

/**
 * @covers \Tonis\Router\Router
 */
class RouterTest extends \PHPUnit_Framework_TestCase
{
    use NewRequestTrait;

    /** @var Router */
    private $router;

    protected function setUp()
    {
        $this->router = new Router;
    }

    public function testMiddlewareRouteIsSkippedIfNotSame()
    {
        $called = false;
        $skipped = true;

        $this->router->get('/foo', function () use (&$skipped) {
            $skipped = false;
        });
        $this->router->get('/', function () use (&$called) {
            $called = true;
        });
        $this->router->__invoke($this->newTonisRequest('/'), $this->newTonisResponse(), function () {});

        $this->assertTrue($skipped);
        $this->assertTrue($called);
    }

    public function testQuery()
    {
        $this->router->query('id', function ($req, $res, $value) {
            $res->write($value);
        });
        $this->router->get('/', function ($req, $res) {
            return $res->write('get');
        });

        $_GET['id'] = '1234';
        $request  = $this->newTonisRequest('/');
        $response = $this->newTonisResponse();
        $handler  = function($req, $res) {
            return $res;
        };

        $result = $this->router->__invoke($request, $response, $handler);
        $this->assertSame('1234get', $result->getBody()->__toString());
    }

    public function testParam()
    {
        $this->router->param('id', function ($req, $res, $value) {
            $res->write($value);
        });
        $this->router->get('/{id}', function ($req, $res) {
            return $res->write('get');
        });

        $request  = $this->newTonisRequest('/1234');
        $response = $this->newTonisResponse();
        $handler  = function($req, $res) {
            return $res;
        };

        $result = $this->router->__invoke($request, $response, $handler);
        $this->assertSame('1234get', $result->getBody()->__toString());
    }

    public function testAddingMiddleware()
    {
        $this->router->add(function ($req, $res) {
            return $res->write('add');
        });
        $this->router->get('/', function($req, $res) {
            return $res;
        });

        $request  = $this->newTonisRequest('/');
        $response = $this->newTonisResponse();
        $result   = $this->router->__invoke($request, $response, function() {});

        $this->assertSame('add', $result->getBody()->__toString());
    }
<commit_msg>Add test for route name attribute<commit_after><?php
namespace Tonis\Router;

use Tonis\TestAsset\NewRequestTrait;
use Zend\Diactoros\Response;

/**
 * @covers \Tonis\Router\Router
 */
class RouterTest extends \PHPUnit_Framework_TestCase
{
    use NewRequestTrait;

    /** @var Router */
    private $router;

    protected function setUp()
    {
        $this->router = new Router;
    }

    public function testMiddlewareRouteIsSkippedIfNotSame()
    {
        $called = false;
        $skipped = true;

        $this->router->get('/foo', function () use (&$skipped) {
            $skipped = false;
        });
        $this->router->get('/', function () use (&$called) {
            $called = true;
        });
        $this->router->__invoke($this->newTonisRequest('/'), $this->newTonisResponse(), function () {});

        $this->assertTrue($skipped);
        $this->assertTrue($called);
    }

    public function testQuery()
    {
        $this->router->query('id', function ($req, $res, $value) {
            $res->write($value);
        });
        $this->router->get('/', function ($req, $res) {
            return $res->write('get');
        });

        $_GET['id'] = '1234';
        $request  = $this->newTonisRequest('/');
        $response = $this->newTonisResponse();
        $handler  = function($req, $res) {
            return $res;
        };

        $result = $this->router->__invoke($request, $response, $handler);
        $this->assertSame('1234get', $result->getBody()->__toString());
    }

    public function testParam()
    {
        $this->router->param('id', function ($req, $res, $value) {
            $res->write($value);
        });
        $this->router->get('/{id}', function ($req, $res) {
            return $res->write('get');
        });

        $request  = $this->newTonisRequest('/1234');
        $response = $this->newTonisResponse();
        $handler  = function($req, $res) {
            return $res;
        };

        $result = $this->router->__invoke($request, $response, $handler);
        $this->assertSame('1234get', $result->getBody()->__toString());
    }

    public function testNamedRoute()
    {
        $named = false;

        $this->router->get('/foo', function ($req, $res) use (&$named) {
            if ($req->getAttribute('route') == 'foo-route') {
                $named = true;
            }
        })->name('foo-route');
        $this->router->__invoke($this->newTonisRequest('/foo'), $this->newTonisResponse(), function () {});

        $this->assertTrue($named);
    }

    public function testAddingMiddleware()
    {
        $this->router->add(function ($req, $res) {
            return $res->write('add');
        });
        $this->router->get('/', function($req, $res) {
            return $res;
        });

        $request  = $this->newTonisRequest('/');
        $response = $this->newTonisResponse();
        $result   = $this->router->__invoke($request, $response, function() {});

        $this->assertSame('add', $result->getBody()->__toString());
    }

{'fraction_non_alphanumeric': 0.19289520426287743, 'fraction_numerical': 0.005683836589698046, 'mean_word_length': 2.2567958357432043, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 110, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7456991', 'n_tokens_mistral': 1724, 'n_tokens_neox': 1657, 'n_words': 294}