]> git.mxchange.org Git - friendica-addons.git/blob
ad5599a1b5e2cd7c96c5bf201ddf2fa15ea2f3e4
[friendica-addons.git] /
1 <?php
2
3 /*
4  * This file is part of the Fxp Composer Asset Plugin package.
5  *
6  * (c) François Pluchino <francois.pluchino@gmail.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Fxp\Composer\AssetPlugin\Tests\Repository\Vcs;
13
14 use Composer\Config;
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;
20
21 /**
22  * Tests of vcs mercurial bitbucket repository.
23  *
24  * @author François Pluchino <francois.pluchino@gmail.com>
25  */
26 class HgBitbucketDriverTest extends \PHPUnit_Framework_TestCase
27 {
28     /**
29      * @var Config
30      */
31     private $config;
32
33     public function setUp()
34     {
35         $this->config = new Config();
36         $this->config->merge(array(
37             'config' => array(
38                 'home' => sys_get_temp_dir().'/composer-test',
39                 'cache-repo-dir' => sys_get_temp_dir().'/composer-test-cache',
40             ),
41         ));
42     }
43
44     public function tearDown()
45     {
46         $fs = new Filesystem();
47         $fs->removeDirectory(sys_get_temp_dir().'/composer-test');
48         $fs->removeDirectory(sys_get_temp_dir().'/composer-test-cache');
49     }
50
51     public function getAssetTypes()
52     {
53         return array(
54             array('npm', 'package.json'),
55             array('bower', 'bower.json'),
56         );
57     }
58
59     /**
60      * @dataProvider getAssetTypes
61      *
62      * @param string $type
63      * @param string $filename
64      */
65     public function testPublicRepositoryWithComposer($type, $filename)
66     {
67         $repoUrl = 'https://bitbucket.org/composer-test/repo-name';
68         $identifier = 'v0.0.0';
69         $sha = 'SOMESHA';
70
71         $io = $this->getMockBuilder('Composer\IO\IOInterface')->getMock();
72         $io->expects($this->any())
73             ->method('isInteractive')
74             ->will($this->returnValue(true));
75
76         $remoteFilesystem = $this->getMockBuilder('Composer\Util\RemoteFilesystem')
77             ->setConstructorArgs(array($io))
78             ->getMock();
79
80         $remoteFilesystem->expects($this->any())
81             ->method('getContents')
82             ->withConsecutive(
83                 array(
84                     'bitbucket.org',
85                     'https://api.bitbucket.org/2.0/repositories/composer-test/repo-name?fields=-project%2C-owner',
86                     false,
87                 ),
88                 array(
89                     'bitbucket.org',
90                     'https://api.bitbucket.org/1.0/repositories/composer-test/repo-name/main-branch',
91                     false,
92                 ),
93                 array(
94                     'bitbucket.org',
95                     'https://bitbucket.org/composer-test/repo-name/raw/v0.0.0/'.$filename,
96                     false,
97                 )
98             )
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"}}'
103             );
104
105         $repoConfig = array(
106             'url' => $repoUrl,
107             'asset-type' => $type,
108             'filename' => $filename,
109         );
110
111         /* @var IOInterface $io */
112         /* @var RemoteFilesystem $remoteFilesystem */
113
114         $driver = new HgBitbucketDriver($repoConfig, $io, $this->config, null, $remoteFilesystem);
115         $driver->initialize();
116         $this->setAttribute($driver, 'tags', array($identifier => $sha));
117
118         $this->assertEquals('test_master', $driver->getRootIdentifier());
119
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']);
124
125         $source = $driver->getSource($sha);
126         $this->assertEquals('hg', $source['type']);
127         $this->assertEquals($repoUrl, $source['url']);
128         $this->assertEquals($sha, $source['reference']);
129
130         $driver->getComposerInformation($identifier);
131     }
132
133     /**
134      * @dataProvider getAssetTypes
135      *
136      * @param string $type
137      * @param string $filename
138      */
139     public function testPublicRepositoryWithEmptyComposer($type, $filename)
140     {
141         $repoUrl = 'https://bitbucket.org/composer-test/repo-name';
142         $identifier = 'v0.0.0';
143         $io = $this->getMockBuilder('Composer\IO\IOInterface')->getMock();
144
145         $remoteFilesystem = $this->getMockBuilder('Composer\Util\RemoteFilesystem')
146             ->setConstructorArgs(array($io))
147             ->getMock();
148
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)));
153
154         $repoConfig = array(
155             'url' => $repoUrl,
156             'asset-type' => $type,
157             'filename' => $filename,
158         );
159
160         /* @var IOInterface $io */
161         /* @var RemoteFilesystem $remoteFilesystem */
162
163         $driver = new HgBitbucketDriver($repoConfig, $io, $this->config, null, $remoteFilesystem);
164         $driver->initialize();
165
166         $validEmpty = array(
167             '_nonexistent_package' => true,
168         );
169
170         $this->assertSame($validEmpty, $driver->getComposerInformation($identifier));
171     }
172
173     /**
174      * @param object $object
175      * @param string $attribute
176      * @param mixed  $value
177      */
178     protected function setAttribute($object, $attribute, $value)
179     {
180         $attr = new \ReflectionProperty($object, $attribute);
181         $attr->setAccessible(true);
182         $attr->setValue($object, $value);
183     }
184
185     /**
186      * Get the url with https or http protocol depending on SSL support.
187      *
188      * @param string $url
189      *
190      * @return string The correct url
191      */
192     protected function getScheme($url)
193     {
194         if (extension_loaded('openssl')) {
195             return $url;
196         }
197
198         return str_replace('https:', 'http:', $url);
199     }
200 }