4 * This file is part of the Fxp Composer Asset Plugin package.
6 * (c) François Pluchino <francois.pluchino@gmail.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Fxp\Composer\AssetPlugin\Tests\Repository\Vcs;
15 use Composer\Downloader\TransportException;
16 use Composer\IO\IOInterface;
17 use Composer\Util\Filesystem;
18 use Composer\Util\RemoteFilesystem;
19 use Fxp\Composer\AssetPlugin\Repository\Vcs\HgBitbucketDriver;
22 * Tests of vcs mercurial bitbucket repository.
24 * @author François Pluchino <francois.pluchino@gmail.com>
26 class HgBitbucketDriverTest extends \PHPUnit_Framework_TestCase
33 public function setUp()
35 $this->config = new Config();
36 $this->config->merge(array(
38 'home' => sys_get_temp_dir().'/composer-test',
39 'cache-repo-dir' => sys_get_temp_dir().'/composer-test-cache',
44 public function tearDown()
46 $fs = new Filesystem();
47 $fs->removeDirectory(sys_get_temp_dir().'/composer-test');
48 $fs->removeDirectory(sys_get_temp_dir().'/composer-test-cache');
51 public function getAssetTypes()
54 array('npm', 'package.json'),
55 array('bower', 'bower.json'),
60 * @dataProvider getAssetTypes
63 * @param string $filename
65 public function testPublicRepositoryWithComposer($type, $filename)
67 $repoUrl = 'https://bitbucket.org/composer-test/repo-name';
68 $identifier = 'v0.0.0';
71 $io = $this->getMockBuilder('Composer\IO\IOInterface')->getMock();
72 $io->expects($this->any())
73 ->method('isInteractive')
74 ->will($this->returnValue(true));
76 $remoteFilesystem = $this->getMockBuilder('Composer\Util\RemoteFilesystem')
77 ->setConstructorArgs(array($io))
80 $remoteFilesystem->expects($this->any())
81 ->method('getContents')
85 'https://api.bitbucket.org/2.0/repositories/composer-test/repo-name?fields=-project%2C-owner',
90 'https://api.bitbucket.org/1.0/repositories/composer-test/repo-name/main-branch',
95 'https://bitbucket.org/composer-test/repo-name/raw/v0.0.0/'.$filename,
99 ->willReturnOnConsecutiveCalls(
100 '{"scm":"hg","website":"","has_wiki":false,"name":"repo","links":{"branches":{"href":"https:\/\/api.bitbucket.org\/2.0\/repositories\/composer-test\/repo-name\/refs\/branches"},"tags":{"href":"https:\/\/api.bitbucket.org\/2.0\/repositories\/composer-test\/repo-name\/refs\/tags"},"clone":[{"href":"https:\/\/user@bitbucket.org\/composer-test\/repo-name","name":"https"}],"html":{"href":"https:\/\/bitbucket.org\/composer-test\/repo-name"}},"language":"php","created_on":"2015-02-18T16:22:24.688+00:00","updated_on":"2016-05-17T13:20:21.993+00:00","is_private":true,"has_issues":false}',
101 '{"name": "test_master"}',
102 '{"name": "composer-test/repo-name","description": "test repo","license": "GPL","authors": [{"name": "Name","email": "local@domain.tld"}],"require": {"creator/package": "^1.0"},"require-dev": {"phpunit/phpunit": "~4.8"}}'
107 'asset-type' => $type,
108 'filename' => $filename,
111 /* @var IOInterface $io */
112 /* @var RemoteFilesystem $remoteFilesystem */
114 $driver = new HgBitbucketDriver($repoConfig, $io, $this->config, null, $remoteFilesystem);
115 $driver->initialize();
116 $this->setAttribute($driver, 'tags', array($identifier => $sha));
118 $this->assertEquals('test_master', $driver->getRootIdentifier());
120 $dist = $driver->getDist($sha);
121 $this->assertEquals('zip', $dist['type']);
122 $this->assertEquals($this->getScheme($repoUrl).'/get/SOMESHA.zip', $dist['url']);
123 $this->assertEquals($sha, $dist['reference']);
125 $source = $driver->getSource($sha);
126 $this->assertEquals('hg', $source['type']);
127 $this->assertEquals($repoUrl, $source['url']);
128 $this->assertEquals($sha, $source['reference']);
130 $driver->getComposerInformation($identifier);
134 * @dataProvider getAssetTypes
136 * @param string $type
137 * @param string $filename
139 public function testPublicRepositoryWithEmptyComposer($type, $filename)
141 $repoUrl = 'https://bitbucket.org/composer-test/repo-name';
142 $identifier = 'v0.0.0';
143 $io = $this->getMockBuilder('Composer\IO\IOInterface')->getMock();
145 $remoteFilesystem = $this->getMockBuilder('Composer\Util\RemoteFilesystem')
146 ->setConstructorArgs(array($io))
149 $remoteFilesystem->expects($this->at(0))
150 ->method('getContents')
151 ->with($this->equalTo('bitbucket.org'), $this->equalTo($this->getScheme($repoUrl).'/raw/'.$identifier.'/'.$filename), $this->equalTo(false))
152 ->will($this->throwException(new TransportException('Not Found', 404)));
156 'asset-type' => $type,
157 'filename' => $filename,
160 /* @var IOInterface $io */
161 /* @var RemoteFilesystem $remoteFilesystem */
163 $driver = new HgBitbucketDriver($repoConfig, $io, $this->config, null, $remoteFilesystem);
164 $driver->initialize();
167 '_nonexistent_package' => true,
170 $this->assertSame($validEmpty, $driver->getComposerInformation($identifier));
174 * @param object $object
175 * @param string $attribute
176 * @param mixed $value
178 protected function setAttribute($object, $attribute, $value)
180 $attr = new \ReflectionProperty($object, $attribute);
181 $attr->setAccessible(true);
182 $attr->setValue($object, $value);
186 * Get the url with https or http protocol depending on SSL support.
190 * @return string The correct url
192 protected function getScheme($url)
194 if (extension_loaded('openssl')) {
198 return str_replace('https:', 'http:', $url);