1
0
Fork 0

Merge pull request #10061 from Seldaek/deprecations

Fix the remainder of PHP8.1 deprecation warnings
pull/10062/head
Jordi Boggiano 2021-08-19 13:14:37 +02:00 committed by GitHub
commit e5a50d1f06
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
30 changed files with 781 additions and 946 deletions

View File

@ -15,7 +15,7 @@
bootstrap="tests/bootstrap.php" bootstrap="tests/bootstrap.php"
> >
<php> <php>
<env name="SYMFONY_DEPRECATIONS_HELPER" value="weak"/> <env name="SYMFONY_DEPRECATIONS_HELPER" value="max[direct]=0"/>
<env name="COMPOSER_TEST_SUITE" value="1"/> <env name="COMPOSER_TEST_SUITE" value="1"/>
</php> </php>
<testsuites> <testsuites>

View File

@ -36,9 +36,9 @@ class ConsoleIO extends BaseIO
/** @var HelperSet */ /** @var HelperSet */
protected $helperSet; protected $helperSet;
/** @var string */ /** @var string */
protected $lastMessage; protected $lastMessage = '';
/** @var string */ /** @var string */
protected $lastMessageErr; protected $lastMessageErr = '';
/** @var float */ /** @var float */
private $startTime; private $startTime;

View File

@ -24,7 +24,7 @@ class JsonValidationException extends Exception
public function __construct($message, $errors = array(), Exception $previous = null) public function __construct($message, $errors = array(), Exception $previous = null)
{ {
$this->errors = $errors; $this->errors = $errors;
parent::__construct($message, 0, $previous); parent::__construct((string) $message, 0, $previous);
} }
public function getErrors() public function getErrors()

View File

