2022-02-23 15:58:18 +00:00
|
|
|
<?php declare(strict_types=1);
|
2021-11-02 10:36:31 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* This file is part of Composer.
|
|
|
|
*
|
|
|
|
* (c) Nils Adermann <naderman@naderman.de>
|
|
|
|
* Jordi Boggiano <j.boggiano@seld.be>
|
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Composer\Test\Installer;
|
|
|
|
|
|
|
|
use Composer\Installer\BinaryInstaller;
|
|
|
|
use Composer\Util\Filesystem;
|
|
|
|
use Composer\Test\TestCase;
|
|
|
|
use Composer\Util\ProcessExecutor;
|
|
|
|
|
|
|
|
class BinaryInstallerTest extends TestCase
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $rootDir;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $vendorDir;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $binDir;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Composer\IO\IOInterface&\PHPUnit\Framework\MockObject\MockObject
|
|
|
|
*/
|
|
|
|
protected $io;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Composer\Util\Filesystem
|
|
|
|
*/
|
|
|
|
protected $fs;
|
|
|
|
|
2021-12-08 16:03:05 +00:00
|
|
|
protected function setUp(): void
|
2021-11-02 10:36:31 +00:00
|
|
|
{
|
|
|
|
$this->fs = new Filesystem;
|
|
|
|
|
2022-05-11 14:05:35 +00:00
|
|
|
$this->rootDir = self::getUniqueTmpDirectory();
|
2021-11-02 10:36:31 +00:00
|
|
|
$this->vendorDir = $this->rootDir.DIRECTORY_SEPARATOR.'vendor';
|
|
|
|
$this->ensureDirectoryExistsAndClear($this->vendorDir);
|
|
|
|
|
|
|
|
$this->binDir = $this->rootDir.DIRECTORY_SEPARATOR.'bin';
|
|
|
|
$this->ensureDirectoryExistsAndClear($this->binDir);
|
|
|
|
|
|
|
|
$this->io = $this->getMockBuilder('Composer\IO\IOInterface')->getMock();
|
|
|
|
}
|
|
|
|
|
2021-12-08 16:03:05 +00:00
|
|
|
protected function tearDown(): void
|
2021-11-02 10:36:31 +00:00
|
|
|
{
|
2021-12-09 16:09:07 +00:00
|
|
|
parent::tearDown();
|
2021-11-02 10:36:31 +00:00
|
|
|
$this->fs->removeDirectory($this->rootDir);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider executableBinaryProvider
|
|
|
|
*/
|
2022-02-22 15:47:09 +00:00
|
|
|
public function testInstallAndExecBinaryWithFullCompat(string $contents): void
|
2021-11-02 10:36:31 +00:00
|
|
|
{
|
|
|
|
$package = $this->createPackageMock();
|
|
|
|
$package->expects($this->any())
|
|
|
|
->method('getBinaries')
|
2022-08-17 12:20:07 +00:00
|
|
|
->willReturn(['binary']);
|
2021-11-02 10:36:31 +00:00
|
|
|
|
|
|
|
$this->ensureDirectoryExistsAndClear($this->vendorDir.'/foo/bar');
|
|
|
|
file_put_contents($this->vendorDir.'/foo/bar/binary', $contents);
|
|
|
|
|
|
|
|
$installer = new BinaryInstaller($this->io, $this->binDir, 'full', $this->fs);
|
|
|
|
$installer->installBinaries($package, $this->vendorDir.'/foo/bar');
|
|
|
|
|
|
|
|
$proc = new ProcessExecutor();
|
|
|
|
$proc->execute($this->binDir.'/binary arg', $output);
|
2024-05-29 21:12:06 +00:00
|
|
|
self::assertEquals('', $proc->getErrorOutput());
|
|
|
|
self::assertEquals('success arg', $output);
|
2021-11-02 10:36:31 +00:00
|
|
|
}
|
|
|
|
|
2022-11-24 13:39:08 +00:00
|
|
|
public static function executableBinaryProvider(): array
|
2021-11-02 10:36:31 +00:00
|
|
|
{
|
2022-08-17 12:20:07 +00:00
|
|
|
return [
|
|
|
|
'simple php file' => [<<<'EOL'
|
2021-11-02 10:36:31 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
echo 'success '.$_SERVER['argv'][1];
|
|
|
|
EOL
|
2022-08-17 12:20:07 +00:00
|
|
|
],
|
|
|
|
'php file with shebang' => [<<<'EOL'
|
2021-11-02 10:36:31 +00:00
|
|
|
#!/usr/bin/env php
|
|
|
|
<?php
|
|
|
|
|
|
|
|
echo 'success '.$_SERVER['argv'][1];
|
|
|
|
EOL
|
2022-08-17 12:20:07 +00:00
|
|
|
],
|
|
|
|
'phar file' => [
|
2022-02-18 07:50:11 +00:00
|
|
|
base64_decode('IyEvdXNyL2Jpbi9lbnYgcGhwCjw/cGhwCgpQaGFyOjptYXBQaGFyKCd0ZXN0LnBoYXInKTsKCnJlcXVpcmUgJ3BoYXI6Ly90ZXN0LnBoYXIvcnVuLnBocCc7CgpfX0hBTFRfQ09NUElMRVIoKTsgPz4NCj4AAAABAAAAEQAAAAEACQAAAHRlc3QucGhhcgAAAAAHAAAAcnVuLnBocCoAAADb9n9hKgAAAMUDDWGkAQAAAAAAADw/cGhwIGVjaG8gInN1Y2Nlc3MgIi4kX1NFUlZFUlsiYXJndiJdWzFdO1SOC0IE3+UN0yzrHIwyspp9slhmAgAAAEdCTUI='),
|
2022-08-17 12:20:07 +00:00
|
|
|
],
|
|
|
|
'shebang with strict types declare' => [<<<'EOL'
|
2021-11-02 10:36:31 +00:00
|
|
|
#!/usr/bin/env php
|
|
|
|
<?php declare(strict_types=1);
|
|
|
|
|
|
|
|
echo 'success '.$_SERVER['argv'][1];
|
|
|
|
EOL
|
2022-08-17 12:20:07 +00:00
|
|
|
],
|
|
|
|
];
|
2021-11-02 10:36:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return \Composer\Package\PackageInterface&\PHPUnit\Framework\MockObject\MockObject
|
|
|
|
*/
|
|
|
|
protected function createPackageMock()
|
|
|
|
{
|
|
|
|
return $this->getMockBuilder('Composer\Package\Package')
|
2022-08-17 12:20:07 +00:00
|
|
|
->setConstructorArgs([md5((string) mt_rand()), '1.0.0.0', '1.0.0'])
|
2021-11-02 10:36:31 +00:00
|
|
|
->getMock();
|
|
|
|
}
|
|
|
|
}
|