Skip to content

Commit b21c5e6

Browse files
author
Frederic Dewinne
committed
call api using guzzlehttp
1 parent c5c07dc commit b21c5e6

File tree

12 files changed

+355
-9
lines changed

12 files changed

+355
-9
lines changed

build.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
phing.bin=vendor/bin/phing

build.xml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<project name="continuousphp-phing-tasks" default="help" basedir=".">
2+
<property file="./build.properties" />
3+
<import file="${project.basedir}/tasks.xml" />
4+
5+
6+
<target name="help" description="List available targets">
7+
<exec executable="{phing.bin}"
8+
passthru="true">
9+
<arg value="-l"/>
10+
</exec>
11+
</target>
12+
13+
<target name="config" description="Config continuousphp tasks">
14+
<continuousphp-config
15+
token="${token}" />
16+
</target>
17+
18+
<target name="package" description="Ouput the package url">
19+
<continuousphp-package
20+
project="${project}"
21+
reference="${reference}"
22+
property="package.url" />
23+
<echo message="---PACKAGE_URL:${package.url}---" />
24+
</target>
25+
26+
</project>

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
"behat/behat": "^3.0"
1818
},
1919
"require": {
20-
"phing/phing": "~2.9"
20+
"phing/phing": "~2.9",
21+
"guzzlehttp/guzzle": "~5.0"
2122
},
2223
"autoload": {
2324
"psr-0": {

features/bootstrap/Continuous/Features/TaskContext.php

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Behat\Behat\Context\Context;
66
use Behat\Behat\Context\SnippetAcceptingContext;
7+
use Behat\Behat\Tester\Exception\PendingException;
78
use Behat\Gherkin\Node\PyStringNode;
89
use Behat\Gherkin\Node\TableNode;
910

@@ -12,6 +13,28 @@
1213
*/
1314
class TaskContext implements Context, SnippetAcceptingContext
1415
{
16+
const PHING_BIN_PATH = 'vendor/bin/phing';
17+
18+
/**
19+
* @var string
20+
*/
21+
protected $token;
22+
23+
/**
24+
* @var string
25+
*/
26+
protected $project;
27+
28+
/**
29+
* @var string
30+
*/
31+
protected $reference;
32+
33+
/**
34+
* @var string
35+
*/
36+
protected $lastOutput;
37+
1538
/**
1639
* Initializes context.
1740
*
@@ -22,4 +45,61 @@ class TaskContext implements Context, SnippetAcceptingContext
2245
public function __construct()
2346
{
2447
}
48+
49+
/**
50+
* @Given I've the token :token
51+
*/
52+
public function setToken($token)
53+
{
54+
$this->token = $token;
55+
}
56+
57+
/**
58+
* @Given the project :project
59+
*/
60+
public function setProject($project)
61+
{
62+
$this->project = $project;
63+
}
64+
65+
/**
66+
* @Given the reference :reference
67+
*/
68+
public function setReference($reference)
69+
{
70+
$this->reference = $reference;
71+
}
72+
73+
/**
74+
* @When I use the continuousphp package task
75+
*/
76+
public function runPackageTask()
77+
{
78+
$command = self::PHING_BIN_PATH . ' config package'
79+
. " -Dtoken=" . $this->token
80+
. " -Dproject=" . $this->project
81+
. " -Dreference=" . $this->reference;
82+
83+
exec($command, $output, $return);
84+
85+
if ($return) {
86+
throw new \RuntimeException(implode(PHP_EOL, $output), $return);
87+
}
88+
89+
$this->lastOutput = implode(PHP_EOL, $output);
90+
}
91+
92+
/**
93+
* @Then I should retrieve a valid download url
94+
*/
95+
public function isValidDownloadUrl()
96+
{
97+
$regex = "/---PACKAGE_URL:(.*)---/";
98+
\PHPUnit_Framework_Assert::assertRegExp($regex, $this->lastOutput);
99+
100+
preg_match($regex, $this->lastOutput, $matches);
101+
$url = $matches[1];
102+
103+
\PHPUnit_Framework_Assert::assertNotEquals('${package.url}', $url);
104+
}
25105
}

features/package.feature

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
Feature: continuousphp package
2+
In order to deploy a package
3+
As a Developer or SysOps
4+
I need to be able to retrieve the URL of the last stable package for a specific GIT reference
5+
6+
Scenario: Get the last build of master branch
7+
Given I've the token "cc2efee7-be03-4611-923e-065bc3dd3326"
8+
And the project "git-hub/continuousphp/phing-tasks"
9+
And the reference "/head/master"
10+
When I use the continuousphp package task
11+
Then I should retrieve a valid download url

src/Continuous/Task/AbstractTask.php

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
namespace Continuous\Task;
1313

14+
use GuzzleHttp\Client;
15+
1416
/**
1517
* AbstractTask
1618
*
@@ -26,15 +28,9 @@ abstract class AbstractTask extends \Task
2628
static protected $token;
2729

2830
/**
29-
* @param string $token
30-
* @return $this
31+
* @var Client
3132
*/
32-
public function setToken($token)
33-
{
34-
self::$token = $token;
35-
36-
return $this;
37-
}
33+
static protected $client;
3834

3935
/**
4036
* @return string
@@ -43,4 +39,26 @@ protected function getToken()
4339
{
4440
return self::$token;
4541
}
42+
43+
/**
44+
* @param Client $client
45+
*/
46+
public function setClient(Client $client)
47+
{
48+
self::$client = $client;
49+
}
50+
51+
/**
52+
* @return Client
53+
*/
54+
protected function getClient()
55+
{
56+
if (is_null(self::$client)) {
57+
$this->setClient(new Client(
58+
array('base_url' => 'https://api.continuousphp.com/')
59+
));
60+
}
61+
62+
return self::$client;
63+
}
4664
}

src/Continuous/Task/ConfigTask.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,18 @@
2020
*/
2121
class ConfigTask extends AbstractTask
2222
{
23+
24+
/**
25+
* @param string $token
26+
* @return $this
27+
*/
28+
public function setToken($token)
29+
{
30+
self::$token = $token;
31+
32+
return $this;
33+
}
34+
2335
/**
2436
* Task entry point
2537
*/
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
<?php
2+
/**
3+
* PackageTask.php
4+
*
5+
* @author Frederic Dewinne <frederic@continuousphp.com>
6+
* @copyright Copyright (c) 2015 Continuous S.A. (http://continuousphp.com)
7+
* @license http://opensource.org/licenses/Apache-2.0 Apache License, Version 2.0
8+
* @file PackageTask.php
9+
* @link http://github.com/continuousphp/phing-tasks the canonical source repo
10+
*/
11+
12+
namespace Continuous\Task;
13+
14+
/**
15+
* PackageTask
16+
*
17+
* @package phing-tasks
18+
* @author Frederic Dewinne <frederic@continuousphp.com>
19+
* @license http://opensource.org/licenses/Apache-2.0 Apache License, Version 2.0
20+
*/
21+
class PackageTask extends AbstractTask
22+
{
23+
24+
/**
25+
* @var string
26+
*/
27+
protected $reference;
28+
29+
/**
30+
* @var string
31+
*/
32+
protected $project;
33+
34+
/**
35+
* @var string
36+
*/
37+
protected $property;
38+
39+
/**
40+
* @param string $reference
41+
* @return $this
42+
*/
43+
public function setReference($reference)
44+
{
45+
$this->reference = $reference;
46+
47+
return $this;
48+
}
49+
50+
/**
51+
* @param string $project
52+
* @return $this
53+
*/
54+
public function setProject($project)
55+
{
56+
$this->project = $project;
57+
58+
return $this;
59+
}
60+
61+
/**
62+
* @param string $property
63+
* @return $this
64+
*/
65+
public function setProperty($property)
66+
{
67+
$this->property = $property;
68+
69+
return $this;
70+
}
71+
72+
/**
73+
* Task entry point
74+
* @codeCoverageIgnore
75+
*/
76+
public function main()
77+
{
78+
$build = $this->getClient()
79+
->get('/api/projects/' . urlencode($this->getProject()) . '/builds?token=' . $this->getToken(),
80+
array(
81+
'headers' => array(
82+
'Accept' => 'application/hal+json',
83+
'Origin' => 'https://app.continuousphp.com'
84+
)
85+
));
86+
87+
}
88+
}

tasks.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<project>
2+
<taskdef name="continuousphp-config" classname="Continuous\Task\ConfigTask" />
3+
<taskdef name="continuousphp-package" classname="Continuous\Task\PackageTask" />
4+
</project>
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
/**
3+
* ConfigTaskTest.php
4+
*
5+
* @author Frederic Dewinne <frederic@continuousphp.com>
6+
* @copyright Copyright (c) 2015 Continuous S.A. (http://continuousphp.com)
7+
* @license http://opensource.org/licenses/Apache-2.0 Apache License, Version 2.0
8+
* @file ConfigTaskTest.php
9+
* @link http://github.com/continuousphp/phing-tasks the canonical source repo
10+
*/
11+
12+
namespace ContinuousTest\Task;
13+
14+
use Continuous\Task\ConfigTask;
15+
16+
/**
17+
* ConfigTaskTest
18+
*
19+
* @package phing-tasks
20+
* @author Frederic Dewinne <frederic@continuousphp.com>
21+
* @license http://opensource.org/licenses/Apache-2.0 Apache License, Version 2.0
22+
*/
23+
class ConfigTaskTest extends \PHPUnit_Framework_TestCase
24+
{
25+
26+
public function testTokenSetter()
27+
{
28+
$token = 'toto';
29+
30+
$task = new ConfigTask();
31+
$this->assertSame($task, $task->setToken($token));
32+
$this->assertAttributeSame($token, 'token', 'Continuous\Task\AbstractTask');
33+
}
34+
}

0 commit comments

Comments
 (0)