This commit is contained in:
Sebastian Grodzicki 2024-11-17 07:00:04 -03:00 committed by GitHub
commit d80ac3c9a0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 225 additions and 1 deletions

61
.github/workflows/test.yml vendored Normal file
View File

@ -0,0 +1,61 @@
name: PHP test
on: [ push, pull_request ]
jobs:
test:
name: Test
runs-on: ${{ matrix.os }}
strategy:
matrix:
unifi-version: [ stable-5, stable-6, v7.0, v7.1, v7.2, v7.3, v7.4, v7.5, v8.0 ]
php-version: [ 7.4, "8.0", 8.1, 8.2, 8.3 ]
mongo-version: [ 3.6 ]
os: [ ubuntu-latest ]
services:
mongo:
image: mongo:${{ matrix.mongo-version }}
ports:
- "27017:27017/tcp"
unifi:
image: jacobalberty/unifi:${{ matrix.unifi-version }}
env:
DB_URI: mongodb://mongo/unifi
STATDB_URI: mongodb://mongo/unifi_stat
DB_NAME: unifi
ports:
- "8443:8443/tcp"
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Use PHP ${{ matrix.php-version }}
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-version }}
extensions: curl, json
- name: Get composer cache directory
id: composercache
run: echo "dir=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT
- name: Cache dependencies
uses: actions/cache@v4
with:
path: ${{ steps.composercache.outputs.dir }}
key: ${{ runner.os }}-php-${{ matrix.php-version }}-${{ hashFiles('**/composer.json') }}
restore-keys: ${{ runner.os }}-php-${{ matrix.php-version }}-
- name: Install dependencies
run: |
composer install --prefer-dist
- name: Unit tests
run: |
composer run-script test
- name: Integration tests
run: |
composer run-script integration-test

6
.gitignore vendored
View File

