From 9b36827b565037b6f71bd8eb0075fd2eace6d325 Mon Sep 17 00:00:00 2001 From: Art4 Date: Thu, 21 Sep 2023 10:59:00 +0200 Subject: [PATCH] Reach PHPStan level 4, simplify MockClient, do not run PHPStan on PHP 7.4 --- .github/workflows/tests.yml | 1 + .phpstan.neon | 2 +- src/Redmine/Client/NativeCurlClient.php | 4 +- tests/Fixtures/MockClient.php | 24 ++++------- tests/Integration/GroupXmlTest.php | 16 ++------ tests/Integration/IssueCategoryXmlTest.php | 16 ++------ tests/Integration/IssueXmlTest.php | 22 +++------- tests/Integration/MembershipXmlTest.php | 16 ++------ tests/Integration/ProjectXmlTest.php | 18 ++------ tests/Integration/UrlTest.php | 48 +++++++++------------- tests/Integration/UserXmlTest.php | 16 ++------ tests/Integration/WikiXmlTest.php | 14 +------ 12 files changed, 55 insertions(+), 142 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index ff743b02..8607ffe3 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -41,6 +41,7 @@ jobs: run: composer run phpstan -- --error-format=github - name: Run tests + if: ${{ matrix.php-versions >= '8.0' }} run: vendor/bin/phpunit --coverage-text - name: Upload coverage reports to Codecov diff --git a/.phpstan.neon b/.phpstan.neon index 66b89dae..97e931b9 100644 --- a/.phpstan.neon +++ b/.phpstan.neon @@ -1,5 +1,5 @@ parameters: - level: 3 + level: 4 paths: - src/ diff --git a/src/Redmine/Client/NativeCurlClient.php b/src/Redmine/Client/NativeCurlClient.php index 2bf143e0..124f9c0d 100644 --- a/src/Redmine/Client/NativeCurlClient.php +++ b/src/Redmine/Client/NativeCurlClient.php @@ -245,7 +245,9 @@ private function request(string $method, string $path, string $body = ''): bool /** * Prepare the request by setting the cURL options. * - * @return resource|\CurlHandle a cURL handle on success, FALSE on errors + * BC for PHP 7.4: Do not add the return type because CurlHandle was introduced in PHP 8.0 + * + * @return \CurlHandle a cURL handle on success, FALSE on errors */ private function createCurl(string $method, string $path, string $body = '') { diff --git a/tests/Fixtures/MockClient.php b/tests/Fixtures/MockClient.php index 57860c1b..593da1cc 100644 --- a/tests/Fixtures/MockClient.php +++ b/tests/Fixtures/MockClient.php @@ -12,10 +12,15 @@ * The runRequest method of this client class just returns the value of * the path, method and data or the $runRequestReturnValue value if set. */ -class MockClient implements Client +final class MockClient implements Client { use ClientApiTrait; + public static function create() + { + return new self(); + } + /** * Return value the mocked runRequest method should return. * @@ -34,22 +39,7 @@ class MockClient implements Client public $responseCodeMock; public $responseContentTypeMock; - private string $url; - private string $apikeyOrUsername; - private ?string $password; - - /** - * $apikeyOrUsername should be your ApiKey, but it could also be your username. - * $password needs to be set if a username is given (not recommended). - */ - public function __construct( - string $url, - string $apikeyOrUsername, - string $password = null - ) { - $this->url = $url; - $this->apikeyOrUsername = $apikeyOrUsername; - $this->password = $password; + private function __csontruct() { } /** diff --git a/tests/Integration/GroupXmlTest.php b/tests/Integration/GroupXmlTest.php index a45d68c0..3dce0c88 100644 --- a/tests/Integration/GroupXmlTest.php +++ b/tests/Integration/GroupXmlTest.php @@ -11,20 +11,10 @@ class GroupXmlTest extends TestCase { - /** - * @var MockClient - */ - private $client; - - public function setup(): void - { - $this->client = new MockClient('http://test.local', 'asdf'); - } - public function testCreateBlank() { /** @var \Redmine\Api\Group */ - $api = $this->client->getApi('group'); + $api = MockClient::create()->getApi('group'); $this->assertInstanceOf('Redmine\Api\Group', $api); $this->expectException(MissingParameterException::class); @@ -36,7 +26,7 @@ public function testCreateBlank() public function testCreateComplex() { /** @var \Redmine\Api\Group */ - $api = $this->client->getApi('group'); + $api = MockClient::create()->getApi('group'); $res = $api->create([ 'name' => 'Developers', 'user_ids' => [3, 5], @@ -58,7 +48,7 @@ public function testCreateComplex() public function testUpdateNotImplemented() { /** @var \Redmine\Api\Group */ - $api = $this->client->getApi('group'); + $api = MockClient::create()->getApi('group'); $this->assertInstanceOf('Redmine\Api\Group', $api); $this->expectException(Exception::class); diff --git a/tests/Integration/IssueCategoryXmlTest.php b/tests/Integration/IssueCategoryXmlTest.php index ab267228..d4bbcbbf 100644 --- a/tests/Integration/IssueCategoryXmlTest.php +++ b/tests/Integration/IssueCategoryXmlTest.php @@ -10,20 +10,10 @@ class IssueCategoryXmlTest extends TestCase { - /** - * @var MockClient - */ - private $client; - - public function setup(): void - { - $this->client = new MockClient('http://test.local', 'asdf'); - } - public function testCreateBlank() { /** @var \Redmine\Api\IssueCategory */ - $api = $this->client->getApi('issue_category'); + $api = MockClient::create()->getApi('issue_category'); $this->assertInstanceOf('Redmine\Api\IssueCategory', $api); $this->expectException(MissingParameterException::class); @@ -35,7 +25,7 @@ public function testCreateBlank() public function testCreateComplex() { /** @var \Redmine\Api\IssueCategory */ - $api = $this->client->getApi('issue_category'); + $api = MockClient::create()->getApi('issue_category'); $res = $api->create('otherProject', [ 'name' => 'test category', ]); @@ -51,7 +41,7 @@ public function testCreateComplex() public function testUpdate() { /** @var \Redmine\Api\IssueCategory */ - $api = $this->client->getApi('issue_category'); + $api = MockClient::create()->getApi('issue_category'); $res = $api->update(1, [ 'name' => 'new category name', ]); diff --git a/tests/Integration/IssueXmlTest.php b/tests/Integration/IssueXmlTest.php index 0a5fa223..ab102d68 100644 --- a/tests/Integration/IssueXmlTest.php +++ b/tests/Integration/IssueXmlTest.php @@ -9,20 +9,10 @@ class IssueXmlTest extends TestCase { - /** - * @var MockClient - */ - private $client; - - public function setup(): void - { - $this->client = new MockClient('http://test.local', 'asdf'); - } - public function testCreateBlank() { /** @var \Redmine\Api\Issue */ - $api = $this->client->getApi('issue'); + $api = MockClient::create()->getApi('issue'); $this->assertInstanceOf('Redmine\Api\Issue', $api); $xml = ' @@ -36,7 +26,7 @@ public function testCreateBlank() public function testCreateComplexWithUpload() { /** @var \Redmine\Api\Issue */ - $api = $this->client->getApi('issue'); + $api = MockClient::create()->getApi('issue'); $res = $api->create([ 'project_id' => 'myproject', 'subject' => 'A test issue', @@ -72,7 +62,7 @@ public function testCreateComplexWithUpload() public function testCreateComplex() { /** @var \Redmine\Api\Issue */ - $api = $this->client->getApi('issue'); + $api = MockClient::create()->getApi('issue'); $res = $api->create([ 'project_id' => 'test', 'subject' => 'test api (xml) 3', @@ -117,7 +107,7 @@ public function testCreateComplex() public function testCreateComplexWithLineBreakInDescription() { /** @var \Redmine\Api\Issue */ - $api = $this->client->getApi('issue'); + $api = MockClient::create()->getApi('issue'); $res = $api->create([ 'project_id' => 'test', 'subject' => 'test api (xml) 3', @@ -163,7 +153,7 @@ public function testCreateComplexWithLineBreakInDescription() public function testUpdateIssue() { /** @var \Redmine\Api\Issue */ - $api = $this->client->getApi('issue'); + $api = MockClient::create()->getApi('issue'); $res = $api->update(1, [ 'subject' => 'test note (xml) 1', 'notes' => 'test note api', @@ -193,7 +183,7 @@ public function testUpdateIssue() public function testAddNoteToIssue() { /** @var \Redmine\Api\Issue */ - $api = $this->client->getApi('issue'); + $api = MockClient::create()->getApi('issue'); $res = $api->addNoteToIssue(1, 'some comment'); $res = json_decode($res, true); diff --git a/tests/Integration/MembershipXmlTest.php b/tests/Integration/MembershipXmlTest.php index 1da77423..32f8b3fe 100644 --- a/tests/Integration/MembershipXmlTest.php +++ b/tests/Integration/MembershipXmlTest.php @@ -10,20 +10,10 @@ class MembershipXmlTest extends TestCase { - /** - * @var MockClient - */ - private $client; - - public function setup(): void - { - $this->client = new MockClient('http://test.local', 'asdf'); - } - public function testCreateBlank() { /** @var \Redmine\Api\Membership */ - $api = $this->client->getApi('membership'); + $api = MockClient::create()->getApi('membership'); $this->assertInstanceOf('Redmine\Api\Membership', $api); $this->expectException(MissingParameterException::class); @@ -35,7 +25,7 @@ public function testCreateBlank() public function testCreateComplex() { /** @var \Redmine\Api\Membership */ - $api = $this->client->getApi('membership'); + $api = MockClient::create()->getApi('membership'); $res = $api->create('otherProject', [ 'user_id' => 1, 'role_ids' => [1, 2], @@ -56,7 +46,7 @@ public function testCreateComplex() public function testUpdate() { /** @var \Redmine\Api\Membership */ - $api = $this->client->getApi('membership'); + $api = MockClient::create()->getApi('membership'); $res = $api->update(1, [ 'role_ids' => [1, 2], ]); diff --git a/tests/Integration/ProjectXmlTest.php b/tests/Integration/ProjectXmlTest.php index 88639869..ceb45eb8 100644 --- a/tests/Integration/ProjectXmlTest.php +++ b/tests/Integration/ProjectXmlTest.php @@ -10,20 +10,10 @@ class ProjectXmlTest extends TestCase { - /** - * @var MockClient - */ - private $client; - - public function setup(): void - { - $this->client = new MockClient('http://test.local', 'asdf'); - } - public function testCreateBlank() { /** @var \Redmine\Api\Project */ - $api = $this->client->getApi('project'); + $api = MockClient::create()->getApi('project'); $this->assertInstanceOf('Redmine\Api\Project', $api); $this->expectException(MissingParameterException::class); @@ -35,7 +25,7 @@ public function testCreateBlank() public function testCreateComplex() { /** @var \Redmine\Api\Project */ - $api = $this->client->getApi('project'); + $api = MockClient::create()->getApi('project'); $res = $api->create([ 'name' => 'some name', 'identifier' => 'the_identifier', @@ -70,7 +60,7 @@ public function testCreateComplex() public function testCreateComplexWithTrackerIds() { /** @var \Redmine\Api\Project */ - $api = $this->client->getApi('project'); + $api = MockClient::create()->getApi('project'); $res = $api->create([ 'name' => 'some name', 'identifier' => 'the_identifier', @@ -96,7 +86,7 @@ public function testCreateComplexWithTrackerIds() public function testUpdate() { /** @var \Redmine\Api\Project */ - $api = $this->client->getApi('project'); + $api = MockClient::create()->getApi('project'); $res = $api->update(1, [ 'name' => 'different name', ]); diff --git a/tests/Integration/UrlTest.php b/tests/Integration/UrlTest.php index 795ad808..549ebebf 100644 --- a/tests/Integration/UrlTest.php +++ b/tests/Integration/UrlTest.php @@ -7,20 +7,10 @@ class UrlTest extends TestCase { - /** - * @var MockClient - */ - private $client; - - public function setup(): void - { - $this->client = new MockClient('http://test.local', 'asdf'); - } - public function testAttachment() { /** @var \Redmine\Api\Attachment */ - $api = $this->client->getApi('attachment'); + $api = MockClient::create()->getApi('attachment'); $res = $api->show(1); $this->assertEquals('/attachments/1.json', $res['path']); @@ -36,7 +26,7 @@ public function testAttachment() public function testCustomFields() { /** @var \Redmine\Api\CustomField */ - $api = $this->client->getApi('custom_fields'); + $api = MockClient::create()->getApi('custom_fields'); $res = $api->all(); $this->assertEquals('/custom_fields.json', $res['path']); @@ -46,7 +36,7 @@ public function testCustomFields() public function testGroup() { /** @var \Redmine\Api\Group */ - $api = $this->client->getApi('group'); + $api = MockClient::create()->getApi('group'); $res = $api->create([ 'name' => 'asdf', ]); @@ -87,7 +77,7 @@ public function testGroup() public function testIssue() { /** @var \Redmine\Api\Issue */ - $api = $this->client->getApi('issue'); + $api = MockClient::create()->getApi('issue'); $res = $api->create([ 'name' => 'asdf', ]); @@ -144,7 +134,7 @@ public function testIssue() public function testIssueCategory() { /** @var \Redmine\Api\IssueCategory */ - $api = $this->client->getApi('issue_category'); + $api = MockClient::create()->getApi('issue_category'); $res = $api->create('testProject', [ 'name' => 'asdf', ]); @@ -187,7 +177,7 @@ public function testIssueCategory() public function testIssuePriority() { /** @var \Redmine\Api\IssuePriority */ - $api = $this->client->getApi('issue_priority'); + $api = MockClient::create()->getApi('issue_priority'); $res = $api->all(); $this->assertEquals('/enumerations/issue_priorities.json', $res['path']); @@ -197,7 +187,7 @@ public function testIssuePriority() public function testIssueRelation() { /** @var \Redmine\Api\IssueRelation */ - $api = $this->client->getApi('issue_relation'); + $api = MockClient::create()->getApi('issue_relation'); $res = $api->all(1); $this->assertEquals('/issues/1/relations.json', $res['path']); @@ -216,7 +206,7 @@ public function testIssueRelation() public function testIssueStatus() { /** @var \Redmine\Api\IssueStatus */ - $api = $this->client->getApi('issue_status'); + $api = MockClient::create()->getApi('issue_status'); $res = $api->all(); $this->assertEquals('/issue_statuses.json', $res['path']); @@ -226,7 +216,7 @@ public function testIssueStatus() public function testMembership() { /** @var \Redmine\Api\Membership */ - $api = $this->client->getApi('membership'); + $api = MockClient::create()->getApi('membership'); $res = $api->create('testProject', [ 'user_id' => 1, 'role_ids' => [1], @@ -260,7 +250,7 @@ public function testMembership() public function testNews() { /** @var \Redmine\Api\News */ - $api = $this->client->getApi('news'); + $api = MockClient::create()->getApi('news'); $res = $api->all(); $this->assertEquals('/news.json', $res['path']); @@ -275,7 +265,7 @@ public function testNews() public function testProject() { /** @var \Redmine\Api\Project */ - $api = $this->client->getApi('project'); + $api = MockClient::create()->getApi('project'); $res = $api->create([ 'name' => 'asdf', 'identifier' => 'asdf', @@ -313,7 +303,7 @@ public function testProject() public function testQuery() { /** @var \Redmine\Api\Query */ - $api = $this->client->getApi('query'); + $api = MockClient::create()->getApi('query'); $res = $api->all(); $this->assertEquals('/queries.json', $res['path']); @@ -323,7 +313,7 @@ public function testQuery() public function testRole() { /** @var \Redmine\Api\Role */ - $api = $this->client->getApi('role'); + $api = MockClient::create()->getApi('role'); $res = $api->all(); $this->assertEquals('/roles.json', $res['path']); @@ -338,7 +328,7 @@ public function testRole() public function testTimeEntry() { /** @var \Redmine\Api\TimeEntry */ - $api = $this->client->getApi('time_entry'); + $api = MockClient::create()->getApi('time_entry'); $res = $api->create([ 'issue_id' => 1, 'hours' => 12, @@ -388,7 +378,7 @@ public function testTimeEntry() public function testTimeEntryActivity() { /** @var \Redmine\Api\TimeEntryActivity */ - $api = $this->client->getApi('time_entry_activity'); + $api = MockClient::create()->getApi('time_entry_activity'); $res = $api->all(); $this->assertEquals('/enumerations/time_entry_activities.json', $res['path']); @@ -398,7 +388,7 @@ public function testTimeEntryActivity() public function testTracker() { /** @var \Redmine\Api\Tracker */ - $api = $this->client->getApi('tracker'); + $api = MockClient::create()->getApi('tracker'); $res = $api->all(); $this->assertEquals('/trackers.json', $res['path']); @@ -408,7 +398,7 @@ public function testTracker() public function testUser() { /** @var \Redmine\Api\User */ - $api = $this->client->getApi('user'); + $api = MockClient::create()->getApi('user'); $res = $api->create([ 'login' => 'asdf', 'lastname' => 'asdf', @@ -461,7 +451,7 @@ public function testUser() public function testVersion() { /** @var \Redmine\Api\Version */ - $api = $this->client->getApi('version'); + $api = MockClient::create()->getApi('version'); $res = $api->create('testProject', [ 'name' => 'asdf', ]); @@ -496,7 +486,7 @@ public function testVersion() public function testWiki() { /** @var \Redmine\Api\Wiki */ - $api = $this->client->getApi('wiki'); + $api = MockClient::create()->getApi('wiki'); $res = $api->create('testProject', 'about', [ 'text' => 'asdf', 'comments' => 'asdf', diff --git a/tests/Integration/UserXmlTest.php b/tests/Integration/UserXmlTest.php index 19f1e4bd..0b25116d 100644 --- a/tests/Integration/UserXmlTest.php +++ b/tests/Integration/UserXmlTest.php @@ -10,20 +10,10 @@ class UserXmlTest extends TestCase { - /** - * @var MockClient - */ - private $client; - - public function setup(): void - { - $this->client = new MockClient('http://test.local', 'asdf'); - } - public function testCreateBlank() { /** @var \Redmine\Api\User */ - $api = $this->client->getApi('user'); + $api = MockClient::create()->getApi('user'); $this->assertInstanceOf('Redmine\Api\User', $api); $this->expectException(MissingParameterException::class); @@ -35,7 +25,7 @@ public function testCreateBlank() public function testCreateComplex() { /** @var \Redmine\Api\User */ - $api = $this->client->getApi('user'); + $api = MockClient::create()->getApi('user'); $res = $api->create([ 'login' => 'test', 'firstname' => 'test', @@ -57,7 +47,7 @@ public function testCreateComplex() public function testUpdate() { /** @var \Redmine\Api\User */ - $api = $this->client->getApi('user'); + $api = MockClient::create()->getApi('user'); $res = $api->update(1, [ 'firstname' => 'Raul', ]); diff --git a/tests/Integration/WikiXmlTest.php b/tests/Integration/WikiXmlTest.php index 096df772..f9c995a4 100644 --- a/tests/Integration/WikiXmlTest.php +++ b/tests/Integration/WikiXmlTest.php @@ -9,20 +9,10 @@ class WikiXmlTest extends TestCase { - /** - * @var MockClient - */ - private $client; - - public function setup(): void - { - $this->client = new MockClient('http://test.local', 'asdf'); - } - public function testCreateComplex() { /** @var \Redmine\Api\Wiki */ - $api = $this->client->getApi('wiki'); + $api = MockClient::create()->getApi('wiki'); $res = $api->create('testProject', 'about', [ 'text' => 'asdf', 'comments' => 'asdf', @@ -42,7 +32,7 @@ public function testCreateComplex() public function testUpdate() { /** @var \Redmine\Api\Wiki */ - $api = $this->client->getApi('wiki'); + $api = MockClient::create()->getApi('wiki'); $res = $api->update('testProject', 'about', [ 'text' => 'asdf', 'comments' => 'asdf',