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\GitBitbucketDriver;
22 * Tests of vcs git bitbucket repository.
24 * @author François Pluchino <francois.pluchino@gmail.com>
26 class GitBitbucketDriverTest 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 $repoBaseUrl = 'https://bitbucket.org/composer-test/repo-name';
68 $repoUrl = $repoBaseUrl.'.git';
69 $identifier = 'v0.0.0';
72 $io = $this->getMockBuilder('Composer\IO\IOInterface')->getMock();
73 $io->expects($this->any())
74 ->method('isInteractive')
75 ->will($this->returnValue(true));
77 $remoteFilesystem = $this->getMockBuilder('Composer\Util\RemoteFilesystem')
78 ->setConstructorArgs(array($io))
81 $remoteFilesystem->expects($this->any())
82 ->method('getContents')
86 'https://api.bitbucket.org/2.0/repositories/composer-test/repo-name?fields=-project%2C-owner',
91 'https://api.bitbucket.org/1.0/repositories/composer-test/repo-name/main-branch',
96 'https://api.bitbucket.org/1.0/repositories/composer-test/repo-name/src/v0.0.0/'.$filename,
100 ->willReturnOnConsecutiveCalls(
101 '{"scm":"git","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.git","name":"https"},{"href":"ssh:\/\/git@bitbucket.org\/composer-test\/repo-name.git","name":"ssh"}],"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}',
102 '{"name": "test_master"}',
103 '{"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"}}'
108 'asset-type' => $type,
109 'filename' => $filename,
112 /* @var IOInterface $io */
113 /* @var RemoteFilesystem $remoteFilesystem */
115 $driver = new GitBitbucketDriver($repoConfig, $io, $this->config, null, $remoteFilesystem);
116 $driver->initialize();
118 $this->assertEquals('test_master', $driver->getRootIdentifier());
120 $dist = $driver->getDist($sha);
121 $this->assertEquals('zip', $dist['type']);
122 $this->assertEquals($this->getScheme($repoBaseUrl).'/get/SOMESHA.zip', $dist['url']);
123 $this->assertEquals($sha, $dist['reference']);
125 $source = $driver->getSource($sha);
126 $this->assertEquals('git', $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 $repoBaseUrl = 'https://bitbucket.org/composer-test/repo-name';
142 $repoUrl = $repoBaseUrl.'.git';
143 $repoApiUrl = 'https://api.bitbucket.org/1.0/repositories/composer-test/repo-name';
144 $identifier = 'v0.0.0';
145 $io = $this->getMockBuilder('Composer\IO\IOInterface')->getMock();
147 $remoteFilesystem = $this->getMockBuilder('Composer\Util\RemoteFilesystem')
148 ->setConstructorArgs(array($io))
151 $remoteFilesystem->expects($this->at(0))
152 ->method('getContents')
154 $this->equalTo('bitbucket.org'),
155 $this->equalTo($repoApiUrl.'/src/'.$identifier.'/'.$filename),
156 $this->equalTo(false)
158 ->will($this->throwException(new TransportException('Not Found', 404)));
162 'asset-type' => $type,
163 'filename' => $filename,
166 /* @var IOInterface $io */
167 /* @var RemoteFilesystem $remoteFilesystem */
169 $driver = new GitBitbucketDriver($repoConfig, $io, $this->config, null, $remoteFilesystem);
170 $driver->initialize();
173 '_nonexistent_package' => true,
176 $this->assertSame($validEmpty, $driver->getComposerInformation($identifier));
180 * @param object $object
181 * @param string $attribute
182 * @param mixed $value
184 protected function setAttribute($object, $attribute, $value)
186 $attr = new \ReflectionProperty($object, $attribute);
187 $attr->setAccessible(true);
188 $attr->setValue($object, $value);
192 * Creates the json composer content.
194 * @param array $content The composer content
195 * @param string $name The name of repository
197 * @return string The json content
199 protected function createJsonComposer(array $content, $name = 'repo-name')
201 return json_encode(array_merge_recursive($content, array(
207 * @param array $content The composer content
208 * @param string $name The name of repository
210 * @return string The API return value with the json content
212 protected function createApiJsonWithRepoData(array $content, $name = 'repo-name')
214 $composerContent = $this->createJsonComposer($content, $name);
218 'node' => 'nodename',
219 'path' => '/path/to/file',
220 'data' => $composerContent,
221 'size' => strlen($composerContent),
227 * Get the url with https or http protocol depending on SSL support.
231 * @return string The correct url
233 protected function getScheme($url)
235 if (extension_loaded('openssl')) {
239 return str_replace('https:', 'http:', $url);