|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Php\PieUnitTest\Installing; |
| 6 | + |
| 7 | +use Composer\IO\BufferIO; |
| 8 | +use Composer\Package\CompletePackageInterface; |
| 9 | +use Php\Pie\DependencyResolver\Package; |
| 10 | +use Php\Pie\Downloading\DownloadedPackage; |
| 11 | +use Php\Pie\ExtensionName; |
| 12 | +use Php\Pie\ExtensionType; |
| 13 | +use Php\Pie\File\BinaryFile; |
| 14 | +use Php\Pie\Installing\Ini\SetupIniApproach; |
| 15 | +use Php\Pie\Installing\SetupIniFile; |
| 16 | +use Php\Pie\Platform\Architecture; |
| 17 | +use Php\Pie\Platform\OperatingSystem; |
| 18 | +use Php\Pie\Platform\OperatingSystemFamily; |
| 19 | +use Php\Pie\Platform\TargetPhp\PhpBinaryPath; |
| 20 | +use Php\Pie\Platform\TargetPlatform; |
| 21 | +use Php\Pie\Platform\ThreadSafetyMode; |
| 22 | +use PHPUnit\Framework\Attributes\CoversClass; |
| 23 | +use PHPUnit\Framework\TestCase; |
| 24 | +use Symfony\Component\Console\Output\StreamOutput; |
| 25 | + |
| 26 | +#[CoversClass(SetupIniFile::class)] |
| 27 | +final class SetupIniFileTest extends TestCase |
| 28 | +{ |
| 29 | + private DownloadedPackage $downloadedPackage; |
| 30 | + private BinaryFile $binaryFile; |
| 31 | + private TargetPlatform $targetPlatform; |
| 32 | + |
| 33 | + protected function setUp(): void |
| 34 | + { |
| 35 | + parent::setUp(); |
| 36 | + |
| 37 | + $package = new Package( |
| 38 | + $this->createMock(CompletePackageInterface::class), |
| 39 | + ExtensionType::PhpModule, |
| 40 | + ExtensionName::normaliseFromString('xdebug'), |
| 41 | + 'foo/bar', |
| 42 | + '1.2.3', |
| 43 | + null, |
| 44 | + ); |
| 45 | + |
| 46 | + $this->downloadedPackage = DownloadedPackage::fromPackageAndExtractedPath($package, __DIR__); |
| 47 | + $this->binaryFile = new BinaryFile(__FILE__, 'abc123'); |
| 48 | + |
| 49 | + $phpBinaryPath = $this->createMock(PhpBinaryPath::class); |
| 50 | + /** @phpstan-ignore property.notFound */ |
| 51 | + (fn () => $this->phpBinaryPath = '/usr/bin/php') |
| 52 | + ->bindTo($phpBinaryPath, PhpBinaryPath::class)(); |
| 53 | + |
| 54 | + $this->targetPlatform = new TargetPlatform( |
| 55 | + OperatingSystem::NonWindows, |
| 56 | + OperatingSystemFamily::Linux, |
| 57 | + $phpBinaryPath, |
| 58 | + Architecture::x86_64, |
| 59 | + ThreadSafetyMode::NonThreadSafe, |
| 60 | + 1, |
| 61 | + null, |
| 62 | + null, |
| 63 | + ); |
| 64 | + } |
| 65 | + |
| 66 | + public function testSuccessfulSetupPrintsEnabledMessage(): void |
| 67 | + { |
| 68 | + $setupIniApproach = $this->createMock(SetupIniApproach::class); |
| 69 | + $setupIniApproach->method('canBeUsed')->willReturn(true); |
| 70 | + $setupIniApproach->method('setup')->willReturn(true); |
| 71 | + |
| 72 | + $io = new BufferIO(); |
| 73 | + |
| 74 | + (new SetupIniFile($setupIniApproach))( |
| 75 | + $this->targetPlatform, |
| 76 | + $this->downloadedPackage, |
| 77 | + $this->binaryFile, |
| 78 | + $io, |
| 79 | + true, |
| 80 | + ); |
| 81 | + |
| 82 | + $output = $io->getOutput(); |
| 83 | + |
| 84 | + self::assertStringContainsString('is enabled and loaded in', $output); |
| 85 | + self::assertStringNotContainsString('Extension has NOT been automatically enabled.', $output); |
| 86 | + self::assertStringNotContainsString('Automatic extension enabling was skipped.', $output); |
| 87 | + } |
| 88 | + |
| 89 | + public function testDeliberateSkipDoesNotPrintWarning(): void |
| 90 | + { |
| 91 | + $setupIniApproach = $this->createMock(SetupIniApproach::class); |
| 92 | + $setupIniApproach->expects(self::never())->method('canBeUsed'); |
| 93 | + $setupIniApproach->expects(self::never())->method('setup'); |
| 94 | + |
| 95 | + $io = new BufferIO('', StreamOutput::VERBOSITY_VERBOSE); |
| 96 | + |
| 97 | + (new SetupIniFile($setupIniApproach))( |
| 98 | + $this->targetPlatform, |
| 99 | + $this->downloadedPackage, |
| 100 | + $this->binaryFile, |
| 101 | + $io, |
| 102 | + false, |
| 103 | + ); |
| 104 | + |
| 105 | + $output = $io->getOutput(); |
| 106 | + |
| 107 | + self::assertStringContainsString('Automatic extension enabling was skipped.', $output); |
| 108 | + self::assertStringNotContainsString('Extension has NOT been automatically enabled.', $output); |
| 109 | + self::assertStringNotContainsString('You must now add', $output); |
| 110 | + } |
| 111 | + |
| 112 | + public function testAttemptedButFailedSetupPrintsWarning(): void |
| 113 | + { |
| 114 | + $setupIniApproach = $this->createMock(SetupIniApproach::class); |
| 115 | + $setupIniApproach->method('canBeUsed')->willReturn(false); |
| 116 | + |
| 117 | + $io = new BufferIO(); |
| 118 | + |
| 119 | + (new SetupIniFile($setupIniApproach))( |
| 120 | + $this->targetPlatform, |
| 121 | + $this->downloadedPackage, |
| 122 | + $this->binaryFile, |
| 123 | + $io, |
| 124 | + true, |
| 125 | + ); |
| 126 | + |
| 127 | + $output = $io->getOutput(); |
| 128 | + |
| 129 | + self::assertStringContainsString('Extension has NOT been automatically enabled.', $output); |
| 130 | + self::assertStringContainsString('You must now add "extension=xdebug" to your php.ini', $output); |
| 131 | + self::assertStringNotContainsString('Automatic extension enabling was skipped.', $output); |
| 132 | + } |
| 133 | +} |
0 commit comments