Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Setup CI #192

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -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 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,8 @@
*.xml

# ignore PHPStorm files
.idea/*
.idea/*

# PHPUnit
/phpunit.xml
.phpunit.result.cache
11 changes: 11 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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 changes: 13 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -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>
16 changes: 16 additions & 0 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -1835,6 +1835,22 @@ public function set_site_connectivity(string $connectivity_id, $payload): bool
$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
*
Expand Down
83 changes: 83 additions & 0 deletions tests/Integration/ClientTest.php
Original file line number Diff line number Diff line change
@@ -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 changes: 36 additions & 0 deletions tests/Unit/ClientTest.php
Original file line number Diff line number Diff line change
@@ -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 ');
}
}