1
0
Fork 0
composer/tests/Composer/Test/Util/GitHubTest.php

132 lines
3.5 KiB
PHP
Raw Normal View History

2022-02-23 15:58:18 +00:00
<?php declare(strict_types=1);
/*
2015-10-13 09:34:02 +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.
*/
namespace Composer\Test\Util;
use Composer\Util\GitHub;
2020-02-07 03:18:45 +00:00
use Composer\Test\TestCase;
/**
2015-09-28 09:51:14 +00:00
* @author Rob Bast <rob.bast@gmail.com>
*/
class GitHubTest extends TestCase
{
2021-10-27 14:18:46 +00:00
/** @var string */
private $password = 'password';
2021-10-27 14:18:46 +00:00
/** @var string */
private $message = 'mymessage';
2021-10-27 14:18:46 +00:00
/** @var string */
private $origin = 'github.com';
public function testUsernamePasswordAuthenticationFlow(): void
{
$io = $this->getIOMock();
$io->expects([
['text' => $this->message],
['ask' => 'Token (hidden): ', 'reply' => $this->password],
]);
$httpDownloader = $this->getHttpDownloaderMock();
$httpDownloader->expects(
[['url' => sprintf('https://api.%s/', $this->origin), 'body' => '{}']],
true
);
$config = $this->getConfigMock();
$config
->expects($this->exactly(2))
->method('getAuthConfigSource')
->willReturn($this->getAuthJsonMock())
;
$config
->expects($this->once())
->method('getConfigSource')
->willReturn($this->getConfJsonMock())
;
$github = new GitHub($io, $config, null, $httpDownloader);
self::assertTrue($github->authorizeOAuthInteractively($this->origin, $this->message));
}
public function testUsernamePasswordFailure(): void
{
$io = $this->getIOMock();
$io->expects([
['ask' => 'Token (hidden): ', 'reply' => $this->password],
]);
$httpDownloader = $this->getHttpDownloaderMock();
$httpDownloader->expects(
[['url' => sprintf('https://api.%s/', $this->origin), 'status' => 401]],
true
);
$config = $this->getConfigMock();
$config
->expects($this->exactly(1))
->method('getAuthConfigSource')
->willReturn($this->getAuthJsonMock())
;
$github = new GitHub($io, $config, null, $httpDownloader);
self::assertFalse($github->authorizeOAuthInteractively($this->origin));
}
2021-10-27 14:18:46 +00:00
/**
* @return \PHPUnit\Framework\MockObject\MockObject&\Composer\Config
*/
private function getConfigMock()
{
return $this->getMockBuilder('Composer\Config')->getMock();
}
2021-10-27 14:18:46 +00:00
/**
* @return \PHPUnit\Framework\MockObject\MockObject&\Composer\Config\JsonConfigSource
*/
private function getAuthJsonMock()
{
$authjson = $this
->getMockBuilder('Composer\Config\JsonConfigSource')
->disableOriginalConstructor()
->getMock()
;
$authjson
->expects($this->atLeastOnce())
->method('getName')
->willReturn('auth.json')
;
return $authjson;
}
2021-10-27 14:18:46 +00:00
/**
* @return \PHPUnit\Framework\MockObject\MockObject&\Composer\Config\JsonConfigSource
*/
private function getConfJsonMock()
{
$confjson = $this
->getMockBuilder('Composer\Config\JsonConfigSource')
->disableOriginalConstructor()
->getMock()
;
$confjson
->expects($this->atLeastOnce())
->method('removeConfigSetting')
->with('github-oauth.'.$this->origin)
;
return $confjson;
}
}