]> git.mxchange.org Git - friendica-addons.git/blob
d317c0a6ef1b2364b229201b6e5d5ccb3bfe3dd2
[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\GitBitbucketDriver;
20
21 /**
22  * Tests of vcs git bitbucket repository.
23  *
24  * @author François Pluchino <francois.pluchino@gmail.com>
25  */
26 class GitBitbucketDriverTest 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         $repoBaseUrl = 'https://bitbucket.org/composer-test/repo-name';
68         $repoUrl = $repoBaseUrl.'.git';
69         $identifier = 'v0.0.0';
70         $sha = 'SOMESHA';
71
72         $io = $this->getMockBuilder('Composer\IO\IOInterface')->getMock();
73         $io->expects($this->any())
74             ->method('isInteractive')
75             ->will($this->returnValue(true));
76
77         $remoteFilesystem = $this->getMockBuilder('Composer\Util\RemoteFilesystem')
78             ->setConstructorArgs(array($io))
79             ->getMock();
80
81         $remoteFilesystem->expects($this->any())
82             ->method('getContents')
83             ->withConsecutive(
84                 array(
85                     'bitbucket.org',
86                     'https://api.bitbucket.org/2.0/repositories/composer-test/repo-name?fields=-project%2C-owner',
87                     false,
88                 ),
89                 array(
90                     'bitbucket.org',
91                     'https://api.bitbucket.org/1.0/repositories/composer-test/repo-name/main-branch',
92                     false,
93                 ),
94                 array(
95                     'bitbucket.org',
96                     'https://api.bitbucket.org/1.0/repositories/composer-test/repo-name/src/v0.0.0/'.$filename,
97                     false,
98                 )
99             )
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"}}'
104             );
105
106         $repoConfig = array(
107             'url' => $repoUrl,
108             'asset-type' => $type,
109             'filename' => $filename,
110         );
111
112         /* @var IOInterface $io */
113         /* @var RemoteFilesystem $remoteFilesystem */
114
115         $driver = new GitBitbucketDriver($repoConfig, $io, $this->config, null, $remoteFilesystem);
116         $driver->initialize();
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($repoBaseUrl).'/get/SOMESHA.zip', $dist['url']);
123         $this->assertEquals($sha, $dist['reference']);
124
125         $source = $driver->getSource($sha);
126         $this->assertEquals('git', $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         $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();
146
147         $remoteFilesystem = $this->getMockBuilder('Composer\Util\RemoteFilesystem')
148             ->setConstructorArgs(array($io))
149             ->getMock();
150
151         $remoteFilesystem->expects($this->at(0))
152             ->method('getContents')
153             ->with(
154                 $this->equalTo('bitbucket.org'),
155                 $this->equalTo($repoApiUrl.'/src/'.$identifier.'/'.$filename),
156                 $this->equalTo(false)
157             )
158             ->will($this->throwException(new TransportException('Not Found', 404)));
159
160         $repoConfig = array(
161             'url' => $repoUrl,
162             'asset-type' => $type,
163             'filename' => $filename,
164         );
165
166         /* @var IOInterface $io */
167         /* @var RemoteFilesystem $remoteFilesystem */
168
169         $driver = new GitBitbucketDriver($repoConfig, $io, $this->config, null, $remoteFilesystem);
170         $driver->initialize();
171
172         $validEmpty = array(
173             '_nonexistent_package' => true,
174         );
175
176         $this->assertSame($validEmpty, $driver->getComposerInformation($identifier));
177     }
178
179     /**
180      * @param object $object
181      * @param string $attribute
182      * @param mixed  $value
183      */
184     protected function setAttribute($object, $attribute, $value)
185     {
186         $attr = new \ReflectionProperty($object, $attribute);
187         $attr->setAccessible(true);
188         $attr->setValue($object, $value);
189     }
190
191     /**
192      * Creates the json composer content.
193      *
194      * @param array  $content The composer content
195      * @param string $name    The name of repository
196      *
197      * @return string The json content
198      */
199     protected function createJsonComposer(array $content, $name = 'repo-name')
200     {
201         return json_encode(array_merge_recursive($content, array(
202             'name' => $name,
203         )));
204     }
205
206     /**
207      * @param array  $content The composer content
208      * @param string $name    The name of repository
209      *
210      * @return string The API return value with the json content
211      */
212     protected function createApiJsonWithRepoData(array $content, $name = 'repo-name')
213     {
214         $composerContent = $this->createJsonComposer($content, $name);
215
216         return json_encode(
217             array(
218                 'node' => 'nodename',
219                 'path' => '/path/to/file',
220                 'data' => $composerContent,
221                 'size' => strlen($composerContent),
222             )
223         );
224     }
225
226     /**
227      * Get the url with https or http protocol depending on SSL support.
228      *
229      * @param string $url
230      *
231      * @return string The correct url
232      */
233     protected function getScheme($url)
234     {
235         if (extension_loaded('openssl')) {
236             return $url;
237         }
238
239         return str_replace('https:', 'http:', $url);
240     }
241 }