2022-02-23 15:58:18 +00:00
< ? php declare ( strict_types = 1 );
2016-06-11 15:25:59 +00:00
/*
* 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 .
*/
2016-10-05 08:45:22 +00:00
namespace Composer\Test\Repository\Vcs ;
2016-06-11 15:25:59 +00:00
use Composer\Config ;
2016-10-05 08:45:22 +00:00
use Composer\Repository\Vcs\GitBitbucketDriver ;
2018-11-12 14:23:32 +00:00
use Composer\Test\TestCase ;
2016-06-11 15:25:59 +00:00
use Composer\Util\Filesystem ;
2018-11-12 14:34:54 +00:00
use Composer\Util\ProcessExecutor ;
2018-10-31 11:44:54 +00:00
use Composer\Util\Http\Response ;
2016-06-11 15:25:59 +00:00
2016-06-17 15:35:43 +00:00
/**
* @ group bitbucket
*/
2016-06-11 15:25:59 +00:00
class GitBitbucketDriverTest extends TestCase
{
2021-10-16 08:16:06 +00:00
/** @var \Composer\IO\IOInterface&\PHPUnit\Framework\MockObject\MockObject */
2016-06-11 15:25:59 +00:00
private $io ;
2021-10-16 08:16:06 +00:00
/** @var Config */
2016-06-11 15:25:59 +00:00
private $config ;
2021-10-16 08:16:06 +00:00
/** @var \Composer\Util\HttpDownloader&\PHPUnit\Framework\MockObject\MockObject */
2018-10-31 11:44:54 +00:00
private $httpDownloader ;
2021-10-16 08:16:06 +00:00
/** @var string */
2016-06-11 15:25:59 +00:00
private $home ;
2021-12-08 16:03:05 +00:00
protected function setUp () : void
2016-06-11 15:25:59 +00:00
{
2018-04-12 08:24:56 +00:00
$this -> io = $this -> getMockBuilder ( 'Composer\IO\IOInterface' ) -> getMock ();
2016-06-11 15:25:59 +00:00
2022-05-11 14:05:35 +00:00
$this -> home = self :: getUniqueTmpDirectory ();
2016-06-11 15:25:59 +00:00
$this -> config = new Config ();
2022-08-17 12:20:07 +00:00
$this -> config -> merge ([
'config' => [
2016-06-11 15:25:59 +00:00
'home' => $this -> home ,
2022-08-17 12:20:07 +00:00
],
]);
2016-06-11 15:25:59 +00:00
2018-10-31 11:44:54 +00:00
$this -> httpDownloader = $this -> getMockBuilder ( 'Composer\Util\HttpDownloader' )
2016-06-11 15:25:59 +00:00
-> disableOriginalConstructor ()
-> getMock ();
}
2021-12-09 16:09:07 +00:00
protected function tearDown () : void
2016-06-11 15:25:59 +00:00
{
2021-12-09 16:09:07 +00:00
parent :: tearDown ();
2016-06-11 15:25:59 +00:00
$fs = new Filesystem ;
$fs -> removeDirectory ( $this -> home );
}
/**
2021-10-27 13:29:52 +00:00
* @ param array < string , mixed > $repoConfig
*
* @ phpstan - param array { url : string } & array < string , mixed > $repoConfig
2016-06-11 15:25:59 +00:00
*/
2022-02-18 10:22:01 +00:00
private function getDriver ( array $repoConfig ) : GitBitbucketDriver
2016-06-11 15:25:59 +00:00
{
$driver = new GitBitbucketDriver (
$repoConfig ,
$this -> io ,
$this -> config ,
2018-11-12 14:34:54 +00:00
$this -> httpDownloader ,
new ProcessExecutor ( $this -> io )
2016-06-11 15:25:59 +00:00
);
$driver -> initialize ();
return $driver ;
}
2022-02-18 09:38:54 +00:00
public function testGetRootIdentifierWrongScmType () : void
2016-06-11 15:25:59 +00:00
{
2021-12-09 19:55:26 +00:00
self :: expectException ( 'RuntimeException' );
self :: expectExceptionMessage ( 'https://bitbucket.org/user/repo.git does not appear to be a git repository, use https://bitbucket.org/user/repo but remember that Bitbucket no longer supports the mercurial repositories. https://bitbucket.org/blog/sunsetting-mercurial-support-in-bitbucket' );
2016-06-11 15:25:59 +00:00
2018-10-31 11:44:54 +00:00
$this -> httpDownloader -> expects ( $this -> once ())
-> method ( 'get' )
2016-06-11 15:25:59 +00:00
-> with (
2018-10-31 11:44:54 +00:00
$url = 'https://api.bitbucket.org/2.0/repositories/user/repo?fields=-project%2C-owner' ,
2022-08-17 12:20:07 +00:00
[]
2016-06-11 15:25:59 +00:00
)
-> willReturn (
2022-08-17 12:20:07 +00:00
new Response ([ 'url' => $url ], 200 , [], '{"scm":"hg","website":"","has_wiki":false,"name":"repo","links":{"branches":{"href":"https:\/\/api.bitbucket.org\/2.0\/repositories\/user\/repo\/refs\/branches"},"tags":{"href":"https:\/\/api.bitbucket.org\/2.0\/repositories\/user\/repo\/refs\/tags"},"clone":[{"href":"https:\/\/user@bitbucket.org\/user\/repo","name":"https"},{"href":"ssh:\/\/hg@bitbucket.org\/user\/repo","name":"ssh"}],"html":{"href":"https:\/\/bitbucket.org\/user\/repo"}},"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}' )
2016-06-11 15:25:59 +00:00
);
2022-08-17 12:20:07 +00:00
$driver = $this -> getDriver ([ 'url' => 'https://bitbucket.org/user/repo.git' ]);
2016-06-11 15:25:59 +00:00
2016-12-27 21:16:32 +00:00
$driver -> getRootIdentifier ();
2016-06-11 15:25:59 +00:00
}
2022-02-22 15:47:09 +00:00
public function testDriver () : GitBitbucketDriver
2016-06-11 15:25:59 +00:00
{
2022-08-17 12:20:07 +00:00
$driver = $this -> getDriver ([ 'url' => 'https://bitbucket.org/user/repo.git' ]);
2016-06-11 15:25:59 +00:00
2022-08-17 12:20:07 +00:00
$urls = [
2018-10-31 11:44:54 +00:00
'https://api.bitbucket.org/2.0/repositories/user/repo?fields=-project%2C-owner' ,
'https://api.bitbucket.org/2.0/repositories/user/repo/refs/tags?pagelen=100&fields=values.name%2Cvalues.target.hash%2Cnext&sort=-target.date' ,
'https://api.bitbucket.org/2.0/repositories/user/repo/refs/branches?pagelen=100&fields=values.name%2Cvalues.target.hash%2Cvalues.heads%2Cnext&sort=-target.date' ,
2022-04-14 08:53:26 +00:00
'https://api.bitbucket.org/2.0/repositories/user/repo/src/main/composer.json' ,
'https://api.bitbucket.org/2.0/repositories/user/repo/commit/main?fields=date' ,
2022-08-17 12:20:07 +00:00
];
2018-10-31 11:44:54 +00:00
$this -> httpDownloader -> expects ( $this -> any ())
-> method ( 'get' )
2016-06-11 15:25:59 +00:00
-> withConsecutive (
2022-08-17 12:20:07 +00:00
[
$urls [ 0 ], [],
],
[
$urls [ 1 ], [],
],
[
$urls [ 2 ], [],
],
[
$urls [ 3 ], [],
],
[
$urls [ 4 ], [],
]
2016-06-11 15:25:59 +00:00
)
-> willReturnOnConsecutiveCalls (
2022-08-17 12:20:07 +00:00
new Response ([ 'url' => $urls [ 0 ]], 200 , [], '{"mainbranch": {"name": "main"}, "scm":"git","website":"","has_wiki":false,"name":"repo","links":{"branches":{"href":"https:\/\/api.bitbucket.org\/2.0\/repositories\/user\/repo\/refs\/branches"},"tags":{"href":"https:\/\/api.bitbucket.org\/2.0\/repositories\/user\/repo\/refs\/tags"},"clone":[{"href":"https:\/\/user@bitbucket.org\/user\/repo.git","name":"https"},{"href":"ssh:\/\/git@bitbucket.org\/user\/repo.git","name":"ssh"}],"html":{"href":"https:\/\/bitbucket.org\/user\/repo"}},"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}' ),
new Response ([ 'url' => $urls [ 1 ]], 200 , [], '{"values":[{"name":"1.0.1","target":{"hash":"9b78a3932143497c519e49b8241083838c8ff8a1"}},{"name":"1.0.0","target":{"hash":"d3393d514318a9267d2f8ebbf463a9aaa389f8eb"}}]}' ),
new Response ([ 'url' => $urls [ 2 ]], 200 , [], '{"values":[{"name":"main","target":{"hash":"937992d19d72b5116c3e8c4a04f960e5fa270b22"}}]}' ),
new Response ([ 'url' => $urls [ 3 ]], 200 , [], '{"name": "user/repo","description": "test repo","license": "GPL","authors": [{"name": "Name","email": "local@domain.tld"}],"require": {"creator/package": "^1.0"},"require-dev": {"phpunit/phpunit": "~4.8"}}' ),
new Response ([ 'url' => $urls [ 4 ]], 200 , [], '{"date": "2016-05-17T13:19:52+00:00"}' )
2016-06-11 15:25:59 +00:00
);
2016-12-27 21:16:32 +00:00
$this -> assertEquals (
2022-04-14 08:53:26 +00:00
'main' ,
2016-12-27 21:16:32 +00:00
$driver -> getRootIdentifier ()
);
$this -> assertEquals (
2022-08-17 12:20:07 +00:00
[
2016-12-27 21:16:32 +00:00
'1.0.1' => '9b78a3932143497c519e49b8241083838c8ff8a1' ,
2017-03-08 14:07:29 +00:00
'1.0.0' => 'd3393d514318a9267d2f8ebbf463a9aaa389f8eb' ,
2022-08-17 12:20:07 +00:00
],
2016-12-27 21:16:32 +00:00
$driver -> getTags ()
);
$this -> assertEquals (
2022-08-17 12:20:07 +00:00
[
2022-04-14 08:53:26 +00:00
'main' => '937992d19d72b5116c3e8c4a04f960e5fa270b22' ,
2022-08-17 12:20:07 +00:00
],
2016-12-27 21:16:32 +00:00
$driver -> getBranches ()
);
2016-06-11 15:25:59 +00:00
$this -> assertEquals (
2022-08-17 12:20:07 +00:00
[
2016-06-11 15:25:59 +00:00
'name' => 'user/repo' ,
'description' => 'test repo' ,
'license' => 'GPL' ,
2022-08-17 12:20:07 +00:00
'authors' => [
[
2016-06-11 15:25:59 +00:00
'name' => 'Name' ,
2017-03-08 14:07:29 +00:00
'email' => 'local@domain.tld' ,
2022-08-17 12:20:07 +00:00
],
],
'require' => [
2017-03-08 14:07:29 +00:00
'creator/package' => '^1.0' ,
2022-08-17 12:20:07 +00:00
],
'require-dev' => [
2017-03-08 14:07:29 +00:00
'phpunit/phpunit' => '~4.8' ,
2022-08-17 12:20:07 +00:00
],
2017-06-07 19:29:15 +00:00
'time' => '2016-05-17T13:19:52+00:00' ,
2022-08-17 12:20:07 +00:00
'support' => [
2022-04-14 08:53:26 +00:00
'source' => 'https://bitbucket.org/user/repo/src/937992d19d72b5116c3e8c4a04f960e5fa270b22/?at=main' ,
2022-08-17 12:20:07 +00:00
],
2017-03-08 14:07:29 +00:00
'homepage' => 'https://bitbucket.org/user/repo' ,
2022-08-17 12:20:07 +00:00
],
2022-04-14 08:53:26 +00:00
$driver -> getComposerInformation ( 'main' )
2016-06-11 15:25:59 +00:00
);
2016-12-27 21:16:32 +00:00
return $driver ;
2016-06-11 15:25:59 +00:00
}
2016-12-27 21:16:32 +00:00
/**
* @ depends testDriver
*/
2022-02-22 15:47:09 +00:00
public function testGetParams ( \Composer\Repository\Vcs\VcsDriverInterface $driver ) : void
2016-06-11 15:25:59 +00:00
{
2016-12-27 21:16:32 +00:00
$url = 'https://bitbucket.org/user/repo.git' ;
2016-06-11 15:25:59 +00:00
2016-12-27 21:16:32 +00:00
$this -> assertEquals ( $url , $driver -> getUrl ());
2016-06-11 15:25:59 +00:00
$this -> assertEquals (
2022-08-17 12:20:07 +00:00
[
2016-12-27 21:16:32 +00:00
'type' => 'zip' ,
'url' => 'https://bitbucket.org/user/repo/get/reference.zip' ,
'reference' => 'reference' ,
2017-03-08 14:07:29 +00:00
'shasum' => '' ,
2022-08-17 12:20:07 +00:00
],
2016-12-27 21:16:32 +00:00
$driver -> getDist ( 'reference' )
2016-06-11 15:25:59 +00:00
);
$this -> assertEquals (
2022-08-17 12:20:07 +00:00
[ 'type' => 'git' , 'url' => $url , 'reference' => 'reference' ],
2016-12-27 21:16:32 +00:00
$driver -> getSource ( 'reference' )
2016-06-11 15:25:59 +00:00
);
}
2022-02-18 09:38:54 +00:00
public function testInitializeInvalidRepositoryUrl () : void
2022-02-16 08:37:36 +00:00
{
2022-02-16 12:35:30 +00:00
$this -> expectException ( '\InvalidArgumentException' );
2022-02-16 08:37:36 +00:00
2022-08-17 12:20:07 +00:00
$driver = $this -> getDriver ([ 'url' => 'https://bitbucket.org/acme' ]);
2022-02-16 08:37:36 +00:00
$driver -> initialize ();
}
2022-02-18 09:38:54 +00:00
public function testSupports () : void
2016-06-11 15:25:59 +00:00
{
$this -> assertTrue (
GitBitbucketDriver :: supports ( $this -> io , $this -> config , 'https://bitbucket.org/user/repo.git' )
);
2020-11-04 20:24:30 +00:00
// should not be changed, see https://github.com/composer/composer/issues/9400
$this -> assertFalse (
2016-06-11 15:25:59 +00:00
GitBitbucketDriver :: supports ( $this -> io , $this -> config , 'git@bitbucket.org:user/repo.git' )
);
$this -> assertFalse (
GitBitbucketDriver :: supports ( $this -> io , $this -> config , 'https://github.com/user/repo.git' )
);
}
}