<commit_before><?php

namespace Siler\Test\Unit;

use PHPUnit\Framework\TestCase;
use Siler\Route;

class RouteTest extends TestCase
{
    protected function setUp()
    {
        $_GET = $_POST = $_REQUEST = ['foo' => 'bar'];

        $_SERVER['HTTP_HOST'] = 'test:8000';
        $_SERVER['SCRIPT_NAME'] = '/foo/test.php';
        $_SERVER['REQUEST_URI'] = '/bar/baz';
        $_SERVER['REQUEST_METHOD'] = 'GET';
    }

    public function testRouteMatching()
    {
        $this->expectOutputString('baz');

        Route\route('get', '/foo', function ($params) {
            echo 'foo';
        });

        Route\route('get', '/bar', function ($params) {
            echo 'bar';
        });

        Route\route('get', '/bar/baz', function ($params) {
            echo 'baz';
        });
    }

    public function testRouteRegexp()
    {
        $this->expectOutputString('baz');

        Route\route('get', '/bar/([a-z]+)', function ($params) {
            echo $params[1];
        });
    }

    public function testRouteNamedGroup()
    {
        $this->expectOutputString('baz');

        Route\route('get', '/bar/{baz}', function ($params) {
            echo $params['baz'];
        });
    }

    public function testRouteWrappedNamedGroup()
    {
        $this->expectOutputString('baz');

        $_SERVER['REQUEST_URI'] = '/bar/baz/qux';

        Route\route('get', '/bar/{baz}', function ($params) {
            echo 'foo';
        });

        Route\route('get', '/bar/{baz}/qux', function ($params) {
            echo $params['baz'];
        });
    }

    public function testRouteNamedGroupWithDash()
    {
        $this->expectOutputString('baz-qux');

        $_SERVER['REQUEST_URI'] = '/bar/baz-qux';

        Route\route('get', '/bar/{baz}', function ($params) {
            echo 'baz-qux';
        });
    }

    public function testRouteNamedGroupWithNumber()
    {
        $this->expectOutputString('baz-2017');

        $_SERVER['REQUEST_URI'] = '/bar/baz-2017';

        Route\route('get', '/bar/{baz}', function ($params) {
            echo $params['baz'];
        });
    }

    public function testRouteNamedGroupWithUnderscore()
    {
        $this->expectOutputString('baz_qux');

        $_SERVER['REQUEST_URI'] = '/bar/baz_qux';

        Route\route('get', '/bar/{baz}', function ($params) {
            echo $params['baz'];
        });
    }

    public function testRouteDefaultPathInfo()
    {
        $this->expectOutputString('foo');

        unset($_SERVER['REQUEST_URI']);

        Route\route('get', '/', function ($params) {
            echo 'foo';
        });
    }

    public function testRouteWithString()
    {
        $this->expectOutputString('foo');
        Route\route('get', '/bar/{bar}', __DIR__.'/../../fixtures/to_be_required.php');
    }

    public function testRouteMethod()
    {
        $this->expectOutputString('bar');

        $_SERVER['REQUEST_METHOD'] = 'POST';

        Route\route('get', '/bar/baz', function ($params) {
            echo 'foo';
        });

        Route\route('post', '/bar/baz', function ($params) {
            echo 'bar';
        });
    }

    public function testRegexify()
    {
        $this->assertEquals('#^//?$#', Route\regexify('/'));
        $this->assertEquals('#^/foo/?$#', Route\regexify('/foo'));
        $this->assertEquals('#^/foo/bar/?$#', Route\regexify('/foo/bar'));
        $this->assertEquals('#^/foo/(?<baz>[A-z0-9_-]+)/?$#', Route\regexify('/foo/{baz}'));
        $this->assertEquals('#^/foo/(?<BaZ>[A-z0-9_-]+)/?$#', Route\regexify('/foo/{BaZ}'));
        $this->assertEquals('#^/foo/(?<bar_baz>[A-z0-9_-]+)/?$#', Route\regexify('/foo/{bar_baz}'));
        $this->assertEquals('#^/foo/(?<baz>[A-z0-9_-]+)/qux/?$#', Route\regexify('/foo/{baz}/qux'));
    }

    public function testRoutify()<commit_msg>Test optional route params<commit_after><?php

namespace Siler\Test\Unit;

use PHPUnit\Framework\TestCase;
use Siler\Route;

class RouteTest extends TestCase
{
    protected function setUp()
    {
        $_GET = $_POST = $_REQUEST = ['foo' => 'bar'];

        $_SERVER['HTTP_HOST'] = 'test:8000';
        $_SERVER['SCRIPT_NAME'] = '/foo/test.php';
        $_SERVER['REQUEST_URI'] = '/bar/baz';
        $_SERVER['REQUEST_METHOD'] = 'GET';
    }