@ -13,4 +13,8 @@
*.xml
# ignore PHPStorm files
.idea/*
.idea/*
# PHPUnit
/phpunit.xml
.phpunit.result.cache

View File

@ -28,5 +28,16 @@
"psr-4": {
"UniFi_API\\": "src/"
}
},
"require-dev": {
"phpunit/phpunit": "^8.5"
},
"scripts": {
"test" : [
"vendor/bin/phpunit --testdox --testsuite \"Unit tests\""
],
"integration-test" : [
"vendor/bin/phpunit --testdox --testsuite \"Integration tests\""
]
}
}

13
phpunit.xml.dist Normal file
View File

@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/8.5/phpunit.xsd"
colors="true">
<testsuites>
<testsuite name="Unit tests">
<directory>tests/Unit</directory>
</testsuite>
<testsuite name="Integration tests">
<directory>tests/Integration</directory>
</testsuite>
</testsuites>
</phpunit>

View File

@ -1746,6 +1746,22 @@ class Client
$payload);
}
/**
* Creates default admin account for controller in setup mode
*
* @return bool true on success
*/
public function add_default_admin()
{
$payload = [
'cmd' => 'add-default-admin',
'name' => $this->user,
'x_password' => $this->password,
];
return $this->fetch_results_boolean('/api/cmd/sitemgr', $payload, false);
}
/**
* Fetch admins
*

View File

@ -0,0 +1,83 @@
<?php
namespace UniFi_API\Tests\Integration;
use UniFi_API\Client;
use PHPUnit\Framework\TestCase;
class ClientTest extends TestCase
{
protected static $client;
public static function setUpBeforeClass(): void
{
self::$client = new Client('unifi', 'unifi');
self::$client->add_default_admin();
}
public function testLogin()
{
$this->assertTrue(self::$client->login());
}
public function testStatSysinfo()
{
$stat_sysinfo = self::$client->stat_sysinfo();
$this->assertCount(1, $stat_sysinfo);
$this->assertObjectHasAttribute('timezone', $stat_sysinfo[0]);
$this->assertObjectHasAttribute('autobackup', $stat_sysinfo[0]);
$this->assertObjectHasAttribute('build', $stat_sysinfo[0]);
$this->assertObjectHasAttribute('version', $stat_sysinfo[0]);
$this->assertObjectHasAttribute('data_retention_days', $stat_sysinfo[0]);
$this->assertObjectHasAttribute('data_retention_time_in_hours_for_5minutes_scale', $stat_sysinfo[0]);
$this->assertObjectHasAttribute('data_retention_time_in_hours_for_hourly_scale', $stat_sysinfo[0]);
$this->assertObjectHasAttribute('data_retention_time_in_hours_for_daily_scale', $stat_sysinfo[0]);
$this->assertObjectHasAttribute('data_retention_time_in_hours_for_monthly_scale', $stat_sysinfo[0]);
$this->assertObjectHasAttribute('data_retention_time_in_hours_for_others', $stat_sysinfo[0]);
$this->assertObjectHasAttribute('update_available', $stat_sysinfo[0]);
$this->assertObjectHasAttribute('update_downloaded', $stat_sysinfo[0]);
$this->assertObjectHasAttribute('live_chat', $stat_sysinfo[0]);
$this->assertObjectHasAttribute('store_enabled', $stat_sysinfo[0]);
$this->assertObjectHasAttribute('hostname', $stat_sysinfo[0]);
$this->assertObjectHasAttribute('name', $stat_sysinfo[0]);
$this->assertObjectHasAttribute('ip_addrs', $stat_sysinfo[0]);
$this->assertObjectHasAttribute('inform_port', $stat_sysinfo[0]);
$this->assertObjectHasAttribute('https_port', $stat_sysinfo[0]);
$this->assertObjectHasAttribute('override_inform_host', $stat_sysinfo[0]);
$this->assertObjectHasAttribute('image_maps_use_google_engine', $stat_sysinfo[0]);
$this->assertObjectHasAttribute('radius_disconnect_running', $stat_sysinfo[0]);
$this->assertObjectHasAttribute('facebook_wifi_registered', $stat_sysinfo[0]);
$this->assertObjectHasAttribute('sso_app_id', $stat_sysinfo[0]);
$this->assertObjectHasAttribute('sso_app_sec', $stat_sysinfo[0]);
$this->assertObjectHasAttribute('unsupported_device_count', $stat_sysinfo[0]);
$this->assertObjectHasAttribute('unsupported_device_list', $stat_sysinfo[0]);
$this->assertObjectHasAttribute('unifi_go_enabled', $stat_sysinfo[0]);
$this->assertObjectHasAttribute('default_site_device_auth_password_alert', $stat_sysinfo[0]);
if (\version_compare($stat_sysinfo[0]->version, '6') >= 0) {
$this->assertObjectHasAttribute('uptime', $stat_sysinfo[0]);
$this->assertObjectHasAttribute('anonymous_controller_id', $stat_sysinfo[0]);
$this->assertObjectHasAttribute('has_webrtc_support', $stat_sysinfo[0]);
}
if (\version_compare($stat_sysinfo[0]->version, '7') >= 0) {
$this->assertObjectHasAttribute('debug_setting_preference', $stat_sysinfo[0]);
$this->assertObjectHasAttribute('debug_mgmt', $stat_sysinfo[0]);
$this->assertObjectHasAttribute('debug_system', $stat_sysinfo[0]);
$this->assertObjectHasAttribute('debug_device', $stat_sysinfo[0]);
$this->assertObjectHasAttribute('debug_sdn', $stat_sysinfo[0]);
}
}
public function testListCountryCodes()
{
$country_codes = self::$client->list_country_codes();
$this->assertGreaterThanOrEqual(168, $country_codes);
foreach ($country_codes as $country_code) {
$this->assertObjectHasAttribute('code', $country_code);
$this->assertObjectHasAttribute('name', $country_code);
$this->assertObjectHasAttribute('key', $country_code);
}
}
}

36
tests/Unit/ClientTest.php Normal file
View File

@ -0,0 +1,36 @@
<?php
namespace UniFi_API\Tests;
use UniFi_API\Client;
use PHPUnit\Framework\TestCase;
class ClientTest extends TestCase
{
private $client;
protected function setUp(): void
{
$this->client = new Client('unifi', 'unifi');
}
public function testSetSite()
{
// default
$this->assertEquals('default', $this->client->get_site());
// custom
$this->client->set_site('foobar');
$this->assertEquals('foobar', $this->client->get_site());
// whitespace
$this->client->set_site(' foobar ');
$this->assertEquals('foobar', $this->client->get_site());
// whitespace (debug mode)
$this->client->set_debug(true);
$this->expectNotice();
$this->expectNoticeMessage('The provided (short) site name may not contain any spaces');
$this->client->set_site(' foobar ');
}
}