Skip to content

Commit c8657e5

Browse files
committed
Start to add basic integration tests.
1 parent c446144 commit c8657e5

File tree

7 files changed

+223
-1
lines changed

7 files changed

+223
-1
lines changed

.github/workflows/build.yml

+46-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ jobs:
3838
matrix:
3939
operating-system: [ubuntu-latest, windows-latest]
4040
php-versions: ['7.1', '7.2', '7.3', '7.4', '8.0', '8.1']
41-
name: Tests for PHP ${{ matrix.php-versions }} on ${{ matrix.operating-system }}
41+
name: Unit Tests for PHP ${{ matrix.php-versions }} on ${{ matrix.operating-system }}
4242
steps:
4343
- name: Checkout
4444
uses: actions/checkout@v2
@@ -71,3 +71,48 @@ jobs:
7171

7272
- name: Run Tests
7373
run: composer test-spec
74+
75+
integration-tests:
76+
runs-on: ${{ matrix.operating-system }}
77+
strategy:
78+
fail-fast: false
79+
matrix:
80+
operating-system: [ubuntu-latest]
81+
php-versions: ['7.1', '7.2', '7.3', '7.4', '8.0', '8.1']
82+
name: Integration Tests for PHP ${{ matrix.php-versions }} on ${{ matrix.operating-system }}
83+
steps:
84+
- name: Checkout
85+
uses: actions/checkout@v2
86+
87+
- name: Setup PHP
88+
uses: shivammathur/setup-php@v2
89+
with:
90+
php-version: ${{ matrix.php-versions }}
91+
tools: composer:v2
92+
coverage: pcov
93+
94+
- name: Start SNMP Container
95+
run: |
96+
docker run --name snmpd -d -p 10161:161/udp polinux/snmpd
97+
98+
- name: Get Composer Cache Directory
99+
id: composer-cache
100+
run: echo "::set-output name=dir::$(composer config cache-files-dir)"
101+
102+
- name: Install Composer dependencies
103+
if: ${{ matrix.php-versions != '8.1' }}
104+
run: composer install --no-progress --no-suggest --prefer-dist --optimize-autoloader
105+
106+
- name: Install Composer dependencies (8.1)
107+
if: ${{ matrix.php-versions == '8.1' }}
108+
run: composer install --no-progress --no-suggest --prefer-dist --optimize-autoloader --ignore-platform-reqs
109+
110+
- name: Cache dependencies
111+
uses: actions/cache@v1
112+
with:
113+
path: ${{ steps.composer-cache.outputs.dir }}
114+
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
115+
restore-keys: ${{ runner.os }}-composer-
116+
117+
- name: Run Tests
118+
run: composer test-integration

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
composer.lock
22
composer.phar
33
vendor/
4+
.phpunit.*.cache

composer.json

+10
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"require-dev": {
2121
"friendsofphp/php-cs-fixer": "^2.0.0",
2222
"phpspec/phpspec": "^5.1|^6.1|^7.1",
23+
"phpunit/phpunit": "^7.5|^8.5|^9.5",
2324
"phpstan/phpstan": "^0.11|^0.12"
2425
},
2526
"suggest": {
@@ -29,6 +30,12 @@
2930
"autoload": {
3031
"psr-4": {"FreeDSx\\Snmp\\": "src/FreeDSx/Snmp"}
3132
},
33+
"autoload-dev": {
34+
"psr-4": {
35+
"integration\\FreeDSx\\Snmp\\": "tests/integration/FreeDSx/Snmp",
36+
"spec\\FreeDSx\\Snmp\\": "tests/spec/FreeDSx/Snmp"
37+
}
38+
},
3239
"config": {
3340
"sort-packages": true
3441
},
@@ -42,6 +49,9 @@
4249
],
4350
"test-spec": [
4451
"phpspec run --format=pretty --no-interaction"
52+
],
53+
"test-integration": [
54+
"phpunit --testsuite integration"
4555
]
4656
}
4757
}

