Page Menu
Home
DevCentral
Search
Configure Global Search
Log In
Files
F57030179
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Flag For Later
Award Token
Size
35 KB
Referenced Files
None
Subscribers
None
View Options
diff --git a/app/Console/Commands/ConfigShow.php b/app/Console/Commands/ConfigShow.php
index 38a41a7..367f7a9 100644
--- a/app/Console/Commands/ConfigShow.php
+++ b/app/Console/Commands/ConfigShow.php
@@ -1,134 +1,134 @@
<?php
namespace Nasqueron\Notifications\Console\Commands;
use Illuminate\Console\Command;
use Nasqueron\Notifications\Features;
use Config;
use ProjectsMap;
use Services;
class ConfigShow extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'config:show';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Show notifications center configuration';
/**
* Creates a new command instance.
*
* @return void
*/
public function __construct () {
parent::__construct();
}
///
/// Prepare information tables
///
/**
* Gets the services (defined in credentials.json) as table rows
*
* @return array
*/
protected function getServicesTableRows () {
$rows = [];
foreach (Services::get() as $service) {
$rows[] = [
$service->gate,
$service->door,
$service->getInstanceName(),
$this->getServiveStatus($service)
];
}
return $rows;
}
/**
* Gets service status
*
* @param $service The service to check
* @return string A description of the issue if something is wrong; otherwise, "✓".
*/
protected function getServiveStatus ($service) {
if ($service->gate === 'Phabricator') {
// Ensure the projects map is cached
- $map = \ProjectsMap::fetch($service->instance);
+ $map = \ProjectsMap::fetch($service->door);
if (!$map->isCached()) {
return "Projects map not cached.";
}
}
return "✓";
}
/**
* Gets features as table rows
*
* @return array
*/
protected function getFeaturesTableRows () {
$rows = [];
foreach (Features::getAll() as $key => $value) {
if ($value) {
$checkMark = '✓';
} else {
$checkMark = '';
}
$rows[] = [$key, $checkMark];
}
return $rows;
}
///
/// Handle the command
///
/**
* Executes the console command.
*
* @return mixed
*/
public function handle () {
$this->printGates();
$this->printFeatures();
$this->printServices();
}
protected final function printGates () {
$this->info("Gates:\n");
foreach (Config::get('gate.controllers') as $gate) {
$this->line('- ' . $gate);
}
}
protected final function printFeatures () {
$this->info("\nFeatures:\n");
$this->table(
['Feature', 'Enabled'],
$this->getFeaturesTableRows()
);
}
protected final function printServices () {
$this->info("\nServices declared in credentials:\n");
$this->table(
['Gate', 'Door', 'Instance', 'Status'],
$this->getServicesTableRows()
);
}
}
diff --git a/app/Console/Commands/PhabricatorProjectsMap.php b/app/Console/Commands/PhabricatorProjectsMap.php
index eb1b06a..70f0bcc 100644
--- a/app/Console/Commands/PhabricatorProjectsMap.php
+++ b/app/Console/Commands/PhabricatorProjectsMap.php
@@ -1,50 +1,50 @@
<?php
namespace Nasqueron\Notifications\Console\Commands;
use Illuminate\Console\Command;
use ProjectsMap;
use Services;
class PhabricatorProjectsMap extends Command {
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'phabricator:projectsmap';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Regenerate the projects map for each Phabricator instances';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct() {
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle() {
foreach (Services::getForGate('Phabricator') as $service) {
$this->info("Querying projects map for " . $service->instance);
- $map = ProjectsMap::fetch($service->instance);
+ $map = ProjectsMap::fetch($service->door);
$map->saveToCache();
$this->table(
['PHID', 'Project name'],
$map->toArray()
);
}
}
}
diff --git a/app/Events/PhabricatorPayloadEvent.php b/app/Events/PhabricatorPayloadEvent.php
index 8410b95..57d9476 100644
--- a/app/Events/PhabricatorPayloadEvent.php
+++ b/app/Events/PhabricatorPayloadEvent.php
@@ -1,62 +1,53 @@
<?php
namespace Nasqueron\Notifications\Events;
use Nasqueron\Notifications\Events\Event;
use Nasqueron\Notifications\Phabricator\PhabricatorStory;
use Illuminate\Queue\SerializesModels;
class PhabricatorPayloadEvent extends Event {
use SerializesModels;
/**
* The gate door which receives the request
* @var string
*/
public $door;
- /**
- * The Phabricator instance
- * @var string
- */
- protected $instance;
-
/**
* The raw payload
*/
public $payload;
/**
* The story sent by the request
* @var PhabricatorStory
*/
public $story;
/**
* Gets story from the request
*
- * @param string $instance The Phabricator instance URL
* @return PhabricatorStory
*/
protected function getStory () {
return PhabricatorStory::loadFromArray(
- $this->instance,
+ $this->door,
$this->payload
);
}
/**
* Creates a new event instance.
*
* @param string $door
- * @param string $instance The Phabricator instance URL
* @param stdClass $payload
*/
- public function __construct($door, $instance, $payload) {
+ public function __construct($door, $payload) {
$this->door = $door;
- $this->instance = $instance;
$this->payload = $payload;
$this->story = $this->getStory();
}
}
diff --git a/app/Http/Controllers/Gate/GateController.php b/app/Http/Controllers/Gate/GateController.php
index 6475124..32ca19d 100644
--- a/app/Http/Controllers/Gate/GateController.php
+++ b/app/Http/Controllers/Gate/GateController.php
@@ -1,107 +1,114 @@
<?php
namespace Nasqueron\Notifications\Http\Controllers\Gate;
use Nasqueron\Notifications\Features;
use Nasqueron\Notifications\Http\Controllers\Controller;
use App;
use Log;
use Report;
use Response;
use Services;
/**
* Represents a controller handling an entry-point for API payloads
*/
class GateController extends Controller {
///
/// Private members
///
/**
* @var string
*/
protected $door;
///
/// Requests
///
/**
* Handles GET requests
*/
public function onGet () {
// Virtually all the push APIs will send they payloads
// using a POST request, so we can provide a sensible
// default GET error message.
return view('gate/ispostonly');
}
/**
* Logs the request
*/
protected function logRequest ($extraContextualData = []) {
Log::info('[Gate] New payload.', [
'service' => static::SERVICE_NAME,
'door' => $this->door,
] + $extraContextualData);
}
///
/// Reports
///
/**
* Initializes the report and registers it
*/
protected function initializeReport () {
if (Features::isEnabled('ActionsReport')) {
Report::attachToGate(static::SERVICE_NAME, $this->door);
}
}
/**
* Renders the report
*
* @return Illuminate\Http\Response|null
*/
protected function renderReport () {
if (Features::isEnabled('ActionsReport')) {
$report = App::make('report');
$statusCode = $report->containsError() ? 503 : 200;
return Response::json($report)
->setStatusCode($statusCode);
}
}
///
/// Credentials
///
/**
* Gets service credentials for this gate and door
*
* @return stdClass the service credentials
*/
public function getService () {
return Services::findServiceByDoor(static::SERVICE_NAME, $this->door);
}
+ /**
+ * Checks if a registered service exists for this service and door
+ */
+ protected function doesServiceExist () {
+ return $this->getService() !== null;
+ }
+
/**
* Gets secret for this service and door.
*
* @return string the secret, or if unknown, an empty string
*/
protected function getSecret () {
$service= $this->getService();
if ($service !== null) {
return $service->secret;
}
return "";
}
}
diff --git a/app/Http/Controllers/Gate/PhabricatorGateController.php b/app/Http/Controllers/Gate/PhabricatorGateController.php
index 547c7da..73f54fc 100644
--- a/app/Http/Controllers/Gate/PhabricatorGateController.php
+++ b/app/Http/Controllers/Gate/PhabricatorGateController.php
@@ -1,91 +1,75 @@
<?php
namespace Nasqueron\Notifications\Http\Controllers\Gate;
use Event;
use Request;
use Nasqueron\Notifications\Events\PhabricatorPayloadEvent;
class PhabricatorGateController extends GateController {
///
/// Private members
///
/**
* The request content, as a structured data
*
* @var string
*/
private $payload;
///
/// Constants
///
/**
* The name of the service this gate accepts payload from.
*/
const SERVICE_NAME = 'Phabricator';
///
/// Requests processing
///
/**
* Handles POST requests
*
* @param Request $request the HTTP request
*/
public function onPost ($door) {
$this->door = $door;
- $this->instance = $this->getInstance();
- if ($this->instance === "") {
+ if (!$this->doesServiceExist()) {
abort(404, 'Unknown Phabricator instance.');
}
$this->extractPayload();
$this->logRequest();
$this->onPayload();
return parent::renderReport();
}
/**
* Extracts payload from the request
*/
protected function extractPayload () {
$this->payload = Request::all();
}
- /**
- * Gets the instance matching this door
- *
- * @return string The Phabricator root URL without trailing slash
- */
- protected function getInstance () {
- $service = $this->getService();
- if ($service === null) {
- return "";
- }
-
- return $service->instance;
- }
-
///
/// Payload processing
///
protected function onPayload () {
$this->initializeReport();
Event::fire(new PhabricatorPayloadEvent(
$this->door,
- $this->instance,
$this->payload
));
}
}
diff --git a/app/Phabricator/PhabricatorStory.php b/app/Phabricator/PhabricatorStory.php
index f2086ca..65fe02b 100644
--- a/app/Phabricator/PhabricatorStory.php
+++ b/app/Phabricator/PhabricatorStory.php
@@ -1,287 +1,287 @@
<?php
namespace Nasqueron\Notifications\Phabricator;
class PhabricatorStory {
///
/// Properties
///
/**
- * The Phabricator main URL
+ * The Phabricator instance name
*
* @var string
*/
- public $instance;
+ public $instanceName;
/**
* The unique identifier Phabricator assigns to each story
*
* @var int
*/
public $id;
/**
* Type of story (e.g. PhabricatorApplicationTransactionFeedStory)
*
* @var string
*/
public $type;
/**
* @var array
*/
public $data;
/**
* The person logged to Phabricator and triggering the event
*
* @var string
*/
public $authorPHID;
/**
* A short English textual description of the event
*
* @var string
*/
public $text;
/**
* The unixtime the event occured
*
* @var int
*/
public $epoch;
/**
* The projects attached to this story.
*
* When there is no project, [].
* When not yet queried, null.
*
* @var string[]|null
*/
private $projects = null;
///
/// Constructors
///
/**
* Initializes a new instance of the Phabricator story class
*
- * @param string $instance The Phabricator main URL, without trailing slash
+ * @param string $instanceName The Phabricator instance name
*/
- public function __construct ($instance) {
- $this->instance = $instance;
+ public function __construct ($instanceName) {
+ $this->instanceName = $instanceName;
}
/**
* Initializes a new instance of PhabricatorStory from an array.
*
* This is intended to parse the feed.hooks payloads.
*
* @param string $phabricatorURL The Phabricator URL (e.g. http://secure.phabricator.com)
* @param string $payload The data submitted by Phabricator
* @return PhabricatorStory
*/
- public static function loadFromArray ($phabricatorURL, $payload) {
- $instance = new self($phabricatorURL);
+ public static function loadFromArray ($instanceName, $payload) {
+ $instance = new self($instanceName);
foreach ($payload as $key => $value) {
$property = self::mapPhabricatorFeedKey($key);
$instance->$property = $value;
}
return $instance;
}
///
/// Helper methods
///
/**
* Gets object type (e.g. TASK for PHID-TASK-l34fw5wievp6n6rnvpuk)
*
* @return string The object type, as a 4 letters string (e.g. 'TASK')
*/
public function getObjectType () {
if ($this->data === null || !array_key_exists('objectPHID', $this->data)) {
return 'VOID';
}
return substr($this->data['objectPHID'], 5, 4);
}
/**
* Gets the identifier of the projets related to this task
*
* return string[] The list of project PHIDs
*/
public function getProjectsPHIDs () {
if (!array_key_exists('objectPHID', $this->data)) {
return [];
}
$objectPHID = $this->data['objectPHID'];
$objectType = $this->getObjectType();
switch ($objectType) {
case 'DREV':
return $this->getItemProjectsPHIDs(
'repository.query',
$this->getRepositoryPHID('differential.query')
);
case 'TASK':
return $this->getItemProjectsPHIDs(
'maniphest.query',
$objectPHID
);
case 'CMIT':
return $this->getItemProjectsPHIDs(
'repository.query',
$this->getRepositoryPHID('diffusion.querycommits')
);
case 'PSTE':
return $this->getItemProjectsPHIDsThroughApplicationSearch(
'paste.search',
$objectPHID
);
default:
return [];
}
}
/**
* Gets the PHID of a repository
*
* @param string $method The API method to call (e.g. differential.query)
* @return string The repository PHID or "" if not found
*/
public function getRepositoryPHID ($method) {
$objectPHID = $this->data['objectPHID'];
- $api = PhabricatorAPI::forInstance($this->instance);
+ $api = PhabricatorAPI::forProject($this->instanceName);
$reply = $api->call(
$method,
[ 'phids[0]' => $objectPHID ]
);
if ($reply === []) {
return "";
}
return PhabricatorAPI::getFirstResult($reply)->repositoryPHID;
}
/**
* Gets the projects for a specific item
*
* @param string $method The API method to call (e.g. differential.query)
* @param string $objectPHID The object PHID to pass as method parameter
* @return string[] The list of project PHIDs
*/
public function getItemProjectsPHIDs ($method, $objectPHID) {
if (!$objectPHID) {
return [];
}
- $api = PhabricatorAPI::forInstance($this->instance);
+ $api = PhabricatorAPI::forProject($this->instanceName);
$reply = $api->call(
$method,
[ 'phids[0]' => $objectPHID ]
);
if ($reply === []) {
return [];
}
return PhabricatorAPI::getFirstResult($reply)->projectPHIDs;
}
/**
* Gets the project for a specific item, using the new ApplicationSearch.
*
* This is a transitional method: when every Phabricator will have been
* migrated from info (generation 1) or query (generation 2) to search
* (generation 3), we'll rename it to getItemProjectsPHIDs and overwrite it.
*/
protected function getItemProjectsPHIDsThroughApplicationSearch ($method, $objectPHID) {
if (!$objectPHID) {
return [];
}
- $api = PhabricatorAPI::forInstance($this->instance);
+ $api = PhabricatorAPI::forProject($this->instanceName);
$reply = $api->call(
$method,
[
'constraints[phids][0]' => $objectPHID,
'attachments[projects]' => 1
]
);
return PhabricatorAPI::getFirstResult($reply)->attachments->projects->projectPHIDs;
}
/**
* Gets the list of the projects associated to the story
*
* @return string[] The list of project PHIDs
*/
public function getProjects () {
if ($this->projects === null) {
$this->attachProjects();
}
return $this->projects;
}
/**
* Queries the list of the projects associated to the story
* and attached it to the projects property.
*/
public function attachProjects () {
$this->projects = [];
$PHIDs = $this->getProjectsPHIDs();
if (count($PHIDs) == 0) {
// No project is attached to the story's object
return;
}
$map = ProjectsMap::load($this->instance);
foreach ($PHIDs as $PHID) {
$this->projects[] = $map->getProjectName($PHID);
}
}
///
/// Static helper methods
///
/**
* Maps a field of the API reply to a property of the PhabricatorStory class
*
* @param string $key The field of the API reply
* @return string The property's name
*/
public static function mapPhabricatorFeedKey ($key) {
if ($key == "storyID") {
return "id";
}
if (starts_with($key, "story")) {
$key = substr($key, 5);
$key[0] = strtolower($key[0]); // lowercase
}
return $key;
}
}
diff --git a/app/Phabricator/ProjectsMap.php b/app/Phabricator/ProjectsMap.php
index b0f8dbc..aeaf39a 100644
--- a/app/Phabricator/ProjectsMap.php
+++ b/app/Phabricator/ProjectsMap.php
@@ -1,278 +1,278 @@
<?php
namespace Nasqueron\Notifications\Phabricator;
use Nasqueron\Notifications\Phabricator\PhabricatorAPIClient as ApiClient;
use App;
use Cache;
class ProjectsMap implements \IteratorAggregate, \ArrayAccess {
///
/// Private properties and constants
///
/**
* The maximum number of projects to fetch
*/
const LIMIT = 1000;
/**
* The projects as an array with phid as keys, project names as $value
*
* @var string[]
*/
private $map = [];
/**
- * The Phabricator instance for this projects map
+ * The Phabricator instance name for this projects map
*
* @var string
*/
- private $instance;
+ private $instanceName;
/**
*
* @var Nasqueron\Notifications\Contracts\APIClient
*/
private $apiClient;
/**
* The source of the map
*
* @var string
*/
private $source = 'unloaded';
///
/// Constructor
///
/**
* Initializes a new instance of ProjectsMap.
*
- * @param string $instance The Phabricator root URL without trailing slash
+ * @param string $instanceName The Phabricator instance name
*/
- public function __construct ($instance) {
- $this->instance = $instance;
+ public function __construct ($instanceName) {
+ $this->instanceName = $instanceName;
}
///
/// IteratorAggregate interface implementation
///
/**
* Gets iterator.
*
* @return Traversable
*/
public function getIterator () {
return new \ArrayIterator($this->map);
}
///
/// ArrayAccess interface implementation
///
/**
* Determines whether an offset exists.
*
* @param mixed $offset The offset
* @return bool
*/
public function offsetExists ($offset) {
return array_key_exists($offset, $this->map);
}
/**
* Gets the value at the specified offset.
*
* @param mixed $offset The offset.
* @return mixed The value
*/
public function offsetGet ($offset) {
return $this->map[$offset];
}
/**
* Assigns a value to the specified offset.
*
* @param mixed $offset The offset
* @param mixed $value The value to assign
*/
public function offsetSet ($offset, $value) {
$this->map[$offset] = $value;
}
/**
* Unsets a value at the specified offset.
*
* @param mixed $offset The offset where to remove the value
*/
public function offsetUnset ($offset) {
unset($this->map[$offset]);
}
///
/// Static constructors
///
/**
* Gets a new ProjectsMap instance from cache or API when not cached.
*
- * @param string $phabricatorURL The Phabricator URL (e.g. http://secure.phabricator.com)
+ * @param string $phabricatorInstanceName The Phabricator instance name
* @return ProjectsMap
*/
- public static function load ($phabricatorURL) {
- $instance = new self($phabricatorURL);
+ public static function load ($phabricatorInstanceName) {
+ $instance = new self($phabricatorInstanceName);
if ($instance->isCached()) {
$instance->loadFromCache();
} else {
$instance->fetchFromAPI();
}
return $instance;
}
/**
* 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 string $phabricatorInstanceName The Phabricator instance name
* @param Nasqueron\Notifications\Contracts\APIClient $apiClient The Phabricator API client
* @return ProjectsMap
*/
- public static function fetch ($phabricatorURL, APIClient $apiClient = null) {
- $instance = new self($phabricatorURL);
+ public static function fetch ($phabricatorInstanceName, APIClient $apiClient = null) {
+ $instance = new self($phabricatorInstanceName);
$instance->setAPIClient($apiClient);
$instance->fetchFromAPI();
return $instance;
}
///
/// API
///
/**
* @return Nasqueron\Notifications\Contracts\APIClient
*/
public function getAPIClient () {
if ($this->apiClient === null) {
$factory = App::make('phabricator-api');
- $this->apiClient = $factory->get($this->instance);
+ $this->apiClient = $factory->getForProject($this->instanceName);
}
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.
- *
+ *
* @throws \Exception when API reply is empty or invalid.
*/
private function fetchFromAPI () {
$reply = $this->getAPIClient()->call(
'project.query',
[ 'limit' => self::LIMIT ]
);
if (!$reply) {
- throw new \Exception("Empty reply calling project.query at $this->instance API.");
+ throw new \Exception("Empty reply calling project.query at $this->instanceName Conduit API.");
}
if (!property_exists($reply, 'data')) {
- throw new \Exception("Invalid reply calling project.query at $this->instance API.");
+ throw new \Exception("Invalid reply calling project.query at $this->instanceName Conduit API.");
}
foreach ($reply->data as $phid => $projectInfo) {
$this->offsetSet($phid, $projectInfo->name);
}
$this->source = 'api';
}
///
/// Cache
///
/**
* Gets cache key.
*
* @return string The cache key for the current projects map
*/
private function getCacheKey () {
- return class_basename(get_class($this)) . '-' . md5($this->instance);
+ return class_basename(get_class($this)) . '-' . md5($this->instanceName);
}
/**
* Determines if the instance is cached
*
* @return bool true if cached; otherwise, false.
*/
public function isCached () {
return Cache::has($this->getCacheKey());
}
/**
* Saves data to cache
*/
public function saveToCache () {
Cache::forever($this->getCacheKey(), $this->map);
}
/**
* Loads data from cache
*
* Populates 'map' and 'source' properties
*/
public function loadFromCache () {
$cachedMap = Cache::get($this->getCacheKey());
if ($cachedMap !== null) {
$this->map = $cachedMap;
$this->source = 'cache';
}
}
///
/// Output
///
/**
* Gets project name, refreshing the cache if needed.
*
* @param string $projectPHID the PHID of the project to query the name
* @return string The name of the poject, or an empty string if not found
*/
public function getProjectName ($projectPHID) {
if ($this->offsetExists($projectPHID)) {
return $this->offsetGet($projectPHID);
}
if ($this->source !== 'api') {
$this->fetchFromAPI();
return $this->getProjectName($projectPHID);
}
return "";
}
/**
* Returns the projects map as an array.
*
* @return array An array, each row containing ['PHID', 'project name']
*/
public function toArray () {
$array = [];
foreach ($this->map as $phid => $projectName) {
$array[] = [$phid, $projectName];
}
return $array;
}
}
diff --git a/app/Phabricator/ProjectsMapFactory.php b/app/Phabricator/ProjectsMapFactory.php
index 66c0a89..a5f84dc 100644
--- a/app/Phabricator/ProjectsMapFactory.php
+++ b/app/Phabricator/ProjectsMapFactory.php
@@ -1,27 +1,27 @@
<?php
namespace Nasqueron\Notifications\Phabricator;
class ProjectsMapFactory {
/**
* Loads projects map from cache or fetches it from API if not cached.
*
- * @param string $instance The Phabricator instance
+ * @param string $instanceName The Phabricator instance name
* @return Nasqueron\Notifications\Phabricator\ProjectsMap
*/
- public function load ($instance) {
- return ProjectsMap::load($instance);
+ public function load ($instanceName) {
+ return ProjectsMap::load($instanceName);
}
/**
* Fetches projects map from API.
*
- * @param string $instance The Phabricator instance
+ * @param string $instanceName The Phabricator instance name
* @return Nasqueron\Notifications\Phabricator\ProjectsMap
*/
- public function fetch ($instance) {
- return ProjectsMap::fetch($instance);
+ public function fetch ($instanceName) {
+ return ProjectsMap::fetch($instanceName);
}
}
diff --git a/tests/Analyzers/Phabricator/WithConfiguration.php b/tests/Analyzers/Phabricator/WithConfiguration.php
index c7987a1..a91e7ca 100644
--- a/tests/Analyzers/Phabricator/WithConfiguration.php
+++ b/tests/Analyzers/Phabricator/WithConfiguration.php
@@ -1,36 +1,36 @@
<?php
namespace Nasqueron\Notifications\Tests\Analyzers\Phabricator;
use Nasqueron\Notifications\Analyzers\Phabricator\PhabricatorPayloadAnalyzerConfiguration;
use Nasqueron\Notifications\Phabricator\PhabricatorStory;
/**
* Helper methods to construct needed objects
*/
trait WithConfiguration {
private function getPhabricatorPayloadAnalyzerConfiguration () {
$filename = __DIR__ . '/../../data/PhabricatorPayloadAnalyzer/Nasqueron.json';
$mapper = new \JsonMapper();
return $mapper->map(
json_decode(file_get_contents($filename)),
new PhabricatorPayloadAnalyzerConfiguration('Nasqueron')
);
}
private function getStory() {
return $this
->getMockBuilder("Nasqueron\Notifications\Phabricator\PhabricatorStory")
- ->setConstructorArgs(["https://phab.acme"])
+ ->setConstructorArgs(["Acme"])
->getMock();
}
private function attachProjectsToStoryMock ($mock, $projects) {
$mock
->expects($this->any())
->method("getProjects")
->will($this->returnValue($projects));
}
}
diff --git a/tests/Phabricator/ProjectsMapFactoryTest.php b/tests/Phabricator/ProjectsMapFactoryTest.php
index 79e29bc..ddefc70 100644
--- a/tests/Phabricator/ProjectsMapFactoryTest.php
+++ b/tests/Phabricator/ProjectsMapFactoryTest.php
@@ -1,35 +1,35 @@
<?php
namespace Nasqueron\Notifications\Tests\Phabricator;
use Nasqueron\Notifications\Tests\TestCase;
class ProjectsMapFactoryTest extends TestCase {
/**
* @var \Nasqueron\Notifications\Phabricator\ProjectsMapFactory
*/
private $factory;
public function setUp () {
parent::setUp();
$this->factory = $this->app->make('phabricator-projectsmap');
$this->mockPhabricatorAPIForProjectsMap();
}
public function testLoadProjectsMap () {
$this->assertInstanceOf(
'\Nasqueron\Notifications\Phabricator\ProjectsMap',
- $this->factory->load("https://phabricator.acme.tld")
+ $this->factory->load("Acme")
);
}
public function testFetchProjectsMap () {
$this->assertInstanceOf(
'\Nasqueron\Notifications\Phabricator\ProjectsMap',
- $this->factory->fetch("https://phabricator.acme.tld")
+ $this->factory->fetch("Acme")
);
}
}
diff --git a/tests/TestCase.php b/tests/TestCase.php
index 5d15c41..839ac0d 100644
--- a/tests/TestCase.php
+++ b/tests/TestCase.php
@@ -1,143 +1,143 @@
<?php
namespace Nasqueron\Notifications\Tests;
use Illuminate\Contracts\Console\Kernel;
use Keruald\Broker\BlackholeBroker;
use Keruald\Broker\Broker;
use Mockery;
class TestCase extends \Illuminate\Foundation\Testing\TestCase
{
/**
* The base URL to use while testing the application.
*
* @var string
*/
protected $baseUrl = 'http://localhost';
/**
* Creates the application.
*
* @return \Illuminate\Foundation\Application
*/
public function createApplication()
{
$app = require __DIR__.'/../bootstrap/app.php';
$app->make(Kernel::class)->bootstrap();
return $app;
}
///
/// Helpers to mock application services
///
/**
* Mocks the events dispatcher
*/
public function disableEvents () {
// Disables events
// This allows to test a single component and not all the application
$mock = Mockery::mock('Illuminate\Contracts\Events\Dispatcher');
$mock->shouldReceive('fire');
$mock->shouldReceive('listen');
$this->app->instance('events', $mock);
}
/**
* Mocks the broker
*/
public function disableBroker () {
$broker = new BlackholeBroker();
$broker->acceptAllMethodCalls(); // allows to be used as a mock
$this->app->instance('broker', $broker);
}
/**
* Mocks the broker, throwing an exception when sendMessage is called.
*/
public function mockNotOperationalBroker () {
$mock = Mockery::mock('Keruald\Broker\Broker');
$mock->shouldReceive('connect');
$mock->shouldReceive('setExchangeTarget->routeTo->sendMessage')
->andThrow(new \RuntimeException);
$this->app->instance('broker', $mock);
}
/**
* Mocks the Phabricator API
*
* @return Nasqueron\Notifications\Phabricator\PhabricatorAPIFactory The mock
*/
protected function mockPhabricatorAPI () {
// Inject into our container a mock of PhabricatorAPI
$mock = Mockery::mock('Nasqueron\Notifications\Phabricator\PhabricatorAPIFactory');
$this->app->instance('phabricator-api', $mock);
return $mock;
}
private function mockPhabricatorAPIProjectsQueryReply () {
$json = file_get_contents('tests/data/PhabricatorProjetsQueryReply.json');
return json_decode($json);
}
/**
* Mocks the Phabricator API
*/
protected function mockPhabricatorAPIForProjectsMap () {
$mock = $this->mockPhabricatorAPI();
$reply = $this->mockPhabricatorAPIProjectsQueryReply();
- $mock->shouldReceive('get->call')->andReturn($reply);
+ $mock->shouldReceive('getForProject->call')->andReturn($reply);
}
///
/// Helpers to post data to gates
///
/**
* Visits the given URI with a JSON request.
*
* @param string $uri
* @param mixed $data
* @param string $method
* @param array $headers
* @return $this
*/
public function sendJsonPayload ($uri, $data, $method = 'POST', array $headers = []) {
$content = json_encode($data);
$headers = array_merge([
'CONTENT_TYPE' => 'application/json',
'Accept' => 'application/json',
], $headers);
return $this->sendPayload($uri, $content, $method, $headers);
}
/**
* Visits the given URI with a raw request.
*
* @param string $uri
* @param string $content
* @param string $method
* @param array $headers
* @return $this
*/
public function sendPayload ($uri, $content, $method = 'POST', array $headers = []) {
$headers = array_merge([
'CONTENT_LENGTH' => mb_strlen($content, '8bit'),
], $headers);
$this->call(
$method, $uri, [], [], [], $this->transformHeadersToServerVars($headers), $content
);
return $this;
}
}
File Metadata
Details
Attached
Mime Type
text/x-diff
Expires
Sun, Nov 15, 06:27 (6 d, 20 h ago)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
4244830
Default Alt Text
(35 KB)
Attached To
Mode
rNOTIF Notifications center
Attached
Detach File
Event Timeline
Log In to Comment