Skip to content

Commit 5c5388f

Browse files
authored
Merge pull request #1 from niladam/feature/add_tests
Add tests and run tests workflow
2 parents a573be4 + 96f80f4 commit 5c5388f

17 files changed

+501
-13
lines changed

.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,4 @@
2626
/tailwind.config.js export-ignore
2727
/testbench.yaml export-ignore
2828
/UPGRADING.md export-ignore
29+
/.phpunit.cache export-ignore

.github/run-tests.yml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
name: Run PHP Tests
2+
3+
on:
4+
pull_request:
5+
branches: [ "main" ]
6+
7+
jobs:
8+
build-and-test:
9+
runs-on: ubuntu-latest
10+
strategy:
11+
matrix:
12+
php-version: [ '7.4', '8.2' ]
13+
steps:
14+
- name: Check out code
15+
uses: actions/checkout@v3
16+
17+
- name: Set up PHP
18+
uses: shivammathur/setup-php@v2
19+
with:
20+
php-version: ${{ matrix.php-version }}
21+
# Add any required extensions here, for example:
22+
extensions: mbstring, intl, pcntl, xml, json
23+
24+
- name: Install dependencies
25+
run: composer install --no-interaction --prefer-dist --optimize-autoloader
26+
27+
- name: Run tests
28+
run: vendor/bin/phpunit

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
.idea
33
.phpunit.result.cache
44
.php-cs-fixer.cache
5+
.phpunit.cache
56
.vscode
67
build
78
composer.lock

composer.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
},
1717
"require-dev": {
1818
"friendsofphp/php-cs-fixer": "^3.65",
19-
"phpunit/phpunit": "^9.6"
19+
"phpunit/phpunit": "^9.6",
20+
"orchestra/testbench": "^6.47"
2021
},
2122
"extra": {
2223
"laravel": {
@@ -32,5 +33,10 @@
3233
"psr-4": {
3334
"Niladam\\LaravelZabbix\\": "src/"
3435
}
36+
},
37+
"autoload-dev": {
38+
"psr-4": {
39+
"Niladam\\LaravelZabbix\\Tests\\": "tests/"
40+
}
3541
}
3642
}

phpunit.xml.dist

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpunit/phpunit.xsd"
4+
bootstrap="vendor/autoload.php"
5+
cacheResultFile=".phpunit.cache/test-results"
6+
colors="true"
7+
executionOrder="depends,defects"
8+
beStrictAboutOutputDuringTests="true"
9+
beStrictAboutTodoAnnotatedTests="true"
10+
convertDeprecationsToExceptions="true"
11+
convertErrorsToExceptions="true"
12+
convertNoticesToExceptions="true"
13+
convertWarningsToExceptions="true"
14+
failOnRisky="true"
15+
failOnWarning="true"
16+
verbose="true">
17+
<testsuites>
18+
<testsuite name="Laravel Zabbix Test Suite">
19+
<directory>tests</directory>
20+
</testsuite>
21+
</testsuites>
22+
23+
<coverage cacheDirectory=".phpunit.cache/code-coverage"
24+
processUncoveredFiles="true">
25+
<include>
26+
<directory suffix=".php">src</directory>
27+
</include>
28+
</coverage>
29+
<php>
30+
<env name="APP_ENV" value="testing"/>
31+
<env name="CACHE_DRIVER" value="array"/>
32+
<env name="SESSION_DRIVER" value="array"/>
33+
<env name="QUEUE_DRIVER" value="sync"/>
34+
</php>
35+
</phpunit>

src/Communication/Batch.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public function __construct(string $request = 'sender data')
1818

1919
public function add(Message $message): void
2020
{
21-
$this->batch['data'][] = $message->serialized();
21+
$this->batch['data'][] = $message;
2222
}
2323

2424
public function getBatch(): array

src/Communication/Message.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ protected function validate(): void
144144
Assert::notNull($this->value, 'You have not set the "value" value on your message. Use usingValue() to do that.');
145145
}
146146

147-
public function serialized(): mixed
147+
public function serialized(): array
148148
{
149149
return $this->jsonSerialize();
150150
}
@@ -156,7 +156,7 @@ public function send()
156156
return $this->manager->send();
157157
}
158158

159-
public function jsonSerialize(): mixed
159+
public function jsonSerialize(): array
160160
{
161161
$this->validate();
162162

src/Communication/Response.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,6 @@ class Response
3333
*/
3434
private ?float $duration;
3535

36-
private ?string $humanDuration;
37-
3836
public function __construct(array $response)
3937
{
4038
$this->parseZabbixResponse($response);
@@ -95,15 +93,15 @@ private function parseZabbixResponse(array $response): void
9593
{
9694
if (!isset($response['response'])) {
9795
throw new ResponseException(
98-
'invalid zabbix server response, missing `response` field'
96+
'Invalid zabbix server response, missing `response` field'
9997
);
10098
}
10199

102100
$this->responseStatus = $response['response'];
103101

104102
if (!isset($response['info'])) {
105103
throw new ResponseException(
106-
'invalid zabbix server response, missing `info` field'
104+
'Invalid zabbix server response, missing `info` field'
107105
);
108106
}
109107

@@ -153,7 +151,6 @@ private function parseZabbixResponse(array $response): void
153151
$this->failedItems = (int) $matches[2];
154152
$this->totalItems = (int) $matches[3];
155153
$this->duration = (float) $matches[4];
156-
$this->humanDuration = $this->convertToHumanReadableTime($matches[4]);
157154
}
158155

159156
public function convertToHumanReadableTime(float $seconds): string

src/Notifications/Contracts/SentViaZabbix.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public function getHostConfiguration();
2626
/**
2727
* @return bool|Response
2828
*/
29-
public function send(): mixed;
29+
public function send();
3030

3131
public function toZabbix($notifiable);
3232
}

src/Notifications/ZabbixAlert.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ abstract class ZabbixAlert extends ZabbixNotification implements SentViaZabbix
1414
{
1515
protected ?ZabbixManager $manager = null;
1616

17+
public ?string $message = null;
18+
1719
public function __construct(
1820
?string $message = null
1921
) {
@@ -27,12 +29,14 @@ public function __construct(
2729
$message = $this->getMessage();
2830
}
2931

32+
$this->message = $message;
33+
3034
$this->manager->add(
31-
$this->toMessage($message)
35+
$this->asZabbixMessage($message)
3236
);
3337
}
3438

35-
protected function toMessage(string $message): Message
39+
protected function asZabbixMessage(string $message): Message
3640
{
3741
$hostConfiguration = $this->getValidConfiguration();
3842

@@ -74,7 +78,7 @@ public function usingServer(array $configuration): ZabbixAlert
7478
*
7579
* @return bool|Response
7680
*/
77-
public function send(): mixed
81+
public function send()
7882
{
7983
return $this->manager->send();
8084
}
@@ -90,4 +94,9 @@ public function toZabbix($notifiable): ZabbixAlert
9094
{
9195
return $this;
9296
}
97+
98+
public function getAlertMessage(): string
99+
{
100+
return $this->message;
101+
}
93102
}

0 commit comments

Comments
 (0)