Skip to content
This repository was archived by the owner on Oct 19, 2021. It is now read-only.

Commit bd0e2f7

Browse files
authored
Fixes #58: Add a getByName method to the Sites action. (#60)
* Issue #58: Add a getByName method to the Sites action. * Issue #58: Add missing return value description. * Issue #58: Add code coverage for Sites changes. * Issue #58: Add missing comma for Travis. * Issue #58: Correct @Covers statement.
1 parent 8ee5ae0 commit bd0e2f7

File tree

6 files changed

+109
-5
lines changed

6 files changed

+109
-5
lines changed

src/Endpoints/Action/Sites.php

+26-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
namespace swichers\Acsf\Client\Endpoints\Action;
44

5+
use swichers\Acsf\Client\Endpoints\Entity\EntityInterface;
56
use swichers\Acsf\Client\Endpoints\ValidationTrait;
7+
use swichers\Acsf\Client\Exceptions\MissingEntityException;
68

79
/**
810
* ACSF Endpoint Wrapper: Sites.
@@ -260,7 +262,7 @@ public function listAll() : array {
260262
];
261263

262264
$site_info = $this->list($options);
263-
$sites['sites'] = array_merge($sites['sites'], $site_info['sites'] ?: []);
265+
$sites['sites'] = array_merge($sites['sites'], $site_info['sites'] ?? []);
264266

265267
$has_more =
266268
!empty($site_info['count']) &&
@@ -293,4 +295,27 @@ public function getAll() : array {
293295
return $sites ?: [];
294296
}
295297

298+
/**
299+
* Get a Site by its human name instead of ID.
300+
*
301+
* @param string $name
302+
* The human name of a site factory site.
303+
*
304+
* @return \swichers\Acsf\Client\Endpoints\Entity\EntityInterface
305+
* The site that matches the given name.
306+
*
307+
* @throws \swichers\Acsf\Client\Exceptions\MissingEntityException
308+
*/
309+
public function getByName(string $name) : EntityInterface {
310+
311+
$sites_list = $this->listAll()['sites'] ?? [];
312+
foreach ($sites_list as $info) {
313+
if ($info['site'] == $name) {
314+
return $this->get($info['id']);
315+
}
316+
}
317+
318+
throw new MissingEntityException(sprintf('Unable to load Site with the name %s.', $name));
319+
}
320+
296321
}

tests/Unit/ClientTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -503,7 +503,7 @@ public function testGetEntityFailId() {
503503
/**
504504
* {@inheritdoc}
505505
*/
506-
protected function setUp() {
506+
protected function setUp() : void {
507507

508508
parent::setUp();
509509

tests/Unit/Discovery/ManagerTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ public function testGet() {
9292
/**
9393
* {@inheritdoc}
9494
*/
95-
protected function setUp() {
95+
protected function setUp() : void {
9696

9797
parent::setUp();
9898

tests/Unit/Endpoints/Action/AbstractActionTestBase.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ abstract class AbstractActionTestBase extends TestCase {
1818
/**
1919
* {@inheritdoc}
2020
*/
21-
protected function setUp() {
21+
protected function setUp() : void {
2222

2323
parent::setUp();
2424

tests/Unit/Endpoints/Action/AbstractEntityActionTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public function testGetFail() {
5858
/**
5959
* {@inheritdoc}
6060
*/
61-
protected function setUp() {
61+
protected function setUp() : void {
6262

6363
parent::setUp();
6464

tests/Unit/Endpoints/Action/SitesTest.php

+79
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
namespace swichers\Acsf\Client\Tests\Endpoints\Action;
44

55
use swichers\Acsf\Client\Endpoints\Action\Sites;
6+
use swichers\Acsf\Client\Endpoints\Entity\EntityInterface;
7+
use swichers\Acsf\Client\Endpoints\Entity\Site;
8+
use swichers\Acsf\Client\Exceptions\MissingEntityException;
69

710
/**
811
* Tests for the SitesTest Action.
@@ -74,4 +77,80 @@ public function testGetEntityType() {
7477
$this->assertSame('Site', $action->getEntityType());
7578
}
7679

80+
/**
81+
* Validates we can get a list of all Sites despite pagination.
82+
*
83+
* @covers ::listAll
84+
*/
85+
public function testListAll() {
86+
87+
$action = $this->getMockBuilder(Sites::class)
88+
->setConstructorArgs([$this->mockClient])
89+
->setMethods(['list'])
90+
->getMock();
91+
$action->method('list')->willReturnCallback(function ($options) {
92+
$page = $options['page'] ?: 1;
93+
94+
if ($page == 1) {
95+
$sites = range(1, 10);
96+
}
97+
elseif ($page == 2) {
98+
$sites = range(1, 5);
99+
}
100+
101+
return ['sites' => $sites ?: [], 'count' => 15];
102+
});
103+
104+
$list = $action->listAll();
105+
$this->assertArrayHasKey('sites', $list);
106+
$this->assertArrayHasKey('count', $list);
107+
$this->assertCount(15, $list['sites']);
108+
$this->assertEquals(15, $list['count']);
109+
}
110+
111+
/**
112+
* Validates we can get a Site by its name.
113+
*
114+
* @covers ::getByName
115+
*/
116+
public function testGetByName() {
117+
118+
$action = $this->getMockBuilder(Sites::class)
119+
->setConstructorArgs([$this->mockClient])
120+
->setMethods(['listAll', 'get'])
121+
->getMock();
122+
123+
$action->method('get')->willReturnMap([
124+
[123, new Site($this->mockClient, 123)],
125+
]);
126+
127+
$action->method('listAll')->willReturn([
128+
'sites' => [
129+
[
130+
'site' => 'UnitTest',
131+
'id' => 123,
132+
],
133+
],
134+
]);
135+
136+
/** @var \swichers\Acsf\Client\Endpoints\Entity\Site $site */
137+
$site = $action->getByName('UnitTest');
138+
$this->assertInstanceOf(EntityInterface::class, $site);
139+
$this->assertEquals(123, $site->id());
140+
}
141+
142+
/**
143+
* Validate we get a MissingEntityException if a bad name is given.
144+
*
145+
* @covers ::getByName
146+
*
147+
* @depends testGetByName
148+
*/
149+
public function testGetByNameMissing() {
150+
151+
$action = new Sites($this->getMockAcsfClient());
152+
$this->expectException(MissingEntityException::class);
153+
$action->getByName('No site');
154+
}
155+
77156
}

0 commit comments

Comments
 (0)