Page MenuHomeDevCentral

D650.diff
No OneTemporary

D650.diff

diff --git a/src/Build/TriggerBuildFactory.php b/src/Build/TriggerBuildFactory.php
new file mode 100644
--- /dev/null
+++ b/src/Build/TriggerBuildFactory.php
@@ -0,0 +1,85 @@
+<?php
+
+namespace Keruald\DockerHub\Build;
+
+use Keruald\DockerHub\DockerHubImage;
+
+use GuzzleHttp\ClientInterface;
+
+use InvalidArgumentException;
+
+/**
+ * Allows to build several TriggerBuild instances
+ * with the same HTTP client and a collection of tokens.
+ */
+class TriggerBuildFactory {
+
+ ///
+ /// Private members, to pass to instances
+ ///
+
+ /**
+ * @var $array
+ */
+ private $tokens;
+
+ /**
+ * @var \GuzzleHttp\ClientInterface
+ */
+ private $client;
+
+ ///
+ /// Constructor
+ ///
+
+ /**
+ * Initializes a new instance of the TriggerBuildFactory object.
+ *
+ * @param \GuzzleHttp\ClientInterface $client HTTP client
+ * @param array $tokens The tokens for the image
+ */
+ public function __construct (ClientInterface $client, array $tokens) {
+ $this->client = $client;
+ $this->tokens = $tokens;
+ }
+
+ ///
+ /// Builder
+ ///
+
+ /**
+ * Builds a new TriggerBuild instance for the specified image.
+ *
+ * @param string $image The image name
+ * @return TriggerBuild
+ */
+ public function getForImage ($image) {
+ return new TriggerBuild(
+ DockerHubImage::loadFromSlashNotation($image),
+ $this->getToken($image),
+ $this->client
+ );
+ }
+
+ /**
+ * Sends to registry a payload to trigger a build for the specified image.
+ *
+ * @param string $image The image name
+ */
+ public function build ($image) {
+ $this->getForImage($image)->sendPayloadForAll();
+ }
+
+ ///
+ /// Helper methods
+ ///
+
+ protected function getToken ($image) {
+ if (array_key_exists($image, $this->tokens)) {
+ return $this->tokens[$image];
+ }
+
+ throw new InvalidArgumentException("No token found for image $image.");
+ }
+
+}
diff --git a/tests/Build/TriggerBuildFactoryTest.php b/tests/Build/TriggerBuildFactoryTest.php
new file mode 100644
--- /dev/null
+++ b/tests/Build/TriggerBuildFactoryTest.php
@@ -0,0 +1,56 @@
+<?php
+
+namespace Keruald\DockerHub\Tests\Build;
+
+use Keruald\DockerHub\DockerHubImage;
+use Keruald\DockerHub\Build\TriggerBuild;
+use Keruald\DockerHub\Build\TriggerBuildFactory;
+
+use Keruald\DockerHub\Tests\WithMockHttpClient;
+
+class TriggerBuildFactoryTest extends \PHPUnit_Framework_TestCase {
+
+ use WithMockHttpClient;
+
+ /**
+ * @var Keruald\DockerHub\Build\TriggerBuildFactory
+ */
+ protected $factory;
+
+ public function setUp () {
+ $client = self::mockHttpClient();
+ $tokens = self::mockTokens();
+ $this->factory = new TriggerBuildFactory($client, $tokens);
+ }
+
+ /**
+ * @return array
+ */
+ private static function mockTokens () {
+ return [
+ "acme/foo" => "0000",
+ ];
+ }
+
+ public function testGetForImage () {
+ $this->assertInstanceOf(
+ TriggerBuild::class,
+ $this->factory->getForImage("acme/foo")
+ );
+ }
+
+ /**
+ * @expectedException \InvalidArgumentException
+ */
+ public function testGetForImageWithoutToken () {
+ $this->factory->getForImage("acme/bar");
+ }
+
+ public function testBuild () {
+ // That returns void.
+ // We don't need to test the sendPayloadForAll method.
+ // So, we only need to check there is no exception or error.
+ $this->factory->build("acme/foo");
+ }
+
+}

File Metadata

Mime Type
text/plain
Expires
Wed, Nov 6, 19:53 (17 h, 59 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2232225
Default Alt Text
D650.diff (3 KB)

Event Timeline