From 24b5e97e161a8f52438e5766ee6b1916a9fa2e99 Mon Sep 17 00:00:00 2001 From: Shaunak Kashyap Date: Fri, 27 Mar 2015 08:51:08 +0100 Subject: [PATCH 1/2] Adding convenience method to retrieve domain by name. --- .../Exceptions/DomainNotFoundException.php | 22 +++++++++++++++++++ lib/OpenCloud/DNS/Service.php | 19 ++++++++++++++++ tests/OpenCloud/Tests/DNS/ServiceTest.php | 9 ++++++++ 3 files changed, 50 insertions(+) create mode 100644 lib/OpenCloud/Common/Exceptions/DomainNotFoundException.php diff --git a/lib/OpenCloud/Common/Exceptions/DomainNotFoundException.php b/lib/OpenCloud/Common/Exceptions/DomainNotFoundException.php new file mode 100644 index 000000000..07ddfb608 --- /dev/null +++ b/lib/OpenCloud/Common/Exceptions/DomainNotFoundException.php @@ -0,0 +1,22 @@ +resource('Domain', $info); } + /** + * Returns a domain, given a domain name + * + * @param string $domainName Domain name + * @return Domain the domain object + */ + public function domainByName($domainName) + { + $domainList = $this->domainList(array("name" => $domainName)); + + if (count($domainList) != 1) { + throw new DomainNotFoundException(); + } + + return $this->resource('Domain', $domainList[0]); + } + + /** * Returns a collection of domains * diff --git a/tests/OpenCloud/Tests/DNS/ServiceTest.php b/tests/OpenCloud/Tests/DNS/ServiceTest.php index 73da7403e..5c299ff1c 100644 --- a/tests/OpenCloud/Tests/DNS/ServiceTest.php +++ b/tests/OpenCloud/Tests/DNS/ServiceTest.php @@ -40,6 +40,15 @@ public function testDomain() $this->assertInstanceOf('OpenCloud\DNS\Resource\Domain', $this->service->domain()); } + public function testDomainByName() + { + $this->addMockSubscriber($this->makeResponse('{"domains":[{"name":"region2.example.net","id":2725352,"updated":"2011-06-23T20:21:06.000+0000","created":"2011-06-23T19:24:27.000+0000"}],"totalEntries":114}', 200)); + $domain = $this->service->domainByName("region2.example.net"); + + $this->assertInstanceOf('OpenCloud\DNS\Resource\Domain', $domain); + $this->assertEquals("region2.example.net", $domain->getName()); + } + /** * @mockFile Domain_List */ From 920e1f3b14f6e54ce4146db11863e60afe5cfec5 Mon Sep 17 00:00:00 2001 From: Shaunak Kashyap Date: Fri, 27 Mar 2015 08:58:48 +0100 Subject: [PATCH 2/2] Adding test for domain not found, given domain name. --- tests/OpenCloud/Tests/DNS/ServiceTest.php | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/tests/OpenCloud/Tests/DNS/ServiceTest.php b/tests/OpenCloud/Tests/DNS/ServiceTest.php index 5c299ff1c..9503e0409 100644 --- a/tests/OpenCloud/Tests/DNS/ServiceTest.php +++ b/tests/OpenCloud/Tests/DNS/ServiceTest.php @@ -49,6 +49,15 @@ public function testDomainByName() $this->assertEquals("region2.example.net", $domain->getName()); } + /** + * @expectedException OpenCloud\Common\Exceptions\DomainNotFoundException + */ + public function testDomainByNameWhenDomainNotFound() + { + $this->addMockSubscriber($this->makeResponse('{"domains":[],"totalEntries":114}', 200)); + $domain = $this->service->domainByName("region2.example.net"); + } + /** * @mockFile Domain_List */