@ -88,7 +88,7 @@ class ArchiveManager
} }
$nameParts = array($baseName); $nameParts = array($baseName);
if (preg_match('{^[a-f0-9]{40}$}', $package->getDistReference())) { if (null !== $package->getDistReference() && preg_match('{^[a-f0-9]{40}$}', $package->getDistReference())) {
array_push($nameParts, $package->getDistReference(), $package->getDistType()); array_push($nameParts, $package->getDistReference(), $package->getDistType());
} else { } else {
array_push($nameParts, $package->getPrettyVersion(), $package->getDistReference()); array_push($nameParts, $package->getPrettyVersion(), $package->getDistReference());

View File

@ -217,6 +217,10 @@ abstract class BasePackage implements PackageInterface
throw new \UnexpectedValueException('Display mode '.$displayMode.' is not supported'); throw new \UnexpectedValueException('Display mode '.$displayMode.' is not supported');
} }
if (null === $reference) {
return $this->getPrettyVersion();
}
// if source reference is a sha1 hash -- truncate // if source reference is a sha1 hash -- truncate
if ($truncate && \strlen($reference) === 40 && $this->getSourceType() !== 'svn') { if ($truncate && \strlen($reference) === 40 && $this->getSourceType() !== 'svn') {
return $this->getPrettyVersion() . ' ' . substr($reference, 0, 7); return $this->getPrettyVersion() . ' ' . substr($reference, 0, 7);

View File

@ -27,11 +27,14 @@ class Package extends BasePackage
protected $installationSource; protected $installationSource;
protected $sourceType; protected $sourceType;
protected $sourceUrl; protected $sourceUrl;
/** @var ?string */
protected $sourceReference; protected $sourceReference;
protected $sourceMirrors; protected $sourceMirrors;
protected $distType; protected $distType;
protected $distUrl; protected $distUrl;
/** @var ?string */
protected $distReference; protected $distReference;
/** @var ?string */
protected $distSha1Checksum; protected $distSha1Checksum;
protected $distMirrors; protected $distMirrors;
protected $version; protected $version;

View File

@ -131,7 +131,7 @@ interface PackageInterface
/** /**
* Returns the repository reference of this package, e.g. master, 1.0.0 or a commit hash for git * Returns the repository reference of this package, e.g. master, 1.0.0 or a commit hash for git
* *
* @return string The repository reference * @return ?string The repository reference
*/ */
public function getSourceReference(); public function getSourceReference();
@ -172,14 +172,14 @@ interface PackageInterface
/** /**
* Returns the reference of the distribution archive of this version, e.g. master, 1.0.0 or a commit hash for git * Returns the reference of the distribution archive of this version, e.g. master, 1.0.0 or a commit hash for git
* *
* @return string * @return ?string
*/ */
public function getDistReference(); public function getDistReference();
/** /**
* Returns the sha1 checksum for the distribution archive of this version * Returns the sha1 checksum for the distribution archive of this version
* *
* @return string * @return ?string
*/ */
public function getDistSha1Checksum(); public function getDistSha1Checksum();

View File

@ -151,7 +151,7 @@ class Bitbucket
$this->io->writeError(sprintf('to create a consumer. It will be stored in "%s" for future use by Composer.', $this->config->getAuthConfigSource()->getName())); $this->io->writeError(sprintf('to create a consumer. It will be stored in "%s" for future use by Composer.', $this->config->getAuthConfigSource()->getName()));
$this->io->writeError('Ensure you enter a "Callback URL" (http://example.com is fine) or it will not be possible to create an Access Token (this callback url will not be used by composer)'); $this->io->writeError('Ensure you enter a "Callback URL" (http://example.com is fine) or it will not be possible to create an Access Token (this callback url will not be used by composer)');
$consumerKey = trim($this->io->askAndHideAnswer('Consumer Key (hidden): ')); $consumerKey = trim((string) $this->io->askAndHideAnswer('Consumer Key (hidden): '));
if (!$consumerKey) { if (!$consumerKey) {
$this->io->writeError('<warning>No consumer key given, aborting.</warning>'); $this->io->writeError('<warning>No consumer key given, aborting.</warning>');
@ -160,7 +160,7 @@ class Bitbucket
return false; return false;
} }
$consumerSecret = trim($this->io->askAndHideAnswer('Consumer Secret (hidden): ')); $consumerSecret = trim((string) $this->io->askAndHideAnswer('Consumer Secret (hidden): '));
if (!$consumerSecret) { if (!$consumerSecret) {
$this->io->writeError('<warning>No consumer secret given, aborting.</warning>'); $this->io->writeError('<warning>No consumer secret given, aborting.</warning>');

View File

@ -160,7 +160,7 @@ class Perforce
public function isStream() public function isStream()
{ {
return (strcmp($this->p4DepotType, 'stream') === 0); return is_string($this->p4DepotType) && (strcmp($this->p4DepotType, 'stream') === 0);
} }
public function getStream() public function getStream()
@ -204,11 +204,11 @@ class Perforce
public function queryP4User() public function queryP4User()
{ {
$this->getUser(); $this->getUser();
if (strlen($this->p4User) > 0) { if (strlen((string) $this->p4User) > 0) {
return; return;
} }
$this->p4User = $this->getP4variable('P4USER'); $this->p4User = $this->getP4variable('P4USER');
if (strlen($this->p4User) > 0) { if (strlen((string) $this->p4User) > 0) {
return; return;
} }
$this->p4User = $this->io->ask('Enter P4 User:'); $this->p4User = $this->io->ask('Enter P4 User:');
@ -262,7 +262,7 @@ class Perforce
return $this->p4Password; return $this->p4Password;
} }
$password = $this->getP4variable('P4PASSWD'); $password = $this->getP4variable('P4PASSWD');
if (strlen($password) <= 0) { if (strlen((string) $password) <= 0) {
$password = $this->io->askAndHideAnswer('Enter password for Perforce user ' . $this->getUser() . ': '); $password = $this->io->askAndHideAnswer('Enter password for Perforce user ' . $this->getUser() . ': ');
} }
$this->p4Password = $password; $this->p4Password = $password;

View File

@ -356,9 +356,9 @@ class ProcessExecutor
*/ */
public function splitLines($output) public function splitLines($output)
{ {
$output = trim($output); $output = trim((string) $output);
return ((string) $output === '') ? array() : preg_split('{\r?\n}', $output); return $output === '' ? array() : preg_split('{\r?\n}', $output);
} }
/** /**
@ -371,6 +371,9 @@ class ProcessExecutor
return $this->errorOutput; return $this->errorOutput;
} }
/**
* @private
*/
public function outputHandler($type, $buffer) public function outputHandler($type, $buffer)
{ {
if ($this->captureOutput) { if ($this->captureOutput) {

View File

@ -18,6 +18,7 @@ use Composer\EventDispatcher\EventDispatcher;
use Composer\Plugin\PluginEvents; use Composer\Plugin\PluginEvents;
use Composer\Plugin\PreFileDownloadEvent; use Composer\Plugin\PreFileDownloadEvent;
use Composer\Test\TestCase; use Composer\Test\TestCase;
use Composer\Test\Mock\ProcessExecutorMock;
use Composer\Util\Filesystem; use Composer\Util\Filesystem;
use Composer\Util\Http\Response; use Composer\Util\Http\Response;
use Composer\Util\Loop; use Composer\Util\Loop;
@ -194,7 +195,7 @@ class FileDownloaderTest extends TestCase
$dispatcher = new EventDispatcher( $dispatcher = new EventDispatcher(
$composerMock, $composerMock,
$this->getMockBuilder('Composer\IO\IOInterface')->getMock(), $this->getMockBuilder('Composer\IO\IOInterface')->getMock(),
$this->getMockBuilder('Composer\Util\ProcessExecutor')->getMock() new ProcessExecutorMock
); );
$dispatcher->addListener(PluginEvents::PRE_FILE_DOWNLOAD, function (PreFileDownloadEvent $event) use ($expectedUrl) { $dispatcher->addListener(PluginEvents::PRE_FILE_DOWNLOAD, function (PreFileDownloadEvent $event) use ($expectedUrl) {
$event->setProcessedUrl($expectedUrl); $event->setProcessedUrl($expectedUrl);
@ -288,7 +289,7 @@ class FileDownloaderTest extends TestCase
$dispatcher = new EventDispatcher( $dispatcher = new EventDispatcher(
$composerMock, $composerMock,
$this->getMockBuilder('Composer\IO\IOInterface')->getMock(), $this->getMockBuilder('Composer\IO\IOInterface')->getMock(),
$this->getMockBuilder('Composer\Util\ProcessExecutor')->getMock() new ProcessExecutorMock
); );
$dispatcher->addListener(PluginEvents::PRE_FILE_DOWNLOAD, function (PreFileDownloadEvent $event) use ($customCacheKey) { $dispatcher->addListener(PluginEvents::PRE_FILE_DOWNLOAD, function (PreFileDownloadEvent $event) use ($customCacheKey) {
$event->setCustomCacheKey($customCacheKey); $event->setCustomCacheKey($customCacheKey);

View File

@ -16,6 +16,7 @@ use Composer\Downloader\FossilDownloader;
use Composer\Test\TestCase; use Composer\Test\TestCase;
use Composer\Util\Filesystem; use Composer\Util\Filesystem;
use Composer\Util\Platform; use Composer\Util\Platform;
use Composer\Test\Mock\ProcessExecutorMock;
class FossilDownloaderTest extends TestCase class FossilDownloaderTest extends TestCase
{ {
@ -39,7 +40,7 @@ class FossilDownloaderTest extends TestCase
{ {
$io = $io ?: $this->getMockBuilder('Composer\IO\IOInterface')->getMock(); $io = $io ?: $this->getMockBuilder('Composer\IO\IOInterface')->getMock();
$config = $config ?: $this->getMockBuilder('Composer\Config')->getMock(); $config = $config ?: $this->getMockBuilder('Composer\Config')->getMock();
$executor = $executor ?: $this->getMockBuilder('Composer\Util\ProcessExecutor')->getMock(); $executor = $executor ?: new ProcessExecutorMock;
$filesystem = $filesystem ?: $this->getMockBuilder('Composer\Util\Filesystem')->getMock(); $filesystem = $filesystem ?: $this->getMockBuilder('Composer\Util\Filesystem')->getMock();
return new FossilDownloader($io, $config, $executor, $filesystem); return new FossilDownloader($io, $config, $executor, $filesystem);
@ -67,28 +68,18 @@ class FossilDownloaderTest extends TestCase
$packageMock->expects($this->once()) $packageMock->expects($this->once())
->method('getSourceUrls') ->method('getSourceUrls')
->will($this->returnValue(array('http://fossil.kd2.org/kd2fw/'))); ->will($this->returnValue(array('http://fossil.kd2.org/kd2fw/')));
$processExecutor = $this->getMockBuilder('Composer\Util\ProcessExecutor')->getMock();
$expectedFossilCommand = $this->getCmd('fossil clone -- \'http://fossil.kd2.org/kd2fw/\' \'repo.fossil\''); $process = new ProcessExecutorMock;
$processExecutor->expects($this->at(0)) $process->expects(array(
->method('execute') $this->getCmd('fossil clone -- \'http://fossil.kd2.org/kd2fw/\' \'repo.fossil\''),
->with($this->equalTo($expectedFossilCommand)) $this->getCmd('fossil open --nested -- \'repo.fossil\''),
->will($this->returnValue(0)); $this->getCmd('fossil update -- \'trunk\''),
), true);
$expectedFossilCommand = $this->getCmd('fossil open --nested -- \'repo.fossil\''); $downloader = $this->getDownloaderMock(null, null, $process);
$processExecutor->expects($this->at(1))
->method('execute')
->with($this->equalTo($expectedFossilCommand))
->will($this->returnValue(0));
$expectedFossilCommand = $this->getCmd('fossil update -- \'trunk\'');
$processExecutor->expects($this->at(2))
->method('execute')
->with($this->equalTo($expectedFossilCommand))
->will($this->returnValue(0));
$downloader = $this->getDownloaderMock(null, null, $processExecutor);
$downloader->install($packageMock, 'repo'); $downloader->install($packageMock, 'repo');
$process->assertComplete($this);
} }
public function testUpdateforPackageWithoutSourceReference() public function testUpdateforPackageWithoutSourceReference()
@ -126,42 +117,46 @@ class FossilDownloaderTest extends TestCase
$packageMock->expects($this->any()) $packageMock->expects($this->any())
->method('getVersion') ->method('getVersion')
->will($this->returnValue('1.0.0.0')); ->will($this->returnValue('1.0.0.0'));
$processExecutor = $this->getMockBuilder('Composer\Util\ProcessExecutor')->getMock();
$expectedFossilCommand = $this->getCmd("fossil changes"); $process = new ProcessExecutorMock;
$processExecutor->expects($this->at(0)) $process->expects(array(
->method('execute') $this->getCmd("fossil changes"),
->with($this->equalTo($expectedFossilCommand)) $this->getCmd("fossil pull && fossil up 'trunk'"),
->will($this->returnValue(0)); ), true);
$expectedFossilCommand = $this->getCmd("fossil pull && fossil up 'trunk'");
$processExecutor->expects($this->at(1))
->method('execute')
->with($this->equalTo($expectedFossilCommand))
->will($this->returnValue(0));
$downloader = $this->getDownloaderMock(null, null, $processExecutor); $downloader = $this->getDownloaderMock(null, null, $process);
$downloader->prepare('update', $packageMock, $this->workingDir, $packageMock); $downloader->prepare('update', $packageMock, $this->workingDir, $packageMock);
$downloader->update($packageMock, $packageMock, $this->workingDir); $downloader->update($packageMock, $packageMock, $this->workingDir);
$downloader->cleanup('update', $packageMock, $this->workingDir, $packageMock); $downloader->cleanup('update', $packageMock, $this->workingDir, $packageMock);
$process->assertComplete($this);
} }
public function testRemove() public function testRemove()
{ {
$expectedResetCommand = $this->getCmd('cd \'composerPath\' && fossil status'); // Ensure file exists
$file = $this->workingDir . '/.fslckout';
touch($file);
$packageMock = $this->getMockBuilder('Composer\Package\PackageInterface')->getMock(); $packageMock = $this->getMockBuilder('Composer\Package\PackageInterface')->getMock();
$processExecutor = $this->getMockBuilder('Composer\Util\ProcessExecutor')->getMock();
$processExecutor->expects($this->any()) $process = new ProcessExecutorMock;
->method('execute') $process->expects(array(
->with($this->equalTo($expectedResetCommand)); $this->getCmd('fossil changes'),
), true);
$filesystem = $this->getMockBuilder('Composer\Util\Filesystem')->getMock(); $filesystem = $this->getMockBuilder('Composer\Util\Filesystem')->getMock();
$filesystem->expects($this->once()) $filesystem->expects($this->once())
->method('removeDirectoryAsync') ->method('removeDirectoryAsync')
->with($this->equalTo('composerPath')) ->with($this->equalTo($this->workingDir))
->will($this->returnValue(\React\Promise\resolve(true))); ->will($this->returnValue(\React\Promise\resolve(true)));
$downloader = $this->getDownloaderMock(null, null, $processExecutor, $filesystem); $downloader = $this->getDownloaderMock(null, null, $process, $filesystem);
$downloader->remove($packageMock, 'composerPath'); $downloader->prepare('uninstall', $packageMock, $this->workingDir);
$downloader->remove($packageMock, $this->workingDir);
$downloader->cleanup('uninstall', $packageMock, $this->workingDir);
$process->assertComplete($this);
} }
public function testGetInstallationSource() public function testGetInstallationSource()

View File

@ -17,7 +17,7 @@ use Composer\Config;
use Composer\Test\TestCase; use Composer\Test\TestCase;
use Composer\Util\Filesystem; use Composer\Util\Filesystem;
use Composer\Util\Platform; use Composer\Util\Platform;
use Prophecy\Argument; use Composer\Test\Mock\ProcessExecutorMock;
class GitDownloaderTest extends TestCase class GitDownloaderTest extends TestCase
{ {
@ -69,7 +69,7 @@ class GitDownloaderTest extends TestCase
protected function getDownloaderMock($io = null, $config = null, $executor = null, $filesystem = null) protected function getDownloaderMock($io = null, $config = null, $executor = null, $filesystem = null)
{ {
$io = $io ?: $this->getMockBuilder('Composer\IO\IOInterface')->getMock(); $io = $io ?: $this->getMockBuilder('Composer\IO\IOInterface')->getMock();
$executor = $executor ?: $this->getMockBuilder('Composer\Util\ProcessExecutor')->getMock(); $executor = $executor ?: new ProcessExecutorMock;
$filesystem = $filesystem ?: $this->getMockBuilder('Composer\Util\Filesystem')->getMock(); $filesystem = $filesystem ?: $this->getMockBuilder('Composer\Util\Filesystem')->getMock();
$config = $this->setupConfig($config); $config = $this->setupConfig($config);
@ -107,29 +107,21 @@ class GitDownloaderTest extends TestCase
$packageMock->expects($this->any()) $packageMock->expects($this->any())
->method('getPrettyVersion') ->method('getPrettyVersion')
->will($this->returnValue('dev-master')); ->will($this->returnValue('dev-master'));
$processExecutor = $this->getMockBuilder('Composer\Util\ProcessExecutor')->getMock();
$expectedGitCommand = $this->winCompat("git clone --no-checkout -- 'https://example.com/composer/composer' 'composerPath' && cd 'composerPath' && git remote add composer -- 'https://example.com/composer/composer' && git fetch composer && git remote set-url origin -- 'https://example.com/composer/composer' && git remote set-url composer -- 'https://example.com/composer/composer'"); $process = new ProcessExecutorMock;
$processExecutor->expects($this->at(0)) $process->expects(array(
->method('execute') $this->winCompat("git clone --no-checkout -- 'https://example.com/composer/composer' 'composerPath' && cd 'composerPath' && git remote add composer -- 'https://example.com/composer/composer' && git fetch composer && git remote set-url origin -- 'https://example.com/composer/composer' && git remote set-url composer -- 'https://example.com/composer/composer'"),
->with($this->equalTo($expectedGitCommand)) $this->winCompat("git branch -r"),
->will($this->returnValue(0)); $this->winCompat("(git checkout 'master' -- || git checkout -B 'master' 'composer/master' --) && git reset --hard '1234567890123456789012345678901234567890' --"),
), true);
$processExecutor->expects($this->at(1)) $downloader = $this->getDownloaderMock(null, null, $process);
->method('execute')
->with($this->equalTo($this->winCompat("git branch -r")), $this->equalTo(null), $this->equalTo($this->winCompat('composerPath')))
->will($this->returnValue(0));
$processExecutor->expects($this->at(2))
->method('execute')
->with($this->equalTo($this->winCompat("(git checkout 'master' -- || git checkout -B 'master' 'composer/master' --) && git reset --hard '1234567890123456789012345678901234567890' --")), $this->equalTo(null), $this->equalTo($this->winCompat('composerPath')))
->will($this->returnValue(0));
$downloader = $this->getDownloaderMock(null, null, $processExecutor);
$downloader->download($packageMock, 'composerPath'); $downloader->download($packageMock, 'composerPath');
$downloader->prepare('install', $packageMock, 'composerPath'); $downloader->prepare('install', $packageMock, 'composerPath');
$downloader->install($packageMock, 'composerPath'); $downloader->install($packageMock, 'composerPath');
$downloader->cleanup('install', $packageMock, 'composerPath'); $downloader->cleanup('install', $packageMock, 'composerPath');
$process->assertComplete($this);
} }
public function testDownloadWithCache() public function testDownloadWithCache()
@ -147,7 +139,6 @@ class GitDownloaderTest extends TestCase
$packageMock->expects($this->any()) $packageMock->expects($this->any())
->method('getPrettyVersion') ->method('getPrettyVersion')
->will($this->returnValue('dev-master')); ->will($this->returnValue('dev-master'));
$processExecutor = $this->getMockBuilder('Composer\Util\ProcessExecutor')->getMock();
$this->initGitVersion('2.17.0'); $this->initGitVersion('2.17.0');
@ -158,50 +149,24 @@ class GitDownloaderTest extends TestCase
$filesystem = new \Composer\Util\Filesystem; $filesystem = new \Composer\Util\Filesystem;
$filesystem->removeDirectory($cachePath); $filesystem->removeDirectory($cachePath);
$expectedGitCommand = $this->winCompat(sprintf("git clone --mirror -- 'https://example.com/composer/composer' '%s'", $cachePath)); $process = new ProcessExecutorMock;
$processExecutor->expects($this->at(0)) $process->expects(array(
->method('execute') array('cmd' => $this->winCompat(sprintf("git clone --mirror -- 'https://example.com/composer/composer' '%s'", $cachePath)), 'callback' => function () use ($cachePath) { @mkdir($cachePath, 0777, true); }),
->with($this->equalTo($expectedGitCommand)) array('cmd' => 'git rev-parse --git-dir', 'stdout' => '.'),
->will($this->returnCallback(function () use ($cachePath) { $this->winCompat('git rev-parse --quiet --verify \'1234567890123456789012345678901234567890^{commit}\''),
@mkdir($cachePath, 0777, true); $this->winCompat(sprintf("git clone --no-checkout '%1\$s' 'composerPath' --dissociate --reference '%1\$s' && cd 'composerPath' && git remote set-url origin -- 'https://example.com/composer/composer' && git remote add composer -- 'https://example.com/composer/composer'", $cachePath)),
'git branch -r',
$this->winCompat("(git checkout 'master' -- || git checkout -B 'master' 'composer/master' --) && git reset --hard '1234567890123456789012345678901234567890' --")
), true);
return 0; $downloader = $this->getDownloaderMock(null, $config, $process);
}));
$processExecutor->expects($this->at(1))
->method('execute')
->with($this->equalTo('git rev-parse --git-dir'), $this->anything(), $this->equalTo($this->winCompat($cachePath)))
->will($this->returnCallback(function ($command, &$output = null) {
$output = '.';
return 0;
}));
$processExecutor->expects($this->at(2))
->method('execute')
->with($this->equalTo($this->winCompat('git rev-parse --quiet --verify \'1234567890123456789012345678901234567890^{commit}\'')), $this->equalTo(null), $this->equalTo($this->winCompat($cachePath)))
->will($this->returnValue(0));
$expectedGitCommand = $this->winCompat(sprintf("git clone --no-checkout '%1\$s' 'composerPath' --dissociate --reference '%1\$s' && cd 'composerPath' && git remote set-url origin -- 'https://example.com/composer/composer' && git remote add composer -- 'https://example.com/composer/composer'", $cachePath));
$processExecutor->expects($this->at(3))
->method('execute')
->with($this->equalTo($expectedGitCommand))
->will($this->returnValue(0));
$processExecutor->expects($this->at(4))
->method('execute')
->with($this->equalTo($this->winCompat("git branch -r")), $this->equalTo(null), $this->equalTo($this->winCompat('composerPath')))
->will($this->returnValue(0));
$processExecutor->expects($this->at(5))
->method('execute')
->with($this->equalTo($this->winCompat("(git checkout 'master' -- || git checkout -B 'master' 'composer/master' --) && git reset --hard '1234567890123456789012345678901234567890' --")), $this->equalTo(null), $this->equalTo($this->winCompat('composerPath')))
->will($this->returnValue(0));
$downloader = $this->getDownloaderMock(null, $config, $processExecutor);
$downloader->download($packageMock, 'composerPath'); $downloader->download($packageMock, 'composerPath');
$downloader->prepare('install', $packageMock, 'composerPath'); $downloader->prepare('install', $packageMock, 'composerPath');
$downloader->install($packageMock, 'composerPath'); $downloader->install($packageMock, 'composerPath');
$downloader->cleanup('install', $packageMock, 'composerPath'); $downloader->cleanup('install', $packageMock, 'composerPath');
@rmdir($cachePath); @rmdir($cachePath);
$process->assertComplete($this);
} }
public function testDownloadUsesVariousProtocolsAndSetsPushUrlForGithub() public function testDownloadUsesVariousProtocolsAndSetsPushUrlForGithub()
@ -219,52 +184,28 @@ class GitDownloaderTest extends TestCase
$packageMock->expects($this->any()) $packageMock->expects($this->any())
->method('getPrettyVersion') ->method('getPrettyVersion')
->will($this->returnValue('1.0.0')); ->will($this->returnValue('1.0.0'));
$processExecutor = $this->getMockBuilder('Composer\Util\ProcessExecutor')->getMock();
$expectedGitCommand = $this->winCompat("git clone --no-checkout -- 'https://github.com/mirrors/composer' 'composerPath' && cd 'composerPath' && git remote add composer -- 'https://github.com/mirrors/composer' && git fetch composer && git remote set-url origin -- 'https://github.com/mirrors/composer' && git remote set-url composer -- 'https://github.com/mirrors/composer'"); $process = new ProcessExecutorMock;
$processExecutor->expects($this->at(0)) $process->expects(array(
->method('execute') array(
->with($this->equalTo($expectedGitCommand)) 'cmd' => $this->winCompat("git clone --no-checkout -- 'https://github.com/mirrors/composer' 'composerPath' && cd 'composerPath' && git remote add composer -- 'https://github.com/mirrors/composer' && git fetch composer && git remote set-url origin -- 'https://github.com/mirrors/composer' && git remote set-url composer -- 'https://github.com/mirrors/composer'"),
->will($this->returnValue(1)); 'return' => 1,
'stderr' => 'Error1',
),
$this->winCompat("git clone --no-checkout -- 'git@github.com:mirrors/composer' 'composerPath' && cd 'composerPath' && git remote add composer -- 'git@github.com:mirrors/composer' && git fetch composer && git remote set-url origin -- 'git@github.com:mirrors/composer' && git remote set-url composer -- 'git@github.com:mirrors/composer'"),
$this->winCompat("git remote set-url origin -- 'https://github.com/composer/composer'"),
$this->winCompat("git remote set-url --push origin -- 'git@github.com:composer/composer.git'"),
'git branch -r',
$this->winCompat("git checkout 'ref' -- && git reset --hard 'ref' --"),
), true);
$processExecutor->expects($this->at(1)) $downloader = $this->getDownloaderMock(null, new Config(), $process);
->method('getErrorOutput')
->with()
->will($this->returnValue('Error1'));
$expectedGitCommand = $this->winCompat("git clone --no-checkout -- 'git@github.com:mirrors/composer' 'composerPath' && cd 'composerPath' && git remote add composer -- 'git@github.com:mirrors/composer' && git fetch composer && git remote set-url origin -- 'git@github.com:mirrors/composer' && git remote set-url composer -- 'git@github.com:mirrors/composer'");
$processExecutor->expects($this->at(2))
->method('execute')
->with($this->equalTo($expectedGitCommand))
->will($this->returnValue(0));
$expectedGitCommand = $this->winCompat("git remote set-url origin -- 'https://github.com/composer/composer'");
$processExecutor->expects($this->at(3))
->method('execute')
->with($this->equalTo($expectedGitCommand), $this->equalTo(null), $this->equalTo($this->winCompat('composerPath')))
->will($this->returnValue(0));
$expectedGitCommand = $this->winCompat("git remote set-url --push origin -- 'git@github.com:composer/composer.git'");
$processExecutor->expects($this->at(4))
->method('execute')
->with($this->equalTo($expectedGitCommand), $this->equalTo(null), $this->equalTo($this->winCompat('composerPath')))
->will($this->returnValue(0));
$processExecutor->expects($this->at(5))
->method('execute')
->with($this->equalTo('git branch -r'))
->will($this->returnValue(0));
$processExecutor->expects($this->at(6))
->method('execute')
->with($this->equalTo($this->winCompat("git checkout 'ref' -- && git reset --hard 'ref' --")), $this->equalTo(null), $this->equalTo($this->winCompat('composerPath')))
->will($this->returnValue(0));
$downloader = $this->getDownloaderMock(null, new Config(), $processExecutor);
$downloader->download($packageMock, 'composerPath'); $downloader->download($packageMock, 'composerPath');
$downloader->prepare('install', $packageMock, 'composerPath'); $downloader->prepare('install', $packageMock, 'composerPath');
$downloader->install($packageMock, 'composerPath'); $downloader->install($packageMock, 'composerPath');
$downloader->cleanup('install', $packageMock, 'composerPath'); $downloader->cleanup('install', $packageMock, 'composerPath');
$process->assertComplete($this);
} }
public function pushUrlProvider() public function pushUrlProvider()
@ -297,37 +238,29 @@ class GitDownloaderTest extends TestCase
$packageMock->expects($this->any()) $packageMock->expects($this->any())
->method('getPrettyVersion') ->method('getPrettyVersion')
->will($this->returnValue('1.0.0')); ->will($this->returnValue('1.0.0'));
$processExecutor = $this->getMockBuilder('Composer\Util\ProcessExecutor')->getMock();
$expectedGitCommand = $this->winCompat("git clone --no-checkout -- '{$url}' 'composerPath' && cd 'composerPath' && git remote add composer -- '{$url}' && git fetch composer && git remote set-url origin -- '{$url}' && git remote set-url composer -- '{$url}'"); $process = new ProcessExecutorMock;
$processExecutor->expects($this->at(0)) $process->expects(array(
->method('execute') $this->winCompat("git clone --no-checkout -- '{$url}' 'composerPath' && cd 'composerPath' && git remote add composer -- '{$url}' && git fetch composer && git remote set-url origin -- '{$url}' && git remote set-url composer -- '{$url}'"),
->with($this->equalTo($expectedGitCommand)) $this->winCompat("git remote set-url --push origin -- '{$pushUrl}'"),
->will($this->returnValue(0)); 'git branch -r',
$this->winCompat("git checkout 'ref' -- && git reset --hard 'ref' --"),
$expectedGitCommand = $this->winCompat("git remote set-url --push origin -- '{$pushUrl}'"); ), true);
$processExecutor->expects($this->at(1))
->method('execute')
->with($this->equalTo($expectedGitCommand), $this->equalTo(null), $this->equalTo($this->winCompat('composerPath')))
->will($this->returnValue(0));
$processExecutor->expects($this->exactly(4))
->method('execute')
->will($this->returnValue(0));
$config = new Config(); $config = new Config();
$config->merge(array('config' => array('github-protocols' => $protocols))); $config->merge(array('config' => array('github-protocols' => $protocols)));
$downloader = $this->getDownloaderMock(null, $config, $processExecutor); $downloader = $this->getDownloaderMock(null, $config, $process);
$downloader->download($packageMock, 'composerPath'); $downloader->download($packageMock, 'composerPath');
$downloader->prepare('install', $packageMock, 'composerPath'); $downloader->prepare('install', $packageMock, 'composerPath');
$downloader->install($packageMock, 'composerPath'); $downloader->install($packageMock, 'composerPath');
$downloader->cleanup('install', $packageMock, 'composerPath'); $downloader->cleanup('install', $packageMock, 'composerPath');
$process->assertComplete($this);
} }
public function testDownloadThrowsRuntimeExceptionIfGitCommandFails() public function testDownloadThrowsRuntimeExceptionIfGitCommandFails()
{ {
$expectedGitCommand = $this->winCompat("git clone --no-checkout -- 'https://example.com/composer/composer' 'composerPath' && cd 'composerPath' && git remote add composer -- 'https://example.com/composer/composer' && git fetch composer && git remote set-url origin -- 'https://example.com/composer/composer' && git remote set-url composer -- 'https://example.com/composer/composer'");
$packageMock = $this->getMockBuilder('Composer\Package\PackageInterface')->getMock(); $packageMock = $this->getMockBuilder('Composer\Package\PackageInterface')->getMock();
$packageMock->expects($this->any()) $packageMock->expects($this->any())
->method('getSourceReference') ->method('getSourceReference')
@ -335,19 +268,31 @@ class GitDownloaderTest extends TestCase
$packageMock->expects($this->any()) $packageMock->expects($this->any())
->method('getSourceUrls') ->method('getSourceUrls')
->will($this->returnValue(array('https://example.com/composer/composer'))); ->will($this->returnValue(array('https://example.com/composer/composer')));
$processExecutor = $this->getMockBuilder('Composer\Util\ProcessExecutor')->getMock(); $packageMock->expects($this->any())
$processExecutor->expects($this->at(0)) ->method('getSourceUrl')
->method('execute') ->will($this->returnValue('https://example.com/composer/composer'));
->with($this->equalTo($expectedGitCommand)) $packageMock->expects($this->any())
->will($this->returnValue(1)); ->method('getPrettyVersion')
->will($this->returnValue('1.0.0'));
$process = new ProcessExecutorMock;
$process->expects(array(
array(
'cmd' => $this->winCompat("git clone --no-checkout -- 'https://example.com/composer/composer' 'composerPath' && cd 'composerPath' && git remote add composer -- 'https://example.com/composer/composer' && git fetch composer && git remote set-url origin -- 'https://example.com/composer/composer' && git remote set-url composer -- 'https://example.com/composer/composer'"),
'return' => 1,
),
));
// not using PHPUnit's expected exception because Prophecy exceptions extend from RuntimeException too so it is not safe // not using PHPUnit's expected exception because Prophecy exceptions extend from RuntimeException too so it is not safe
try { try {
$downloader = $this->getDownloaderMock(null, null, $processExecutor); $downloader = $this->getDownloaderMock(null, null, $process);
$downloader->download($packageMock, 'composerPath'); $downloader->download($packageMock, 'composerPath');
$downloader->prepare('install', $packageMock, 'composerPath'); $downloader->prepare('install', $packageMock, 'composerPath');
$downloader->install($packageMock, 'composerPath'); $downloader->install($packageMock, 'composerPath');
$downloader->cleanup('install', $packageMock, 'composerPath'); $downloader->cleanup('install', $packageMock, 'composerPath');
$process->assertComplete($this);
$this->fail('This test should throw'); $this->fail('This test should throw');
} catch (\RuntimeException $e) { } catch (\RuntimeException $e) {
if ('RuntimeException' !== get_class($e)) { if ('RuntimeException' !== get_class($e)) {
@ -388,21 +333,29 @@ class GitDownloaderTest extends TestCase
$packageMock->expects($this->any()) $packageMock->expects($this->any())
->method('getVersion') ->method('getVersion')
->will($this->returnValue('1.0.0.0')); ->will($this->returnValue('1.0.0.0'));
$packageMock->expects($this->any())
->method('getPrettyVersion')
->will($this->returnValue('1.0.0'));
$process = $this->prophesize('Composer\Util\ProcessExecutor'); $process = new ProcessExecutorMock;
$process->execute($this->winCompat('git show-ref --head -d'), Argument::cetera())->willReturn(0); $process->expects(array(
$process->execute($this->winCompat('git status --porcelain --untracked-files=no'), Argument::cetera())->willReturn(0); $this->winCompat('git show-ref --head -d'),
$process->execute($this->winCompat('git remote -v'), Argument::cetera())->willReturn(0); $this->winCompat('git status --porcelain --untracked-files=no'),
$process->execute($this->winCompat('git branch -r'), Argument::cetera())->willReturn(0); $this->winCompat('git remote -v'),
$process->execute($expectedGitUpdateCommand, null, $this->winCompat($this->workingDir))->willReturn(0)->shouldBeCalled(); $expectedGitUpdateCommand,
$process->execute($this->winCompat("git checkout 'ref' -- && git reset --hard 'ref' --"), null, $this->winCompat($this->workingDir))->willReturn(0)->shouldBeCalled(); $this->winCompat('git branch -r'),
$this->winCompat("git checkout 'ref' -- && git reset --hard 'ref' --"),
$this->winCompat('git remote -v'),
), true);
$this->fs->ensureDirectoryExists($this->workingDir.'/.git'); $this->fs->ensureDirectoryExists($this->workingDir.'/.git');
$downloader = $this->getDownloaderMock(null, new Config(), $process->reveal()); $downloader = $this->getDownloaderMock(null, new Config(), $process);
$downloader->download($packageMock, $this->workingDir, $packageMock); $downloader->download($packageMock, $this->workingDir, $packageMock);
$downloader->prepare('update', $packageMock, $this->workingDir, $packageMock); $downloader->prepare('update', $packageMock, $this->workingDir, $packageMock);
$downloader->update($packageMock, $packageMock, $this->workingDir); $downloader->update($packageMock, $packageMock, $this->workingDir);
$downloader->cleanup('update', $packageMock, $this->workingDir, $packageMock); $downloader->cleanup('update', $packageMock, $this->workingDir, $packageMock);
$process->assertComplete($this);
} }
public function testUpdateWithNewRepoUrl() public function testUpdateWithNewRepoUrl()
@ -422,59 +375,38 @@ class GitDownloaderTest extends TestCase
$packageMock->expects($this->any()) $packageMock->expects($this->any())
->method('getVersion') ->method('getVersion')
->will($this->returnValue('1.0.0.0')); ->will($this->returnValue('1.0.0.0'));
$packageMock->expects($this->any())
->method('getPrettyVersion')
->will($this->returnValue('1.0.0'));
$processExecutor = $this->getMockBuilder('Composer\Util\ProcessExecutor')->getMock(); $process = new ProcessExecutorMock;
$processExecutor->expects($this->at(0)) $process->expects(array(
->method('execute') $this->winCompat("git show-ref --head -d"),
->with($this->equalTo($this->winCompat("git show-ref --head -d"))) $this->winCompat("git status --porcelain --untracked-files=no"),
->will($this->returnValue(0)); $this->winCompat("git remote -v"),
$processExecutor->expects($this->at(1)) $this->winCompat($expectedGitUpdateCommand),
->method('execute') 'git branch -r',
->with($this->equalTo($this->winCompat("git status --porcelain --untracked-files=no"))) $this->winCompat("git checkout 'ref' -- && git reset --hard 'ref' --"),
->will($this->returnValue(0)); array(
$processExecutor->expects($this->at(2)) 'cmd' => $this->winCompat("git remote -v"),
->method('execute') 'stdout' => 'origin https://github.com/old/url (fetch)
->with($this->equalTo($this->winCompat("git remote -v")))
->will($this->returnValue(0));
$processExecutor->expects($this->at(3))
->method('execute')
->with($this->equalTo($this->winCompat($expectedGitUpdateCommand)), $this->equalTo(null), $this->equalTo($this->winCompat($this->workingDir)))
->will($this->returnValue(0));
$processExecutor->expects($this->at(4))
->method('execute')
->with($this->equalTo('git branch -r'))
->will($this->returnValue(0));
$processExecutor->expects($this->at(5))
->method('execute')
->with($this->equalTo($this->winCompat("git checkout 'ref' -- && git reset --hard 'ref' --")), $this->equalTo(null), $this->equalTo($this->winCompat($this->workingDir)))
->will($this->returnValue(0));
$processExecutor->expects($this->at(6))
->method('execute')
->with($this->equalTo($this->winCompat("git remote -v")))
->will($this->returnCallback(function ($cmd, &$output, $cwd) {
$output = 'origin https://github.com/old/url (fetch)
origin https://github.com/old/url (push) origin https://github.com/old/url (push)
composer https://github.com/old/url (fetch) composer https://github.com/old/url (fetch)
composer https://github.com/old/url (push) composer https://github.com/old/url (push)
'; ',
),
return 0; $this->winCompat("git remote set-url origin -- 'https://github.com/composer/composer'"),
})); $this->winCompat("git remote set-url --push origin -- 'git@github.com:composer/composer.git'"),
$processExecutor->expects($this->at(7)) ), true);
->method('execute')
->with($this->equalTo($this->winCompat("git remote set-url origin -- 'https://github.com/composer/composer'")), $this->equalTo(null), $this->equalTo($this->winCompat($this->workingDir)))
->will($this->returnValue(0));
$processExecutor->expects($this->at(8))
->method('execute')
->with($this->equalTo($this->winCompat("git remote set-url --push origin -- 'git@github.com:composer/composer.git'")), $this->equalTo(null), $this->equalTo($this->winCompat($this->workingDir)))
->will($this->returnValue(0));
$this->fs->ensureDirectoryExists($this->workingDir.'/.git'); $this->fs->ensureDirectoryExists($this->workingDir.'/.git');
$downloader = $this->getDownloaderMock(null, new Config(), $processExecutor); $downloader = $this->getDownloaderMock(null, new Config(), $process);
$downloader->download($packageMock, $this->workingDir, $packageMock); $downloader->download($packageMock, $this->workingDir, $packageMock);
$downloader->prepare('update', $packageMock, $this->workingDir, $packageMock); $downloader->prepare('update', $packageMock, $this->workingDir, $packageMock);
$downloader->update($packageMock, $packageMock, $this->workingDir); $downloader->update($packageMock, $packageMock, $this->workingDir);
$downloader->cleanup('update', $packageMock, $this->workingDir, $packageMock); $downloader->cleanup('update', $packageMock, $this->workingDir, $packageMock);
$process->assertComplete($this);
} }
/** /**
@ -496,25 +428,35 @@ composer https://github.com/old/url (push)
->method('getVersion') ->method('getVersion')
->will($this->returnValue('1.0.0.0')); ->will($this->returnValue('1.0.0.0'));
$process = $this->prophesize('Composer\Util\ProcessExecutor'); $process = new ProcessExecutorMock;
$process->execute($this->winCompat('git --version'), Argument::cetera())->willReturn(0); $process->expects(array(
$process->execute($this->winCompat('git show-ref --head -d'), Argument::cetera())->willReturn(0); $this->winCompat('git show-ref --head -d'),
$process->execute($this->winCompat('git status --porcelain --untracked-files=no'), Argument::cetera())->willReturn(0); $this->winCompat('git status --porcelain --untracked-files=no'),
$process->execute($this->winCompat('git remote -v'), Argument::cetera())->willReturn(0); $this->winCompat('git remote -v'),
$process->execute($this->winCompat('git branch -r'), Argument::cetera())->willReturn(0); array(
$process->execute($expectedGitUpdateCommand, null, $this->winCompat($this->workingDir))->willReturn(1)->shouldBeCalled(); 'cmd' => $expectedGitUpdateCommand,
$process->execute($expectedGitUpdateCommand2, null, $this->winCompat($this->workingDir))->willReturn(1)->shouldBeCalled(); 'return' => 1,
$process->getErrorOutput()->willReturn(''); ),
array(
'cmd' => $expectedGitUpdateCommand2,
'return' => 1,
),
$this->winCompat('git --version'),
$this->winCompat('git branch -r'),
), true);
$this->fs->ensureDirectoryExists($this->workingDir.'/.git'); $this->fs->ensureDirectoryExists($this->workingDir.'/.git');
// not using PHPUnit's expected exception because Prophecy exceptions extend from RuntimeException too so it is not safe // not using PHPUnit's expected exception because Prophecy exceptions extend from RuntimeException too so it is not safe
try { try {
$downloader = $this->getDownloaderMock(null, new Config(), $process->reveal()); $downloader = $this->getDownloaderMock(null, new Config(), $process);
$downloader->download($packageMock, $this->workingDir, $packageMock); $downloader->download($packageMock, $this->workingDir, $packageMock);
$downloader->prepare('update', $packageMock, $this->workingDir, $packageMock); $downloader->prepare('update', $packageMock, $this->workingDir, $packageMock);
$downloader->update($packageMock, $packageMock, $this->workingDir); $downloader->update($packageMock, $packageMock, $this->workingDir);
$downloader->cleanup('update', $packageMock, $this->workingDir, $packageMock); $downloader->cleanup('update', $packageMock, $this->workingDir, $packageMock);
$process->assertComplete($this);
$this->fail('This test should throw'); $this->fail('This test should throw');
} catch (\RuntimeException $e) { } catch (\RuntimeException $e) {
if ('RuntimeException' !== get_class($e)) { if ('RuntimeException' !== get_class($e)) {
@ -539,24 +481,38 @@ composer https://github.com/old/url (push)
$packageMock->expects($this->any()) $packageMock->expects($this->any())
->method('getSourceUrls') ->method('getSourceUrls')
->will($this->returnValue(array(Platform::isWindows() ? 'C:\\' : '/', 'https://github.com/composer/composer'))); ->will($this->returnValue(array(Platform::isWindows() ? 'C:\\' : '/', 'https://github.com/composer/composer')));
$packageMock->expects($this->any())
->method('getPrettyVersion')
->will($this->returnValue('1.0.0'));
$process = $this->prophesize('Composer\Util\ProcessExecutor'); $process = new ProcessExecutorMock;
$process->execute($this->winCompat('git --version'), Argument::cetera())->willReturn(0); $process->expects(array(
$process->execute($this->winCompat('git show-ref --head -d'), Argument::cetera())->willReturn(0); $this->winCompat('git show-ref --head -d'),
$process->execute($this->winCompat('git status --porcelain --untracked-files=no'), Argument::cetera())->willReturn(0); $this->winCompat('git status --porcelain --untracked-files=no'),
$process->execute($this->winCompat('git remote -v'), Argument::cetera())->willReturn(0); $this->winCompat('git remote -v'),
$process->execute($this->winCompat('git branch -r'), Argument::cetera())->willReturn(0); array(
$process->execute($expectedFirstGitUpdateCommand, Argument::cetera())->willReturn(1)->shouldBeCalled(); 'cmd' => $expectedFirstGitUpdateCommand,
$process->execute($expectedSecondGitUpdateCommand, Argument::cetera())->willReturn(0)->shouldBeCalled(); 'return' => 1,
$process->execute($this->winCompat("git checkout 'ref' -- && git reset --hard 'ref' --"), null, $this->winCompat($this->workingDir))->willReturn(0)->shouldBeCalled(); ),
$process->getErrorOutput()->willReturn(''); $this->winCompat('git --version'),
$this->winCompat('git remote -v'),
array(
'cmd' => $expectedSecondGitUpdateCommand,
'return' => 0,
),
$this->winCompat('git branch -r'),
$this->winCompat("git checkout 'ref' -- && git reset --hard 'ref' --"),
$this->winCompat('git remote -v'),
), true);
$this->fs->ensureDirectoryExists($this->workingDir.'/.git'); $this->fs->ensureDirectoryExists($this->workingDir.'/.git');
$downloader = $this->getDownloaderMock(null, new Config(), $process->reveal()); $downloader = $this->getDownloaderMock(null, new Config(), $process);
$downloader->download($packageMock, $this->workingDir, $packageMock); $downloader->download($packageMock, $this->workingDir, $packageMock);
$downloader->prepare('update', $packageMock, $this->workingDir, $packageMock); $downloader->prepare('update', $packageMock, $this->workingDir, $packageMock);
$downloader->update($packageMock, $packageMock, $this->workingDir); $downloader->update($packageMock, $packageMock, $this->workingDir);
$downloader->cleanup('update', $packageMock, $this->workingDir, $packageMock); $downloader->cleanup('update', $packageMock, $this->workingDir, $packageMock);
$process->assertComplete($this);
} }
public function testDowngradeShowsAppropriateMessage() public function testDowngradeShowsAppropriateMessage()
@ -585,14 +541,14 @@ composer https://github.com/old/url (push)
$newPackage->expects($this->any()) $newPackage->expects($this->any())
->method('getVersion') ->method('getVersion')
->will($this->returnValue('1.0.0.0')); ->will($this->returnValue('1.0.0.0'));
$newPackage->expects($this->any())
->method('getPrettyVersion')
->will($this->returnValue('1.0.0'));
$newPackage->expects($this->any()) $newPackage->expects($this->any())
->method('getFullPrettyVersion') ->method('getFullPrettyVersion')
->will($this->returnValue('1.0.0')); ->will($this->returnValue('1.0.0'));
$processExecutor = $this->getMockBuilder('Composer\Util\ProcessExecutor')->getMock(); $process = new ProcessExecutorMock;
$processExecutor->expects($this->any())
->method('execute')
->will($this->returnValue(0));
$ioMock = $this->getMockBuilder('Composer\IO\IOInterface')->getMock(); $ioMock = $this->getMockBuilder('Composer\IO\IOInterface')->getMock();
$ioMock->expects($this->at(0)) $ioMock->expects($this->at(0))
@ -600,7 +556,7 @@ composer https://github.com/old/url (push)
->with($this->stringContains('Downgrading')); ->with($this->stringContains('Downgrading'));
$this->fs->ensureDirectoryExists($this->workingDir.'/.git'); $this->fs->ensureDirectoryExists($this->workingDir.'/.git');
$downloader = $this->getDownloaderMock($ioMock, null, $processExecutor); $downloader = $this->getDownloaderMock($ioMock, null, $process);
$downloader->download($newPackage, $this->workingDir, $oldPackage); $downloader->download($newPackage, $this->workingDir, $oldPackage);
$downloader->prepare('update', $newPackage, $this->workingDir, $oldPackage); $downloader->prepare('update', $newPackage, $this->workingDir, $oldPackage);
$downloader->update($oldPackage, $newPackage, $this->workingDir); $downloader->update($oldPackage, $newPackage, $this->workingDir);
@ -630,11 +586,11 @@ composer https://github.com/old/url (push)
$newPackage->expects($this->any()) $newPackage->expects($this->any())
->method('getVersion') ->method('getVersion')
->will($this->returnValue('dev-ref2')); ->will($this->returnValue('dev-ref2'));
$newPackage->expects($this->any())
->method('getPrettyVersion')
->will($this->returnValue('dev-ref2'));
$processExecutor = $this->getMockBuilder('Composer\Util\ProcessExecutor')->getMock(); $process = new ProcessExecutorMock;
$processExecutor->expects($this->any())
->method('execute')
->will($this->returnValue(0));
$ioMock = $this->getMockBuilder('Composer\IO\IOInterface')->getMock(); $ioMock = $this->getMockBuilder('Composer\IO\IOInterface')->getMock();
$ioMock->expects($this->at(0)) $ioMock->expects($this->at(0))
@ -642,7 +598,7 @@ composer https://github.com/old/url (push)
->with($this->stringContains('Upgrading')); ->with($this->stringContains('Upgrading'));
$this->fs->ensureDirectoryExists($this->workingDir.'/.git'); $this->fs->ensureDirectoryExists($this->workingDir.'/.git');
$downloader = $this->getDownloaderMock($ioMock, null, $processExecutor); $downloader = $this->getDownloaderMock($ioMock, null, $process);
$downloader->download($newPackage, $this->workingDir, $oldPackage); $downloader->download($newPackage, $this->workingDir, $oldPackage);
$downloader->prepare('update', $newPackage, $this->workingDir, $oldPackage); $downloader->prepare('update', $newPackage, $this->workingDir, $oldPackage);
$downloader->update($oldPackage, $newPackage, $this->workingDir); $downloader->update($oldPackage, $newPackage, $this->workingDir);
@ -651,24 +607,29 @@ composer https://github.com/old/url (push)
public function testRemove() public function testRemove()
{ {
$expectedGitResetCommand = $this->winCompat("cd 'composerPath' && git status --porcelain --untracked-files=no"); $expectedGitResetCommand = $this->winCompat("git status --porcelain --untracked-files=no");
$packageMock = $this->getMockBuilder('Composer\Package\PackageInterface')->getMock(); $packageMock = $this->getMockBuilder('Composer\Package\PackageInterface')->getMock();
$processExecutor = $this->getMockBuilder('Composer\Util\ProcessExecutor')->getMock(); $process = new ProcessExecutorMock;
$processExecutor->expects($this->any()) $process->expects(array(
->method('execute') 'git show-ref --head -d',
->with($this->equalTo($expectedGitResetCommand)) $expectedGitResetCommand,
->will($this->returnValue(0)); ), true);
$this->fs->ensureDirectoryExists($this->workingDir.'/.git');
$filesystem = $this->getMockBuilder('Composer\Util\Filesystem')->getMock(); $filesystem = $this->getMockBuilder('Composer\Util\Filesystem')->getMock();
$filesystem->expects($this->once()) $filesystem->expects($this->once())
->method('removeDirectoryAsync') ->method('removeDirectoryAsync')
->with($this->equalTo('composerPath')) ->with($this->equalTo($this->workingDir))
->will($this->returnValue(\React\Promise\resolve(true))); ->will($this->returnValue(\React\Promise\resolve(true)));
$downloader = $this->getDownloaderMock(null, null, $processExecutor, $filesystem); $downloader = $this->getDownloaderMock(null, null, $process, $filesystem);
$downloader->prepare('uninstall', $packageMock, 'composerPath'); $downloader->prepare('uninstall', $packageMock, $this->workingDir);
$downloader->remove($packageMock, 'composerPath'); $downloader->remove($packageMock, $this->workingDir);
$downloader->cleanup('uninstall', $packageMock, 'composerPath'); $downloader->cleanup('uninstall', $packageMock, $this->workingDir);
$process->assertComplete($this);
} }
public function testGetInstallationSource() public function testGetInstallationSource()

View File

@ -16,6 +16,7 @@ use Composer\Downloader\HgDownloader;
use Composer\Test\TestCase; use Composer\Test\TestCase;
use Composer\Util\Filesystem; use Composer\Util\Filesystem;
use Composer\Util\Platform; use Composer\Util\Platform;
use Composer\Test\Mock\ProcessExecutorMock;
class HgDownloaderTest extends TestCase class HgDownloaderTest extends TestCase
{ {
@ -39,7 +40,7 @@ class HgDownloaderTest extends TestCase
{ {
$io = $io ?: $this->getMockBuilder('Composer\IO\IOInterface')->getMock(); $io = $io ?: $this->getMockBuilder('Composer\IO\IOInterface')->getMock();
$config = $config ?: $this->getMockBuilder('Composer\Config')->getMock(); $config = $config ?: $this->getMockBuilder('Composer\Config')->getMock();
$executor = $executor ?: $this->getMockBuilder('Composer\Util\ProcessExecutor')->getMock(); $executor = $executor ?: new ProcessExecutorMock;
$filesystem = $filesystem ?: $this->getMockBuilder('Composer\Util\Filesystem')->getMock(); $filesystem = $filesystem ?: $this->getMockBuilder('Composer\Util\Filesystem')->getMock();
return new HgDownloader($io, $config, $executor, $filesystem); return new HgDownloader($io, $config, $executor, $filesystem);
@ -67,22 +68,17 @@ class HgDownloaderTest extends TestCase
$packageMock->expects($this->once()) $packageMock->expects($this->once())
->method('getSourceUrls') ->method('getSourceUrls')
->will($this->returnValue(array('https://mercurial.dev/l3l0/composer'))); ->will($this->returnValue(array('https://mercurial.dev/l3l0/composer')));
$processExecutor = $this->getMockBuilder('Composer\Util\ProcessExecutor')->getMock();
$expectedGitCommand = $this->getCmd('hg clone -- \'https://mercurial.dev/l3l0/composer\' \'composerPath\''); $process = new ProcessExecutorMock;
$processExecutor->expects($this->at(0)) $process->expects(array(
->method('execute') $this->getCmd('hg clone -- \'https://mercurial.dev/l3l0/composer\' \'composerPath\''),
->with($this->equalTo($expectedGitCommand)) $this->getCmd('hg up -- \'ref\''),
->will($this->returnValue(0)); ), true);
$expectedGitCommand = $this->getCmd('hg up -- \'ref\''); $downloader = $this->getDownloaderMock(null, null, $process);
$processExecutor->expects($this->at(1))
->method('execute')
->with($this->equalTo($expectedGitCommand))
->will($this->returnValue(0));
$downloader = $this->getDownloaderMock(null, null, $processExecutor);
$downloader->install($packageMock, 'composerPath'); $downloader->install($packageMock, 'composerPath');
$process->assertComplete($this);
} }
public function testUpdateforPackageWithoutSourceReference() public function testUpdateforPackageWithoutSourceReference()
@ -115,44 +111,44 @@ class HgDownloaderTest extends TestCase
$packageMock->expects($this->any()) $packageMock->expects($this->any())
->method('getSourceUrls') ->method('getSourceUrls')
->will($this->returnValue(array('https://github.com/l3l0/composer'))); ->will($this->returnValue(array('https://github.com/l3l0/composer')));
$processExecutor = $this->getMockBuilder('Composer\Util\ProcessExecutor')->getMock();
$expectedHgCommand = $this->getCmd("hg st"); $process = new ProcessExecutorMock;
$processExecutor->expects($this->at(0)) $process->expects(array(
->method('execute') $this->getCmd('hg st'),
->with($this->equalTo($expectedHgCommand)) $this->getCmd("hg pull -- 'https://github.com/l3l0/composer' && hg up -- 'ref'"),
->will($this->returnValue(0)); ), true);
$expectedHgCommand = $this->getCmd("hg pull -- 'https://github.com/l3l0/composer' && hg up -- 'ref'");
$processExecutor->expects($this->at(1))
->method('execute')
->with($this->equalTo($expectedHgCommand))
->will($this->returnValue(0));
$downloader = $this->getDownloaderMock(null, null, $processExecutor); $downloader = $this->getDownloaderMock(null, null, $process);
$downloader->prepare('update', $packageMock, $this->workingDir, $packageMock); $downloader->prepare('update', $packageMock, $this->workingDir, $packageMock);
$downloader->update($packageMock, $packageMock, $this->workingDir); $downloader->update($packageMock, $packageMock, $this->workingDir);
$downloader->cleanup('update', $packageMock, $this->workingDir, $packageMock); $downloader->cleanup('update', $packageMock, $this->workingDir, $packageMock);
$process->assertComplete($this);
} }
public function testRemove() public function testRemove()
{ {
$expectedResetCommand = $this->getCmd('cd \'composerPath\' && hg st'); $fs = new Filesystem;
$fs->ensureDirectoryExists($this->workingDir.'/.hg');
$packageMock = $this->getMockBuilder('Composer\Package\PackageInterface')->getMock(); $packageMock = $this->getMockBuilder('Composer\Package\PackageInterface')->getMock();
$processExecutor = $this->getMockBuilder('Composer\Util\ProcessExecutor')->getMock();
$processExecutor->expects($this->any()) $process = new ProcessExecutorMock;
->method('execute') $process->expects(array(
->with($this->equalTo($expectedResetCommand)); $this->getCmd('hg st'),
), true);
$filesystem = $this->getMockBuilder('Composer\Util\Filesystem')->getMock(); $filesystem = $this->getMockBuilder('Composer\Util\Filesystem')->getMock();
$filesystem->expects($this->once()) $filesystem->expects($this->once())
->method('removeDirectoryAsync') ->method('removeDirectoryAsync')
->with($this->equalTo('composerPath')) ->with($this->equalTo($this->workingDir))
->will($this->returnValue(\React\Promise\resolve(true))); ->will($this->returnValue(\React\Promise\resolve(true)));
$downloader = $this->getDownloaderMock(null, null, $processExecutor, $filesystem); $downloader = $this->getDownloaderMock(null, null, $process, $filesystem);
$downloader->prepare('uninstall', $packageMock, 'composerPath'); $downloader->prepare('uninstall', $packageMock, $this->workingDir);
$downloader->remove($packageMock, 'composerPath'); $downloader->remove($packageMock, $this->workingDir);
$downloader->cleanup('uninstall', $packageMock, 'composerPath'); $downloader->cleanup('uninstall', $packageMock, $this->workingDir);
$process->assertComplete($this);
} }
public function testGetInstallationSource() public function testGetInstallationSource()

View File

@ -19,6 +19,7 @@ use Composer\IO\IOInterface;
use Composer\Test\TestCase; use Composer\Test\TestCase;
use Composer\Factory; use Composer\Factory;
use Composer\Util\Filesystem; use Composer\Util\Filesystem;
use Composer\Test\Mock\ProcessExecutorMock;
/** /**
* @author Matt Whittom <Matt.Whittom@veteransunited.com> * @author Matt Whittom <Matt.Whittom@veteransunited.com>
@ -41,7 +42,7 @@ class PerforceDownloaderTest extends TestCase
$this->repoConfig = $this->getRepoConfig(); $this->repoConfig = $this->getRepoConfig();
$this->config = $this->getConfig(); $this->config = $this->getConfig();
$this->io = $this->getMockIoInterface(); $this->io = $this->getMockIoInterface();
$this->processExecutor = $this->getMockProcessExecutor(); $this->processExecutor = new ProcessExecutorMock;
$this->repository = $this->getMockRepository($this->repoConfig, $this->io, $this->config); $this->repository = $this->getMockRepository($this->repoConfig, $this->io, $this->config);
$this->package = $this->getMockPackageInterface($this->repository); $this->package = $this->getMockPackageInterface($this->repository);
$this->downloader = new PerforceDownloader($this->io, $this->config, $this->processExecutor); $this->downloader = new PerforceDownloader($this->io, $this->config, $this->processExecutor);
@ -61,11 +62,6 @@ class PerforceDownloaderTest extends TestCase
} }
} }
protected function getMockProcessExecutor()
{
return $this->getMockBuilder('Composer\Util\ProcessExecutor')->getMock();
}
protected function getConfig() protected function getConfig()
{ {
$config = new Config(); $config = new Config();

View File

@ -22,6 +22,7 @@ use Composer\IO\BufferIO;
use Composer\Script\ScriptEvents; use Composer\Script\ScriptEvents;
use Composer\Script\Event as ScriptEvent; use Composer\Script\Event as ScriptEvent;
use Composer\Util\ProcessExecutor; use Composer\Util\ProcessExecutor;
use Composer\Test\Mock\ProcessExecutorMock;
use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Output\OutputInterface;
class EventDispatcherTest extends TestCase class EventDispatcherTest extends TestCase
@ -56,7 +57,11 @@ class EventDispatcherTest extends TestCase
*/ */
public function testDispatcherCanExecuteSingleCommandLineScript($command) public function testDispatcherCanExecuteSingleCommandLineScript($command)
{ {
$process = $this->getMockBuilder('Composer\Util\ProcessExecutor')->getMock(); $process = new ProcessExecutorMock;
$process->expects(array(
$command,
), true);
$dispatcher = $this->getMockBuilder('Composer\EventDispatcher\EventDispatcher') $dispatcher = $this->getMockBuilder('Composer\EventDispatcher\EventDispatcher')
->setConstructorArgs(array( ->setConstructorArgs(array(
$this->createComposerInstance(), $this->createComposerInstance(),
@ -71,12 +76,9 @@ class EventDispatcherTest extends TestCase
->method('getListeners') ->method('getListeners')
->will($this->returnValue($listener)); ->will($this->returnValue($listener));
$process->expects($this->once())
->method('execute')
->with($command)
->will($this->returnValue(0));
$dispatcher->dispatchScript(ScriptEvents::POST_INSTALL_CMD, false); $dispatcher->dispatchScript(ScriptEvents::POST_INSTALL_CMD, false);
$process->assertComplete($this);
} }
/** /**
@ -104,7 +106,7 @@ class EventDispatcherTest extends TestCase
$dispatcher = new EventDispatcher( $dispatcher = new EventDispatcher(
$composer, $composer,
$this->getMockBuilder('Composer\IO\IOInterface')->getMock(), $this->getMockBuilder('Composer\IO\IOInterface')->getMock(),
$this->getMockBuilder('Composer\Util\ProcessExecutor')->getMock() new ProcessExecutorMock
); );
$event = $this->getMockBuilder('Composer\Script\Event') $event = $this->getMockBuilder('Composer\Script\Event')
@ -179,7 +181,7 @@ class EventDispatcherTest extends TestCase
$dispatcher = new EventDispatcher( $dispatcher = new EventDispatcher(
$composer, $composer,
$io = new BufferIO('', OutputInterface::VERBOSITY_VERBOSE), $io = new BufferIO('', OutputInterface::VERBOSITY_VERBOSE),
$this->getMockBuilder('Composer\Util\ProcessExecutor')->getMock() new ProcessExecutorMock
); );
$listener = array($this, 'someMethod'); $listener = array($this, 'someMethod');
@ -214,7 +216,12 @@ class EventDispatcherTest extends TestCase
public function testDispatcherCanExecuteCliAndPhpInSameEventScriptStack() public function testDispatcherCanExecuteCliAndPhpInSameEventScriptStack()
{ {
$process = $this->getMockBuilder('Composer\Util\ProcessExecutor')->getMock(); $process = new ProcessExecutorMock;
$process->expects(array(
'echo -n foo',
'echo -n bar',
), true);
$dispatcher = $this->getMockBuilder('Composer\EventDispatcher\EventDispatcher') $dispatcher = $this->getMockBuilder('Composer\EventDispatcher\EventDispatcher')
->setConstructorArgs(array( ->setConstructorArgs(array(
$this->createComposerInstance(), $this->createComposerInstance(),
@ -226,10 +233,6 @@ class EventDispatcherTest extends TestCase
)) ))
->getMock(); ->getMock();
$process->expects($this->exactly(2))
->method('execute')
->will($this->returnValue(0));
$listeners = array( $listeners = array(
'echo -n foo', 'echo -n foo',
'Composer\\Test\\EventDispatcher\\EventDispatcherTest::someMethod', 'Composer\\Test\\EventDispatcher\\EventDispatcherTest::someMethod',
@ -246,16 +249,17 @@ class EventDispatcherTest extends TestCase
'> post-install-cmd: Composer\Test\EventDispatcher\EventDispatcherTest::someMethod'.PHP_EOL. '> post-install-cmd: Composer\Test\EventDispatcher\EventDispatcherTest::someMethod'.PHP_EOL.
'> post-install-cmd: echo -n bar'.PHP_EOL; '> post-install-cmd: echo -n bar'.PHP_EOL;
$this->assertEquals($expected, $io->getOutput()); $this->assertEquals($expected, $io->getOutput());
$process->assertComplete($this);
} }
public function testDispatcherCanPutEnv() public function testDispatcherCanPutEnv()
{ {
$process = $this->getMockBuilder('Composer\Util\ProcessExecutor')->getMock();
$dispatcher = $this->getMockBuilder('Composer\EventDispatcher\EventDispatcher') $dispatcher = $this->getMockBuilder('Composer\EventDispatcher\EventDispatcher')
->setConstructorArgs(array( ->setConstructorArgs(array(
$this->createComposerInstance(), $this->createComposerInstance(),
$io = new BufferIO('', OutputInterface::VERBOSITY_VERBOSE), $io = new BufferIO('', OutputInterface::VERBOSITY_VERBOSE),
$process, new ProcessExecutorMock,
)) ))
->setMethods(array( ->setMethods(array(
'getListeners', 'getListeners',
@ -285,11 +289,10 @@ class EventDispatcherTest extends TestCase
chdir(__DIR__); chdir(__DIR__);
putenv('COMPOSER_BIN_DIR=' . __DIR__ . sprintf('%svendor%sbin', DIRECTORY_SEPARATOR, DIRECTORY_SEPARATOR)); putenv('COMPOSER_BIN_DIR=' . __DIR__ . sprintf('%svendor%sbin', DIRECTORY_SEPARATOR, DIRECTORY_SEPARATOR));
$process = $this->getMockBuilder('Composer\Util\ProcessExecutor')->getMock();
$dispatcher = $this->getMockBuilder('Composer\EventDispatcher\EventDispatcher')->setConstructorArgs(array( $dispatcher = $this->getMockBuilder('Composer\EventDispatcher\EventDispatcher')->setConstructorArgs(array(
$this->createComposerInstance(), $this->createComposerInstance(),
$io = new BufferIO('', OutputInterface::VERBOSITY_VERBOSE), $io = new BufferIO('', OutputInterface::VERBOSITY_VERBOSE),
$process, new ProcessExecutorMock,
))->setMethods(array( ))->setMethods(array(
'getListeners', 'getListeners',
))->getMock(); ))->getMock();
@ -342,7 +345,13 @@ class EventDispatcherTest extends TestCase
public function testDispatcherCanExecuteComposerScriptGroups() public function testDispatcherCanExecuteComposerScriptGroups()
{ {
$process = $this->getMockBuilder('Composer\Util\ProcessExecutor')->getMock(); $process = new ProcessExecutorMock;
$process->expects(array(
'echo -n foo',
'echo -n baz',
'echo -n bar',
), true);
$dispatcher = $this->getMockBuilder('Composer\EventDispatcher\EventDispatcher') $dispatcher = $this->getMockBuilder('Composer\EventDispatcher\EventDispatcher')
->setConstructorArgs(array( ->setConstructorArgs(array(
$composer = $this->createComposerInstance(), $composer = $this->createComposerInstance(),
@ -354,10 +363,6 @@ class EventDispatcherTest extends TestCase
)) ))
->getMock(); ->getMock();
$process->expects($this->exactly(3))
->method('execute')
->will($this->returnValue(0));
$dispatcher->expects($this->atLeastOnce()) $dispatcher->expects($this->atLeastOnce())
->method('getListeners') ->method('getListeners')
->will($this->returnCallback(function (Event $event) { ->will($this->returnCallback(function (Event $event) {
@ -383,11 +388,17 @@ class EventDispatcherTest extends TestCase
'> subgroup: echo -n baz'.PHP_EOL. '> subgroup: echo -n baz'.PHP_EOL.
'> group: echo -n bar'.PHP_EOL; '> group: echo -n bar'.PHP_EOL;
$this->assertEquals($expected, $io->getOutput()); $this->assertEquals($expected, $io->getOutput());
$process->assertComplete($this);
} }
public function testRecursionInScriptsNames() public function testRecursionInScriptsNames()
{ {
$process = $this->getMockBuilder('Composer\Util\ProcessExecutor')->getMock(); $process = new ProcessExecutorMock;
$process->expects(array(
'echo Hello '.ProcessExecutor::escape('World'),
), true);
$dispatcher = $this->getMockBuilder('Composer\EventDispatcher\EventDispatcher') $dispatcher = $this->getMockBuilder('Composer\EventDispatcher\EventDispatcher')
->setConstructorArgs(array( ->setConstructorArgs(array(
$composer = $this->createComposerInstance(), $composer = $this->createComposerInstance(),
@ -399,10 +410,6 @@ class EventDispatcherTest extends TestCase
)) ))
->getMock(); ->getMock();
$process->expects($this->exactly(1))
->method('execute')
->will($this->returnValue(0));
$dispatcher->expects($this->atLeastOnce()) $dispatcher->expects($this->atLeastOnce())
->method('getListeners') ->method('getListeners')
->will($this->returnCallback(function (Event $event) { ->will($this->returnCallback(function (Event $event) {
@ -422,18 +429,19 @@ class EventDispatcherTest extends TestCase
"> hello: echo Hello " .escapeshellarg('World').PHP_EOL; "> hello: echo Hello " .escapeshellarg('World').PHP_EOL;
$this->assertEquals($expected, $io->getOutput()); $this->assertEquals($expected, $io->getOutput());
$process->assertComplete($this);
} }
public function testDispatcherDetectInfiniteRecursion() public function testDispatcherDetectInfiniteRecursion()
{ {
$this->setExpectedException('RuntimeException'); $this->setExpectedException('RuntimeException');
$process = $this->getMockBuilder('Composer\Util\ProcessExecutor')->getMock();
$dispatcher = $this->getMockBuilder('Composer\EventDispatcher\EventDispatcher') $dispatcher = $this->getMockBuilder('Composer\EventDispatcher\EventDispatcher')
->setConstructorArgs(array( ->setConstructorArgs(array(
$composer = $this->createComposerInstance(), $composer = $this->createComposerInstance(),
$io = $this->getMockBuilder('Composer\IO\IOInterface')->getMock(), $io = $this->getMockBuilder('Composer\IO\IOInterface')->getMock(),
$process, new ProcessExecutorMock,
)) ))
->setMethods(array( ->setMethods(array(
'getListeners', 'getListeners',
@ -549,12 +557,11 @@ class EventDispatcherTest extends TestCase
public function testDispatcherInstallerEvents() public function testDispatcherInstallerEvents()
{ {
$process = $this->getMockBuilder('Composer\Util\ProcessExecutor')->getMock();
$dispatcher = $this->getMockBuilder('Composer\EventDispatcher\EventDispatcher') $dispatcher = $this->getMockBuilder('Composer\EventDispatcher\EventDispatcher')
->setConstructorArgs(array( ->setConstructorArgs(array(
$this->createComposerInstance(), $this->createComposerInstance(),
$this->getMockBuilder('Composer\IO\IOInterface')->getMock(), $this->getMockBuilder('Composer\IO\IOInterface')->getMock(),
$process, new ProcessExecutorMock,
)) ))
->setMethods(array('getListeners')) ->setMethods(array('getListeners'))
->getMock(); ->getMock();

View File

@ -205,6 +205,9 @@ class InstalledVersionsTest extends TestCase
), InstalledVersions::getRootPackage()); ), InstalledVersions::getRootPackage());
} }
/**
* @group legacy
*/
public function testGetRawData() public function testGetRawData()
{ {
$dir = $this->root; $dir = $this->root;

View File

@ -67,6 +67,21 @@ class InstallerTest extends TestCase
->setConstructorArgs(array($io)) ->setConstructorArgs(array($io))
->getMock(); ->getMock();
$config = $this->getMockBuilder('Composer\Config')->getMock(); $config = $this->getMockBuilder('Composer\Config')->getMock();
$config->expects($this->any())
->method('get')
->will($this->returnCallback(function ($key) {
switch ($key) {
case 'vendor-dir':
return 'foo';
case 'lock';
case 'notify-on-install';
return true;
case 'platform';
return array();
}
throw new \UnexpectedValueException('Unknown key '.$key);
}));
$eventDispatcher = $this->getMockBuilder('Composer\EventDispatcher\EventDispatcher')->disableOriginalConstructor()->getMock(); $eventDispatcher = $this->getMockBuilder('Composer\EventDispatcher\EventDispatcher')->disableOriginalConstructor()->getMock();
$httpDownloader = $this->getMockBuilder('Composer\Util\HttpDownloader')->disableOriginalConstructor()->getMock(); $httpDownloader = $this->getMockBuilder('Composer\Util\HttpDownloader')->disableOriginalConstructor()->getMock();

View File

@ -0,0 +1,150 @@
<?php
/*
* 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\Mock;
use Composer\Util\ProcessExecutor;
use Composer\Util\Platform;
use PHPUnit\Framework\TestCase;
use PHPUnit\Framework\AssertionFailedError;
use Symfony\Component\Process\Process;
use React\Promise\Promise;
/**
* @author Jordi Boggiano <j.boggiano@seld.be>
*/
class ProcessExecutorMock extends ProcessExecutor
{
private $expectations = array();
private $strict = false;
private $defaultHandler = array('return' => 0, 'stdout' => '', 'stderr' => '');
private $log = array();
/**
* @param array<string|array{cmd: string, return: int, stdout?: string, stderr?: string, callback?: callable}> $expectations
* @param bool $strict set to true if you want to provide *all* expected commands, and not just a subset you are interested in testing
* @param array{return: int, stdout?: string, stderr?: string} $defaultHandler default command handler for undefined commands if not in strict mode
*/
public function expects(array $expectations, $strict = false, array $defaultHandler = array('return' => 0, 'stdout' => '', 'stderr' => ''))
{
$default = array('cmd' => '', 'return' => 0, 'stdout' => '', 'stderr' => '', 'callback' => null);
$this->expectations = array_map(function ($expect) use ($default) {
if (is_string($expect)) {
$expect = array('cmd' => $expect);
} elseif ($diff = array_diff_key(array_merge($default, $expect), $default)) {
throw new \UnexpectedValueException('Unexpected keys in process execution step: '.implode(', ', array_keys($diff)));
}
return array_merge($default, $expect);
}, $expectations);
$this->strict = $strict;
$this->defaultHandler = array_merge($default, $defaultHandler);
}
public function assertComplete(TestCase $testCase)
{
if ($this->expectations) {
$expectations = array_map(function ($expect) {
return $expect['cmd'];
}, $this->expectations);
throw new AssertionFailedError(
'There are still '.count($this->expectations).' expected process calls which have not been consumed:'.PHP_EOL.
implode(PHP_EOL, $expectations).PHP_EOL.PHP_EOL.
'Received calls:'.PHP_EOL.implode(PHP_EOL, $this->log)
);
}
$testCase->assertTrue(true);
}
public function execute($command, &$output = null, $cwd = null)
{
if (func_num_args() > 1) {
return $this->doExecute($command, $cwd, false, $output);
}
return $this->doExecute($command, $cwd, false);
}
public function executeTty($command, $cwd = null)
{
if (Platform::isTty()) {
return $this->doExecute($command, $cwd, true);
}
return $this->doExecute($command, $cwd, false);
}
private function doExecute($command, $cwd, $tty, &$output = null)
{
$this->captureOutput = func_num_args() > 3;
$this->errorOutput = '';
$callback = is_callable($output) ? $output : array($this, 'outputHandler');
$this->log[] = $command;
if ($this->expectations && $command === $this->expectations[0]['cmd']) {
$expect = array_shift($this->expectations);
$stdout = $expect['stdout'];
$stderr = $expect['stderr'];
$return = $expect['return'];
if (isset($expect['callback'])) {
call_user_func($expect['callback']);
}
} elseif (!$this->strict) {
$stdout = $this->defaultHandler['stdout'];
$stderr = $this->defaultHandler['stderr'];
$return = $this->defaultHandler['return'];
} else {
throw new AssertionFailedError(
'Received unexpected command "'.$command.'" in "'.$cwd.'"'.PHP_EOL.
($this->expectations ? 'Expected "'.$this->expectations[0]['cmd'].'" at this point.' : 'Expected no more calls at this point.').PHP_EOL.
'Received calls:'.PHP_EOL.implode(PHP_EOL, array_slice($this->log, 0, -1))
);
}
if ($stdout) {
call_user_func($callback, Process::STDOUT, $stdout);
}
if ($stderr) {
call_user_func($callback, Process::ERR, $stderr);
}
if ($this->captureOutput && !is_callable($output)) {
$output = $stdout;
}
$this->errorOutput = $stderr;
return $return;
}
public function executeAsync($command, $cwd = null)
{
$resolver = function ($resolve, $reject) {
// TODO strictly speaking this should resolve with a mock Process instance here
$resolve();
};
$canceler = function () {
throw new \RuntimeException('Aborted process');
};
return new Promise($resolver, $canceler);
}
public function getErrorOutput()
{
return $this->errorOutput;
}
}

View File

@ -19,13 +19,14 @@ use Composer\Package\RootPackage;
use Composer\Package\Version\VersionGuesser; use Composer\Package\Version\VersionGuesser;
use Composer\Semver\VersionParser; use Composer\Semver\VersionParser;
use Composer\Test\TestCase; use Composer\Test\TestCase;
use Composer\Test\Mock\ProcessExecutorMock;
use Prophecy\Argument; use Prophecy\Argument;
class RootPackageLoaderTest extends TestCase class RootPackageLoaderTest extends TestCase
{ {
protected function loadPackage($data) protected function loadPackage($data)
{ {
$manager = $this->getMockBuilder('\\Composer\\Repository\\RepositoryManager') $manager = $this->getMockBuilder('Composer\\Repository\\RepositoryManager')
->disableOriginalConstructor() ->disableOriginalConstructor()
->getMock(); ->getMock();
@ -67,38 +68,28 @@ class RootPackageLoaderTest extends TestCase
public function testNoVersionIsVisibleInPrettyVersion() public function testNoVersionIsVisibleInPrettyVersion()
{ {
$manager = $this->getMockBuilder('\\Composer\\Repository\\RepositoryManager') $manager = $this->getMockBuilder('Composer\\Repository\\RepositoryManager')
->disableOriginalConstructor() ->disableOriginalConstructor()
->getMock() ->getMock()
; ;
$executor = $this->getMockBuilder('\\Composer\\Util\\ProcessExecutor')
->setMethods(array('execute'))
->disableArgumentCloning()
->disableOriginalConstructor()
->getMock()
;
$executor
->expects($this->any())
->method('execute')
->willReturn(null)
;
$config = new Config; $config = new Config;
$config->merge(array('repositories' => array('packagist' => false))); $config->merge(array('repositories' => array('packagist' => false)));
$loader = new RootPackageLoader($manager, $config, null, new VersionGuesser($config, $executor, new VersionParser())); $loader = new RootPackageLoader($manager, $config, null, new VersionGuesser($config, $process = new ProcessExecutorMock, new VersionParser()));
$process->expects(array(), false, array('return' => 1));
$package = $loader->load(array()); $package = $loader->load(array());
$this->assertEquals("1.0.0.0", $package->getVersion()); $this->assertEquals("1.0.0.0", $package->getVersion());
$this->assertEquals(RootPackage::DEFAULT_PRETTY_VERSION, $package->getPrettyVersion()); $this->assertEquals(RootPackage::DEFAULT_PRETTY_VERSION, $package->getPrettyVersion());
} }
public function testPrettyVersionForRootPackageInVersionBranch() public function testPrettyVersionForRootPackageInVersionBranch()
{ {
// see #6845 // see #6845
$manager = $this->prophesize('\\Composer\\Repository\\RepositoryManager'); $manager = $this->prophesize('Composer\\Repository\\RepositoryManager');
$versionGuesser = $this->prophesize('\\Composer\\Package\\Version\\VersionGuesser'); $versionGuesser = $this->prophesize('Composer\\Package\\Version\\VersionGuesser');
$versionGuesser->guessVersion(Argument::cetera()) $versionGuesser->guessVersion(Argument::cetera())
->willReturn(array( ->willReturn(array(
'name' => 'A', 'name' => 'A',
@ -120,48 +111,28 @@ class RootPackageLoaderTest extends TestCase
$this->markTestSkipped('proc_open() is not available'); $this->markTestSkipped('proc_open() is not available');
} }
$manager = $this->getMockBuilder('\\Composer\\Repository\\RepositoryManager') $manager = $this->getMockBuilder('Composer\\Repository\\RepositoryManager')
->disableOriginalConstructor() ->disableOriginalConstructor()
->getMock() ->getMock()
; ;
$executor = $this->getMockBuilder('\\Composer\\Util\\ProcessExecutor') $process = new ProcessExecutorMock;
->setMethods(array('execute')) $process->expects(array(
->disableArgumentCloning() array(
->disableOriginalConstructor() 'cmd' => 'git branch -a --no-color --no-abbrev -v',
->getMock() 'stdout' => "* latest-production 38137d2f6c70e775e137b2d8a7a7d3eaebf7c7e5 Commit message\n master 4f6ed96b0bc363d2aa4404c3412de1c011f67c66 Commit message\n",
; ),
'git rev-list master..latest-production',
$self = $this; ), true);
$executor
->expects($this->at(0))
->method('execute')
->willReturnCallback(function ($command, &$output) use ($self) {
$self->assertEquals('git branch -a --no-color --no-abbrev -v', $command);
$output = "* latest-production 38137d2f6c70e775e137b2d8a7a7d3eaebf7c7e5 Commit message\n master 4f6ed96b0bc363d2aa4404c3412de1c011f67c66 Commit message\n";
return 0;
})
;
$executor
->expects($this->at(1))
->method('execute')
->willReturnCallback(function ($command, &$output) use ($self) {
$self->assertEquals('git rev-list master..latest-production', $command);
$output = "";
return 0;
})
;
$config = new Config; $config = new Config;
$config->merge(array('repositories' => array('packagist' => false))); $config->merge(array('repositories' => array('packagist' => false)));
$loader = new RootPackageLoader($manager, $config, null, new VersionGuesser($config, $executor, new VersionParser())); $loader = new RootPackageLoader($manager, $config, null, new VersionGuesser($config, $process, new VersionParser()));
$package = $loader->load(array('require' => array('foo/bar' => 'self.version'))); $package = $loader->load(array('require' => array('foo/bar' => 'self.version')));
$this->assertEquals("dev-master", $package->getPrettyVersion()); $this->assertEquals("dev-master", $package->getPrettyVersion());
$process->assertComplete($this);
} }
public function testNonFeatureBranchPrettyVersion() public function testNonFeatureBranchPrettyVersion()
@ -170,36 +141,26 @@ class RootPackageLoaderTest extends TestCase
$this->markTestSkipped('proc_open() is not available'); $this->markTestSkipped('proc_open() is not available');
} }
$manager = $this->getMockBuilder('\\Composer\\Repository\\RepositoryManager') $manager = $this->getMockBuilder('Composer\\Repository\\RepositoryManager')
->disableOriginalConstructor() ->disableOriginalConstructor()
->getMock() ->getMock()
; ;
$executor = $this->getMockBuilder('\\Composer\\Util\\ProcessExecutor') $process = new ProcessExecutorMock;
->setMethods(array('execute')) $process->expects(array(
->disableArgumentCloning() array(
->disableOriginalConstructor() 'cmd' => 'git branch -a --no-color --no-abbrev -v',
->getMock() 'stdout' => "* latest-production 38137d2f6c70e775e137b2d8a7a7d3eaebf7c7e5 Commit message\n master 4f6ed96b0bc363d2aa4404c3412de1c011f67c66 Commit message\n"
; ),
), true);
$self = $this;
$executor
->expects($this->at(0))
->method('execute')
->willReturnCallback(function ($command, &$output) use ($self) {
$self->assertEquals('git branch -a --no-color --no-abbrev -v', $command);
$output = "* latest-production 38137d2f6c70e775e137b2d8a7a7d3eaebf7c7e5 Commit message\n master 4f6ed96b0bc363d2aa4404c3412de1c011f67c66 Commit message\n";
return 0;
})
;
$config = new Config; $config = new Config;
$config->merge(array('repositories' => array('packagist' => false))); $config->merge(array('repositories' => array('packagist' => false)));
$loader = new RootPackageLoader($manager, $config, null, new VersionGuesser($config, $executor, new VersionParser())); $loader = new RootPackageLoader($manager, $config, null, new VersionGuesser($config, $process, new VersionParser()));
$package = $loader->load(array('require' => array('foo/bar' => 'self.version'), "non-feature-branches" => array("latest-.*"))); $package = $loader->load(array('require' => array('foo/bar' => 'self.version'), "non-feature-branches" => array("latest-.*")));
$this->assertEquals("dev-latest-production", $package->getPrettyVersion()); $this->assertEquals("dev-latest-production", $package->getPrettyVersion());
$process->assertComplete($this);
} }
} }

View File

@ -18,6 +18,7 @@ use Composer\Semver\VersionParser;
use Composer\Test\TestCase; use Composer\Test\TestCase;
use Composer\Util\Git as GitUtil; use Composer\Util\Git as GitUtil;
use Composer\Util\ProcessExecutor; use Composer\Util\ProcessExecutor;
use Composer\Test\Mock\ProcessExecutorMock;
class VersionGuesserTest extends TestCase class VersionGuesserTest extends TestCase
{ {
@ -32,70 +33,28 @@ class VersionGuesserTest extends TestCase
{ {
$branch = 'default'; $branch = 'default';
$executor = $this->getMockBuilder('Composer\\Util\\ProcessExecutor') $process = new ProcessExecutorMock;
->setMethods(array('execute')) $process->expects(array(
->disableArgumentCloning() array('cmd' => 'git branch -a --no-color --no-abbrev -v', 'return' => 128),
->disableOriginalConstructor() array('cmd' => 'git describe --exact-match --tags', 'return' => 128),
->getMock() array('cmd' => 'git log --pretty="%H" -n1 HEAD'.GitUtil::getNoShowSignatureFlag($process), 'return' => 128),
; array('cmd' => 'hg branch', 'return' => 0, 'stdout' => $branch),
array('cmd' => 'hg branches', 'return' => 0),
$self = $this; array('cmd' => 'hg bookmarks', 'return' => 0),
$step = 0; ), true);
GitUtil::getVersion(new ProcessExecutor); GitUtil::getVersion(new ProcessExecutor);
$executor
->expects($this->at($step))
->method('execute')
->willReturnCallback(function ($command, &$output) use ($self) {
$self->assertEquals('git branch -a --no-color --no-abbrev -v', $command);
return 128;
})
;
++$step;
$executor
->expects($this->at($step))
->method('execute')
->willReturnCallback(function ($command, &$output) use ($self) {
$self->assertEquals('git describe --exact-match --tags', $command);
return 128;
})
;
++$step;
$executor
->expects($this->at($step))
->method('execute')
->willReturnCallback(function ($command, &$output) use ($self, $executor) {
$self->assertEquals('git log --pretty="%H" -n1 HEAD'.GitUtil::getNoShowSignatureFlag($executor), $command);
return 128;
})
;
++$step;
$executor
->expects($this->at($step))
->method('execute')
->willReturnCallback(function ($command, &$output) use ($self, $branch) {
$self->assertEquals('hg branch', $command);
$output = $branch;
return 0;
})
;
$config = new Config; $config = new Config;
$config->merge(array('repositories' => array('packagist' => false))); $config->merge(array('repositories' => array('packagist' => false)));
$guesser = new VersionGuesser($config, $executor, new VersionParser()); $guesser = new VersionGuesser($config, $process, new VersionParser());
$versionArray = $guesser->guessVersion(array(), 'dummy/path'); $versionArray = $guesser->guessVersion(array(), 'dummy/path');
$this->assertEquals("dev-".$branch, $versionArray['version']); $this->assertEquals("dev-".$branch, $versionArray['version']);
$this->assertEquals("dev-".$branch, $versionArray['pretty_version']); $this->assertEquals("dev-".$branch, $versionArray['pretty_version']);
$this->assertEmpty($versionArray['commit']); $this->assertEmpty($versionArray['commit']);
$process->assertComplete($this);
} }
public function testGuessVersionReturnsData() public function testGuessVersionReturnsData()
@ -103,29 +62,17 @@ class VersionGuesserTest extends TestCase
$commitHash = '03a15d220da53c52eddd5f32ffca64a7b3801bea'; $commitHash = '03a15d220da53c52eddd5f32ffca64a7b3801bea';
$anotherCommitHash = '03a15d220da53c52eddd5f32ffca64a7b3801bea'; $anotherCommitHash = '03a15d220da53c52eddd5f32ffca64a7b3801bea';
$executor = $this->getMockBuilder('\\Composer\\Util\\ProcessExecutor') $process = new ProcessExecutorMock;
->setMethods(array('execute')) $process->expects(array(
->disableArgumentCloning() array(
->disableOriginalConstructor() 'cmd' => 'git branch -a --no-color --no-abbrev -v',
->getMock() 'stdout' => "* master $commitHash Commit message\n(no branch) $anotherCommitHash Commit message\n"
; ),
), true);
$self = $this;
$executor
->expects($this->at(0))
->method('execute')
->willReturnCallback(function ($command, &$output) use ($self, $commitHash, $anotherCommitHash) {
$self->assertEquals('git branch -a --no-color --no-abbrev -v', $command);
$output = "* master $commitHash Commit message\n(no branch) $anotherCommitHash Commit message\n";
return 0;
})
;
$config = new Config; $config = new Config;
$config->merge(array('repositories' => array('packagist' => false))); $config->merge(array('repositories' => array('packagist' => false)));
$guesser = new VersionGuesser($config, $executor, new VersionParser()); $guesser = new VersionGuesser($config, $process, new VersionParser());
$versionArray = $guesser->guessVersion(array(), 'dummy/path'); $versionArray = $guesser->guessVersion(array(), 'dummy/path');
$this->assertEquals("dev-master", $versionArray['version']); $this->assertEquals("dev-master", $versionArray['version']);
@ -133,6 +80,8 @@ class VersionGuesserTest extends TestCase
$this->assertArrayNotHasKey('feature_version', $versionArray); $this->assertArrayNotHasKey('feature_version', $versionArray);
$this->assertArrayNotHasKey('feature_pretty_version', $versionArray); $this->assertArrayNotHasKey('feature_pretty_version', $versionArray);
$this->assertEquals($commitHash, $versionArray['commit']); $this->assertEquals($commitHash, $versionArray['commit']);
$process->assertComplete($this);
} }
public function testGuessVersionDoesNotSeeCustomDefaultBranchAsNonFeatureBranch() public function testGuessVersionDoesNotSeeCustomDefaultBranchAsNonFeatureBranch()
@ -140,34 +89,24 @@ class VersionGuesserTest extends TestCase
$commitHash = '03a15d220da53c52eddd5f32ffca64a7b3801bea'; $commitHash = '03a15d220da53c52eddd5f32ffca64a7b3801bea';
$anotherCommitHash = '13a15d220da53c52eddd5f32ffca64a7b3801bea'; $anotherCommitHash = '13a15d220da53c52eddd5f32ffca64a7b3801bea';
$executor = $this->getMockBuilder('\\Composer\\Util\\ProcessExecutor') $process = new ProcessExecutorMock;
->setMethods(array('execute')) $process->expects(array(
->disableArgumentCloning() array(
->disableOriginalConstructor() 'cmd' => 'git branch -a --no-color --no-abbrev -v',
->getMock()
;
$self = $this;
// Assumption here is that arbitrary would be the default branch // Assumption here is that arbitrary would be the default branch
$executor 'stdout' => " arbitrary $commitHash Commit message\n* current $anotherCommitHash Another message\n"
->expects($this->at(0)) ),
->method('execute') ), true);
->willReturnCallback(function ($command, &$output) use ($self, $commitHash, $anotherCommitHash) {
$self->assertEquals('git branch -a --no-color --no-abbrev -v', $command);
$output = " arbitrary $commitHash Commit message\n* current $anotherCommitHash Another message\n";
return 0;
})
;
$config = new Config; $config = new Config;
$config->merge(array('repositories' => array('packagist' => false))); $config->merge(array('repositories' => array('packagist' => false)));
$guesser = new VersionGuesser($config, $executor, new VersionParser()); $guesser = new VersionGuesser($config, $process, new VersionParser());
$versionArray = $guesser->guessVersion(array('version' => 'self.version'), 'dummy/path'); $versionArray = $guesser->guessVersion(array('version' => 'self.version'), 'dummy/path');
$this->assertEquals("dev-current", $versionArray['version']); $this->assertEquals("dev-current", $versionArray['version']);
$this->assertEquals($anotherCommitHash, $versionArray['commit']); $this->assertEquals($anotherCommitHash, $versionArray['commit']);
$process->assertComplete($this);
} }
public function testGuessVersionReadsAndRespectsNonFeatureBranchesConfigurationForArbitraryNaming() public function testGuessVersionReadsAndRespectsNonFeatureBranchesConfigurationForArbitraryNaming()
@ -175,46 +114,29 @@ class VersionGuesserTest extends TestCase
$commitHash = '03a15d220da53c52eddd5f32ffca64a7b3801bea'; $commitHash = '03a15d220da53c52eddd5f32ffca64a7b3801bea';
$anotherCommitHash = '13a15d220da53c52eddd5f32ffca64a7b3801bea'; $anotherCommitHash = '13a15d220da53c52eddd5f32ffca64a7b3801bea';
$executor = $this->getMockBuilder('\\Composer\\Util\\ProcessExecutor') $process = new ProcessExecutorMock;
->setMethods(array('execute')) $process->expects(array(
->disableArgumentCloning() array(
->disableOriginalConstructor() 'cmd' => 'git branch -a --no-color --no-abbrev -v',
->getMock() 'stdout' => " arbitrary $commitHash Commit message\n* feature $anotherCommitHash Another message\n"
; ),
array(
$self = $this; 'cmd' => 'git rev-list arbitrary..feature',
'stdout' => "$anotherCommitHash\n"
$executor ),
->expects($this->at(0)) ), true);
->method('execute')
->willReturnCallback(function ($command, &$output) use ($self, $commitHash, $anotherCommitHash) {
$self->assertEquals('git branch -a --no-color --no-abbrev -v', $command);
$output = " arbitrary $commitHash Commit message\n* feature $anotherCommitHash Another message\n";
return 0;
})
;
$executor
->expects($this->at(1))
->method('execute')
->willReturnCallback(function ($command, &$output, $path) use ($self, $anotherCommitHash) {
$self->assertEquals('git rev-list arbitrary..feature', $command);
$output = "$anotherCommitHash\n";
return 0;
})
;
$config = new Config; $config = new Config;
$config->merge(array('repositories' => array('packagist' => false))); $config->merge(array('repositories' => array('packagist' => false)));
$guesser = new VersionGuesser($config, $executor, new VersionParser()); $guesser = new VersionGuesser($config, $process, new VersionParser());
$versionArray = $guesser->guessVersion(array('version' => 'self.version', 'non-feature-branches' => array('arbitrary')), 'dummy/path'); $versionArray = $guesser->guessVersion(array('version' => 'self.version', 'non-feature-branches' => array('arbitrary')), 'dummy/path');
$this->assertEquals("dev-arbitrary", $versionArray['version']); $this->assertEquals("dev-arbitrary", $versionArray['version']);
$this->assertEquals($anotherCommitHash, $versionArray['commit']); $this->assertEquals($anotherCommitHash, $versionArray['commit']);
$this->assertEquals("dev-feature", $versionArray['feature_version']); $this->assertEquals("dev-feature", $versionArray['feature_version']);
$this->assertEquals("dev-feature", $versionArray['feature_pretty_version']); $this->assertEquals("dev-feature", $versionArray['feature_pretty_version']);
$process->assertComplete($this);
} }
public function testGuessVersionReadsAndRespectsNonFeatureBranchesConfigurationForArbitraryNamingRegex() public function testGuessVersionReadsAndRespectsNonFeatureBranchesConfigurationForArbitraryNamingRegex()
@ -222,45 +144,29 @@ class VersionGuesserTest extends TestCase
$commitHash = '03a15d220da53c52eddd5f32ffca64a7b3801bea'; $commitHash = '03a15d220da53c52eddd5f32ffca64a7b3801bea';
$anotherCommitHash = '13a15d220da53c52eddd5f32ffca64a7b3801bea'; $anotherCommitHash = '13a15d220da53c52eddd5f32ffca64a7b3801bea';
$executor = $this->getMockBuilder('\\Composer\\Util\\ProcessExecutor') $process = new ProcessExecutorMock;
->setMethods(array('execute')) $process->expects(array(
->disableArgumentCloning() array(
->disableOriginalConstructor() 'cmd' => 'git branch -a --no-color --no-abbrev -v',
->getMock() 'stdout' => " latest-testing $commitHash Commit message\n* feature $anotherCommitHash Another message\n",
; ),
array(
$self = $this; 'cmd' => 'git rev-list latest-testing..feature',
'stdout' => "$anotherCommitHash\n",
$executor ),
->expects($this->at(0)) ), true);
->method('execute')
->willReturnCallback(function ($command, &$output) use ($self, $commitHash, $anotherCommitHash) {
$self->assertEquals('git branch -a --no-color --no-abbrev -v', $command);
$output = " latest-testing $commitHash Commit message\n* feature $anotherCommitHash Another message\n";
return 0;
})
;
$executor
->expects($this->at(1))
->method('execute')
->willReturnCallback(function ($command, &$output, $path) use ($self, $anotherCommitHash) {
$self->assertEquals('git rev-list latest-testing..feature', $command);
$output = "$anotherCommitHash\n";
return 0;
})
;
$config = new Config; $config = new Config;
$config->merge(array('repositories' => array('packagist' => false))); $config->merge(array('repositories' => array('packagist' => false)));
$guesser = new VersionGuesser($config, $executor, new VersionParser()); $guesser = new VersionGuesser($config, $process, new VersionParser());
$versionArray = $guesser->guessVersion(array('version' => 'self.version', 'non-feature-branches' => array('latest-.*')), 'dummy/path'); $versionArray = $guesser->guessVersion(array('version' => 'self.version', 'non-feature-branches' => array('latest-.*')), 'dummy/path');
$this->assertEquals("dev-latest-testing", $versionArray['version']); $this->assertEquals("dev-latest-testing", $versionArray['version']);
$this->assertEquals($anotherCommitHash, $versionArray['commit']); $this->assertEquals($anotherCommitHash, $versionArray['commit']);
$this->assertEquals("dev-feature", $versionArray['feature_version']); $this->assertEquals("dev-feature", $versionArray['feature_version']);
$this->assertEquals("dev-feature", $versionArray['feature_pretty_version']); $this->assertEquals("dev-feature", $versionArray['feature_pretty_version']);
$process->assertComplete($this);
} }
public function testGuessVersionReadsAndRespectsNonFeatureBranchesConfigurationForArbitraryNamingWhenOnNonFeatureBranch() public function testGuessVersionReadsAndRespectsNonFeatureBranchesConfigurationForArbitraryNamingWhenOnNonFeatureBranch()
@ -268,310 +174,209 @@ class VersionGuesserTest extends TestCase
$commitHash = '03a15d220da53c52eddd5f32ffca64a7b3801bea'; $commitHash = '03a15d220da53c52eddd5f32ffca64a7b3801bea';
$anotherCommitHash = '13a15d220da53c52eddd5f32ffca64a7b3801bea'; $anotherCommitHash = '13a15d220da53c52eddd5f32ffca64a7b3801bea';
$executor = $this->getMockBuilder('\\Composer\\Util\\ProcessExecutor') $process = new ProcessExecutorMock;
->setMethods(array('execute')) $process->expects(array(
->disableArgumentCloning() array(
->disableOriginalConstructor() 'cmd' => 'git branch -a --no-color --no-abbrev -v',
->getMock() 'stdout' => "* latest-testing $commitHash Commit message\n current $anotherCommitHash Another message\n master $anotherCommitHash Another message\n",
; ),
), true);
$self = $this;
$executor
->expects($this->at(0))
->method('execute')
->willReturnCallback(function ($command, &$output) use ($self, $commitHash, $anotherCommitHash) {
$self->assertEquals('git branch -a --no-color --no-abbrev -v', $command);
$output = "* latest-testing $commitHash Commit message\n current $anotherCommitHash Another message\n master $anotherCommitHash Another message\n";
return 0;
})
;
$config = new Config; $config = new Config;
$config->merge(array('repositories' => array('packagist' => false))); $config->merge(array('repositories' => array('packagist' => false)));
$guesser = new VersionGuesser($config, $executor, new VersionParser()); $guesser = new VersionGuesser($config, $process, new VersionParser());
$versionArray = $guesser->guessVersion(array('version' => 'self.version', 'non-feature-branches' => array('latest-.*')), 'dummy/path'); $versionArray = $guesser->guessVersion(array('version' => 'self.version', 'non-feature-branches' => array('latest-.*')), 'dummy/path');
$this->assertEquals("dev-latest-testing", $versionArray['version']); $this->assertEquals("dev-latest-testing", $versionArray['version']);
$this->assertEquals($commitHash, $versionArray['commit']); $this->assertEquals($commitHash, $versionArray['commit']);
$this->assertArrayNotHasKey('feature_version', $versionArray); $this->assertArrayNotHasKey('feature_version', $versionArray);
$this->assertArrayNotHasKey('feature_pretty_version', $versionArray); $this->assertArrayNotHasKey('feature_pretty_version', $versionArray);
$process->assertComplete($this);
} }
public function testDetachedHeadBecomesDevHash() public function testDetachedHeadBecomesDevHash()
{ {
$commitHash = '03a15d220da53c52eddd5f32ffca64a7b3801bea'; $commitHash = '03a15d220da53c52eddd5f32ffca64a7b3801bea';
$executor = $this->getMockBuilder('\\Composer\\Util\\ProcessExecutor') $process = new ProcessExecutorMock;
->setMethods(array('execute')) $process->expects(array(
->disableArgumentCloning() array(
->disableOriginalConstructor() 'cmd' => 'git branch -a --no-color --no-abbrev -v',
->getMock() 'stdout' => "* (no branch) $commitHash Commit message\n",
; ),
'git describe --exact-match --tags',
$self = $this; ), true);
$executor
->expects($this->at(0))
->method('execute')
->willReturnCallback(function ($command, &$output) use ($self, $commitHash) {
$self->assertEquals('git branch -a --no-color --no-abbrev -v', $command);
$output = "* (no branch) $commitHash Commit message\n";
return 0;
})
;
$config = new Config; $config = new Config;
$config->merge(array('repositories' => array('packagist' => false))); $config->merge(array('repositories' => array('packagist' => false)));
$guesser = new VersionGuesser($config, $executor, new VersionParser()); $guesser = new VersionGuesser($config, $process, new VersionParser());
$versionData = $guesser->guessVersion(array(), 'dummy/path'); $versionData = $guesser->guessVersion(array(), 'dummy/path');
$this->assertEquals("dev-$commitHash", $versionData['version']); $this->assertEquals("dev-$commitHash", $versionData['version']);
$process->assertComplete($this);
} }
public function testDetachedFetchHeadBecomesDevHashGit2() public function testDetachedFetchHeadBecomesDevHashGit2()
{ {
$commitHash = '03a15d220da53c52eddd5f32ffca64a7b3801bea'; $commitHash = '03a15d220da53c52eddd5f32ffca64a7b3801bea';
$executor = $this->getMockBuilder('\\Composer\\Util\\ProcessExecutor') $process = new ProcessExecutorMock;
->setMethods(array('execute')) $process->expects(array(
->disableArgumentCloning() array(
->disableOriginalConstructor() 'cmd' => 'git branch -a --no-color --no-abbrev -v',
->getMock(); 'stdout' => "* (HEAD detached at FETCH_HEAD) $commitHash Commit message\n",
),
$self = $this; 'git describe --exact-match --tags',
), true);
$executor
->expects($this->at(0))
->method('execute')
->willReturnCallback(function ($command, &$output) use ($self, $commitHash) {
$self->assertEquals('git branch -a --no-color --no-abbrev -v', $command);
$output = "* (HEAD detached at FETCH_HEAD) $commitHash Commit message\n";
return 0;
});
$config = new Config; $config = new Config;
$config->merge(array('repositories' => array('packagist' => false))); $config->merge(array('repositories' => array('packagist' => false)));
$guesser = new VersionGuesser($config, $executor, new VersionParser()); $guesser = new VersionGuesser($config, $process, new VersionParser());
$versionData = $guesser->guessVersion(array(), 'dummy/path'); $versionData = $guesser->guessVersion(array(), 'dummy/path');
$this->assertEquals("dev-$commitHash", $versionData['version']); $this->assertEquals("dev-$commitHash", $versionData['version']);
$process->assertComplete($this);
} }
public function testDetachedCommitHeadBecomesDevHashGit2() public function testDetachedCommitHeadBecomesDevHashGit2()
{ {
$commitHash = '03a15d220da53c52eddd5f32ffca64a7b3801bea'; $commitHash = '03a15d220da53c52eddd5f32ffca64a7b3801bea';
$executor = $this->getMockBuilder('\\Composer\\Util\\ProcessExecutor')
->setMethods(array('execute'))
->disableArgumentCloning()
->disableOriginalConstructor()
->getMock();
$self = $this; $process = new ProcessExecutorMock;
$process->expects(array(
$executor array(
->expects($this->at(0)) 'cmd' => 'git branch -a --no-color --no-abbrev -v',
->method('execute') 'stdout' => "* (HEAD detached at 03a15d220) $commitHash Commit message\n",
->willReturnCallback(function ($command, &$output) use ($self, $commitHash) { ),
$self->assertEquals('git branch -a --no-color --no-abbrev -v', $command); 'git describe --exact-match --tags',
$output = "* (HEAD detached at 03a15d220) $commitHash Commit message\n"; ), true);
return 0;
});
$config = new Config; $config = new Config;
$config->merge(array('repositories' => array('packagist' => false))); $config->merge(array('repositories' => array('packagist' => false)));
$guesser = new VersionGuesser($config, $executor, new VersionParser()); $guesser = new VersionGuesser($config, $process, new VersionParser());
$versionData = $guesser->guessVersion(array(), 'dummy/path'); $versionData = $guesser->guessVersion(array(), 'dummy/path');
$this->assertEquals("dev-$commitHash", $versionData['version']); $this->assertEquals("dev-$commitHash", $versionData['version']);
$process->assertComplete($this);
} }
public function testTagBecomesVersion() public function testTagBecomesVersion()
{ {
$executor = $this->getMockBuilder('\\Composer\\Util\\ProcessExecutor') $process = new ProcessExecutorMock;
->setMethods(array('execute')) $process->expects(array(
->disableArgumentCloning() array(
->disableOriginalConstructor() 'cmd' => 'git branch -a --no-color --no-abbrev -v',
->getMock() 'stdout' => "* (HEAD detached at v2.0.5-alpha2) 433b98d4218c181bae01865901aac045585e8a1a Commit message\n",
; ),
array(
$self = $this; 'cmd' => 'git describe --exact-match --tags',
'stdout' => "v2.0.5-alpha2",
$executor ),
->expects($this->at(0)) ), true);
->method('execute')
->willReturnCallback(function ($command, &$output) use ($self) {
$self->assertEquals('git branch -a --no-color --no-abbrev -v', $command);
$output = "* (HEAD detached at v2.0.5-alpha2) 433b98d4218c181bae01865901aac045585e8a1a Commit message\n";
return 0;
})
;
$executor
->expects($this->at(1))
->method('execute')
->willReturnCallback(function ($command, &$output) use ($self) {
$self->assertEquals('git describe --exact-match --tags', $command);
$output = "v2.0.5-alpha2";
return 0;
})
;
$config = new Config; $config = new Config;
$config->merge(array('repositories' => array('packagist' => false))); $config->merge(array('repositories' => array('packagist' => false)));
$guesser = new VersionGuesser($config, $executor, new VersionParser()); $guesser = new VersionGuesser($config, $process, new VersionParser());
$versionData = $guesser->guessVersion(array(), 'dummy/path'); $versionData = $guesser->guessVersion(array(), 'dummy/path');
$this->assertEquals("2.0.5.0-alpha2", $versionData['version']); $this->assertEquals("2.0.5.0-alpha2", $versionData['version']);
$process->assertComplete($this);
} }
public function testTagBecomesPrettyVersion() public function testTagBecomesPrettyVersion()
{ {
$executor = $this->getMockBuilder('\\Composer\\Util\\ProcessExecutor') $process = new ProcessExecutorMock;
->setMethods(array('execute')) $process->expects(array(
->disableArgumentCloning() array(
->disableOriginalConstructor() 'cmd' => 'git branch -a --no-color --no-abbrev -v',
->getMock() 'stdout' => "* (HEAD detached at 1.0.0) c006f0c12bbbf197b5c071ffb1c0e9812bb14a4d Commit message\n",
; ),
array(
$self = $this; 'cmd' => 'git describe --exact-match --tags',
'stdout' => '1.0.0',
$executor ),
->expects($this->at(0)) ), true);
->method('execute')
->willReturnCallback(function ($command, &$output) use ($self) {
$self->assertEquals('git branch -a --no-color --no-abbrev -v', $command);
$output = "* (HEAD detached at 1.0.0) c006f0c12bbbf197b5c071ffb1c0e9812bb14a4d Commit message\n";
return 0;
})
;
$executor
->expects($this->at(1))
->method('execute')
->willReturnCallback(function ($command, &$output) use ($self) {
$self->assertEquals('git describe --exact-match --tags', $command);
$output = '1.0.0';
return 0;
})
;
$config = new Config; $config = new Config;
$config->merge(array('repositories' => array('packagist' => false))); $config->merge(array('repositories' => array('packagist' => false)));
$guesser = new VersionGuesser($config, $executor, new VersionParser()); $guesser = new VersionGuesser($config, $process, new VersionParser());
$versionData = $guesser->guessVersion(array(), 'dummy/path'); $versionData = $guesser->guessVersion(array(), 'dummy/path');
$this->assertEquals('1.0.0.0', $versionData['version']); $this->assertEquals('1.0.0.0', $versionData['version']);
$this->assertEquals('1.0.0', $versionData['pretty_version']); $this->assertEquals('1.0.0', $versionData['pretty_version']);
$process->assertComplete($this);
} }
public function testInvalidTagBecomesVersion() public function testInvalidTagBecomesVersion()
{ {
$executor = $this->getMockBuilder('\\Composer\\Util\\ProcessExecutor') $process = new ProcessExecutorMock;
->setMethods(array('execute')) $process->expects(array(
->disableArgumentCloning() array(
->disableOriginalConstructor() 'cmd' => 'git branch -a --no-color --no-abbrev -v',
->getMock() 'stdout' => "* foo 03a15d220da53c52eddd5f32ffca64a7b3801bea Commit message\n",
; ),
), true);
$self = $this;
$executor
->expects($this->at(0))
->method('execute')
->willReturnCallback(function ($command, &$output) use ($self) {
$self->assertEquals('git branch -a --no-color --no-abbrev -v', $command);
$output = "* foo 03a15d220da53c52eddd5f32ffca64a7b3801bea Commit message\n";
return 0;
})
;
$config = new Config; $config = new Config;
$config->merge(array('repositories' => array('packagist' => false))); $config->merge(array('repositories' => array('packagist' => false)));
$guesser = new VersionGuesser($config, $executor, new VersionParser()); $guesser = new VersionGuesser($config, $process, new VersionParser());
$versionData = $guesser->guessVersion(array(), 'dummy/path'); $versionData = $guesser->guessVersion(array(), 'dummy/path');
$this->assertEquals("dev-foo", $versionData['version']); $this->assertEquals("dev-foo", $versionData['version']);
$process->assertComplete($this);
} }
public function testNumericBranchesShowNicely() public function testNumericBranchesShowNicely()
{ {
$executor = $this->getMockBuilder('\\Composer\\Util\\ProcessExecutor') $process = new ProcessExecutorMock;
->setMethods(array('execute')) $process->expects(array(
->disableArgumentCloning() array(
->disableOriginalConstructor() 'cmd' => 'git branch -a --no-color --no-abbrev -v',
->getMock() 'stdout' => "* 1.5 03a15d220da53c52eddd5f32ffca64a7b3801bea Commit message\n",
; ),
), true);
$self = $this;
$executor
->expects($this->at(0))
->method('execute')
->willReturnCallback(function ($command, &$output) use ($self) {
$self->assertEquals('git branch -a --no-color --no-abbrev -v', $command);
$output = "* 1.5 03a15d220da53c52eddd5f32ffca64a7b3801bea Commit message\n";
return 0;
})
;
$config = new Config; $config = new Config;
$config->merge(array('repositories' => array('packagist' => false))); $config->merge(array('repositories' => array('packagist' => false)));
$guesser = new VersionGuesser($config, $executor, new VersionParser()); $guesser = new VersionGuesser($config, $process, new VersionParser());
$versionData = $guesser->guessVersion(array(), 'dummy/path'); $versionData = $guesser->guessVersion(array(), 'dummy/path');
$this->assertEquals("1.5.x-dev", $versionData['pretty_version']); $this->assertEquals("1.5.x-dev", $versionData['pretty_version']);
$this->assertEquals("1.5.9999999.9999999-dev", $versionData['version']); $this->assertEquals("1.5.9999999.9999999-dev", $versionData['version']);
$process->assertComplete($this);
} }
public function testRemoteBranchesAreSelected() public function testRemoteBranchesAreSelected()
{ {
$executor = $this->getMockBuilder('\\Composer\\Util\\ProcessExecutor') $process = new ProcessExecutorMock;
->setMethods(array('execute')) $process->expects(array(
->disableArgumentCloning() array(
->disableOriginalConstructor() 'cmd' => 'git branch -a --no-color --no-abbrev -v',
->getMock() 'stdout' => "* feature-branch 03a15d220da53c52eddd5f32ffca64a7b3801bea Commit message\n".
; "remotes/origin/1.5 03a15d220da53c52eddd5f32ffca64a7b3801bea Commit message\n",
),
$self = $this; array(
'cmd' => 'git rev-list remotes/origin/1.5..feature-branch',
$executor 'stdout' => "\n",
->expects($this->at(0)) ),
->method('execute') ), true);
->willReturnCallback(function ($command, &$output) use ($self) {
$self->assertEquals('git branch -a --no-color --no-abbrev -v', $command);
$output = "* feature-branch 03a15d220da53c52eddd5f32ffca64a7b3801bea Commit message\n".
"remotes/origin/1.5 03a15d220da53c52eddd5f32ffca64a7b3801bea Commit message\n";
return 0;
})
;
$executor
->expects($this->at(1))
->method('execute')
->willReturnCallback(function ($command, &$output, $path) use ($self) {
$self->assertEquals('git rev-list remotes/origin/1.5..feature-branch', $command);
$output = "\n";
return 0;
})
;
$config = new Config; $config = new Config;
$config->merge(array('repositories' => array('packagist' => false))); $config->merge(array('repositories' => array('packagist' => false)));
$guesser = new VersionGuesser($config, $executor, new VersionParser()); $guesser = new VersionGuesser($config, $process, new VersionParser());
$versionData = $guesser->guessVersion(array('version' => 'self.version'), 'dummy/path'); $versionData = $guesser->guessVersion(array('version' => 'self.version'), 'dummy/path');
$this->assertEquals("1.5.x-dev", $versionData['pretty_version']); $this->assertEquals("1.5.x-dev", $versionData['pretty_version']);
$this->assertEquals("1.5.9999999.9999999-dev", $versionData['version']); $this->assertEquals("1.5.9999999.9999999-dev", $versionData['version']);
$process->assertComplete($this);
} }
} }

View File

@ -270,42 +270,17 @@ class VersionSelectorTest extends TestCase
/** /**
* @dataProvider getRecommendedRequireVersionPackages * @dataProvider getRecommendedRequireVersionPackages
*/ */
public function testFindRecommendedRequireVersion($prettyVersion, $isDev, $stability, $expectedVersion, $branchAlias = null, $packageName = null) public function testFindRecommendedRequireVersion($prettyVersion, $expectedVersion, $branchAlias = null, $packageName = 'foo/bar')
{ {
$repositorySet = $this->createMockRepositorySet(); $repositorySet = $this->createMockRepositorySet();
$versionSelector = new VersionSelector($repositorySet); $versionSelector = new VersionSelector($repositorySet);
$versionParser = new VersionParser(); $versionParser = new VersionParser();
$package = $this->getMockBuilder('\Composer\Package\PackageInterface')->getMock(); $package = new Package($packageName, $versionParser->normalize($prettyVersion), $prettyVersion);
$package
->expects($this->any())
->method('getPrettyVersion')
->will($this->returnValue($prettyVersion));
$package
->expects($this->any())
->method('getName')
->will($this->returnValue($packageName));
$package
->expects($this->any())
->method('getVersion')
->will($this->returnValue($versionParser->normalize($prettyVersion)));
$package
->expects($this->any())
->method('isDev')
->will($this->returnValue($isDev));
$package
->expects($this->any())
->method('getStability')
->will($this->returnValue($stability));
$package
->expects($this->any())
->method('getTransportOptions')
->will($this->returnValue(array()));
$branchAlias = $branchAlias === null ? array() : array('branch-alias' => array($prettyVersion => $branchAlias)); if ($branchAlias) {
$package->expects($this->any()) $package->setExtra(array('branch-alias' => array($prettyVersion => $branchAlias)));
->method('getExtra') }
->will($this->returnValue($branchAlias));
$recommended = $versionSelector->findRecommendedRequireVersion($package); $recommended = $versionSelector->findRecommendedRequireVersion($package);
@ -316,40 +291,40 @@ class VersionSelectorTest extends TestCase
public function getRecommendedRequireVersionPackages() public function getRecommendedRequireVersionPackages()
{ {
return array( return array(
// real version, is dev package, stability, expected recommendation, [branch-alias], [pkg name] // real version, expected recommendation, [branch-alias], [pkg name]
array('1.2.1', false, 'stable', '^1.2'), array('1.2.1', '^1.2'),
array('1.2', false, 'stable', '^1.2'), array('1.2', '^1.2'),
array('v1.2.1', false, 'stable', '^1.2'), array('v1.2.1', '^1.2'),
array('3.1.2-pl2', false, 'stable', '^3.1'), array('3.1.2-pl2', '^3.1'),
array('3.1.2-patch', false, 'stable', '^3.1'), array('3.1.2-patch', '^3.1'),
array('2.0-beta.1', false, 'beta', '^2.0@beta'), array('2.0-beta.1', '^2.0@beta'),
array('3.1.2-alpha5', false, 'alpha', '^3.1@alpha'), array('3.1.2-alpha5', '^3.1@alpha'),
array('3.0-RC2', false, 'RC', '^3.0@RC'), array('3.0-RC2', '^3.0@RC'),
array('0.1.0', false, 'stable', '^0.1.0'), array('0.1.0', '^0.1.0'),
array('0.1.3', false, 'stable', '^0.1.3'), array('0.1.3', '^0.1.3'),
array('0.0.3', false, 'stable', '^0.0.3'), array('0.0.3', '^0.0.3'),
array('0.0.3-alpha', false, 'alpha', '^0.0.3@alpha'), array('0.0.3-alpha', '^0.0.3@alpha'),
// date-based versions are not touched at all // date-based versions are not touched at all
array('v20121020', false, 'stable', 'v20121020'), array('v20121020', 'v20121020'),
array('v20121020.2', false, 'stable', 'v20121020.2'), array('v20121020.2', 'v20121020.2'),
// dev packages without alias are not touched at all // dev packages without alias are not touched at all
array('dev-master', true, 'dev', 'dev-master'), array('dev-master', 'dev-master'),
array('3.1.2-dev', true, 'dev', '3.1.2-dev'), array('3.1.2-dev', '3.1.2-dev'),
// dev packages with alias inherit the alias // dev packages with alias inherit the alias
array('dev-master', true, 'dev', '^2.1@dev', '2.1.x-dev'), array('dev-master', '^2.1@dev', '2.1.x-dev'),
array('dev-master', true, 'dev', '^2.1@dev', '2.1-dev'), array('dev-master', '^2.1@dev', '2.1-dev'),
array('dev-master', true, 'dev', '^2.1@dev', '2.1.3.x-dev'), array('dev-master', '^2.1@dev', '2.1.3.x-dev'),
array('dev-master', true, 'dev', '^2.0@dev', '2.x-dev'), array('dev-master', '^2.0@dev', '2.x-dev'),
array('dev-master', true, 'dev', '^0.3.0@dev', '0.3.x-dev'), array('dev-master', '^0.3.0@dev', '0.3.x-dev'),
array('dev-master', true, 'dev', '^0.0.3@dev', '0.0.3.x-dev'), array('dev-master', '^0.0.3@dev', '0.0.3.x-dev'),
array('dev-master', true, 'dev', 'dev-master', VersionParser::DEFAULT_BRANCH_ALIAS), array('dev-master', 'dev-master', VersionParser::DEFAULT_BRANCH_ALIAS),
// numeric alias // numeric alias
array('3.x-dev', true, 'dev', '^3.0@dev', '3.0.x-dev'), array('3.x-dev', '^3.0@dev', '3.0.x-dev'),
array('3.x-dev', true, 'dev', '^3.0@dev', '3.0-dev'), array('3.x-dev', '^3.0@dev', '3.0-dev'),
// ext in sync with php // ext in sync with php
array(PHP_MAJOR_VERSION . '.' . PHP_MINOR_VERSION . '.' . PHP_RELEASE_VERSION, false, 'stable', '*', null, 'ext-filter'), array(PHP_MAJOR_VERSION . '.' . PHP_MINOR_VERSION . '.' . PHP_RELEASE_VERSION, '*', null, 'ext-filter'),
// ext versioned individually // ext versioned individually
array('3.0.5', false, 'stable', '^3.0', null, 'ext-xdebug'), array('3.0.5', '^3.0', null, 'ext-xdebug'),
); );
} }

View File

@ -17,7 +17,10 @@ use Composer\Repository\Vcs\GitHubDriver;
use Composer\Test\TestCase; use Composer\Test\TestCase;
use Composer\Util\Filesystem; use Composer\Util\Filesystem;
use Composer\Util\Http\Response; use Composer\Util\Http\Response;
use Composer\Test\Mock\ProcessExecutorMock;
use Composer\Config; use Composer\Config;
use Composer\Util\ProcessExecutor;
use Symfony\Component\Process\Process;
class GitHubDriverTest extends TestCase class GitHubDriverTest extends TestCase
{ {
@ -58,10 +61,8 @@ class GitHubDriverTest extends TestCase
->setConstructorArgs(array($io, $this->config)) ->setConstructorArgs(array($io, $this->config))
->getMock(); ->getMock();
$process = $this->getMockBuilder('Composer\Util\ProcessExecutor')->getMock(); $process = new ProcessExecutorMock;
$process->expects($this->any()) $process->expects(array(), false, array('return' => 1));
->method('execute')
->will($this->returnValue(1));
$httpDownloader->expects($this->at(0)) $httpDownloader->expects($this->at(0))
->method('get') ->method('get')
@ -139,11 +140,7 @@ class GitHubDriverTest extends TestCase
); );
$repoUrl = 'https://github.com/composer/packagist.git'; $repoUrl = 'https://github.com/composer/packagist.git';
$process = $this->getMockBuilder('Composer\Util\ProcessExecutor') $gitHubDriver = new GitHubDriver($repoConfig, $io, $this->config, $httpDownloader, new ProcessExecutorMock);
->disableOriginalConstructor()
->getMock();
$gitHubDriver = new GitHubDriver($repoConfig, $io, $this->config, $httpDownloader, $process);
$gitHubDriver->initialize(); $gitHubDriver->initialize();
$this->setAttribute($gitHubDriver, 'tags', array($identifier => $sha)); $this->setAttribute($gitHubDriver, 'tags', array($identifier => $sha));
@ -201,11 +198,7 @@ class GitHubDriverTest extends TestCase
); );
$repoUrl = 'https://github.com/composer/packagist.git'; $repoUrl = 'https://github.com/composer/packagist.git';
$process = $this->getMockBuilder('Composer\Util\ProcessExecutor') $gitHubDriver = new GitHubDriver($repoConfig, $io, $this->config, $httpDownloader, new ProcessExecutorMock);
->disableOriginalConstructor()
->getMock();
$gitHubDriver = new GitHubDriver($repoConfig, $io, $this->config, $httpDownloader, $process);
$gitHubDriver->initialize(); $gitHubDriver->initialize();
$this->setAttribute($gitHubDriver, 'tags', array($identifier => $sha)); $this->setAttribute($gitHubDriver, 'tags', array($identifier => $sha));
@ -239,10 +232,6 @@ class GitHubDriverTest extends TestCase
->method('isInteractive') ->method('isInteractive')
->will($this->returnValue(true)); ->will($this->returnValue(true));
$process = $this->getMockBuilder('Composer\Util\ProcessExecutor')
->disableOriginalConstructor()
->getMock();
$httpDownloader = $this->getMockBuilder('Composer\Util\HttpDownloader') $httpDownloader = $this->getMockBuilder('Composer\Util\HttpDownloader')
->setConstructorArgs(array($io, $this->config)) ->setConstructorArgs(array($io, $this->config))
->getMock(); ->getMock();
@ -271,7 +260,7 @@ class GitHubDriverTest extends TestCase
'url' => $repoUrl, 'url' => $repoUrl,
); );
$gitHubDriver = new GitHubDriver($repoConfig, $io, $this->config, $httpDownloader, $process); $gitHubDriver = new GitHubDriver($repoConfig, $io, $this->config, $httpDownloader, new ProcessExecutorMock);
$gitHubDriver->initialize(); $gitHubDriver->initialize();
$this->setAttribute($gitHubDriver, 'tags', array($identifier => $sha)); $this->setAttribute($gitHubDriver, 'tags', array($identifier => $sha));
@ -288,10 +277,6 @@ class GitHubDriverTest extends TestCase
$identifier = 'v0.0.0'; $identifier = 'v0.0.0';
$sha = 'SOMESHA'; $sha = 'SOMESHA';
$process = $this->getMockBuilder('Composer\Util\ProcessExecutor')
->disableOriginalConstructor()
->getMock();
$io = $this->getMockBuilder('Composer\IO\IOInterface')->getMock(); $io = $this->getMockBuilder('Composer\IO\IOInterface')->getMock();
$io->expects($this->any()) $io->expects($this->any())
->method('isInteractive') ->method('isInteractive')
@ -310,39 +295,23 @@ class GitHubDriverTest extends TestCase
$fs = new Filesystem(); $fs = new Filesystem();
$fs->removeDirectory(sys_get_temp_dir() . '/composer-test'); $fs->removeDirectory(sys_get_temp_dir() . '/composer-test');
$process->expects($this->at(0)) $process = new ProcessExecutorMock;
->method('execute') $process->expects(array(
->with($this->equalTo('git config github.accesstoken')) array('cmd' => 'git config github.accesstoken', 'return' => 1),
->will($this->returnValue(1)); 'git clone --mirror -- '.ProcessExecutor::escape($repoSshUrl).' '.ProcessExecutor::escape($this->config->get('cache-vcs-dir').'/git-github.com-composer-packagist.git/'),
array(
$process->expects($this->at(1)) 'cmd' => 'git show-ref --tags --dereference',
->method('execute') 'stdout' => $sha.' refs/tags/'.$identifier,
->with($this->stringContains($repoSshUrl)) ),
->will($this->returnValue(0)); array(
'cmd' => 'git branch --no-color --no-abbrev -v',
$process->expects($this->at(2)) 'stdout' => ' test_master edf93f1fccaebd8764383dc12016d0a1a9672d89 Fix test & behavior',
->method('execute') ),
->with($this->stringContains('git show-ref --tags')); array(
'cmd' => 'git branch --no-color',
$process->expects($this->at(3)) 'stdout' => '* test_master',
->method('splitLines') ),
->will($this->returnValue(array($sha.' refs/tags/'.$identifier))); ), true);
$process->expects($this->at(4))
->method('execute')
->with($this->stringContains('git branch --no-color --no-abbrev -v'));
$process->expects($this->at(5))
->method('splitLines')
->will($this->returnValue(array(' test_master edf93f1fccaebd8764383dc12016d0a1a9672d89 Fix test & behavior')));
$process->expects($this->at(6))
->method('execute')
->with($this->stringContains('git branch --no-color'));
$process->expects($this->at(7))
->method('splitLines')
->will($this->returnValue(array('* test_master')));
$repoConfig = array( $repoConfig = array(
'url' => $repoUrl, 'url' => $repoUrl,
@ -367,6 +336,8 @@ class GitHubDriverTest extends TestCase
$this->assertEquals('git', $source['type']); $this->assertEquals('git', $source['type']);
$this->assertEquals($repoSshUrl, $source['url']); $this->assertEquals($repoSshUrl, $source['url']);
$this->assertEquals($sha, $source['reference']); $this->assertEquals($sha, $source['reference']);
$process->assertComplete($this);
} }
protected function setAttribute($object, $attribute, $value) protected function setAttribute($object, $attribute, $value)

View File

@ -17,6 +17,7 @@ use Composer\Test\TestCase;
use Composer\Util\Filesystem; use Composer\Util\Filesystem;
use Composer\Config; use Composer\Config;
use Composer\Util\Perforce; use Composer\Util\Perforce;
use Composer\Test\Mock\ProcessExecutorMock;
/** /**
* @author Matt Whittom <Matt.Whittom@veteransunited.com> * @author Matt Whittom <Matt.Whittom@veteransunited.com>
@ -42,7 +43,7 @@ class PerforceDriverTest extends TestCase
$this->config = $this->getTestConfig($this->testPath); $this->config = $this->getTestConfig($this->testPath);
$this->repoConfig = $this->getTestRepoConfig(); $this->repoConfig = $this->getTestRepoConfig();
$this->io = $this->getMockIOInterface(); $this->io = $this->getMockIOInterface();
$this->process = $this->getMockProcessExecutor(); $this->process = new ProcessExecutorMock;
$this->httpDownloader = $this->getMockHttpDownloader(); $this->httpDownloader = $this->getMockHttpDownloader();
$this->perforce = $this->getMockPerforce(); $this->perforce = $this->getMockPerforce();
$this->driver = new PerforceDriver($this->repoConfig, $this->io, $this->config, $this->httpDownloader, $this->process); $this->driver = new PerforceDriver($this->repoConfig, $this->io, $this->config, $this->httpDownloader, $this->process);
@ -94,11 +95,6 @@ class PerforceDriverTest extends TestCase
return $this->getMockBuilder('Composer\IO\IOInterface')->getMock(); return $this->getMockBuilder('Composer\IO\IOInterface')->getMock();
} }
protected function getMockProcessExecutor()
{
return $this->getMockBuilder('Composer\Util\ProcessExecutor')->getMock();
}
protected function getMockHttpDownloader() protected function getMockHttpDownloader()
{ {
return $this->getMockBuilder('Composer\Util\HttpDownloader')->disableOriginalConstructor()->getMock(); return $this->getMockBuilder('Composer\Util\HttpDownloader')->disableOriginalConstructor()->getMock();

View File

@ -16,6 +16,7 @@ use Composer\Repository\Vcs\SvnDriver;
use Composer\Config; use Composer\Config;
use Composer\Test\TestCase; use Composer\Test\TestCase;
use Composer\Util\Filesystem; use Composer\Util\Filesystem;
use Composer\Test\Mock\ProcessExecutorMock;
class SvnDriverTest extends TestCase class SvnDriverTest extends TestCase
{ {
@ -50,16 +51,11 @@ class SvnDriverTest extends TestCase
$output .= " authorization failed: Could not authenticate to server:"; $output .= " authorization failed: Could not authenticate to server:";
$output .= " rejected Basic challenge (https://corp.svn.local/)"; $output .= " rejected Basic challenge (https://corp.svn.local/)";
$process = $this->getMockBuilder('Composer\Util\ProcessExecutor')->getMock(); $process = new ProcessExecutorMock;
$process->expects($this->at(1)) $process->expects(array(
->method('execute') 'svn --version',
->will($this->returnValue(1)); array('cmd' => '', 'return' => 1, 'stderr' => $output),
$process->expects($this->exactly(7)) ), true);
->method('getErrorOutput')
->will($this->returnValue($output));
$process->expects($this->at(2))
->method('execute')
->will($this->returnValue(0));
$repoConfig = array( $repoConfig = array(
'url' => 'https://till:secret@corp.svn.local/repo', 'url' => 'https://till:secret@corp.svn.local/repo',
@ -67,6 +63,8 @@ class SvnDriverTest extends TestCase
$svn = new SvnDriver($repoConfig, $console, $this->config, $httpDownloader, $process); $svn = new SvnDriver($repoConfig, $console, $this->config, $httpDownloader, $process);
$svn->initialize(); $svn->initialize();
$process->assertComplete($this);
} }
public static function supportProvider() public static function supportProvider()

View File

@ -15,6 +15,7 @@ namespace Composer\Test\Util;
use Composer\Util\Bitbucket; use Composer\Util\Bitbucket;
use Composer\Util\Http\Response; use Composer\Util\Http\Response;
use Composer\Test\TestCase; use Composer\Test\TestCase;
use Composer\Test\Mock\ProcessExecutorMock;
/** /**
* @author Paul Wenke <wenke.paul@gmail.com> * @author Paul Wenke <wenke.paul@gmail.com>
@ -456,10 +457,8 @@ class BitbucketTest extends TestCase
public function testAuthorizeOAuthWithoutAvailableGitConfigToken() public function testAuthorizeOAuthWithoutAvailableGitConfigToken()
{ {
$process = $this->getMockBuilder('Composer\Util\ProcessExecutor')->getMock(); $process = new ProcessExecutorMock;
$process->expects($this->once()) $process->expects(array(), false, array('return' => -1));
->method('execute')
->willReturn(-1);
$bitbucket = new Bitbucket($this->io, $this->config, $process, $this->httpDownloader, $this->time); $bitbucket = new Bitbucket($this->io, $this->config, $process, $this->httpDownloader, $this->time);
@ -468,10 +467,7 @@ class BitbucketTest extends TestCase
public function testAuthorizeOAuthWithAvailableGitConfigToken() public function testAuthorizeOAuthWithAvailableGitConfigToken()
{ {
$process = $this->getMockBuilder('Composer\Util\ProcessExecutor')->getMock(); $process = new ProcessExecutorMock;
$process->expects($this->once())
->method('execute')
->willReturn(0);
$bitbucket = new Bitbucket($this->io, $this->config, $process, $this->httpDownloader, $this->time); $bitbucket = new Bitbucket($this->io, $this->config, $process, $this->httpDownloader, $this->time);

View File

@ -93,8 +93,9 @@ class GitLabTest extends TestCase
$httpDownloader $httpDownloader
->expects($this->exactly(5)) ->expects($this->exactly(5))
->method('get') ->method('get')
->will($this->throwException(new TransportException('', 401))) ->will($this->throwException($e = new TransportException('', 401)))
; ;
$e->setResponse('{}');
$config = $this->getConfigMock(); $config = $this->getConfigMock();
$config $config

View File

@ -16,7 +16,7 @@ use Composer\Config;
use Composer\IO\IOInterface; use Composer\IO\IOInterface;
use Composer\Util\Filesystem; use Composer\Util\Filesystem;
use Composer\Util\Git; use Composer\Util\Git;
use Composer\Util\ProcessExecutor; use Composer\Test\Mock\ProcessExecutorMock;
use Composer\Test\TestCase; use Composer\Test\TestCase;
class GitTest extends TestCase class GitTest extends TestCase
@ -27,7 +27,7 @@ class GitTest extends TestCase
private $io; private $io;
/** @var Config&\PHPUnit\Framework\MockObject\MockObject */ /** @var Config&\PHPUnit\Framework\MockObject\MockObject */
private $config; private $config;
/** @var ProcessExecutor&\PHPUnit\Framework\MockObject\MockObject */ /** @var ProcessExecutorMock */
private $process; private $process;
/** @var Filesystem&\PHPUnit\Framework\MockObject\MockObject */ /** @var Filesystem&\PHPUnit\Framework\MockObject\MockObject */
private $fs; private $fs;
@ -36,7 +36,7 @@ class GitTest extends TestCase
{ {
$this->io = $this->getMockBuilder('Composer\IO\IOInterface')->getMock(); $this->io = $this->getMockBuilder('Composer\IO\IOInterface')->getMock();
$this->config = $this->getMockBuilder('Composer\Config')->disableOriginalConstructor()->getMock(); $this->config = $this->getMockBuilder('Composer\Config')->disableOriginalConstructor()->getMock();
$this->process = $this->getMockBuilder('Composer\Util\ProcessExecutor')->disableOriginalConstructor()->getMock(); $this->process = new ProcessExecutorMock;
$this->fs = $this->getMockBuilder('Composer\Util\Filesystem')->disableOriginalConstructor()->getMock(); $this->fs = $this->getMockBuilder('Composer\Util\Filesystem')->disableOriginalConstructor()->getMock();
$this->git = new Git($this->io, $this->config, $this->process, $this->fs); $this->git = new Git($this->io, $this->config, $this->process, $this->fs);
} }
@ -55,13 +55,11 @@ class GitTest extends TestCase
$this->mockConfig($protocol); $this->mockConfig($protocol);
$this->process $this->process->expects(array('git command'), true);
->expects($this->once())
->method('execute')
->with($this->equalTo('git command'))
->willReturn(0);
$this->git->runCommand($commandCallable, 'https://github.com/acme/repo', null, true); $this->git->runCommand($commandCallable, 'https://github.com/acme/repo', null, true);
$this->process->assertComplete($this);
} }
public function publicGithubNoCredentialsProvider() public function publicGithubNoCredentialsProvider()
@ -85,20 +83,21 @@ class GitTest extends TestCase
$this->mockConfig('https'); $this->mockConfig('https');
$this->process
->method('execute') $this->process->expects(array(
->willReturnMap(array( array('cmd' => 'git command', 'return' => 1),
array('git command', null, null, 1), array('cmd' => 'git --version', 'return' => 0),
array('git --version', null, null, 0), ), true);
));
$this->git->runCommand($commandCallable, 'https://github.com/acme/repo', null, true); $this->git->runCommand($commandCallable, 'https://github.com/acme/repo', null, true);
$this->process->assertComplete($this);
} }
/** /**
* @dataProvider privateGithubWithCredentialsProvider * @dataProvider privateGithubWithCredentialsProvider
*/ */
public function testRunCommandPrivateGitHubRepositoryNotInitialCloneNotInteractiveWithAuthentication($gitUrl, $protocol, $gitHubToken, $expectedUrl) public function testRunCommandPrivateGitHubRepositoryNotInitialCloneNotInteractiveWithAuthentication($gitUrl, $protocol, $gitHubToken, $expectedUrl, $expectedFailuresBeforeSuccess)
{ {
$commandCallable = function ($url) use ($expectedUrl) { $commandCallable = function ($url) use ($expectedUrl) {
if ($url !== $expectedUrl) { if ($url !== $expectedUrl) {
@ -110,13 +109,10 @@ class GitTest extends TestCase
$this->mockConfig($protocol); $this->mockConfig($protocol);
$this->process $expectedCalls = array_fill(0, $expectedFailuresBeforeSuccess, array('cmd' => 'git command failing', 'return' => 1));
->expects($this->atLeast(2)) $expectedCalls[] = array('cmd' => 'git command ok', 'return' => 0);
->method('execute')
->willReturnMap(array( $this->process->expects($expectedCalls, true);
array('git command failing', null, null, 1),
array('git command ok', null, null, 0),
));
$this->io $this->io
->method('isInteractive') ->method('isInteractive')
@ -135,13 +131,15 @@ class GitTest extends TestCase
->willReturn(array('username' => 'token', 'password' => $gitHubToken)); ->willReturn(array('username' => 'token', 'password' => $gitHubToken));
$this->git->runCommand($commandCallable, $gitUrl, null, true); $this->git->runCommand($commandCallable, $gitUrl, null, true);
$this->process->assertComplete($this);
} }
public function privateGithubWithCredentialsProvider() public function privateGithubWithCredentialsProvider()
{ {
return array( return array(
array('git@github.com:acme/repo.git', 'ssh', 'MY_GITHUB_TOKEN', 'https://token:MY_GITHUB_TOKEN@github.com/acme/repo.git'), array('git@github.com:acme/repo.git', 'ssh', 'MY_GITHUB_TOKEN', 'https://token:MY_GITHUB_TOKEN@github.com/acme/repo.git', 1),
array('https://github.com/acme/repo', 'https', 'MY_GITHUB_TOKEN', 'https://token:MY_GITHUB_TOKEN@github.com/acme/repo.git'), array('https://github.com/acme/repo', 'https', 'MY_GITHUB_TOKEN', 'https://token:MY_GITHUB_TOKEN@github.com/acme/repo.git', 2),
); );
} }

View File

@ -12,7 +12,7 @@
namespace Composer\Test\Util; namespace Composer\Test\Util;
use Composer\Util\MetadataMinifier; use Composer\MetadataMinifier\MetadataMinifier;
use Composer\Package\CompletePackage; use Composer\Package\CompletePackage;
use Composer\Package\Dumper\ArrayDumper; use Composer\Package\Dumper\ArrayDumper;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;