Page MenuHomeDevCentral

D807.id2046.diff
No OneTemporary

D807.id2046.diff

diff --git a/app/Jobs/NotifyNewCommitsToDiffusion.php b/app/Jobs/NotifyNewCommitsToDiffusion.php
--- a/app/Jobs/NotifyNewCommitsToDiffusion.php
+++ b/app/Jobs/NotifyNewCommitsToDiffusion.php
@@ -8,11 +8,12 @@
use Nasqueron\Notifications\Phabricator\PhabricatorAPI as API;
use Nasqueron\Notifications\Phabricator\PhabricatorAPIException;
-
use Event;
use Log;
use PhabricatorAPI;
+use RuntimeException;
+
/**
* This class allows to notify Phabricator of new commits, so daemons can pull
* these new commits and add them into Diffusion.
@@ -143,9 +144,13 @@
*/
private function fetchAPI () : bool {
$project = $this->getPhabricatorProject();
- $this->api = PhabricatorAPI::getForProject($project);
- return $this->api !== null;
+ try {
+ $this->api = PhabricatorAPI::getForProject($project);
+ return true;
+ } catch (RuntimeException $ex) {
+ return false;
+ }
}
/**
diff --git a/tests/Jobs/NotifyNewCommitsToDiffusionTest.php b/tests/Jobs/NotifyNewCommitsToDiffusionTest.php
--- a/tests/Jobs/NotifyNewCommitsToDiffusionTest.php
+++ b/tests/Jobs/NotifyNewCommitsToDiffusionTest.php
@@ -11,34 +11,70 @@
class NotifyNewCommitsToDiffusionTest extends TestCase {
/**
- * Mock for the Phabricator API factory
- * @var \Mockery\MockInterface
- */
- private $apiFactoryMock;
-
- /**
* The job to test
* @var NotifyNewCommitsToDiffusion
*/
private $job;
- public function setUp () {
- parent::setUp();
+ ///
+ /// Tests
+ ///
- $this->apiFactoryMock = $this->mockPhabricatorAPI();
- $this->job = $this->mockJob();
+ public function testJobWantsPhabricatorAPI () {
+ $this->mockJobAndPhabricatorAPI(
+ [new class { public $callsign = "K2"; }], // Reply for repository.query
+ 2 // API calls count
+ );
+ $this->job->handle();
}
- /**
- * @return NotifyNewCommitsToDiffusion
- */
- protected function mockJob() {
- return new NotifyNewCommitsToDiffusion("acme", "ssh://acme/k2.git");
+ public function testJobWhenRepositoryIsUnknownByPhabricator () {
+ $this->mockJobAndPhabricatorAPI(
+ [], // Reply for repository.query
+ 1 // API calls count
+ );
+ $this->job->handle();
}
- public function testJobWantsPhabricatorAPI () {
- $this->apiFactoryMock->shouldReceive('getForProject')->once();
+ public function testJobWhenThereIsNoPhabricatorInstanceForTheProject () {
+ $this->mockJob("not-existing-project");
$this->job->handle();
}
+ ///
+ /// Mock helper methods
+ ///
+
+ protected function mockJobAndPhabricatorAPI ($apiRepositoryReply, $apiCallCounts) : void {
+ $this->mockPhabricatorAPI()
+ ->shouldReceive('getForProject->call')
+ ->andReturn(
+ // First API call: repository.query
+ $apiRepositoryReply,
+
+ // Second API call: diffusion.looksoon
+ null
+ )
+ ->times($apiCallCounts);
+
+ $this->mockJob();
+ }
+
+ protected function mockJob(string $project = "acme") : void {
+ $this->job = new NotifyNewCommitsToDiffusion(
+ $project,
+ "ssh://acme/k2.git"
+ );
+ }
+
+ /**
+ * Mocks the Phabricator API
+ */
+ protected function mockPhabricatorAPIFor () : void {
+ $mock = $this->mockPhabricatorAPI();
+
+ $reply = $this->mockPhabricatorAPIProjectsQueryReply();
+ $mock->shouldReceive('getForProject->call')->andReturn($reply);
+ }
+
}

File Metadata

Mime Type
text/plain
Expires
Sun, Nov 17, 12:43 (20 h, 11 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2249388
Default Alt Text
D807.id2046.diff (3 KB)

Event Timeline