123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315 |
- <?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\Repository\Vcs;
- use Composer\Downloader\TransportException;
- use Composer\Repository\Vcs\GitHubDriver;
- use Composer\Util\Filesystem;
- use Composer\Config;
- class GitHubDriverTest extends \PHPUnit_Framework_TestCase
- {
- private $config;
- public function setUp()
- {
- $this->config = new Config();
- $this->config->merge(array(
- 'config' => array(
- 'home' => sys_get_temp_dir() . '/composer-test',
- ),
- ));
- }
- public function tearDown()
- {
- $fs = new Filesystem;
- $fs->removeDirectory(sys_get_temp_dir() . '/composer-test');
- }
- public function testPrivateRepository()
- {
- $repoUrl = 'http://github.com/composer/packagist';
- $repoApiUrl = 'https://api.github.com/repos/composer/packagist';
- $repoSshUrl = 'git@github.com:composer/packagist.git';
- $identifier = 'v0.0.0';
- $sha = 'SOMESHA';
- $io = $this->getMock('Composer\IO\IOInterface');
- $io->expects($this->any())
- ->method('isInteractive')
- ->will($this->returnValue(true));
- $remoteFilesystem = $this->getMockBuilder('Composer\Util\RemoteFilesystem')
- ->setConstructorArgs(array($io))
- ->getMock();
- $process = $this->getMock('Composer\Util\ProcessExecutor');
- $process->expects($this->any())
- ->method('execute')
- ->will($this->returnValue(1));
- $remoteFilesystem->expects($this->at(0))
- ->method('getContents')
- ->with($this->equalTo('github.com'), $this->equalTo($repoApiUrl), $this->equalTo(false))
- ->will($this->throwException(new TransportException('HTTP/1.1 404 Not Found', 404)));
- $io->expects($this->once())
- ->method('ask')
- ->with($this->equalTo('Username: '))
- ->will($this->returnValue('someuser'));
- $io->expects($this->once())
- ->method('askAndHideAnswer')
- ->with($this->equalTo('Password: '))
- ->will($this->returnValue('somepassword'));
- $io->expects($this->any())
- ->method('setAuthentication')
- ->with($this->equalTo('github.com'), $this->matchesRegularExpression('{someuser|abcdef}'), $this->matchesRegularExpression('{somepassword|x-oauth-basic}'));
- $remoteFilesystem->expects($this->at(1))
- ->method('getContents')
- ->with($this->equalTo('github.com'), $this->equalTo('https://api.github.com/authorizations'), $this->equalTo(false))
- ->will($this->returnValue('[]'));
- $remoteFilesystem->expects($this->at(2))
- ->method('getContents')
- ->with($this->equalTo('github.com'), $this->equalTo('https://api.github.com/authorizations'), $this->equalTo(false))
- ->will($this->returnValue('{"token": "abcdef"}'));
- $remoteFilesystem->expects($this->at(3))
- ->method('getContents')
- ->with($this->equalTo('github.com'), $this->equalTo($repoApiUrl), $this->equalTo(false))
- ->will($this->returnValue('{"master_branch": "test_master", "private": true}'));
- $configSource = $this->getMock('Composer\Config\ConfigSourceInterface');
- $authConfigSource = $this->getMock('Composer\Config\ConfigSourceInterface');
- $this->config->setConfigSource($configSource);
- $this->config->setAuthConfigSource($authConfigSource);
- $repoConfig = array(
- 'url' => $repoUrl,
- );
- $gitHubDriver = new GitHubDriver($repoConfig, $io, $this->config, $process, $remoteFilesystem);
- $gitHubDriver->initialize();
- $this->setAttribute($gitHubDriver, 'tags', array($identifier => $sha));
- $this->assertEquals('test_master', $gitHubDriver->getRootIdentifier());
- $dist = $gitHubDriver->getDist($sha);
- $this->assertEquals('zip', $dist['type']);
- $this->assertEquals('https://api.github.com/repos/composer/packagist/zipball/SOMESHA', $dist['url']);
- $this->assertEquals('SOMESHA', $dist['reference']);
- $source = $gitHubDriver->getSource($sha);
- $this->assertEquals('git', $source['type']);
- $this->assertEquals($repoSshUrl, $source['url']);
- $this->assertEquals('SOMESHA', $source['reference']);
- }
- public function testPublicRepository()
- {
- $repoUrl = 'http://github.com/composer/packagist';
- $repoApiUrl = 'https://api.github.com/repos/composer/packagist';
- $identifier = 'v0.0.0';
- $sha = 'SOMESHA';
- $io = $this->getMock('Composer\IO\IOInterface');
- $io->expects($this->any())
- ->method('isInteractive')
- ->will($this->returnValue(true));
- $remoteFilesystem = $this->getMockBuilder('Composer\Util\RemoteFilesystem')
- ->setConstructorArgs(array($io))
- ->getMock();
- $remoteFilesystem->expects($this->at(0))
- ->method('getContents')
- ->with($this->equalTo('github.com'), $this->equalTo($repoApiUrl), $this->equalTo(false))
- ->will($this->returnValue('{"master_branch": "test_master"}'));
- $repoConfig = array(
- 'url' => $repoUrl,
- );
- $repoUrl = 'https://github.com/composer/packagist.git';
- $gitHubDriver = new GitHubDriver($repoConfig, $io, $this->config, null, $remoteFilesystem);
- $gitHubDriver->initialize();
- $this->setAttribute($gitHubDriver, 'tags', array($identifier => $sha));
- $this->assertEquals('test_master', $gitHubDriver->getRootIdentifier());
- $dist = $gitHubDriver->getDist($sha);
- $this->assertEquals('zip', $dist['type']);
- $this->assertEquals('https://api.github.com/repos/composer/packagist/zipball/SOMESHA', $dist['url']);
- $this->assertEquals($sha, $dist['reference']);
- $source = $gitHubDriver->getSource($sha);
- $this->assertEquals('git', $source['type']);
- $this->assertEquals($repoUrl, $source['url']);
- $this->assertEquals($sha, $source['reference']);
- }
- public function testPublicRepository2()
- {
- $repoUrl = 'http://github.com/composer/packagist';
- $repoApiUrl = 'https://api.github.com/repos/composer/packagist';
- $identifier = 'feature/3.2-foo';
- $sha = 'SOMESHA';
- $io = $this->getMock('Composer\IO\IOInterface');
- $io->expects($this->any())
- ->method('isInteractive')
- ->will($this->returnValue(true));
- $remoteFilesystem = $this->getMockBuilder('Composer\Util\RemoteFilesystem')
- ->setConstructorArgs(array($io))
- ->getMock();
- $remoteFilesystem->expects($this->at(0))
- ->method('getContents')
- ->with($this->equalTo('github.com'), $this->equalTo($repoApiUrl), $this->equalTo(false))
- ->will($this->returnValue('{"master_branch": "test_master"}'));
- $remoteFilesystem->expects($this->at(1))
- ->method('getContents')
- ->with($this->equalTo('github.com'), $this->equalTo('https://api.github.com/repos/composer/packagist/contents/composer.json?ref=feature%2F3.2-foo'), $this->equalTo(false))
- ->will($this->returnValue('{"encoding":"base64","content":"'.base64_encode('{"support": {"source": "'.$repoUrl.'" }}').'"}'));
- $remoteFilesystem->expects($this->at(2))
- ->method('getContents')
- ->with($this->equalTo('github.com'), $this->equalTo('https://api.github.com/repos/composer/packagist/commits/feature%2F3.2-foo'), $this->equalTo(false))
- ->will($this->returnValue('{"commit": {"committer":{ "date": "2012-09-10"}}}'));
- $repoConfig = array(
- 'url' => $repoUrl,
- );
- $repoUrl = 'https://github.com/composer/packagist.git';
- $gitHubDriver = new GitHubDriver($repoConfig, $io, $this->config, null, $remoteFilesystem);
- $gitHubDriver->initialize();
- $this->setAttribute($gitHubDriver, 'tags', array($identifier => $sha));
- $this->assertEquals('test_master', $gitHubDriver->getRootIdentifier());
- $dist = $gitHubDriver->getDist($sha);
- $this->assertEquals('zip', $dist['type']);
- $this->assertEquals('https://api.github.com/repos/composer/packagist/zipball/SOMESHA', $dist['url']);
- $this->assertEquals($sha, $dist['reference']);
- $source = $gitHubDriver->getSource($sha);
- $this->assertEquals('git', $source['type']);
- $this->assertEquals($repoUrl, $source['url']);
- $this->assertEquals($sha, $source['reference']);
- $gitHubDriver->getComposerInformation($identifier);
- }
- public function testPrivateRepositoryNoInteraction()
- {
- $repoUrl = 'http://github.com/composer/packagist';
- $repoApiUrl = 'https://api.github.com/repos/composer/packagist';
- $repoSshUrl = 'git@github.com:composer/packagist.git';
- $identifier = 'v0.0.0';
- $sha = 'SOMESHA';
- $process = $this->getMockBuilder('Composer\Util\ProcessExecutor')
- ->disableOriginalConstructor()
- ->getMock();
- $io = $this->getMock('Composer\IO\IOInterface');
- $io->expects($this->any())
- ->method('isInteractive')
- ->will($this->returnValue(false));
- $remoteFilesystem = $this->getMockBuilder('Composer\Util\RemoteFilesystem')
- ->setConstructorArgs(array($io))
- ->getMock();
- $remoteFilesystem->expects($this->at(0))
- ->method('getContents')
- ->with($this->equalTo('github.com'), $this->equalTo($repoApiUrl), $this->equalTo(false))
- ->will($this->throwException(new TransportException('HTTP/1.1 404 Not Found', 404)));
- // clean local clone if present
- $fs = new Filesystem();
- $fs->removeDirectory(sys_get_temp_dir() . '/composer-test');
- $process->expects($this->at(0))
- ->method('execute')
- ->with($this->equalTo('git config github.accesstoken'))
- ->will($this->returnValue(1));
- $process->expects($this->at(1))
- ->method('execute')
- ->with($this->stringContains($repoSshUrl))
- ->will($this->returnValue(0));
- $process->expects($this->at(2))
- ->method('execute')
- ->with($this->stringContains('git show-ref --tags'));
- $process->expects($this->at(3))
- ->method('splitLines')
- ->will($this->returnValue(array($sha.' refs/tags/'.$identifier)));
- $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(
- 'url' => $repoUrl,
- );
- $gitHubDriver = new GitHubDriver($repoConfig, $io, $this->config, $process, $remoteFilesystem);
- $gitHubDriver->initialize();
- $this->assertEquals('test_master', $gitHubDriver->getRootIdentifier());
- $dist = $gitHubDriver->getDist($sha);
- $this->assertEquals('zip', $dist['type']);
- $this->assertEquals('https://api.github.com/repos/composer/packagist/zipball/SOMESHA', $dist['url']);
- $this->assertEquals($sha, $dist['reference']);
- $source = $gitHubDriver->getSource($identifier);
- $this->assertEquals('git', $source['type']);
- $this->assertEquals($repoSshUrl, $source['url']);
- $this->assertEquals($identifier, $source['reference']);
- $source = $gitHubDriver->getSource($sha);
- $this->assertEquals('git', $source['type']);
- $this->assertEquals($repoSshUrl, $source['url']);
- $this->assertEquals($sha, $source['reference']);
- }
- protected function setAttribute($object, $attribute, $value)
- {
- $attr = new \ReflectionProperty($object, $attribute);
- $attr->setAccessible(true);
- $attr->setValue($object, $value);
- }
- }
|