Page Menu
Home
DevCentral
Search
Configure Global Search
Log In
Files
F3602697
D334.id1476.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
6 KB
Referenced Files
None
Subscribers
None
D334.id1476.diff
View Options
diff --git a/app/Contracts/APIClient.php b/app/Contracts/APIClient.php
new file mode 100644
--- /dev/null
+++ b/app/Contracts/APIClient.php
@@ -0,0 +1,24 @@
+<?php
+
+namespace Nasqueron\Notifications\Contracts;
+
+interface APIClient {
+
+ /**
+ * Sets API end point
+ *
+ * @param string $url The API end point URL
+ * @return void
+ */
+ public function setEndPoint ($url);
+
+ /**
+ * Calls an API method
+ *
+ * @param string $method The method to call
+ * @param array $arguments The arguments to use
+ * @return mixed The API result
+ */
+ public function call ($method, $arguments = []);
+
+}
diff --git a/app/Contracts/APIFactory.php b/app/Contracts/APIFactory.php
new file mode 100644
--- /dev/null
+++ b/app/Contracts/APIFactory.php
@@ -0,0 +1,15 @@
+<?php
+
+namespace Nasqueron\Notifications\Contracts;
+
+interface APIFactory {
+
+ /**
+ * Gets an instance of the API client class
+ *
+ * @param string $endPoint The API end point
+ * @return Nasqueron\Notifications\Contracts\APIClient
+ */
+ public function get ($endPoint);
+
+}
diff --git a/app/Phabricator/PhabricatorAPI.php b/app/Phabricator/PhabricatorAPI.php
--- a/app/Phabricator/PhabricatorAPI.php
+++ b/app/Phabricator/PhabricatorAPI.php
@@ -2,9 +2,11 @@
namespace Nasqueron\Notifications\Phabricator;
+use Nasqueron\Notifications\Contracts\APIClient;
+
use Services;
-class PhabricatorAPI {
+class PhabricatorAPI implements APIClient {
///
/// Private members
@@ -15,7 +17,7 @@
*
* @var string
*/
- private $instance;
+ private $endPoint;
/**
* The token generated at /settings/panel/apitokens/ to query the API
@@ -31,11 +33,11 @@
/**
* Initializes a new instance of the Phabricator API class
*
- * @param string $instance The Phabricator main URL, without trailing slash
+ * @param string $endPoint The Phabricator main URL, without trailing slash
* @param string $apiToken The token generated at /settings/panel/apitokens/
*/
- public function __construct ($instance, $apiToken) {
- $this->instance = $instance;
+ public function __construct ($endPoint, $apiToken) {
+ $this->endPoint = $endPoint;
$this->apiToken = $apiToken;
}
@@ -68,17 +70,27 @@
}
///
- /// Public methods
+ /// APIClient implementation
///
/**
+ * Sets API end point
+ *
+ * @param string $url The API end point URL
+ */
+ public function setEndPoint ($url) {
+ $this->endPoint = $url;
+ }
+
+ /**
* Calls a Conduit API method
*
- * @param $method The method to call (e.g. repository.create)
- * @param $arguments The arguments to use
+ * @param string $method The method to call (e.g. repository.create)
+ * @param array $arguments The arguments to use
+ * @return mixed The API result
*/
public function call ($method, $arguments = []) {
- $url = $this->instance . '/api/' . $method;
+ $url = $this->endPoint . '/api/' . $method;
$arguments['api.token'] = $this->apiToken;
$reply = json_decode(static::post($url, $arguments));
@@ -93,6 +105,10 @@
return $reply->result;
}
+ ///
+ /// Helper methods
+ ///
+
/**
* Gets the first result of an API reply
*
diff --git a/app/Phabricator/PhabricatorAPIFactory.php b/app/Phabricator/PhabricatorAPIFactory.php
--- a/app/Phabricator/PhabricatorAPIFactory.php
+++ b/app/Phabricator/PhabricatorAPIFactory.php
@@ -2,7 +2,9 @@
namespace Nasqueron\Notifications\Phabricator;
-class PhabricatorAPIFactory {
+use Nasqueron\Notifications\Contracts\APIFactory;
+
+class PhabricatorAPIFactory implements APIFactory {
/**
* Gets an instance of the Phabricator API client class
diff --git a/app/Phabricator/ProjectsMap.php b/app/Phabricator/ProjectsMap.php
--- a/app/Phabricator/ProjectsMap.php
+++ b/app/Phabricator/ProjectsMap.php
@@ -2,6 +2,9 @@
namespace Nasqueron\Notifications\Phabricator;
+use Nasqueron\Notifications\Phabricator\PhabricatorAPIClient as ApiClient;
+
+use App;
use Cache;
class ProjectsMap implements \IteratorAggregate, \ArrayAccess {
@@ -30,6 +33,12 @@
private $instance;
/**
+ *
+ * @var Nasqueron\Notifications\Contracts\APIClient
+ */
+ private $apiClient;
+
+ /**
* The source of the map
*
* @var string
@@ -130,10 +139,12 @@
* Gets a new ProjectsMap instance and queries Phabricator API to fill it
*
* @param string $phabricatorURL The Phabricator URL (e.g. http://secure.phabricator.com)
+ * @param Nasqueron\Notifications\Contracts\APIClient $apiClient The Phabricator API client
* @return ProjectsMap
*/
- public static function fetch ($phabricatorURL) {
+ public static function fetch ($phabricatorURL, APIClient $apiClient = null) {
$instance = new self($phabricatorURL);
+ $instance->setAPIClient($apiClient);
$instance->fetchFromAPI();
return $instance;
}
@@ -143,10 +154,28 @@
///
/**
+ * @return Nasqueron\Notifications\Contracts\APIClient
+ */
+ public function getAPIClient () {
+ if ($this->apiClient === null) {
+ $factory = App::make('phabricator-api');
+ $this->apiClient = $factory->get($this->instance);
+ }
+ return $this->apiClient;
+ }
+
+ /**
+ * @param Nasqueron\Notifications\Contracts\APIClient $apiClient
+ */
+ public function setAPIClient (APIClient $apiClient = null) {
+ $this->apiClient = $apiClient;
+ }
+
+ /**
* Fetches the projects' map from the Phabricator API
*/
private function fetchFromAPI () {
- $reply = \PhabricatorAPI::get($this->instance)->call(
+ $reply = $this->getAPIClient()->call(
'project.query',
[ 'limit' => self::LIMIT ]
);
diff --git a/tests/Phabricator/ProjectsMapTest.php b/tests/Phabricator/ProjectsMapTest.php
--- a/tests/Phabricator/ProjectsMapTest.php
+++ b/tests/Phabricator/ProjectsMapTest.php
@@ -5,6 +5,8 @@
use Nasqueron\Notifications\Phabricator\ProjectsMap;
use Nasqueron\Notifications\Tests\TestCase;
+use Mockery;
+
class ProjectsMapTest extends TestCase {
/**
@@ -161,4 +163,30 @@
);
}
+ ///
+ /// Tests API
+ ///
+
+ private function mockPhabricatorAPIWithReply ($reply) {
+ return Mockery::mock('Nasqueron\Notifications\Contract\APIClient')
+ ->shouldReceive('call')
+ ->andReturn($reply);
+ }
+
+ /**
+ * @expectedException Exception
+ */
+ public function testFetchFromAPIWithoutReply () {
+ $mock = $this->mockPhabricatorAPIWithReply(false);
+ ProjectsMap::fetch("http://phabricator.acme.tld", $mock);
+ }
+
+ /**
+ * @expectedException Exception
+ */
+ public function testFetchFromAPIInvalidReply () {
+ $mock = $this->mockPhabricatorAPIWithReply(new \stdClass);
+ ProjectsMap::fetch("http://phabricator.acme.tld", $mock);
+ }
+
}
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Mon, Sep 30, 03:30 (22 h, 9 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2163537
Default Alt Text
D334.id1476.diff (6 KB)
Attached To
Mode
D334: API dependency injection for ProjectsMap
Attached
Detach File
Event Timeline
Log In to Comment