Skip to content

Commit 3020882

Browse files
authored
Merge pull request #1 from Gared/add_phpstan
Add phpstan to project
2 parents cfe9ada + cefba09 commit 3020882

File tree

6 files changed

+30
-40
lines changed

6 files changed

+30
-40
lines changed

.github/workflows/php.yml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,5 @@ jobs:
3737
php_version: 7.4
3838
version: 9
3939

40-
- name: PHPStan
41-
uses: docker://oskarstark/phpstan-ga
42-
with:
43-
args: analyse lib/ --level=5
40+
- name: Run PHPStan
41+
run: vendor/bin/phpstan

composer.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@
3030
"phpdocumentor/reflection-docblock": "^5.2"
3131
},
3232
"require-dev": {
33-
"phpunit/phpunit": "^9.5"
33+
"phpunit/phpunit": "^9.5",
34+
"phpstan/phpstan": "^1.0",
35+
"phpstan/phpstan-phpunit": "^1.0",
36+
"phpstan/phpstan-strict-rules": "^1.0"
3437
}
3538
}

lib/HTTP/Client.php

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
namespace ShellyClient\HTTP;
55

66
use GuzzleHttp\ClientInterface;
7+
use GuzzleHttp\RequestOptions;
78
use ShellyClient\Model\Request\SettingsActionsRequest;
89
use ShellyClient\Model\Request\SettingsLightRequest;
910
use ShellyClient\Model\Response\SettingsActionsResponse;
@@ -29,11 +30,7 @@
2930
class Client
3031
{
3132
private ClientInterface $httpClient;
32-
33-
private SerializerInterface $serializer;
34-
3533
private string $baseUrl;
36-
3734
private ?string $deviceName;
3835

3936
/**
@@ -45,7 +42,7 @@ public function __construct(string $baseUrl, string $deviceName = null, ClientIn
4542
{
4643
$this->baseUrl = $baseUrl;
4744
$this->deviceName = $deviceName;
48-
$this->serializer = $this->getSerializer();
45+
4946
if ($client === null) {
5047
$this->httpClient = $this->createDefaultHttpClient();
5148
} else {
@@ -104,8 +101,8 @@ protected function createDefaultHttpClient(): ClientInterface
104101
{
105102
return new \GuzzleHttp\Client([
106103
'base_uri' => $this->baseUrl,
107-
'timeout' => 5,
108-
'connect_timeout' => 5,
104+
RequestOptions::TIMEOUT => 5,
105+
RequestOptions::CONNECT_TIMEOUT => 5,
109106
]);
110107
}
111108

phpstan.neon

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
includes:
2+
- vendor/phpstan/phpstan-phpunit/extension.neon
3+
- vendor/phpstan/phpstan-strict-rules/rules.neon
4+
5+
parameters:
6+
level: 5
7+
paths:
8+
- lib
9+
- tests
10+
phpVersion: 80000

tests/Model/Request/AbstractRequestTest.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,17 @@
33

44
namespace ShellyClientTest\Model\Request;
55

6+
use PHPUnit\Framework\MockObject\MockObject;
67
use PHPUnit\Framework\TestCase;
7-
use ShellyClient\HTTP\Client;
88
use ShellyClient\HTTP\RequestService;
99
use ShellyClientTest\Mock\HTTP\ClientMock;
1010

1111
abstract class AbstractRequestTest extends TestCase
1212
{
13-
protected static Client $client;
14-
13+
protected static ClientMock $client;
14+
/**
15+
* @var RequestService&MockObject
16+
*/
1517
protected RequestService $requestService;
1618

1719
public static function setUpBeforeClass(): void
@@ -23,7 +25,7 @@ protected function setUp(): void
2325
{
2426
$this->requestService = $this
2527
->getMockBuilder(RequestService::class)
26-
->setMethods(['getResponse', 'getRequest'])
28+
->onlyMethods(['getResponse', 'getRequest'])
2729
->setConstructorArgs([self::$client->getHttpClient(), self::$client->getSerializer()])
2830
->getMock();
2931
}

tests/Model/Request/SettingsRequestTest.php

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,32 +4,11 @@
44
namespace ShellyClientTest\Model\Request;
55

66
use GuzzleHttp\Psr7\Response;
7-
use PHPUnit\Framework\TestCase;
8-
use ShellyClient\HTTP\Client;
9-
use ShellyClient\HTTP\RequestService;
107
use ShellyClient\Model\Request\SettingsRequest;
11-
use ShellyClientTest\Mock\HTTP\ClientMock;
8+
use ShellyClient\Model\Response\SettingsResponse;
129

13-
class SettingsRequestTest extends TestCase
10+
class SettingsRequestTest extends AbstractRequestTest
1411
{
15-
private static Client $client;
16-
17-
private RequestService $requestService;
18-
19-
public static function setUpBeforeClass(): void
20-
{
21-
self::$client = new ClientMock('');
22-
}
23-
24-
protected function setUp(): void
25-
{
26-
$this->requestService = $this
27-
->getMockBuilder(RequestService::class)
28-
->setMethods(['getResponse', 'getRequest'])
29-
->setConstructorArgs([self::$client->getHttpClient(), self::$client->getSerializer()])
30-
->getMock();
31-
}
32-
3312
public function simpleDataProvider(): array
3413
{
3514
return [
@@ -54,12 +33,13 @@ public function testSimple(string $testFile, string $expectedType): void
5433
$this->requestService->method('getResponse')->willReturn($response);
5534
$this->requestService->method('getRequest')->willReturn(new SettingsRequest());
5635

36+
/** @var SettingsResponse $settings */
5737
$settings = $this->requestService->getResponseSerialized();
5838

5939
self::assertSame('PC', $settings->getName());
6040
self::assertSame($expectedType, $settings->getDevice()->getType());
6141

62-
self::assertIsArray($settings->getRelays());
42+
self::assertNotEmpty($settings->getRelays());
6343
self::assertCount($settings->getDevice()->getNumOutputs(), $settings->getRelays());
6444
}
6545

0 commit comments

Comments
 (0)