phpunit.xml.dist

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
backupGlobals="false"
4+
backupStaticAttributes="false"
5+
colors="true"
6+
convertErrorsToExceptions="true"
7+
convertNoticesToExceptions="true"
8+
convertWarningsToExceptions="true"
9+
processIsolation="false"
10+
stopOnFailure="false"
11+
bootstrap="vendor/autoload.php"
12+
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd"
13+
>
14+
15+
<coverage processUncoveredFiles="true">
16+
<include>
17+
<directory suffix=".php">src</directory>
18+
</include>
19+
</coverage>
20+
21+
<testsuites>
22+
<testsuite name="integration">
23+
<directory>./tests/integration</directory>
24+
</testsuite>
25+
</testsuites>
26+
27+
<php>
28+
<env name="SNMP_SERVER" value="127.0.0.1"/>
29+
<env name="SNMP_COMMUNITY_RO" value="public"/>
30+
<env name="SNMP_COMMUNITY_RW" value="secret"/>
31+
<env name="SNMP_PASS_PRIV" value="P@ssword12345"/>
32+
<env name="SNMP_PASS_USER" value="P@ssword12345"/>
33+
<env name="SNMP_USER" value="user1"/>
34+
<env name="SNMP_PORT" value="10161"/>
35+
</php>
36+
37+
</phpunit>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
namespace integration\FreeDSx\Snmp;
4+
5+
use FreeDSx\Snmp\Oid;
6+
use FreeDSx\Snmp\SnmpClient;
7+
8+
class SnmpClientTest extends TestCase
9+
{
10+
/**
11+
* @var SnmpClient
12+
*/
13+
private $subject;
14+
15+
public function setUp(): void
16+
{
17+
parent::setUp();
18+
$this->subject = $this->makeReadOnlyClient();
19+
}
20+
21+
public function testGetReturnsOidListWithValue(): void
22+
{
23+
$value = $this->subject->get('1.3.6.1.2.1.1.1.0');
24+
25+
$this->assertCount(1, $value);
26+
$this->assertStringContainsStringIgnoringCase(
27+
'Linux',
28+
(string)$value->first()->getValue()
29+
);
30+
}
31+
32+
public function testGetValueReturnsSingleOidValue(): void
33+
{
34+
$value = $this->subject->getValue('1.3.6.1.2.1.1.1.0');
35+
36+
$this->assertIsString($value);
37+
$this->assertStringContainsStringIgnoringCase(
38+
'Linux',
39+
$value
40+
);
41+
}
42+
43+
public function testGetOidReturnsSingleOid(): void
44+
{
45+
$oid = $this->subject->getOid('1.3.6.1.2.1.1.1.0');
46+
47+
$this->assertInstanceOf(
48+
Oid::class,
49+
$oid
50+
);
51+
$this->assertStringContainsStringIgnoringCase(
52+
'Linux',
53+
$oid->getValue()
54+
);
55+
}
56+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
namespace integration\FreeDSx\Snmp;
4+
5+
use FreeDSx\Snmp\Oid;
6+
use FreeDSx\Snmp\SnmpClient;
7+
8+
class SnmpWalkTest extends TestCase
9+
{
10+
/**
11+
* @var SnmpClient
12+
*/
13+
private $client;
14+
15+
public function setUp(): void
16+
{
17+
parent::setUp();
18+
$this->client = $this->makeReadOnlyClient();
19+
}
20+
21+
public function testItWalksAllOids()
22+
{
23+
$walk = $this->client->walk();
24+
25+
while($walk->hasOids()) {
26+
$oid = $walk->next();
27+
28+
$this->assertInstanceOf(
29+
Oid::class,
30+
$oid
31+
);
32+
}
33+
34+
$this->assertGreaterThan(
35+
1,
36+
$walk->count()
37+
);
38+
}
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace integration\FreeDSx\Snmp;
4+
5+
use FreeDSx\Snmp\SnmpClient;
6+
use PHPUnit\Framework\TestCase as BaseTestCase;
7+
8+
class TestCase extends BaseTestCase
9+
{
10+
public function makeReadOnlyClient(array $options = []): SnmpClient
11+
{
12+
return new SnmpClient(array_merge(
13+
$this->defaultOptions(),
14+
$options
15+
));
16+
}
17+
18+
private function defaultOptions(): array
19+
{
20+
return [
21+
'version' => 2,
22+
'community' => getenv('SNMP_COMMUNITY_RO'),
23+
'user' => getenv('SNMP_USER'),
24+
'use_auth' => true,
25+
'auth_pwd' => getenv('SNMP_PASS_USER'),
26+
'auth_mech' => 'md5',
27+
'use_priv' => false,
28+
'priv_mech' => 'des',
29+
'priv_pwd' => getenv('SNMP_PASS_PRIV'),
30+
'host' => getenv('SNMP_SERVER'),
31+
'port' => (int) getenv('SNMP_PORT'),
32+
];
33+
}
34+
}

0 commit comments

Comments
 (0)