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 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
|
<?php
/*
* This file is part of composer/xdebug-handler.
*
* (c) Composer <https://github.com/composer>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Composer\XdebugHandler\Tests;
use Composer\Pcre\Preg;
use Composer\XdebugHandler\Tests\Helpers\BaseTestCase;
use Composer\XdebugHandler\Tests\Mocks\CoreMock;
use Composer\XdebugHandler\Tests\Mocks\FailMock;
use Composer\XdebugHandler\Tests\Mocks\PartialMock;
use Composer\XdebugHandler\Tests\Mocks\RequiredMock;
use PHPUnit\Framework\Attributes\DataProvider;
class RestartTest extends BaseTestCase
{
public function testRestartWhenLoaded(): void
{
$loaded = true;
$this->setArgv();
$xdebug = CoreMock::createAndCheck($loaded);
$this->checkRestart($xdebug);
// Check command
$xdebug = PartialMock::createAndCheck($loaded);
$command = implode(' ', $xdebug->getCommand());
$tmpIni = $xdebug->getTmpIni();
$pattern = preg_quote(sprintf(' -n -c %s ', $tmpIni), '/');
$matched = Preg::isMatch('/'.$pattern.'/', $command);
self::assertTrue($matched);
}
public function testRestartWhenModeIsNotOff(): void
{
$loaded = [true, 'debug,trace'];
$xdebug = CoreMock::createAndCheck($loaded);
$this->checkRestart($xdebug);
self::assertFalse($xdebug::isXdebugActive());
}
public function testNoRestartWhenNotLoaded(): void
{
$loaded = false;
$xdebug = CoreMock::createAndCheck($loaded);
$this->checkNoRestart($xdebug);
self::assertFalse($xdebug::isXdebugActive());
}
public function testNoRestartWhenLoadedAndAllowed(): void
{
$loaded = true;
putenv(CoreMock::ALLOW_XDEBUG.'=1');
$xdebug = CoreMock::createAndCheck($loaded);
$this->checkNoRestart($xdebug);
self::assertTrue($xdebug::isXdebugActive());
}
public function testNoRestartWhenModeIsOff(): void
{
$loaded = [true, 'off'];
$xdebug = CoreMock::createAndCheck($loaded);
$this->checkNoRestart($xdebug);
self::assertFalse($xdebug::isXdebugActive());
}
public function testFailedRestart(): void
{
$loaded = true;
$xdebug = FailMock::createAndCheck($loaded);
$this->checkRestart($xdebug);
}
public function testNoRestartWithUnexpectedArgv(): void
{
$loaded = true;
$_SERVER['argv'] = false;
$xdebug = CoreMock::createAndCheck($loaded);
$this->checkNoRestart($xdebug);
$_SERVER['argv'] = [];
$xdebug = CoreMock::createAndCheck($loaded);
$this->checkNoRestart($xdebug);
$_SERVER['argv'] = [1, 2];
$xdebug = CoreMock::createAndCheck($loaded);
$this->checkNoRestart($xdebug);
}
/**
* @dataProvider unreachableScriptProvider
*/
#[DataProvider('unreachableScriptProvider')]
public function testNoRestartWithUnreachableScript(string $script): void
{
$loaded = true;
// We can only check this by setting a script
$settings = ['setMainScript' => [$script]];
$xdebug = CoreMock::createAndCheck($loaded, null, $settings);
$this->checkNoRestart($xdebug);
}
/**
* @return array<string[]>
*/
public static function unreachableScriptProvider(): array
{
return [
['nonexistent.php'],
['-'],
['Standard input code'],
];
}
/**
* @dataProvider scriptSetterProvider
*/
#[DataProvider('scriptSetterProvider')]
public function testRestartWithScriptSetter(string $script): void
{
$loaded = true;
$this->setArgv();
$settings = ['setMainScript' => [$script]];
$xdebug = CoreMock::createAndCheck($loaded, null, $settings);
$this->checkRestart($xdebug);
// Check command
$xdebug = PartialMock::createAndCheck($loaded, null, $settings);
$command = $xdebug->getCommand();
self::assertContains($script, $command);
}
/**
* @return array<string[]>
*/
public static function scriptSetterProvider(): array
{
return [
// @phpstan-ignore-next-line
[(string) realpath($_SERVER['argv'][0])],
['--'],
];
}
public function testSetPersistent(): void
{
$loaded = true;
$this->setArgv();
$settings = ['setPersistent' => []];
// Check command
$xdebug = PartialMock::createAndCheck($loaded, null, $settings);
$command = $xdebug->getCommand();
$tmpIni = $xdebug->getTmpIni();
foreach (['-n', '-c', $tmpIni] as $param) {
self::assertNotContains($param, $command);
}
}
public function testNoRestartWhenNotRequired(): void
{
$loaded = true;
$required = false;
$xdebug = RequiredMock::runCoreMock($loaded, $required);
$this->checkNoRestart($xdebug);
}
public function testNoRestartWhenRequiredAndAllowed(): void
{
$loaded = true;
putenv(CoreMock::ALLOW_XDEBUG.'=1');
$required = true;
$xdebug = RequiredMock::runCoreMock($loaded, $required);
$this->checkNoRestart($xdebug);
}
/**
* Sets $_SERVER['argv'] for testing commands
*/
private function setArgv(): void
{
$_SERVER['argv'] = [__FILE__, 'command', '--param1, --param2'];
}
}
|