Page Menu
Home
DevCentral
Search
Configure Global Search
Log In
Files
F3716740
D647.id.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
8 KB
Referenced Files
None
Subscribers
None
D647.id.diff
View Options
diff --git a/composer.json b/composer.json
--- a/composer.json
+++ b/composer.json
@@ -18,7 +18,9 @@
"issues": "https://devcentral.nasqueron.org"
},
"require": {
- "php": ">=5.6.0"
+ "php": ">=5.6.0",
+ "guzzlehttp/psr7": "~1.3",
+ "guzzlehttp/guzzle": "~6.0"
},
"require-dev": {
"phpunit/phpunit": "5.0.*",
diff --git a/src/Build/TriggerBuild.php b/src/Build/TriggerBuild.php
--- a/src/Build/TriggerBuild.php
+++ b/src/Build/TriggerBuild.php
@@ -4,11 +4,17 @@
use Keruald\DockerHub\DockerHubImage as Image;
+use Keruald\DockerHub\Build\Payloads\BuildPayload;
use Keruald\DockerHub\Build\Payloads\AllBuildPayload;
use Keruald\DockerHub\Build\Payloads\DockerTagBuildPayload;
use Keruald\DockerHub\Build\Payloads\SourceRepositoryBranchBuildPayload;
use Keruald\DockerHub\Build\Payloads\SourceRepositoryTagBuildPayload;
+use GuzzleHttp\ClientInterface;
+
+use BadMethodCallException;
+use InvalidArgumentException;
+
/**
* Represents a trigger for a new build on the Docker Hub registry.
*/
@@ -32,6 +38,13 @@
*/
protected $token;
+ /**
+ * The name of the class implementing PSR-7 message, or such an instance
+ *
+ * @var \GuzzleHttp\ClientInterface
+ */
+ protected $client;
+
///
/// Constructor
///
@@ -41,10 +54,12 @@
*
* @param Keruald\DockerHub\DockerHubImage $image The image to build
* @param string $token The token to authentify the build request
+ * @param \GuzzleHttp\ClientInterface|null $client A PSR-7 HTTP client
*/
- public function __construct (Image $image, $token) {
+ public function __construct (Image $image, $token, ClientInterface $client = null) {
$this->image = $image;
$this->token = $token;
+ $this->client = $client;
}
///
@@ -104,4 +119,82 @@
return new SourceRepositoryTagBuildPayload($tag);
}
+ ///
+ /// Overloads to send payloads
+ ///
+
+ /**
+ * Method triggered when invoking inaccessible methods in an object context.
+ *
+ * This allows to mirror getPayloadFor... to sendPayloadFor... methods.
+ *
+ * @param string $methodName The name of the method called
+ * @param array $args The arguments of the method called
+ * @return mixed
+ *
+ * @throws \BadMethodCallException when the method isn't implemented.
+ */
+ public function __call ($methodName, array $args) {
+ if (substr($methodName, 0, 4) === "send") {
+ $actionName = substr($methodName, 4);
+ return $this->sendPayload($actionName, $args);
+ }
+
+ throw new BadMethodCallException;
+ }
+
+ /**
+ * Sends a payload to Docker Hub to trigger a build.
+ *
+ * @param string $payloadActionSubjectName The subject part of the name of the method called (e.g. "PayloadForAll")
+ * @param array $args The arguments of the method called
+ */
+ private function sendPayload ($payloadActionSubjectName, array $args) {
+ $payload = $this->getPayload($payloadActionSubjectName, $args);
+ $url = $this->getTriggerUrl();
+
+ $this->postPayload($payload, $url);
+
+ }
+
+ /**
+ * @param \Keruald\DockerHub\Build\Payloads\BuildPayload $payload The payload to post
+ * @param string $url
+ */
+ private function postPayload (BuildPayload $payload, $url) {
+ if ($this->client === null) {
+ throw new InvalidArgumentException("Client can't be null to post a payload.");
+ }
+
+ $this->client->request('POST', $url, [ 'json' => $payload ]);
+ }
+
+ /**
+ * Gets the payload for the specified overload method.
+ *
+ * @param string $payloadActionSubjectName The subject part of the name of the method called
+ * @param array $args The arguments of the method called
+ * @return \Keruald\DockerHub\Build\Payloads\BuildPayload
+ */
+ private function getPayload ($payloadActionSubjectName, array $args) {
+ $methodName = $this->getGetPayloadMethodName($payloadActionSubjectName);
+ return $this->$methodName(...$args);
+ }
+
+ /**
+ * Gets the getPayload… method name.
+ *
+ * @return string $payloadActionSubjectName The subject part of the name of the method called
+ * @throws \BadMethodCallException when the method doesn't exist.
+ */
+ private function getGetPayloadMethodName ($payloadActionSubjectName) {
+ $methodName = "get" . $payloadActionSubjectName;
+
+ if (!method_exists($this, $methodName)) {
+ throw new BadMethodCallException;
+ }
+
+ return $methodName;
+ }
+
}
diff --git a/src/DockerHubImage.php b/src/DockerHubImage.php
--- a/src/DockerHubImage.php
+++ b/src/DockerHubImage.php
@@ -4,6 +4,8 @@
use Keruald\DockerHub\Build\TriggerBuild;
+use GuzzleHttp\ClientInterface;
+
/**
* Represents a DockerHub image.
*/
@@ -25,8 +27,17 @@
* @param string $token The token to authentify the build request
* @return Keruald\DockerHub\Build\TriggerBuild
*/
- public function getTriggerBuild ($token) {
+ public function getTriggerBuild (ClientInterface $client, $token) {
return new TriggerBuild($this, $token);
}
+ /**
+ * Triggers a full build for this image.
+ *
+ * @param string $token The token to authentify the build request
+ */
+ public function triggerBuild (ClientInterface $client, $token) {
+ $this->getTriggerBuild($client, $token)->sendPayloadForAll();
+ }
+
}
diff --git a/tests/Build/TriggerBuildTest.php b/tests/Build/TriggerBuildTest.php
--- a/tests/Build/TriggerBuildTest.php
+++ b/tests/Build/TriggerBuildTest.php
@@ -8,16 +8,25 @@
use Keruald\DockerHub\Build\Payloads\DockerTagBuildPayload;
use Keruald\DockerHub\Build\Payloads\SourceRepositoryBuildPayload;
+use Keruald\DockerHub\Tests\WithMockHttpClient;
+
class TriggerBuildTest extends \PHPUnit_Framework_TestCase {
+ use WithMockHttpClient;
+
/**
* @var Keruald\DockerHub\Build\TriggerBuild
*/
protected $trigger;
+ /**
+ * @var Keruald\DockerHub\DockerHubImage
+ */
+ protected $image;
+
public function setUp () {
- $image = new DockerHubImage("acme", "foo");
- $this->trigger = new TriggerBuild($image, "0000");
+ $this->image = new DockerHubImage("acme", "foo");
+ $this->trigger = new TriggerBuild($this->image, "0000");
}
public function testGetTriggerUrl () {
@@ -57,4 +66,42 @@
$this->assertSame($payload->source_name, "v1.1");
}
+ ///
+ /// Overloads test
+ ///
+
+ /**
+ * @expectedException \BadMethodCallException
+ */
+ public function testMethodOverloadingForNonExistingMethod () {
+ $this->trigger->loremIpsum();
+ }
+
+ /**
+ * @expectedException \BadMethodCallException
+ */
+ public function testMethodOverloadingForNonExistingPayloadMethod () {
+ $this->trigger->sendPayloadForLoremIpsum();
+ }
+
+ ///
+ /// HTTP client tests
+ ///
+
+ /**
+ * @expectedException \InvalidArgumentException
+ */
+ public function testPostWhenClientIsNull () {
+ $this->trigger->sendPayloadForAll();
+ }
+
+ /**
+ * @expectedException \RuntimeException
+ */
+ public function testPostThrowsRuntimeExceptionWhenResponseIsNot200 () {
+ $mockClient = $this->mockHttpClient(500);
+ $trigger = new TriggerBuild($this->image, "0000", $mockClient);
+ $trigger->sendPayloadForAll();
+ }
+
}
diff --git a/tests/DockerHubImageTest.php b/tests/DockerHubImageTest.php
--- a/tests/DockerHubImageTest.php
+++ b/tests/DockerHubImageTest.php
@@ -7,6 +7,8 @@
class DockerHubImageTest extends \PHPUnit_Framework_TestCase {
+ use WithMockHttpClient;
+
/**
* @var Keruald\DockerHub\DockerHubImage
*/
@@ -36,9 +38,10 @@
}
public function testGetTriggerBuild () {
+ $client = $this->mockHttpClient(200);
$this->assertInstanceOf(
TriggerBuild::class,
- $this->image->getTriggerBuild('0000')
+ $this->image->getTriggerBuild($client, '0000')
);
}
diff --git a/tests/WithMockHttpClient.php b/tests/WithMockHttpClient.php
new file mode 100644
--- /dev/null
+++ b/tests/WithMockHttpClient.php
@@ -0,0 +1,32 @@
+<?php
+
+namespace Keruald\DockerHub\Tests;
+
+use GuzzleHttp\Client;
+use GuzzleHttp\HandlerStack;
+use GuzzleHttp\Handler\MockHandler;
+use GuzzleHttp\Psr7\Response;
+
+trait WithMockHttpClient {
+
+ /**
+ * Mocks HTTP client
+ *
+ * @param int $httpResponseCode The HTTP response code to mock
+ * @return \GuzzleHttp\Client
+ */
+ protected function mockHttpClient ($httpResponseCode) {
+ $handler = self::getCustomMockHttpClientHandler($httpResponseCode);
+ return new Client(['handler' => $handler]);
+ }
+
+ /**
+ * @return \GuzzleHttp\HandlerStack
+ */
+ protected static function getCustomMockHttpClientHandler ($code) {
+ return HandlerStack::create(new MockHandler([
+ new Response($code),
+ ]));
+ }
+
+}
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Wed, Nov 6, 19:39 (20 h, 4 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2232203
Default Alt Text
D647.id.diff (8 KB)
Attached To
Mode
D647: Send HTTP payload to Docker Hub to trigger a build
Attached
Detach File
Event Timeline
Log In to Comment