<commit_before><?php

namespace Dingo\Api\Tests\Routing;

use Mockery as m;
use Dingo\Api\Http;
use Dingo\Api\Routing\Router;
use Illuminate\Container\Container;
use Dingo\Api\Tests\Stubs\ThrottleStub;
use Symfony\Component\HttpKernel\Exception\HttpException;

class RouterTest extends Adapter\BaseAdapterTest
{
    public function getAdapterInstance()
    {
        return $this->container->make(\Dingo\Api\Tests\Stubs\RoutingAdapterStub::class);
    }

    public function getContainerInstance()
    {
        return new Container;
    }

    public function testRouteOptionsMergeCorrectly()
    {
        $this->router->version('v1', ['scopes' => 'foo|bar'], function () {
            $this->router->get('foo', ['scopes' => ['baz'], function () {
                $this->assertSame(
                    ['foo', 'bar', 'baz'],
                    $this->router->getCurrentRoute()->getScopes(),
                    'Router did not merge string based group scopes with route based array scopes.'
                );
            }]);

            $this->router->get('baz', function () {
                $this->assertSame(
                    ['foo', 'bar'],
                    $this->router->getCurrentRoute()->getScopes(),
                    'Router did not merge string based group scopes with route.'
                );
            });
        });

        $request = $this->createRequest('foo', 'GET', ['accept' => 'application/vnd.api.v1+json']);
        $this->router->dispatch($request);

        $request = $this->createRequest('baz', 'GET', ['accept' => 'application/vnd.api.v1+json']);
        $this->router->dispatch($request);

        $this->router->version('v2', ['providers' => 'foo', 'throttle' => new ThrottleStub(['limit' => 10, 'expires' => 15]), 'namespace' => \Dingo\Api\Tests::class], function () {
            $this->router->get('foo', 'Stubs\RoutingControllerStub@index');
        });

        $request = $this->createRequest('foo', 'GET', ['accept' => 'application/vnd.api.v2+json']);
        $this->router->dispatch($request);

        $route = $this->router->getCurrentRoute();

        $this->assertSame(['baz', 'bing'], $route->scopes());
        $this->assertSame(['foo', 'red', 'black'], $route->getAuthenticationProviders());
        $this->assertSame(10, $route->getRateLimit());
        $this->assertSame(20, $route->getRateLimitExpiration());
        $this->assertInstanceOf(\Dingo\Api\Tests\Stubs\BasicThrottleStub::class, $route->getThrottle());
    }

    public function testGroupAsPrefixesRouteAs()
    {
        $this->router->version('v1', ['as' => 'api'], function ($api) {
            $api->get('users', ['as' => 'users', function () {
                return 'foo';
            }]);
        });

        $routes = $this->router->getRoutes('v1');

        $this->assertInstanceOf(\Dingo\Api\Routing\Route::class, $routes->getByName('api.users'));
    }

    /**
     * @expectedException RuntimeException
     * @expectedMessage A version is required for an API group definition.
     */
    public function testNoGroupVersionThrowsException()
    {
        $this->router->group([], function () {
            //
        });
    }

    public function testMatchRoutes()
    {
        $this->router->version('v1', function ($api) {
            $api->match(['get', 'post'], 'foo', function () {
                return 'bar';
            });
        });

        $this->router->setConditionalRequest(false);

        $response = $this->router->dispatch(
            $request = $this->createRequest('foo', 'GET', ['accept' => 'application/vnd.api.v1+json'])
        );

        $this->assertSame(200, $response->getStatusCode());
        $this->assertSame('bar', $response->getContent());

        $response = $this->router->dispatch(<commit_msg>Fix expected exception (#1284)<commit_after><?php

namespace Dingo\Api\Tests\Routing;

use Mockery as m;
use Dingo\Api\Http;
use Dingo\Api\Routing\Router;
use Illuminate\Container\Container;
use Dingo\Api\Tests\Stubs\ThrottleStub;
use Symfony\Component\HttpKernel\Exception\HttpException;

class RouterTest extends Adapter\BaseAdapterTest
{
    public function getAdapterInstance()
    {
        return $this->container->make(\Dingo\Api\Tests\Stubs\RoutingAdapterStub::class);
    }

    public function getContainerInstance()
    {
        return new Container;
    }

    public function testRouteOptionsMergeCorrectly()
    {
        $this->router->version('v1', ['scopes' => 'foo|bar'], function () {
            $this->router->get('foo', ['scopes' => ['baz'], function () {
                $this->assertSame(
                    ['foo', 'bar', 'baz'],
                    $this->router->getCurrentRoute()->getScopes(),
                    'Router did not merge string based group scopes with route based array scopes.'
                );
            }]);

            $this->router->get('baz', function () {
                $this->assertSame(
                    ['foo', 'bar'],
                    $this->router->getCurrentRoute()->getScopes(),
                    'Router did not merge string based group scopes with route.'
                );
            });
        });

        $request = $this->createRequest('foo', 'GET', ['accept' => 'application/vnd.api.v1+json']);
        $this->router->dispatch($request);

        $request = $this->createRequest('baz', 'GET', ['accept' => 'application/vnd.api.v1+json']);
        $this->router->dispatch($request);

        $this->router->version('v2', ['providers' => 'foo', 'throttle' => new ThrottleStub(['limit' => 10, 'expires' => 15]), 'namespace' => \Dingo\Api\Tests::class], function () {
            $this->router->get('foo', 'Stubs\RoutingControllerStub@index');
        });

        $request = $this->createRequest('foo', 'GET', ['accept' => 'application/vnd.api.v2+json']);
        $this->router->dispatch($request);

        $route = $this->router->getCurrentRoute();

        $this->assertSame(['baz', 'bing'], $route->scopes());
        $this->assertSame(['foo', 'red', 'black'], $route->getAuthenticationProviders());
        $this->assertSame(10, $route->getRateLimit());
        $this->assertSame(20, $route->getRateLimitExpiration());
        $this->assertInstanceOf(\Dingo\Api\Tests\Stubs\BasicThrottleStub::class, $route->getThrottle());
    }

    public function testGroupAsPrefixesRouteAs()
    {
        $this->router->version('v1', ['as' => 'api'], function ($api) {
            $api->get('users', ['as' => 'users', function () {
                return 'foo';
            }]);
        });

        $routes = $this->router->getRoutes('v1');

        $this->assertInstanceOf(\Dingo\Api\Routing\Route::class, $routes->getByName('api.users'));
    }

    /**
     * @expectedException \RuntimeException
     * @expectedMessage A version is required for an API group definition.
     */
    public function testNoGroupVersionThrowsException()
    {
        $this->router->group([], function () {
            //
        });
    }

    public function testMatchRoutes()
    {
        $this->router->version('v1', function ($api) {
            $api->match(['get', 'post'], 'foo', function () {
                return 'bar';
            });
        });

        $this->router->setConditionalRequest(false);

        $response = $this->router->dispatch(
            $request = $this->createRequest('foo', 'GET', ['accept' => 'application/vnd.api.v1+json'])
        );

        $this->assertSame(200, $response->getStatusCode());
        $this->assertSame('bar', $response->getContent());

        $response = $this->router->dispatch(
{'fraction_non_alphanumeric': 0.18275316455696203, 'fraction_numerical': 0.0058016877637130804, 'mean_word_length': 2.4937816674343622, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 157, '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': '2734776', 'n_tokens_mistral': 2203, 'n_tokens_neox': 2149, 'n_words': 372}