Page MenuHomeDevCentral

D324.diff
No OneTemporary

D324.diff

diff --git a/tests/Phabricator/ProjectsMapTest.php b/tests/Phabricator/ProjectsMapTest.php
new file mode 100644
--- /dev/null
+++ b/tests/Phabricator/ProjectsMapTest.php
@@ -0,0 +1,190 @@
+<?php
+
+namespace Nasqueron\Notifications\Tests\Phabricator;
+
+use Nasqueron\Notifications\Phabricator\ProjectsMap;
+use Nasqueron\Notifications\Tests\TestCase;
+
+class ProjectsMapTest extends TestCase {
+
+ /**
+ * @var Nasqueron\Notifications\Phabricator\ProjectsMap
+ */
+ private $map;
+
+ public function setUp () {
+ parent::setUp();
+
+ //
+ // We mock the API, so an imaginary instance of Phabricator
+ // will return 3 results: Accounts, Agora & architecture.
+ //
+ // Agora has the key "PHID-PROJ-cztcgpvqr6smnnekotq7".
+ //
+
+ $this->mockPhabricatorAPIForProjectsMap();
+ $this->map = ProjectsMap::fetch("http://phabricator.acme.tld");
+ }
+
+ public function testIteratorIsTraversable () {
+ $this->assertInstanceOf(
+ "Traversable",
+ $this->map->getIterator()
+ );
+ }
+
+ ///
+ /// Tests for ArrayAccess
+ ///
+
+ public function testOffsetExistsWhenItDoes () {
+ $this->assertTrue(
+ $this->map->offsetExists("PHID-PROJ-cztcgpvqr6smnnekotq7")
+ );
+ }
+
+ public function testOffsetExistsWhenItDoesNot () {
+ $this->assertFalse(
+ $this->map->offsetExists("non-existing-key")
+ );
+ }
+
+ public function testOffsetGetWhenItDoesExist () {
+ $this->assertSame(
+ "Agora",
+ $this->map->offsetGet("PHID-PROJ-cztcgpvqr6smnnekotq7")
+ );
+ }
+
+ /**
+ * @expectedException ErrorException
+ */
+ public function testOffsetGetWhenItDoesNotExist () {
+ $this->map->offsetGet("non-existing-key");
+ }
+
+ /**
+ * @covers Nasqueron\Notifications\Phabricator\ProjectsMap::offsetSet
+ */
+ public function testOffsetSet () {
+ $this->map->offsetSet("newkey", "quux");
+ $this->assertSame("quux", $this->map->offsetGet("newkey"));
+ }
+
+ /**
+ * @covers Nasqueron\Notifications\Phabricator\ProjectsMap::offsetUnset
+ */
+ public function testOffsetUnset () {
+ unset($this->map["PHID-PROJ-cztcgpvqr6smnnekotq7"]);
+ $this->assertFalse(
+ $this->map->offsetExists("PHID-PROJ-cztcgpvqr6smnnekotq7")
+ );
+ }
+
+ ///
+ /// Tests for cache
+ ///
+
+ public function testCache () {
+ $this->assertFalse($this->map->isCached());
+ $this->map->saveToCache();
+ $this->assertTrue($this->map->isCached());
+ }
+
+ public function testLoadFromCache () {
+ $this->map->saveToCache();
+
+ $map = new ProjectsMap("http://phabricator.acme.tld");
+ $map->loadFromCache();
+
+ $this->assertTrue(
+ $map->offsetExists("PHID-PROJ-cztcgpvqr6smnnekotq7")
+ );
+ }
+
+ public function testLoadWhenInCache () {
+ $this->map->saveToCache();
+
+ $map = ProjectsMap::load("http://phabricator.acme.tld");
+ $this->assertTrue(
+ $map->offsetExists("PHID-PROJ-cztcgpvqr6smnnekotq7")
+ );
+ }
+
+ ///
+ /// Tests for helper methods
+ ///
+
+ public function testGetProjectName () {
+ $this->assertSame(
+ "Agora",
+ $this->map->getProjectName("PHID-PROJ-cztcgpvqr6smnnekotq7")
+ );
+ }
+
+ public function testGetProjectNameForNewInstance () {
+ $map = new ProjectsMap("http://phabricator.acme.tld");
+ $this->assertSame(
+ "Agora",
+ $map->getProjectName("PHID-PROJ-cztcgpvqr6smnnekotq7")
+ );
+ }
+
+ public function testGetProjectNameWhenItDoesNotExist () {
+ $this->assertSame(
+ "",
+ $this->map->getProjectName("non-existing-key")
+ );
+ }
+
+ public function testToArrayProducesArray () {
+ $array = $this->map->toArray();
+ $this->assertTrue(
+ is_array($array),
+ "Test if toArray return an array"
+ );
+ }
+
+ public function testThatArrayCount () {
+ $array = $this->map->toArray();
+ $this->assertSame(3, count($array));
+ }
+
+ public function testThatArrayContainsExpectedData () {
+ $this->assertSame(
+ [
+ ["PHID-PROJ-6dg6ogx5pjmk24ur4tp4", "Accounts"],
+ ["PHID-PROJ-cztcgpvqr6smnnekotq7", "Agora"],
+ ["PHID-PROJ-3iew3cqf3htpazfyzb5a", "architecture"]
+ ],
+ $this->map->toArray()
+ );
+ }
+
+ ///
+ /// Tests API
+ ///
+
+ private function mockPhabricatorAPIWithReply ($reply) {
+ $mock = $this->mockPhabricatorAPI();
+ $mock->shouldReceive('get->call')->andReturn($reply);
+ eval(\Psy\sh());
+ }
+
+ /**
+ * @expectedException Exception
+ */
+ public function testFetchFromAPIWithoutReply () {
+ $this->mockPhabricatorAPIWithReply(false);
+ ProjectsMap::fetch("http://phabricator.acme.tld");
+ }
+
+ /**
+ * @expectedException Exception
+ */
+ public function testFetchFromAPIInvalidReply () {
+ $this->mockPhabricatorAPIWithReply(new stdClass);
+ ProjectsMap::fetch("http://phabricator.acme.tld");
+ }
+
+}

File Metadata

Mime Type
text/plain
Expires
Fri, Dec 20, 10:07 (21 h, 14 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2307941
Default Alt Text
D324.diff (5 KB)

Event Timeline