Page MenuHomeDevCentral

D807.diff
No OneTemporary

D807.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
@@ -2,43 +2,69 @@
namespace Nasqueron\Notifications\Tests\Jobs;
+use Nasqueron\Notifications\Jobs\Job;
use Nasqueron\Notifications\Jobs\NotifyNewCommitsToDiffusion;
-use Nasqueron\Notifications\Phabricator\PhabricatorAPI;
use Nasqueron\Notifications\Tests\TestCase;
-use Mockery;
-
class NotifyNewCommitsToDiffusionTest extends TestCase {
- /**
- * Mock for the Phabricator API factory
- * @var \Mockery\MockInterface
- */
- private $apiFactoryMock;
+ ///
+ /// Tests
+ ///
/**
- * The job to test
- * @var NotifyNewCommitsToDiffusion
+ * @dataProvider apiRepositoryReplyProvider
*/
- private $job;
+ public function testHandle ($apiRepositoryReply, int $apiCallCounts) {
+ $this->mockPhabricatorAPI()
+ ->shouldReceive('getForProject->call')
+ ->andReturn(
+ // First API call: repository.query
+ $apiRepositoryReply,
- public function setUp () {
- parent::setUp();
+ // Second API call: diffusion.looksoon
+ null
+ )
+ ->times($apiCallCounts); // 2 when repository.query is valid
+ // 1 otherwise
+
+ $job = $this->mockJob();
+ $job->handle();
+ }
- $this->apiFactoryMock = $this->mockPhabricatorAPI();
- $this->job = $this->mockJob();
+ public function testJobWhenThereIsNoPhabricatorInstanceForTheProject () {
+ $job = $this->mockJob("not-existing-project");
+ $job->handle();
}
+ ///
+ /// Helper methods
+ ///
+
/**
- * @return NotifyNewCommitsToDiffusion
+ * Mocks a job
*/
- protected function mockJob() {
- return new NotifyNewCommitsToDiffusion("acme", "ssh://acme/k2.git");
+ protected function mockJob(string $project = "acme") : Job {
+ return new NotifyNewCommitsToDiffusion(
+ $project,
+ "ssh://acme/k2.git"
+ );
}
- public function testJobWantsPhabricatorAPI () {
- $this->apiFactoryMock->shouldReceive('getForProject')->once();
- $this->job->handle();
+ /**
+ * Provides API repository reply and associated API calls count
+ */
+ public function apiRepositoryReplyProvider () : array {
+ return [
+ // Regular behavior
+ [[new class { public $callsign = "K2"; }], 2],
+
+ // Phabricator doesn't know this repo
+ [[], 1],
+
+ // Some error occurs and the API reply is null
+ [null, 1],
+ ];
}
}

File Metadata

Mime Type
text/plain
Expires
Sun, Nov 17, 10:46 (22 h, 1 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2249226
Default Alt Text
D807.diff (3 KB)

Event Timeline