|
3 | 3 | namespace swichers\Acsf\Client\Tests\Endpoints\Action;
|
4 | 4 |
|
5 | 5 | 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; |
6 | 9 |
|
7 | 10 | /**
|
8 | 11 | * Tests for the SitesTest Action.
|
@@ -74,4 +77,80 @@ public function testGetEntityType() {
|
74 | 77 | $this->assertSame('Site', $action->getEntityType());
|
75 | 78 | }
|
76 | 79 |
|
| 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 | + |
77 | 156 | }
|
0 commit comments