restoreError) { restore_error_handler(); } } /** * testDocRef method */ public function testDocRef(): void { ini_set('docref_root', ''); $this->assertEquals(ini_get('docref_root'), ''); // Force a new instance. Debugger::getInstance(TestDebugger::class); Debugger::getInstance(Debugger::class); $this->assertEquals(ini_get('docref_root'), 'https://secure.php.net/'); } /** * test Excerpt writing */ public function testExcerpt(): void { $result = Debugger::excerpt(__FILE__, __LINE__ - 1, 2); $this->assertIsArray($result); $this->assertCount(5, $result); $this->assertMatchesRegularExpression('/function(.+)testExcerpt/', $result[1]); $result = Debugger::excerpt(__FILE__, 2, 2); $this->assertIsArray($result); $this->assertCount(4, $result); $this->skipIf(defined('HHVM_VERSION'), 'HHVM does not highlight php code'); // Due to different highlight_string() function behavior, see. https://3v4l.org/HcfBN. Since 8.3, it wraps it around
        $pattern = version_compare(PHP_VERSION, '8.3', '<')
            ? '/.*?.*?<\?php/'
            : '/
.*?.*?.*?<\?php/';
        $this->assertMatchesRegularExpression($pattern, $result[0]);

        $result = Debugger::excerpt(__FILE__, 11, 2);
        $this->assertCount(5, $result);

        $pattern = '/.*?<\/span>/';
        $this->assertMatchesRegularExpression($pattern, $result[0]);

        $return = Debugger::excerpt('[internal]', 2, 2);
        $this->assertEmpty($return);

        $result = Debugger::excerpt(__FILE__, __LINE__, 5);
        $this->assertCount(11, $result);
        $this->assertStringContainsString('Debugger', $result[5]);
        $this->assertStringContainsString('excerpt', $result[5]);
        $this->assertStringContainsString('__FILE__', $result[5]);

        $result = Debugger::excerpt(__FILE__, 1, 2);
        $this->assertCount(3, $result);

        $lastLine = count(explode("\n", file_get_contents(__FILE__)));
        $result = Debugger::excerpt(__FILE__, $lastLine, 2);
        $this->assertCount(3, $result);
    }

    /**
     * Test that setOutputFormat works.
     */
    public function testSetOutputFormat(): void
    {
        $this->deprecated(function () {
            Debugger::setOutputFormat('html');
            $this->assertSame('html', Debugger::getOutputFormat());
        });
    }

    /**
     * Test that getOutputFormat/setOutputFormat works.
     */
    public function testGetSetOutputFormat(): void
    {
        $this->deprecated(function () {
            Debugger::setOutputFormat('html');
            $this->assertSame('html', Debugger::getOutputFormat());
        });
    }

    /**
     * Test that choosing a nonexistent format causes an exception
     */
    public function testSetOutputAsException(): void
    {
        $this->expectException(InvalidArgumentException::class);
        $this->deprecated(function () {
            Debugger::setOutputFormat('Invalid junk');
        });
    }

    /**
     * Test outputError with description encoding
     */
    public function testOutputErrorDescriptionEncoding(): void
    {
        $this->deprecated(function () {
            Debugger::setOutputFormat('html');

            ob_start();
            $debugger = Debugger::getInstance();
            $debugger->outputError([
                'error' => 'Notice',
                'code' => E_NOTICE,
                'level' => E_NOTICE,
                'description' => 'Undefined index ',
                'file' => __FILE__,
                'line' => __LINE__,
            ]);
        });
        $result = ob_get_clean();
        $this->assertStringContainsString('<script>', $result);
        $this->assertStringNotContainsString('\nmore");
        $this->assertSame(
            "Some code to <script>alert("test")</script>
\nmore", $output ); } /** * test adding invalid editor */ public function testAddEditorInvalid(): void { $this->expectException(RuntimeException::class); Debugger::addEditor('nope', ['invalid']); } /** * test choosing an unknown editor */ public function testSetEditorInvalid(): void { $this->expectException(RuntimeException::class); Debugger::setEditor('nope'); } /** * test choosing a default editor */ public function testSetEditorPredefined(): void { Debugger::setEditor('phpstorm'); Debugger::setEditor('macvim'); Debugger::setEditor('sublime'); Debugger::setEditor('emacs'); // No exceptions raised. $this->assertTrue(true); } /** * Test configure based editor setup */ public function testConfigureEditor(): void { Configure::write('Debugger.editor', 'emacs'); Debugger::getInstance(TestDebugger::class); Debugger::getInstance(Debugger::class); $result = Debugger::editorUrl('file.php', 123); $this->assertStringContainsString('emacs://', $result); } /** * test using a valid editor. */ public function testEditorUrlValid(): void { Debugger::addEditor('open', 'open://{file}:{line}'); Debugger::setEditor('open'); $this->assertSame('open://test.php:123', Debugger::editorUrl('test.php', 123)); } /** * test using a valid editor. */ public function testEditorUrlClosure(): void { Debugger::addEditor('open', function (string $file, int $line) { return "{$file}/{$line}"; }); Debugger::setEditor('open'); $this->assertSame('test.php/123', Debugger::editorUrl('test.php', 123)); } }