Skip to content

Commit f0027c4

Browse files
committed
Run PHPStan over the tests too, and fix what it found
Adds tests/ to the PHPStan paths so the test suite is held to the same level 5 as src. The nine errors that surfaced: Resource::setId() and setIdOrFail() were declared ": self", which resolves to Resource rather than the called class, so chaining off them lost the concrete type. A "@return static" docblock fixes it at the source, which also helps anyone running static analysis against the SDK. Two of the findings were real bugs in the live tests: SiteTest::testCreateExampleDotCom() dropped the result of its own recursive call and then fell off the end of a method declared to return stdClass, so the @Depends chain got null after cleaning up a leftover site. Its $foundSite flag was also dead - never set true, so the break was unreachable. SshKeyTest::testDeleteSshKey() wrapped its only assertion in "if (!empty($sshKey))" on a parameter typed stdClass, which can never be empty. Harmless, but it hid that the guard did nothing. The rest were assertions that could not fail: assertIsArray() on Response::toArray() and on getHistory(), both of which are declared to return arrays. Replaced with assertions that check something.
1 parent 6965b2f commit f0027c4

6 files changed

Lines changed: 19 additions & 14 deletions

File tree

phpstan.neon

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ parameters:
22
level: 5
33
paths:
44
- src
5+
- tests

src/Ploi/Resources/Resource.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ public function __construct(?Ploi $ploi = null, ?int $id = null)
3131
}
3232
}
3333

34+
/**
35+
* @return static
36+
*/
3437
public function setId(?int $id = null): self
3538
{
3639
$this->id = $id;
@@ -40,6 +43,9 @@ public function setId(?int $id = null): self
4043
return $this;
4144
}
4245

46+
/**
47+
* @return static
48+
*/
4349
public function setIdOrFail(?int $id = null): self
4450
{
4551
if ($id) {

tests/Integration/Resources/ServerTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public function testGetAllServers()
5959
$this->assertInstanceOf(\stdClass::class, $servers->getJson());
6060

6161
// Test the array response
62-
$this->assertIsArray($servers->toArray());
62+
$this->assertSame(['json', 'response'], array_keys($servers->toArray()));
6363

6464
// Test to make sure that the data is an array
6565
$this->assertIsArray($servers->getJson()->data);
@@ -84,8 +84,8 @@ public function testGetPaginatedServers()
8484
$this->assertInstanceOf(\stdClass::class, $serversPage2->getJson());
8585

8686
// Test the array response
87-
$this->assertIsArray($serversPage1->toArray());
88-
$this->assertIsArray($serversPage2->toArray());
87+
$this->assertSame(['json', 'response'], array_keys($serversPage1->toArray()));
88+
$this->assertSame(['json', 'response'], array_keys($serversPage2->toArray()));
8989

9090
// Test responses contain paginated result
9191
$this->assertEquals(1, $serversPage1->getJson()->meta->current_page);

tests/Integration/Resources/SiteTest.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -102,18 +102,16 @@ public function testCreateExampleDotCom(): stdClass
102102
$this->assertInstanceOf(NotValid::class, $e);
103103

104104
$allSites = $this->server->sites()->get();
105-
$foundSite = false;
106-
foreach ($allSites->getJson()->data as $site) {
107-
if ($foundSite) {
108-
break;
109-
}
110105

106+
foreach ($allSites->getJson()->data as $site) {
111107
if ($site->domain === 'example.com') {
112108
$this->server->sites($site->id)->delete();
113109

114-
$this->testCreateExampleDotCom();
110+
return $this->testCreateExampleDotCom();
115111
}
116112
}
113+
114+
$this->fail('Could not create example.com and found no existing site to remove');
117115
}
118116
}
119117

tests/Integration/Resources/SshKeyTest.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,9 @@ public function testCreateSshKey(): stdClass
9898
*/
9999
public function testDeleteSshKey(stdClass $sshKey)
100100
{
101-
if (!empty($sshKey)) {
102-
$deleted = $this->server->sshKeys($sshKey->id)->delete();
103-
$this->assertTrue($deleted->getResponse()->getStatusCode() === 200);
104-
}
101+
$deleted = $this->server->sshKeys($sshKey->id)->delete();
102+
103+
$this->assertTrue($deleted->getResponse()->getStatusCode() === 200);
105104
}
106105

107106
public function testDeleteInvalidSshKey()

tests/Unit/Traits/HistoryTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ protected function setUp(): void
2323

2424
public function testGetHistory(): void
2525
{
26-
$this->assertIsArray($this->resource->getHistory());
26+
// Constructing a resource already records the Ploi instance being set
27+
$this->assertNotEmpty($this->resource->getHistory());
2728
}
2829

2930
public function testAddHistory(): void

0 commit comments

Comments
 (0)