File: FileLinkFormatterTest.php

package info (click to toggle)
symfony 6.4.25%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 138,776 kB
  • sloc: php: 1,443,643; xml: 6,601; sh: 605; javascript: 597; makefile: 188; pascal: 71
file content (126 lines) | stat: -rw-r--r-- 4,348 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<?php

/*
 * This file is part of the Symfony package.
 *
 * (c) Fabien Potencier <fabien@symfony.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Symfony\Component\ErrorHandler\Tests\ErrorRenderer;

use PHPUnit\Framework\TestCase;
use Symfony\Component\ErrorHandler\ErrorRenderer\FileLinkFormatter;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;

class FileLinkFormatterTest extends TestCase
{
    public function testWhenNoFileLinkFormatAndNoRequest()
    {
        $sut = new FileLinkFormatter([]);

        $this->assertFalse($sut->format('/kernel/root/src/my/very/best/file.php', 3));
    }

    public function testAfterUnserialize()
    {
        if (get_cfg_var('xdebug.file_link_format')) {
            // There is no way to override "xdebug.file_link_format" option in a test.
            $this->markTestSkipped('php.ini has a custom option for "xdebug.file_link_format".');
        }

        $ide = $_ENV['SYMFONY_IDE'] ?? $_SERVER['SYMFONY_IDE'] ?? null;
        $_ENV['SYMFONY_IDE'] = $_SERVER['SYMFONY_IDE'] = null;
        $sut = unserialize(serialize(new FileLinkFormatter()));

        $this->assertSame('file:///kernel/root/src/my/very/best/file.php#L3', $sut->format('/kernel/root/src/my/very/best/file.php', 3));

        if (null === $ide) {
            unset($_ENV['SYMFONY_IDE'], $_SERVER['SYMFONY_IDE']);
        } else {
            $_ENV['SYMFONY_IDE'] = $_SERVER['SYMFONY_IDE'] = $ide;
        }
    }

    public function testWhenFileLinkFormatAndNoRequest()
    {
        $file = __DIR__.\DIRECTORY_SEPARATOR.'file.php';

        $sut = new FileLinkFormatter('debug://open?url=file://%f&line=%l', new RequestStack());

        $this->assertSame("debug://open?url=file://$file&line=3", $sut->format($file, 3));
    }

    public function testWhenNoFileLinkFormatAndRequest()
    {
        $file = __DIR__.\DIRECTORY_SEPARATOR.'file.php';
        $requestStack = new RequestStack();
        $request = new Request();
        $requestStack->push($request);

        $request->server->set('SERVER_NAME', 'www.example.org');
        $request->server->set('SERVER_PORT', 80);
        $request->server->set('SCRIPT_NAME', '/index.php');
        $request->server->set('SCRIPT_FILENAME', '/public/index.php');
        $request->server->set('REQUEST_URI', '/index.php/example');

        $sut = new FileLinkFormatter([], $requestStack, __DIR__, '/_profiler/open?file=%f&line=%l#line%l');

        $this->assertSame('http://www.example.org/_profiler/open?file=file.php&line=3#line3', $sut->format($file, 3));
    }

    public function testIdeFileLinkFormat()
    {
        $file = __DIR__.\DIRECTORY_SEPARATOR.'file.php';

        $sut = new FileLinkFormatter('atom');

        $this->assertSame("atom://core/open/file?filename=$file&line=3", $sut->format($file, 3));
    }

    public function testSerialize()
    {
        $this->assertInstanceOf(FileLinkFormatter::class, unserialize(serialize(new FileLinkFormatter())));
    }

    #[\PHPUnit\Framework\Attributes\DataProvider('providePathMappings')]
    public function testIdeFileLinkFormatWithPathMappingParameters($mappings)
    {
        $params = array_reduce($mappings, function ($c, $m) {
            return "$c&".implode('>', $m);
        }, '');
        $sut = new FileLinkFormatter("vscode://file/%f:%l$params");
        foreach ($mappings as $mapping) {
            $fileGuest = $mapping['guest'].'file.php';
            $fileHost = $mapping['host'].'file.php';
            $this->assertSame("vscode://file/$fileHost:3", $sut->format($fileGuest, 3));
        }
    }

    public static function providePathMappings()
    {
        yield 'single path mapping' => [
            [
                [
                    'guest' => '/var/www/app/',
                    'host' => '/user/name/project/',
                ],
            ],
        ];
        yield 'multiple path mapping' => [
            [
                [
                    'guest' => '/var/www/app/',
                    'host' => '/user/name/project/',
                ],
                [
                    'guest' => '/var/www/app2/',
                    'host' => '/user/name/project2/',
                ],
            ],
        ];
    }
}