Skip to content

Commit

Permalink
Reach PHPStan level 4, simplify MockClient, do not run PHPStan on PHP…
Browse files Browse the repository at this point in the history
… 7.4
  • Loading branch information
Art4 committed Sep 21, 2023
1 parent 23c82bb commit 9b36827
Show file tree
Hide file tree
Showing 12 changed files with 55 additions and 142 deletions.
1 change: 1 addition & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion .phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
parameters:
level: 3
level: 4

paths:
- src/
Expand Down
4 changes: 3 additions & 1 deletion src/Redmine/Client/NativeCurlClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -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, <b>FALSE</b> 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, <b>FALSE</b> on errors
*/
private function createCurl(string $method, string $path, string $body = '')

Check failure on line 252 in src/Redmine/Client/NativeCurlClient.php

View workflow job for this annotation

GitHub Actions / Tests (PHP 7.4 on ubuntu-latest)

Method Redmine\Client\NativeCurlClient::createCurl() has invalid return type CurlHandle.
{
Expand Down
24 changes: 7 additions & 17 deletions tests/Fixtures/MockClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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() {

Check failure on line 42 in tests/Fixtures/MockClient.php

View workflow job for this annotation

GitHub Actions / Tests (PHP 7.4 on ubuntu-latest)

Method Redmine\Tests\Fixtures\MockClient::__csontruct() is unused.

Check failure on line 42 in tests/Fixtures/MockClient.php

View workflow job for this annotation

GitHub Actions / Tests (PHP 8.0 on ubuntu-latest)

Method Redmine\Tests\Fixtures\MockClient::__csontruct() is unused.

Check failure on line 42 in tests/Fixtures/MockClient.php

View workflow job for this annotation

GitHub Actions / Tests (PHP 8.1 on ubuntu-latest)

Method Redmine\Tests\Fixtures\MockClient::__csontruct() is unused.

Check failure on line 42 in tests/Fixtures/MockClient.php

View workflow job for this annotation

GitHub Actions / Tests (PHP 8.2 on ubuntu-latest)

Method Redmine\Tests\Fixtures\MockClient::__csontruct() is unused.
}

/**
Expand Down
16 changes: 3 additions & 13 deletions tests/Integration/GroupXmlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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],
Expand All @@ -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);
Expand Down
16 changes: 3 additions & 13 deletions tests/Integration/IssueCategoryXmlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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',
]);
Expand All @@ -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',
]);
Expand Down
22 changes: 6 additions & 16 deletions tests/Integration/IssueXmlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '<?xml version="1.0"?>
Expand All @@ -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',
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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);

Expand Down
16 changes: 3 additions & 13 deletions tests/Integration/MembershipXmlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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],
Expand All @@ -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],
]);
Expand Down
18 changes: 4 additions & 14 deletions tests/Integration/ProjectXmlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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',
Expand Down Expand Up @@ -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',
Expand All @@ -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',
]);
Expand Down
Loading

0 comments on commit 9b36827

Please sign in to comment.