<commit_before><?php

namespace Tightenco\Tests\Unit;

use Illuminate\Support\Str;
use Tightenco\Tests\TestCase;
use Tightenco\Ziggy\BladeRouteGenerator;

class BladeRouteGeneratorTest extends TestCase
{
    /** @test */
    function generator_at_least_vaguely_works_and_outputs_something_vaguely_right_ish()
    {
        $generator = app(BladeRouteGenerator::class);

        $this->assertStringContainsString('"namedRoutes":[]', $generator->generate());
    }

    /** @test */
    function generator_outputs_non_domain_named_routes_with_expected_structure()
    {
        $router = app('router');

        // Named. Should end up in JSON
        $router->get('/posts/{post}/comments', function () {
            return '';
        })
            ->name('postComments.index');

        $router->getRoutes()->refreshNameLookups();

        $generator = (new BladeRouteGenerator);

        $this->assertStringContainsString(json_encode([
            'postComments.index' => [
                'uri' => 'posts/{post}/comments',
                'methods' => ['GET', 'HEAD'],
                'domain' => null,
            ],
        ]), $generator->generate());
    }

    /** @test */
    function generator_outputs_domain_as_defined()
    {
        $router = app('router');

        // Named. Should end up in JSON
        $router->domain('{account}.myapp.com')->group(function () use ($router) {
            $router->get('/posts/{post}/comments', function () {
                return '';
            })
                ->name('postComments.index');
        });

        $router->getRoutes()->refreshNameLookups();

        $generator = (new BladeRouteGenerator);

        $this->assertStringContainsString(json_encode([
            'postComments.index' => [
                'uri' => 'posts/{post}/comments',
                'methods' => ['GET', 'HEAD'],
                'domain' => '{account}.myapp.com',
            ],
        ]), $generator->generate());
    }

    /** @test */
    function generator_returns_only_named_routes()
    {
        $router = app('router');

        // Not named. Shouldn't end up in JSON
        $router->get('/', function () {
            return '';
        });

        // Named. Should end up in JSON
        $router->get('/posts', function () {
            return '';
        })
            ->name('posts.index');
        $router->get('/posts/{post}', function () {
            return '';
        })
            ->name('posts.show');
        $router->get('/posts/{post}/comments', function () {
            return '';
        })
            ->name('postComments.index');
        $router->post('/posts', function () {
            return '';
        })
            ->name('posts.store');

        $router->getRoutes()->refreshNameLookups();

        $generator = (new BladeRouteGenerator);

        $payload = $generator->generate();
        $array = json_decode(Str::between($payload, ' = ', ";\n\n"), true);

        $this->assertCount(4, $array['namedRoutes']);

        $this->assertArrayHasKey('posts.index', $array['namedRoutes']);
        $this->assertArrayHasKey('posts.show', $array['namedRoutes']);
        $this->assertArrayHasKey('posts.store', $array['namedRoutes']);
        $this->assertArrayHasKey('postComments.index', $array['namedRoutes']);
    }

    /** @test */
    function generator_can_set_csp_nonce()
    {
        $generator = app(BladeRouteGenerator::class);

        $this->assertStringContainsString(
            '<script type="text/javascript" nonce="supercalifragilisticexpialidocious">',
            $generator->generate(false, 'supercalifragilisticexpialidocious')
        );
    }
}<commit_msg>Remove Str::between to make tests compatible with Laravel <7<commit_after><?php

namespace Tightenco\Tests\Unit;

use Illuminate\Support\Str;
use Tightenco\Tests\TestCase;
use Tightenco\Ziggy\BladeRouteGenerator;

class BladeRouteGeneratorTest extends TestCase
{
    /** @test */
    function generator_at_least_vaguely_works_and_outputs_something_vaguely_right_ish()
    {
        $generator = app(BladeRouteGenerator::class);

        $this->assertStringContainsString('"namedRoutes":[]', $generator->generate());
    }

    /** @test */
    function generator_outputs_non_domain_named_routes_with_expected_structure()
    {
        $router = app('router');

        // Named. Should end up in JSON
        $router->get('/posts/{post}/comments', function () {
            return '';
        })
            ->name('postComments.index');

        $router->getRoutes()->refreshNameLookups();

        $generator = (new BladeRouteGenerator);

        $this->assertStringContainsString(json_encode([
            'postComments.index' => [
                'uri' => 'posts/{post}/comments',
                'methods' => ['GET', 'HEAD'],
                'domain' => null,
            ],
        ]), $generator->generate());
    }

    /** @test */
    function generator_outputs_domain_as_defined()
    {
        $router = app('router');

        // Named. Should end up in JSON
        $router->domain('{account}.myapp.com')->group(function () use ($router) {
            $router->get('/posts/{post}/comments', function () {
                return '';
            })
                ->name('postComments.index');
        });

        $router->getRoutes()->refreshNameLookups();

        $generator = (new BladeRouteGenerator);

        $this->assertStringContainsString(json_encode([
            'postComments.index' => [
                'uri' => 'posts/{post}/comments',
                'methods' => ['GET', 'HEAD'],
                'domain' => '{account}.myapp.com',
            ],
        ]), $generator->generate());
    }

    /** @test */
    function generator_returns_only_named_routes()
    {
        $router = app('router');

        // Not named. Shouldn't end up in JSON
        $router->get('/', function () {
            return '';
        });

        // Named. Should end up in JSON
        $router->get('/posts', function () {
            return '';
        })
            ->name('posts.index');
        $router->get('/posts/{post}', function () {
            return '';
        })
            ->name('posts.show');
        $router->get('/posts/{post}/comments', function () {
            return '';
        })
            ->name('postComments.index');
        $router->post('/posts', function () {
            return '';
        })
            ->name('posts.store');

        $router->getRoutes()->refreshNameLookups();

        $generator = (new BladeRouteGenerator);

        $payload = $generator->generate();
        $array = json_decode(Str::after(Str::before($payload, ";\n\n"), ' = '), true);

        $this->assertCount(4, $array['namedRoutes']);

        $this->assertArrayHasKey('posts.index', $array['namedRoutes']);
        $this->assertArrayHasKey('posts.show', $array['namedRoutes']);
        $this->assertArrayHasKey('posts.store', $array['namedRoutes']);
        $this->assertArrayHasKey('postComments.index', $array['namedRoutes']);
    }

    /** @test */
    function generator_can_set_csp_nonce()
    {
        $generator = app(BladeRouteGenerator::class);

        $this->assertStringContainsString(
            '<script type="text/javascript" nonce="supercalifragilisticexpialidocious">',
            $generator->generate(false, 'supercalifragilisticexpialidocious')
        );
    }
}
{'fraction_non_alphanumeric': 0.16815718157181572, 'fraction_numerical': 0.0004065040650406504, 'mean_word_length': 2.2273721031919544, 'pattern_counts': {'":': 2, '<': 8, '<?xml version=': 0, '>': 91, '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': '1995941', 'n_tokens_mistral': 2090, 'n_tokens_neox': 2028, 'n_words': 306}