2022-02-23 15:58:18 +00:00
|
|
|
<?php declare(strict_types=1);
|
2012-03-05 10:26:07 +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\Util;
|
|
|
|
|
2020-06-16 07:35:33 +00:00
|
|
|
use Composer\Config;
|
|
|
|
use Composer\IO\ConsoleIO;
|
|
|
|
use Composer\IO\IOInterface;
|
|
|
|
use Composer\Util\AuthHelper;
|
2012-03-05 10:26:07 +00:00
|
|
|
use Composer\Util\RemoteFilesystem;
|
2020-02-07 03:18:45 +00:00
|
|
|
use Composer\Test\TestCase;
|
2020-06-16 07:35:33 +00:00
|
|
|
use PHPUnit\Framework\MockObject\MockObject;
|
|
|
|
use ReflectionMethod;
|
|
|
|
use ReflectionProperty;
|
2012-03-05 10:26:07 +00:00
|
|
|
|
2017-11-04 14:52:13 +00:00
|
|
|
class RemoteFilesystemTest extends TestCase
|
2012-03-05 10:26:07 +00:00
|
|
|
{
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testGetOptionsForUrl(): void
|
2012-03-05 10:26:07 +00:00
|
|
|
{
|
2020-06-16 07:35:33 +00:00
|
|
|
$io = $this->getIOInterfaceMock();
|
2012-03-05 10:26:07 +00:00
|
|
|
$io
|
|
|
|
->expects($this->once())
|
2012-11-07 12:33:50 +00:00
|
|
|
->method('hasAuthentication')
|
2020-06-16 07:35:33 +00:00
|
|
|
->willReturn(false)
|
2012-03-05 10:26:07 +00:00
|
|
|
;
|
|
|
|
|
2022-08-17 12:20:07 +00:00
|
|
|
$res = $this->callGetOptionsForUrl($io, ['http://example.org', []]);
|
2012-10-19 10:25:53 +00:00
|
|
|
$this->assertTrue(isset($res['http']['header']) && is_array($res['http']['header']), 'getOptions must return an array with headers');
|
2012-03-05 10:26:07 +00:00
|
|
|
}
|
|
|
|
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testGetOptionsForUrlWithAuthorization(): void
|
2012-03-05 10:26:07 +00:00
|
|
|
{
|
2020-06-16 07:35:33 +00:00
|
|
|
$io = $this->getIOInterfaceMock();
|
2012-03-05 10:26:07 +00:00
|
|
|
$io
|
|
|
|
->expects($this->once())
|
2012-11-07 12:33:50 +00:00
|
|
|
->method('hasAuthentication')
|
2020-06-16 07:35:33 +00:00
|
|
|
->willReturn(true)
|
2012-03-05 10:26:07 +00:00
|
|
|
;
|
|
|
|
$io
|
|
|
|
->expects($this->once())
|
2012-11-07 12:33:50 +00:00
|
|
|
->method('getAuthentication')
|
2022-08-17 12:20:07 +00:00
|
|
|
->willReturn(['username' => 'login', 'password' => 'password'])
|
2012-03-05 10:26:07 +00:00
|
|
|
;
|
|
|
|
|
2022-08-17 12:20:07 +00:00
|
|
|
$options = $this->callGetOptionsForUrl($io, ['http://example.org', []]);
|
2012-10-19 10:25:53 +00:00
|
|
|
|
|
|
|
$found = false;
|
|
|
|
foreach ($options['http']['header'] as $header) {
|
|
|
|
if (0 === strpos($header, 'Authorization: Basic')) {
|
|
|
|
$found = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$this->assertTrue($found, 'getOptions must have an Authorization header');
|
2012-03-05 10:26:07 +00:00
|
|
|
}
|
|
|
|
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testGetOptionsForUrlWithStreamOptions(): void
|
2012-10-03 13:09:47 +00:00
|
|
|
{
|
2020-06-16 07:35:33 +00:00
|
|
|
$io = $this->getIOInterfaceMock();
|
2012-10-03 13:09:47 +00:00
|
|
|
$io
|
|
|
|
->expects($this->once())
|
2012-11-07 12:33:50 +00:00
|
|
|
->method('hasAuthentication')
|
2020-06-16 07:35:33 +00:00
|
|
|
->willReturn(true)
|
2012-10-03 13:09:47 +00:00
|
|
|
;
|
|
|
|
|
2020-02-07 03:18:45 +00:00
|
|
|
$io
|
|
|
|
->expects($this->once())
|
|
|
|
->method('getAuthentication')
|
2022-08-17 12:20:07 +00:00
|
|
|
->willReturn(['username' => null, 'password' => null])
|
2020-02-07 03:18:45 +00:00
|
|
|
;
|
|
|
|
|
2022-08-17 12:20:07 +00:00
|
|
|
$streamOptions = ['ssl' => [
|
2012-10-03 13:09:47 +00:00
|
|
|
'allow_self_signed' => true,
|
2022-08-17 12:20:07 +00:00
|
|
|
]];
|
2012-10-03 13:09:47 +00:00
|
|
|
|
2022-08-17 12:20:07 +00:00
|
|
|
$res = $this->callGetOptionsForUrl($io, ['https://example.org', []], $streamOptions);
|
2020-09-11 21:51:55 +00:00
|
|
|
$this->assertTrue(
|
|
|
|
isset($res['ssl'], $res['ssl']['allow_self_signed']) && true === $res['ssl']['allow_self_signed'],
|
|
|
|
'getOptions must return an array with a allow_self_signed set to true'
|
|
|
|
);
|
2012-10-03 13:09:47 +00:00
|
|
|
}
|
|
|
|
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testGetOptionsForUrlWithCallOptionsKeepsHeader(): void
|
2012-10-18 16:43:31 +00:00
|
|
|
{
|
2020-06-16 07:35:33 +00:00
|
|
|
$io = $this->getIOInterfaceMock();
|
2012-10-18 16:43:31 +00:00
|
|
|
$io
|
|
|
|
->expects($this->once())
|
2012-11-07 12:33:50 +00:00
|
|
|
->method('hasAuthentication')
|
2020-06-16 07:35:33 +00:00
|
|
|
->willReturn(true)
|
2012-10-18 16:43:31 +00:00
|
|
|
;
|
|
|
|
|
2020-02-07 03:18:45 +00:00
|
|
|
$io
|
|
|
|
->expects($this->once())
|
|
|
|
->method('getAuthentication')
|
2022-08-17 12:20:07 +00:00
|
|
|
->willReturn(['username' => null, 'password' => null])
|
2020-02-07 03:18:45 +00:00
|
|
|
;
|
|
|
|
|
2022-08-17 12:20:07 +00:00
|
|
|
$streamOptions = ['http' => [
|
2012-10-18 16:43:31 +00:00
|
|
|
'header' => 'Foo: bar',
|
2022-08-17 12:20:07 +00:00
|
|
|
]];
|
2012-10-18 16:43:31 +00:00
|
|
|
|
2022-08-17 12:20:07 +00:00
|
|
|
$res = $this->callGetOptionsForUrl($io, ['https://example.org', $streamOptions]);
|
2012-10-18 16:43:31 +00:00
|
|
|
$this->assertTrue(isset($res['http']['header']), 'getOptions must return an array with a http.header key');
|
2012-10-19 10:25:53 +00:00
|
|
|
|
|
|
|
$found = false;
|
|
|
|
foreach ($res['http']['header'] as $header) {
|
|
|
|
if ($header === 'Foo: bar') {
|
|
|
|
$found = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->assertTrue($found, 'getOptions must have a Foo: bar header');
|
|
|
|
$this->assertGreaterThan(1, count($res['http']['header']));
|
2012-10-18 16:43:31 +00:00
|
|
|
}
|
|
|
|
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testCallbackGetFileSize(): void
|
2012-03-05 10:26:07 +00:00
|
|
|
{
|
2020-06-16 07:35:33 +00:00
|
|
|
$fs = new RemoteFilesystem($this->getIOInterfaceMock(), $this->getConfigMock());
|
2012-03-05 10:26:07 +00:00
|
|
|
$this->callCallbackGet($fs, STREAM_NOTIFY_FILE_SIZE_IS, 0, '', 0, 0, 20);
|
2020-09-10 15:21:11 +00:00
|
|
|
$this->assertAttributeEqualsCustom(20, 'bytesMax', $fs);
|
2012-03-05 10:26:07 +00:00
|
|
|
}
|
|
|
|
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testCallbackGetNotifyProgress(): void
|
2012-03-05 10:26:07 +00:00
|
|
|
{
|
2020-06-16 07:35:33 +00:00
|
|
|
$io = $this->getIOInterfaceMock();
|
2012-03-05 10:26:07 +00:00
|
|
|
$io
|
|
|
|
->expects($this->once())
|
2015-02-18 09:04:39 +00:00
|
|
|
->method('overwriteError')
|
2012-03-05 10:26:07 +00:00
|
|
|
;
|
|
|
|
|
2018-11-14 16:54:19 +00:00
|
|
|
$fs = new RemoteFilesystem($io, $this->getConfigMock());
|
2012-03-05 10:26:07 +00:00
|
|
|
$this->setAttribute($fs, 'bytesMax', 20);
|
|
|
|
$this->setAttribute($fs, 'progress', true);
|
|
|
|
|
|
|
|
$this->callCallbackGet($fs, STREAM_NOTIFY_PROGRESS, 0, '', 0, 10, 20);
|
2021-08-29 18:10:24 +00:00
|
|
|
$this->assertAttributeEqualsCustom(50, 'lastProgress', $fs);
|
2012-03-05 10:26:07 +00:00
|
|
|
}
|
|
|
|
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testCallbackGetPassesThrough404(): void
|
2012-03-05 10:26:07 +00:00
|
|
|
{
|
2020-06-16 07:35:33 +00:00
|
|
|
$fs = new RemoteFilesystem($this->getIOInterfaceMock(), $this->getConfigMock());
|
2012-03-05 10:26:07 +00:00
|
|
|
|
2021-10-27 14:18:46 +00:00
|
|
|
$this->callCallbackGet($fs, STREAM_NOTIFY_FAILURE, 0, 'HTTP/1.1 404 Not Found', 404, 0, 0);
|
|
|
|
$this->assertTrue(true, 'callbackGet must pass through 404');
|
2012-03-05 10:26:07 +00:00
|
|
|
}
|
|
|
|
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testGetContents(): void
|
2012-03-05 10:26:07 +00:00
|
|
|
{
|
2020-06-16 07:35:33 +00:00
|
|
|
$fs = new RemoteFilesystem($this->getIOInterfaceMock(), $this->getConfigMock());
|
2012-03-05 10:26:07 +00:00
|
|
|
|
2020-09-10 15:21:11 +00:00
|
|
|
$this->assertStringContainsString('testGetContents', $fs->getContents('http://example.org', 'file://'.__FILE__));
|
2012-03-05 10:26:07 +00:00
|
|
|
}
|
|
|
|
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testCopy(): void
|
2012-03-05 10:26:07 +00:00
|
|
|
{
|
2020-06-16 07:35:33 +00:00
|
|
|
$fs = new RemoteFilesystem($this->getIOInterfaceMock(), $this->getConfigMock());
|
2012-03-05 10:26:07 +00:00
|
|
|
|
2021-12-10 12:14:04 +00:00
|
|
|
$file = $this->createTempFile();
|
2012-03-29 13:09:01 +00:00
|
|
|
$this->assertTrue($fs->copy('http://example.org', 'file://'.__FILE__, $file));
|
2012-03-05 10:26:07 +00:00
|
|
|
$this->assertFileExists($file);
|
2020-09-10 15:21:11 +00:00
|
|
|
$this->assertStringContainsString('testCopy', file_get_contents($file));
|
2012-03-05 10:26:07 +00:00
|
|
|
unlink($file);
|
|
|
|
}
|
|
|
|
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testCopyWithNoRetryOnFailure(): void
|
2020-06-16 07:35:33 +00:00
|
|
|
{
|
2021-12-09 19:55:26 +00:00
|
|
|
self::expectException('Composer\Downloader\TransportException');
|
2022-08-17 12:20:07 +00:00
|
|
|
$fs = $this->getRemoteFilesystemWithMockedMethods(['getRemoteContents']);
|
2020-06-16 07:35:33 +00:00
|
|
|
|
|
|
|
$fs->expects($this->once())->method('getRemoteContents')
|
2022-08-17 12:20:07 +00:00
|
|
|
->willReturnCallback(static function ($originUrl, $fileUrl, $ctx, &$http_response_header): string {
|
|
|
|
$http_response_header = ['http/1.1 401 unauthorized'];
|
2020-06-16 07:35:33 +00:00
|
|
|
|
|
|
|
return '';
|
|
|
|
});
|
|
|
|
|
2021-12-10 12:14:04 +00:00
|
|
|
$file = $this->createTempFile();
|
2020-06-16 07:35:33 +00:00
|
|
|
unlink($file);
|
|
|
|
|
|
|
|
$fs->copy(
|
|
|
|
'http://example.org',
|
|
|
|
'file://' . __FILE__,
|
|
|
|
$file,
|
|
|
|
true,
|
2022-08-17 12:20:07 +00:00
|
|
|
['retry-auth-failure' => false]
|
2020-06-16 07:35:33 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testCopyWithSuccessOnRetry(): void
|
2020-06-16 07:35:33 +00:00
|
|
|
{
|
2022-08-17 12:20:07 +00:00
|
|
|
$authHelper = $this->getAuthHelperWithMockedMethods(['promptAuthIfNeeded']);
|
|
|
|
$fs = $this->getRemoteFilesystemWithMockedMethods(['getRemoteContents'], $authHelper);
|
2020-06-16 07:35:33 +00:00
|
|
|
|
|
|
|
$authHelper->expects($this->once())
|
|
|
|
->method('promptAuthIfNeeded')
|
2022-08-17 12:20:07 +00:00
|
|
|
->willReturn([
|
2020-06-16 07:35:33 +00:00
|
|
|
'storeAuth' => true,
|
2020-11-22 13:48:56 +00:00
|
|
|
'retry' => true,
|
2022-08-17 12:20:07 +00:00
|
|
|
]);
|
2020-06-16 07:35:33 +00:00
|
|
|
|
2021-12-09 16:09:07 +00:00
|
|
|
$counter = 0;
|
|
|
|
$fs->expects($this->exactly(2))
|
2020-06-16 07:35:33 +00:00
|
|
|
->method('getRemoteContents')
|
2022-08-17 12:20:07 +00:00
|
|
|
->willReturnCallback(static function ($originUrl, $fileUrl, $ctx, &$http_response_header) use (&$counter) {
|
2021-12-09 16:09:07 +00:00
|
|
|
if ($counter++ === 0) {
|
2022-08-17 12:20:07 +00:00
|
|
|
$http_response_header = ['http/1.1 401 unauthorized'];
|
2020-06-16 07:35:33 +00:00
|
|
|
|
2021-12-09 16:09:07 +00:00
|
|
|
return '';
|
|
|
|
} else {
|
2022-08-17 12:20:07 +00:00
|
|
|
$http_response_header = ['http/1.1 200 OK'];
|
2020-06-16 07:35:33 +00:00
|
|
|
|
2021-12-09 16:09:07 +00:00
|
|
|
return '<?php $copied = "Copied"; ';
|
|
|
|
}
|
2020-06-16 07:35:33 +00:00
|
|
|
});
|
|
|
|
|
2021-12-10 12:14:04 +00:00
|
|
|
$file = $this->createTempFile();
|
2020-06-16 07:35:33 +00:00
|
|
|
|
|
|
|
$copyResult = $fs->copy(
|
|
|
|
'http://example.org',
|
|
|
|
'file://' . __FILE__,
|
|
|
|
$file,
|
|
|
|
true,
|
2022-08-17 12:20:07 +00:00
|
|
|
['retry-auth-failure' => true]
|
2020-06-16 07:35:33 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertTrue($copyResult);
|
|
|
|
$this->assertFileExists($file);
|
2020-09-10 15:21:11 +00:00
|
|
|
$this->assertStringContainsString('Copied', file_get_contents($file));
|
2020-06-16 07:35:33 +00:00
|
|
|
|
|
|
|
unlink($file);
|
|
|
|
}
|
|
|
|
|
2014-02-23 13:33:29 +00:00
|
|
|
/**
|
|
|
|
* @group TLS
|
|
|
|
*/
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testGetOptionsForUrlCreatesSecureTlsDefaults(): void
|
2014-02-23 13:33:29 +00:00
|
|
|
{
|
2020-06-16 07:35:33 +00:00
|
|
|
$io = $this->getIOInterfaceMock();
|
2014-02-23 13:33:29 +00:00
|
|
|
|
2022-08-17 12:20:07 +00:00
|
|
|
$res = $this->callGetOptionsForUrl($io, ['example.org', ['ssl' => ['cafile' => '/some/path/file.crt']]], [], 'http://www.example.org');
|
2014-02-23 13:33:29 +00:00
|
|
|
|
|
|
|
$this->assertTrue(isset($res['ssl']['ciphers']));
|
2020-09-11 09:27:26 +00:00
|
|
|
$this->assertMatchesRegularExpression('|!aNULL:!eNULL:!EXPORT:!DES:!RC4:!MD5:!PSK:!aECDH:!EDH-DSS-DES-CBC3-SHA:!EDH-RSA-DES-CBC3-SHA:!KRB5-DES-CBC3-SHA|', $res['ssl']['ciphers']);
|
2014-02-23 13:33:29 +00:00
|
|
|
$this->assertTrue($res['ssl']['verify_peer']);
|
|
|
|
$this->assertTrue($res['ssl']['SNI_enabled']);
|
|
|
|
$this->assertEquals(7, $res['ssl']['verify_depth']);
|
|
|
|
$this->assertEquals('/some/path/file.crt', $res['ssl']['cafile']);
|
|
|
|
if (version_compare(PHP_VERSION, '5.4.13') >= 0) {
|
|
|
|
$this->assertTrue($res['ssl']['disable_compression']);
|
2014-02-23 14:15:24 +00:00
|
|
|
} else {
|
|
|
|
$this->assertFalse(isset($res['ssl']['disable_compression']));
|
2014-02-23 13:33:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-28 17:08:24 +00:00
|
|
|
/**
|
|
|
|
* Provides URLs to public downloads at BitBucket.
|
|
|
|
*
|
|
|
|
* @return string[][]
|
|
|
|
*/
|
2022-02-18 10:22:01 +00:00
|
|
|
public function provideBitbucketPublicDownloadUrls(): array
|
2016-09-28 17:08:24 +00:00
|
|
|
{
|
2022-08-17 12:20:07 +00:00
|
|
|
return [
|
|
|
|
['https://bitbucket.org/seldaek/composer-live-test-repo/downloads/composer-unit-test-download-me.txt', '1234'],
|
|
|
|
];
|
2016-09-28 17:08:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests that a BitBucket public download is correctly retrieved.
|
|
|
|
*
|
|
|
|
* @dataProvider provideBitbucketPublicDownloadUrls
|
|
|
|
*/
|
2022-02-22 15:47:09 +00:00
|
|
|
public function testBitBucketPublicDownload(string $url, string $contents): void
|
2016-09-28 17:08:24 +00:00
|
|
|
{
|
2020-06-16 07:35:33 +00:00
|
|
|
/** @var ConsoleIO $io */
|
2016-09-28 17:08:24 +00:00
|
|
|
$io = $this
|
|
|
|
->getMockBuilder('Composer\IO\ConsoleIO')
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
|
2018-11-14 16:54:19 +00:00
|
|
|
$rfs = new RemoteFilesystem($io, $this->getConfigMock());
|
2016-09-28 17:08:24 +00:00
|
|
|
$hostname = parse_url($url, PHP_URL_HOST);
|
|
|
|
|
|
|
|
$result = $rfs->getContents($hostname, $url, false);
|
|
|
|
|
|
|
|
$this->assertEquals($contents, $result);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests that a BitBucket public download is correctly retrieved when `bitbucket-oauth` is configured.
|
|
|
|
*
|
|
|
|
* @dataProvider provideBitbucketPublicDownloadUrls
|
|
|
|
*/
|
2022-02-22 15:47:09 +00:00
|
|
|
public function testBitBucketPublicDownloadWithAuthConfigured(string $url, string $contents): void
|
2016-09-28 17:08:24 +00:00
|
|
|
{
|
2020-06-16 07:35:33 +00:00
|
|
|
/** @var MockObject|ConsoleIO $io */
|
2016-09-28 17:08:24 +00:00
|
|
|
$io = $this
|
|
|
|
->getMockBuilder('Composer\IO\ConsoleIO')
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
|
2022-08-17 12:20:07 +00:00
|
|
|
$domains = [];
|
2016-09-28 17:08:24 +00:00
|
|
|
$io
|
|
|
|
->method('hasAuthentication')
|
2022-08-17 12:20:07 +00:00
|
|
|
->willReturnCallback(static function ($arg) use (&$domains): bool {
|
2017-11-29 15:37:00 +00:00
|
|
|
$domains[] = $arg;
|
|
|
|
// first time is called with bitbucket.org, then it redirects to bbuseruploads.s3.amazonaws.com so next time we have no auth configured
|
|
|
|
return $arg === 'bitbucket.org';
|
2020-06-16 07:35:33 +00:00
|
|
|
});
|
2016-09-28 17:08:24 +00:00
|
|
|
$io
|
2021-12-09 16:09:07 +00:00
|
|
|
->method('getAuthentication')
|
2016-09-28 17:08:24 +00:00
|
|
|
->with('bitbucket.org')
|
2022-08-17 12:20:07 +00:00
|
|
|
->willReturn([
|
2016-09-28 17:08:24 +00:00
|
|
|
'username' => 'x-token-auth',
|
|
|
|
// This token is fake, but it matches a valid token's pattern.
|
2017-03-08 14:07:29 +00:00
|
|
|
'password' => '1A0yeK5Po3ZEeiiRiMWLivS0jirLdoGuaSGq9NvESFx1Fsdn493wUDXC8rz_1iKVRTl1GINHEUCsDxGh5lZ=',
|
2022-08-17 12:20:07 +00:00
|
|
|
]);
|
2016-09-28 17:08:24 +00:00
|
|
|
|
2018-11-14 16:54:19 +00:00
|
|
|
$rfs = new RemoteFilesystem($io, $this->getConfigMock());
|
2016-09-28 17:08:24 +00:00
|
|
|
$hostname = parse_url($url, PHP_URL_HOST);
|
|
|
|
|
|
|
|
$result = $rfs->getContents($hostname, $url, false);
|
|
|
|
|
|
|
|
$this->assertEquals($contents, $result);
|
2022-08-17 12:20:07 +00:00
|
|
|
$this->assertEquals(['bitbucket.org', 'bbuseruploads.s3.amazonaws.com'], $domains);
|
2016-09-28 17:08:24 +00:00
|
|
|
}
|
|
|
|
|
2021-10-27 14:18:46 +00:00
|
|
|
/**
|
|
|
|
* @param mixed[] $args
|
|
|
|
* @param mixed[] $options
|
|
|
|
*
|
|
|
|
* @return mixed[]
|
|
|
|
*/
|
2022-08-17 12:20:07 +00:00
|
|
|
private function callGetOptionsForUrl(IOInterface $io, array $args = [], array $options = [], string $fileUrl = ''): array
|
2012-03-05 10:26:07 +00:00
|
|
|
{
|
2018-11-14 16:54:19 +00:00
|
|
|
$fs = new RemoteFilesystem($io, $this->getConfigMock(), $options);
|
2020-06-16 07:35:33 +00:00
|
|
|
$ref = new ReflectionMethod($fs, 'getOptionsForUrl');
|
|
|
|
$prop = new ReflectionProperty($fs, 'fileUrl');
|
2012-03-05 10:26:07 +00:00
|
|
|
$ref->setAccessible(true);
|
2014-02-28 18:46:49 +00:00
|
|
|
$prop->setAccessible(true);
|
|
|
|
|
|
|
|
$prop->setValue($fs, $fileUrl);
|
2012-03-05 10:26:07 +00:00
|
|
|
|
|
|
|
return $ref->invokeArgs($fs, $args);
|
|
|
|
}
|
|
|
|
|
2020-06-16 07:35:33 +00:00
|
|
|
/**
|
|
|
|
* @return MockObject|Config
|
|
|
|
*/
|
|
|
|
private function getConfigMock()
|
|
|
|
{
|
|
|
|
$config = $this->getMockBuilder('Composer\Config')->getMock();
|
|
|
|
$config
|
|
|
|
->method('get')
|
2022-08-17 12:20:07 +00:00
|
|
|
->willReturnCallback(static function ($key) {
|
2020-06-16 07:35:33 +00:00
|
|
|
if ($key === 'github-domains' || $key === 'gitlab-domains') {
|
2022-08-17 12:20:07 +00:00
|
|
|
return [];
|
2020-06-16 07:35:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
});
|
|
|
|
|
|
|
|
return $config;
|
|
|
|
}
|
|
|
|
|
2022-02-22 15:47:09 +00:00
|
|
|
private function callCallbackGet(RemoteFilesystem $fs, int $notificationCode, int $severity, string $message, int $messageCode, int $bytesTransferred, int $bytesMax): void
|
2012-03-05 10:26:07 +00:00
|
|
|
{
|
2020-06-16 07:35:33 +00:00
|
|
|
$ref = new ReflectionMethod($fs, 'callbackGet');
|
2012-03-05 10:26:07 +00:00
|
|
|
$ref->setAccessible(true);
|
|
|
|
$ref->invoke($fs, $notificationCode, $severity, $message, $messageCode, $bytesTransferred, $bytesMax);
|
|
|
|
}
|
|
|
|
|
2021-10-27 14:18:46 +00:00
|
|
|
/**
|
|
|
|
* @param object|string $object
|
|
|
|
* @param mixed $value
|
|
|
|
*/
|
2022-02-22 15:47:09 +00:00
|
|
|
private function setAttribute($object, string $attribute, $value): void
|
2012-03-05 10:26:07 +00:00
|
|
|
{
|
2020-06-16 07:35:33 +00:00
|
|
|
$attr = new ReflectionProperty($object, $attribute);
|
2012-03-05 10:26:07 +00:00
|
|
|
$attr->setAccessible(true);
|
|
|
|
$attr->setValue($object, $value);
|
|
|
|
}
|
2020-06-16 07:35:33 +00:00
|
|
|
|
2021-10-27 14:18:46 +00:00
|
|
|
/**
|
|
|
|
* @param mixed $value
|
|
|
|
* @param object|string $object
|
|
|
|
*/
|
2022-02-22 15:47:09 +00:00
|
|
|
private function assertAttributeEqualsCustom($value, string $attribute, $object): void
|
2020-09-10 15:21:11 +00:00
|
|
|
{
|
|
|
|
$attr = new ReflectionProperty($object, $attribute);
|
|
|
|
$attr->setAccessible(true);
|
|
|
|
$this->assertSame($value, $attr->getValue($object));
|
|
|
|
}
|
|
|
|
|
2020-06-16 07:35:33 +00:00
|
|
|
/**
|
|
|
|
* @return MockObject|IOInterface
|
|
|
|
*/
|
|
|
|
private function getIOInterfaceMock()
|
|
|
|
{
|
|
|
|
return $this->getMockBuilder('Composer\IO\IOInterface')->getMock();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-10-27 14:18:46 +00:00
|
|
|
* @param string[] $mockedMethods
|
2020-06-16 07:35:33 +00:00
|
|
|
*
|
|
|
|
* @return RemoteFilesystem|MockObject
|
|
|
|
*/
|
2022-08-17 12:20:07 +00:00
|
|
|
private function getRemoteFilesystemWithMockedMethods(array $mockedMethods, ?AuthHelper $authHelper = null)
|
2020-06-16 07:35:33 +00:00
|
|
|
{
|
|
|
|
return $this->getMockBuilder('Composer\Util\RemoteFilesystem')
|
2022-08-17 12:20:07 +00:00
|
|
|
->setConstructorArgs([
|
2020-06-16 07:35:33 +00:00
|
|
|
$this->getIOInterfaceMock(),
|
|
|
|
$this->getConfigMock(),
|
2022-08-17 12:20:07 +00:00
|
|
|
[],
|
2020-06-16 07:35:33 +00:00
|
|
|
false,
|
2020-11-22 13:48:56 +00:00
|
|
|
$authHelper,
|
2022-08-17 12:20:07 +00:00
|
|
|
])
|
2021-12-09 21:14:04 +00:00
|
|
|
->onlyMethods($mockedMethods)
|
2020-06-16 07:35:33 +00:00
|
|
|
->getMock();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-10-27 14:18:46 +00:00
|
|
|
* @param string[] $mockedMethods
|
2020-06-16 07:35:33 +00:00
|
|
|
*
|
|
|
|
* @return AuthHelper|MockObject
|
|
|
|
*/
|
|
|
|
private function getAuthHelperWithMockedMethods(array $mockedMethods)
|
|
|
|
{
|
|
|
|
return $this->getMockBuilder('Composer\Util\AuthHelper')
|
2022-08-17 12:20:07 +00:00
|
|
|
->setConstructorArgs([
|
2020-06-16 07:35:33 +00:00
|
|
|
$this->getIOInterfaceMock(),
|
2020-11-22 13:48:56 +00:00
|
|
|
$this->getConfigMock(),
|
2022-08-17 12:20:07 +00:00
|
|
|
])
|
2021-12-09 21:14:04 +00:00
|
|
|
->onlyMethods($mockedMethods)
|
2020-06-16 07:35:33 +00:00
|
|
|
->getMock();
|
|
|
|
}
|
2012-03-05 10:26:07 +00:00
|
|
|
}
|