    public function testRouteMatching()
    {
        $this->expectOutputString('baz');

        Route\route('get', '/foo', function ($params) {
            echo 'foo';
        });

        Route\route('get', '/bar', function ($params) {
            echo 'bar';
        });

        Route\route('get', '/bar/baz', function ($params) {
            echo 'baz';
        });
    }

    public function testRouteRegexp()
    {
        $this->expectOutputString('baz');

        Route\route('get', '/bar/([a-z]+)', function ($params) {
            echo $params[1];
        });
    }

    public function testRouteNamedGroup()
    {
        $this->expectOutputString('baz');

        Route\route('get', '/bar/{baz}', function ($params) {
            echo $params['baz'];
        });
    }

    public function testOptionalParam()
    {
        $this->expectOutputString('qux');

        Route\route('get', '/bar/baz/?{qux}?', function ($params) {
            echo array_key_exists('qux', $params) ? 'foobar' : 'qux';
        });
    }

    public function testOptionalParamMatch()
    {
        $this->expectOutputString('biz');

        $_SERVER['REQUEST_URI'] = '/bar/baz/biz';

        Route\route('get', '/bar/baz/?{qux}?', function ($params) {
            echo array_key_exists('qux', $params) ? $params['qux'] : 'qux';
        });
    }

    public function testRouteWrappedNamedGroup()
    {
        $this->expectOutputString('baz');

        $_SERVER['REQUEST_URI'] = '/bar/baz/qux';

        Route\route('get', '/bar/{baz}', function ($params) {
            echo 'foo';
        });

        Route\route('get', '/bar/{baz}/qux', function ($params) {
            echo $params['baz'];
        });
    }

    public function testRouteNamedGroupWithDash()
    {
        $this->expectOutputString('baz-qux');

        $_SERVER['REQUEST_URI'] = '/bar/baz-qux';

        Route\route('get', '/bar/{baz}', function ($params) {
            echo 'baz-qux';
        });
    }

    public function testRouteNamedGroupWithNumber()
    {
        $this->expectOutputString('baz-2017');

        $_SERVER['REQUEST_URI'] = '/bar/baz-2017';

        Route\route('get', '/bar/{baz}', function ($params) {
            echo $params['baz'];
        });
    }

    public function testRouteNamedGroupWithUnderscore()
    {
        $this->expectOutputString('baz_qux');

        $_SERVER['REQUEST_URI'] = '/bar/baz_qux';

        Route\route('get', '/bar/{baz}', function ($params) {
            echo $params['baz'];
        });
    }

    public function testRouteDefaultPathInfo()
    {
        $this->expectOutputString('foo');

        unset($_SERVER['REQUEST_URI']);

        Route\route('get', '/', function ($params) {
            echo 'foo';
        });
    }

    public function testRouteWithString()
    {
        $this->expectOutputString('foo');
        Route\route('get', '/bar/{bar}', __DIR__.'/../../fixtures/to_be_required.php');
    }

    public function testRouteMethod()
    {
        $this->expectOutputString('bar');

        $_SERVER['REQUEST_METHOD'] = 'POST';

        Route\route('get', '/bar/baz', function ($params) {
            echo 'foo';
        });

        Route\route('post', '/bar/baz', function ($params) {
            echo 'bar';
        });
    }

    public function testRegexify()
    {
        $this->assertEquals('#^//?$#', Route\regexify('/'));
        $this->assertEquals('#^/foo/?$#', Route\regexify('/foo'));
        $this->assertEquals('#^/foo/bar/?$#', Route\regexify('/foo/bar'));
        $this->assertEquals('#^/foo/(?<baz>[A-z0-9_-]+)/?$#', Route\regexify('/foo/{baz}'));
        $this->assertEquals('#^/foo/(?<BaZ>[A-z0-9_-]+)/?$#', Route\regexify('/foo/{BaZ}'));
        $this->assertEquals('#^/foo/(?<bar_baz>[A-z0-9_-]+)/?$#', Route\regexify('/foo/{bar_baz}'));
        $this->assertEquals('#^/foo/(?<baz>[A-z0-9_-]+)/qux/?$#', Route\regexify('/foo/{baz}/qux'));
        $this->assertEquals('#^/foo/(?<baz>[A-z0-9_-]+)?/?$#', Route\regexify('/foo/{baz}?'));
    }

    public function testRoutify()
{'fraction_non_alphanumeric': 0.21104679208876026, 'fraction_numerical': 0.005306319343945972, 'mean_word_length': 2.2947953913388957, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 51, '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': '5192533', 'n_tokens_mistral': 2713, 'n_tokens_neox': 2527, 'n_words': 388}