Page MenuHomeDevCentral

D596.id1450.diff
No OneTemporary

D596.id1450.diff

diff --git a/src/Build/Payloads/AllBuildPayload.php b/src/Build/Payloads/AllBuildPayload.php
new file mode 100644
--- /dev/null
+++ b/src/Build/Payloads/AllBuildPayload.php
@@ -0,0 +1,15 @@
+<?php
+
+namespace Keruald\DockerHub\Build\Payloads;
+
+/**
+ * Represents a build trigger payload to build all branches/tags.
+ */
+class AllBuildPayload extends BuildPayload {
+
+ /**
+ * @var bool
+ */
+ public $build = true;
+
+}
diff --git a/src/Build/Payloads/BuildPayload.php b/src/Build/Payloads/BuildPayload.php
new file mode 100644
--- /dev/null
+++ b/src/Build/Payloads/BuildPayload.php
@@ -0,0 +1,10 @@
+<?php
+
+namespace Keruald\DockerHub\Build\Payloads;
+
+/**
+ * Represents a build trigger payload.
+ */
+abstract class BuildPayload {
+
+}
diff --git a/src/Build/Payloads/DockerTagBuildPayload.php b/src/Build/Payloads/DockerTagBuildPayload.php
new file mode 100644
--- /dev/null
+++ b/src/Build/Payloads/DockerTagBuildPayload.php
@@ -0,0 +1,24 @@
+<?php
+
+namespace Keruald\DockerHub\Build\Payloads;
+
+/**
+ * Represents a build trigger payload to build a specific Docker tag.
+ */
+class DockerTagBuildPayload extends BuildPayload {
+
+ /**
+ * @var string
+ */
+ public $docker_tag;
+
+ /**
+ * Initializes a new instance of the DockerTagBuildPayload class.
+ *
+ * @param string $tag The name of the Docker tag to build
+ */
+ public function __construct ($tag) {
+ $this->docker_tag = $tag;
+ }
+
+}
diff --git a/src/Build/Payloads/SourceRepositoryBranchBuildPayload.php b/src/Build/Payloads/SourceRepositoryBranchBuildPayload.php
new file mode 100644
--- /dev/null
+++ b/src/Build/Payloads/SourceRepositoryBranchBuildPayload.php
@@ -0,0 +1,19 @@
+<?php
+
+namespace Keruald\DockerHub\Build\Payloads;
+
+/**
+ * Represents a build trigger payload to build a specific repo branch.
+ */
+class SourceRepositoryBranchBuildPayload extends SourceRepositoryBuildPayload {
+
+ /**
+ * Initializes a new instance of the SourceRepositoryBranchBuildPayload class.
+ *
+ * @param string $branch The name of the repo branch to build
+ */
+ public function __construct ($branch) {
+ parent::__construct(SourceRepositoryType::BRANCH, $branch);
+ }
+
+}
diff --git a/src/Build/Payloads/SourceRepositoryBuildPayload.php b/src/Build/Payloads/SourceRepositoryBuildPayload.php
new file mode 100644
--- /dev/null
+++ b/src/Build/Payloads/SourceRepositoryBuildPayload.php
@@ -0,0 +1,51 @@
+<?php
+
+namespace Keruald\DockerHub\Build\Payloads;
+
+/**
+ * Represents a build trigger payload to build a specific tag or branch
+ * from the source repository.
+ */
+abstract class SourceRepositoryBuildPayload extends BuildPayload {
+
+ /**
+ * @var string
+ */
+ public $source_type;
+
+ /**
+ * @var string
+ */
+ public $source_name;
+
+ /**
+ * Initializes a new instance of the SourceRepositoryBuildPayload class.
+ *
+ * @param string $type "Tag" or "Branch"
+ * @param string $name The name of the tag or branch in the repository
+ */
+ public function __construct ($type, $name) {
+ if (!self::isValidType($type)) {
+ throw new \InvalidArgumentException("Unexpected type: $type.");
+ }
+
+ $this->source_type = $type;
+ $this->source_name = $name;
+ }
+
+ ///
+ /// Helper methods
+ ///
+
+ /**
+ * Determines if type is "Tag" or "Branch".
+ *
+ * @param string $type
+ * @return bool
+ */
+ public static function isValidType ($type) {
+ return $type === SourceRepositoryType::TAG
+ || $type === SourceRepositoryType::BRANCH;
+ }
+
+}
diff --git a/src/Build/Payloads/SourceRepositoryTagBuildPayload.php b/src/Build/Payloads/SourceRepositoryTagBuildPayload.php
new file mode 100644
--- /dev/null
+++ b/src/Build/Payloads/SourceRepositoryTagBuildPayload.php
@@ -0,0 +1,19 @@
+<?php
+
+namespace Keruald\DockerHub\Build\Payloads;
+
+/**
+ * Represents a build trigger payload to build a specific repo tag.
+ */
+class SourceRepositoryTagBuildPayload extends SourceRepositoryBuildPayload {
+
+ /**
+ * Initializes a new instance of the SourceRepositoryTagBuildPayload class.
+ *
+ * @param string $tag The name of the repo tag to build
+ */
+ public function __construct ($tag) {
+ parent::__construct(SourceRepositoryType::TAG, $tag);
+ }
+
+}
diff --git a/src/Build/Payloads/SourceRepositoryType.php b/src/Build/Payloads/SourceRepositoryType.php
new file mode 100644
--- /dev/null
+++ b/src/Build/Payloads/SourceRepositoryType.php
@@ -0,0 +1,20 @@
+<?php
+
+namespace Keruald\DockerHub\Build\Payloads;
+
+/**
+ * Represents types related to kind of objects existing in a repository.
+ */
+class SourceRepositoryType {
+
+ /**
+ * @const string
+ */
+ const BRANCH = "Branch";
+
+ /**
+ * @const string
+ */
+ const TAG = "Tag";
+
+}
diff --git a/src/Build/TriggerBuild.php b/src/Build/TriggerBuild.php
--- a/src/Build/TriggerBuild.php
+++ b/src/Build/TriggerBuild.php
@@ -4,6 +4,11 @@
use Keruald\DockerHub\DockerHubImage as Image;
+use Keruald\DockerHub\Build\Payloads\AllBuildPayload;
+use Keruald\DockerHub\Build\Payloads\DockerTagBuildPayload;
+use Keruald\DockerHub\Build\Payloads\SourceRepositoryBranchBuildPayload;
+use Keruald\DockerHub\Build\Payloads\SourceRepositoryTagBuildPayload;
+
/**
* this classe represents a trigger for a new build on the Docker Hub registry.
*/
@@ -56,4 +61,47 @@
. '/trigger/' . $this->token . '/';
}
+ ///
+ /// Payloads to specify what to build
+ ///
+
+ /**
+ * Gets a payload to build all branch/tag.
+ *
+ * @return Keruald\DockerHub\Build\Payloads\AllBuildPayload
+ */
+ public function getPayloadForAll () {
+ return new AllBuildPayload;
+ }
+
+ /**
+ * Gets a payload to build the specified Docker tag.
+ *
+ * @param string $tag
+ * @return Keruald\DockerHub\Build\Payloads\DockerTagBuildPayload
+ */
+ public function getPayloadForDockerTag ($tag) {
+ return new DockerTagBuildPayload($tag);
+ }
+
+ /**
+ * Gets a payload to build the specified repository branch.
+ *
+ * @param string $branch
+ * @return Keruald\DockerHub\Build\Payloads\SourceRepositoryBranchBuildPayload
+ */
+ public function getPayloadForSourceRepositoryBranch ($branch) {
+ return new SourceRepositoryBranchBuildPayload($branch);
+ }
+
+ /**
+ * Gets a payload to build the specified repository tag.
+ *
+ * @param string $tag
+ * @return Keruald\DockerHub\Build\Payloads\SourceRepositoryTagBuildPayload
+ */
+ public function getPayloadForSourceRepositoryTag ($tag) {
+ return new SourceRepositoryTagBuildPayload($tag);
+ }
+
}
diff --git a/tests/Build/Payloads/NotExistingObjectBuildPayloadMock.php b/tests/Build/Payloads/NotExistingObjectBuildPayloadMock.php
new file mode 100644
--- /dev/null
+++ b/tests/Build/Payloads/NotExistingObjectBuildPayloadMock.php
@@ -0,0 +1,11 @@
+<?php
+
+namespace Keruald\DockerHub\Tests\Build\Payloads;
+
+use Keruald\DockerHub\Build\Payloads\SourceRepositoryBuildPayload;
+
+class NotExistingObjectBuildPayloadMock extends SourceRepositoryBuildPayload {
+ public function __construct () {
+ parent::__construct('notexisting', 'acme');
+ }
+}
diff --git a/tests/Build/Payloads/SourceRepositoryBuildPayloadTest.php b/tests/Build/Payloads/SourceRepositoryBuildPayloadTest.php
new file mode 100644
--- /dev/null
+++ b/tests/Build/Payloads/SourceRepositoryBuildPayloadTest.php
@@ -0,0 +1,24 @@
+<?php
+
+namespace Keruald\DockerHub\Tests\Build\Payloads;
+
+use Keruald\DockerHub\Build\Payloads\SourceRepositoryBuildPayload;
+
+class SourceRepositoryBuildPayloadTest extends \PHPUnit_Framework_TestCase {
+
+ public function testIsValidType () {
+ $this->assertTrue(SourceRepositoryBuildPayload::isValidType('Tag'));
+ $this->assertTrue(SourceRepositoryBuildPayload::isValidType('Branch'));
+ $this->assertFalse(SourceRepositoryBuildPayload::isValidType('foo'));
+ $this->assertFalse(SourceRepositoryBuildPayload::isValidType(''));
+ $this->assertFalse(SourceRepositoryBuildPayload::isValidType(null));
+ }
+
+ /**
+ * @expectedException \InvalidArgumentException
+ */
+ public function testInvalidTypeThrowException () {
+ $mock = new NotExistingObjectBuildPayloadMock();
+ }
+
+}
diff --git a/tests/Build/TriggerBuildTest.php b/tests/Build/TriggerBuildTest.php
--- a/tests/Build/TriggerBuildTest.php
+++ b/tests/Build/TriggerBuildTest.php
@@ -4,6 +4,9 @@
use Keruald\DockerHub\DockerHubImage;
use Keruald\DockerHub\Build\TriggerBuild;
+use Keruald\DockerHub\Build\Payloads\AllBuildPayload;
+use Keruald\DockerHub\Build\Payloads\DockerTagBuildPayload;
+use Keruald\DockerHub\Build\Payloads\SourceRepositoryBuildPayload;
class TriggerBuildTest extends \PHPUnit_Framework_TestCase {
@@ -24,4 +27,34 @@
);
}
+ public function testGetPayloadForAll () {
+ // {"build": true}
+ $payload = $this->trigger->getPayloadForAll();
+ $this->assertInstanceOf(AllBuildPayload::class, $payload);
+ $this->assertTrue($payload->build);
+ }
+
+ public function testGetPayloadForDockerTag () {
+ // {"docker_tag": "master"}
+ $payload = $this->trigger->getPayloadForDockerTag("master");
+ $this->assertInstanceOf(DockerTagBuildPayload::class, $payload);
+ $this->assertSame($payload->docker_tag, "master");
+ }
+
+ public function testGetPayloadForSourceRepositoryBranch () {
+ // {"source_type": "Branch", "source_name": "staging"}
+ $payload = $this->trigger->getPayloadForSourceRepositoryBranch("staging");
+ $this->assertInstanceOf(SourceRepositoryBuildPayload::class, $payload);
+ $this->assertSame($payload->source_type, "Branch");
+ $this->assertSame($payload->source_name, "staging");
+ }
+
+ public function testGetPayloadForSourceRepositoryTag () {
+ // {"source_type": "Tag", "source_name": "v1.1"}
+ $payload = $this->trigger->getPayloadForSourceRepositoryTag("v1.1");
+ $this->assertInstanceOf(SourceRepositoryBuildPayload::class, $payload);
+ $this->assertSame($payload->source_type, "Tag");
+ $this->assertSame($payload->source_name, "v1.1");
+ }
+
}

File Metadata

Mime Type
text/plain
Expires
Thu, Nov 7, 01:26 (22 h, 1 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2232452
Default Alt Text
D596.id1450.diff (10 KB)

Event Timeline