Skip to content

Commit 48f377e

Browse files
author
Frédéric Dewinne
authored
add support for CPHP_TOKEN env variable (#4)
* add support for CPHP_TOKEN env variable * add documentation
1 parent de8410d commit 48f377e

File tree

5 files changed

+29
-4
lines changed

5 files changed

+29
-4
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ require 'vendor/autoload.php';
3131

3232
$service = Continuous\Sdk\Service::factory(['token' => 'my-access-token']);
3333
```
34+
Another way to use your access token consist in declaring it in `CPHP_TOKEN` environment variable
3435

3536
### Get your repository list
3637
```php

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
"require-dev": {
1515
"phpunit/phpunit": "^4.6",
1616
"squizlabs/php_codesniffer": "^2.3",
17-
"behat/behat": "^3.0"
17+
"behat/behat": "^3.0",
18+
"php-mock/php-mock-phpunit": "^1.1"
1819
},
1920
"require": {
2021
"php": ">=5.4.0",

phpcs.xml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?xml version="1.0"?>
2-
<ruleset name="continuousphp-phing-tasks">
2+
<ruleset name="continuousphp-sdk">
33
<description>PSR2 Coding standard</description>
44
<rule ref="PSR2"/>
5+
<file>src</file>
6+
<file>tests</file>
57
</ruleset>

src/Service.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ public static function getHttpClient(array $config = array())
7474

7575
if (isset($config['token'])) {
7676
$args['defaults']['headers']['Authorization'] = "Bearer " . $config['token'];
77+
} elseif ($token = getenv('CPHP_TOKEN')) {
78+
$args['defaults']['headers']['Authorization'] = "Bearer " . $token;
7779
}
7880

7981
return new $className($args);

tests/ServiceTest.php

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Continuous\Tests\Sdk;
1313

1414
use Continuous\Sdk\Service;
15+
use phpmock\phpunit\PHPMock;
1516

1617
/**
1718
* ServiceTest
@@ -22,6 +23,8 @@
2223
*/
2324
class ServiceTest extends \PHPUnit_Framework_TestCase
2425
{
26+
use PHPMock;
27+
2528
protected $httpClientMock;
2629
protected $originalHttpClientClass;
2730
protected $descriptionMock;
@@ -70,6 +73,23 @@ public function testGetHttpClientSetCorrectlyAccessToken()
7073
$this->assertEquals("Bearer " . $token, $client->getDefaultOption('headers')['Authorization']);
7174
}
7275

76+
public function testGetHttpClientSetCorrectlyAccessTokenIfSetAsEnvVar()
77+
{
78+
$token = 'my-awesome-token';
79+
$getenv = $this->getFunctionMock('Continuous\Sdk', 'getenv');
80+
$getenv->expects($this->once())
81+
->with('CPHP_TOKEN')
82+
->willReturn($token);
83+
84+
Service::setHttpClientClass('GuzzleHttp\Client');
85+
86+
$client = Service::getHttpClient();
87+
$this->assertInstanceOf('GuzzleHttp\Client', $client);
88+
89+
$this->assertArrayHasKey('Authorization', $client->getDefaultOption('headers'));
90+
$this->assertEquals("Bearer " . $token, $client->getDefaultOption('headers')['Authorization']);
91+
}
92+
7393
public function testGetHttpClientHasNoTokenIfNotProvided()
7494
{
7595
Service::setHttpClientClass('GuzzleHttp\Client');
@@ -100,5 +120,4 @@ public function testGetDescriptionUsesTheRightClassname()
100120
{
101121
$this->assertInstanceOf(Service::getDescriptionClass(), Service::getDescription());
102122
}
103-
104-
}
123+
}

0 commit comments

Comments
 (0)