From fecb1cd81086b6c24f6c1502fd3d55a7b73e4f55 Mon Sep 17 00:00:00 2001 From: Sebastian Grodzicki Date: Mon, 17 Apr 2023 09:15:27 +0200 Subject: [PATCH 1/9] Setup PHPUnit --- composer.json | 3 +++ phpunit.xml.dist | 10 ++++++++++ tests/ClientTest.php | 13 +++++++++++++ 3 files changed, 26 insertions(+) create mode 100644 phpunit.xml.dist create mode 100644 tests/ClientTest.php diff --git a/composer.json b/composer.json index 4043a24..e8b5a82 100755 --- a/composer.json +++ b/composer.json @@ -28,5 +28,8 @@ "psr-4": { "UniFi_API\\": "src/" } + }, + "require-dev": { + "phpunit/phpunit": "^5.7" } } diff --git a/phpunit.xml.dist b/phpunit.xml.dist new file mode 100644 index 0000000..b65be17 --- /dev/null +++ b/phpunit.xml.dist @@ -0,0 +1,10 @@ + + + + + tests + + + diff --git a/tests/ClientTest.php b/tests/ClientTest.php new file mode 100644 index 0000000..0e36d57 --- /dev/null +++ b/tests/ClientTest.php @@ -0,0 +1,13 @@ + Date: Mon, 17 Apr 2023 09:50:50 +0200 Subject: [PATCH 2/9] Setup CI --- .github/workflows/test.yml | 41 ++++++++++++++++++++++++++++++++++++++ composer.json | 5 +++++ 2 files changed, 46 insertions(+) create mode 100644 .github/workflows/test.yml diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 0000000..70c62ee --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,41 @@ +name: PHP test + +on: [ push, pull_request ] + +jobs: + test: + name: Test + runs-on: ${{ matrix.os }} + + strategy: + matrix: + php-version: [ 5.6, "7.0", 7.1, 7.2, 7.3, 7.4 ] + os: [ ubuntu-latest ] + + steps: + - name: Checkout + uses: actions/checkout@v3 + + - 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@v3 + 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 diff --git a/composer.json b/composer.json index e8b5a82..8cd3856 100755 --- a/composer.json +++ b/composer.json @@ -31,5 +31,10 @@ }, "require-dev": { "phpunit/phpunit": "^5.7" + }, + "scripts": { + "test" : [ + "vendor/bin/phpunit --testdox" + ] } } From a95b37b7fa20a9275d2eb8fdf45c9587a0662fd5 Mon Sep 17 00:00:00 2001 From: Sebastian Grodzicki Date: Mon, 17 Apr 2023 10:46:44 +0200 Subject: [PATCH 3/9] Test `set_site` --- tests/ClientTest.php | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/tests/ClientTest.php b/tests/ClientTest.php index 0e36d57..c90a1b0 100644 --- a/tests/ClientTest.php +++ b/tests/ClientTest.php @@ -2,12 +2,36 @@ namespace UniFi_API\Tests; +use UniFi_API\Client; use PHPUnit\Framework\TestCase; class ClientTest extends TestCase { - public function testSomething() + private $client; + + protected function setUp() + { + $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->expectException(\PHPUnit_Framework_Error_Notice::class); + $this->expectExceptionCode(E_USER_NOTICE); + $this->expectExceptionMessage('The provided (short) site name may not contain any spaces'); + $this->client->set_site(' foobar '); } } From af05836d081e5d2549fcc2b62936cd84503c757d Mon Sep 17 00:00:00 2001 From: Sebastian Grodzicki Date: Mon, 17 Apr 2023 15:37:37 +0200 Subject: [PATCH 4/9] Use PHPUnit 8 --- .github/workflows/test.yml | 2 +- .gitignore | 6 +++++- composer.json | 2 +- phpunit.xml.dist | 2 +- tests/ClientTest.php | 7 +++---- 5 files changed, 11 insertions(+), 8 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 70c62ee..799cd49 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -9,7 +9,7 @@ jobs: strategy: matrix: - php-version: [ 5.6, "7.0", 7.1, 7.2, 7.3, 7.4 ] + php-version: [ 7.2, 7.3, 7.4, "8.0", 8.1, 8.2 ] os: [ ubuntu-latest ] steps: diff --git a/.gitignore b/.gitignore index 33306ea..acecdef 100755 --- a/.gitignore +++ b/.gitignore @@ -13,4 +13,8 @@ *.xml # ignore PHPStorm files -.idea/* \ No newline at end of file +.idea/* + +# PHPUnit +/phpunit.xml +.phpunit.result.cache diff --git a/composer.json b/composer.json index 8cd3856..6cc454e 100755 --- a/composer.json +++ b/composer.json @@ -30,7 +30,7 @@ } }, "require-dev": { - "phpunit/phpunit": "^5.7" + "phpunit/phpunit": "^8.5" }, "scripts": { "test" : [ diff --git a/phpunit.xml.dist b/phpunit.xml.dist index b65be17..6894478 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,6 +1,6 @@ diff --git a/tests/ClientTest.php b/tests/ClientTest.php index c90a1b0..3ba4040 100644 --- a/tests/ClientTest.php +++ b/tests/ClientTest.php @@ -9,7 +9,7 @@ class ClientTest extends TestCase { private $client; - protected function setUp() + protected function setUp(): void { $this->client = new Client('unifi', 'unifi'); } @@ -29,9 +29,8 @@ public function testSetSite() // whitespace (debug mode) $this->client->set_debug(true); - $this->expectException(\PHPUnit_Framework_Error_Notice::class); - $this->expectExceptionCode(E_USER_NOTICE); - $this->expectExceptionMessage('The provided (short) site name may not contain any spaces'); + $this->expectNotice(); + $this->expectNoticeMessage('The provided (short) site name may not contain any spaces'); $this->client->set_site(' foobar '); } } From 8d17eb72a01947473f2133cfdb8902e2ccc1f237 Mon Sep 17 00:00:00 2001 From: Sebastian Grodzicki Date: Mon, 17 Apr 2023 13:55:28 +0200 Subject: [PATCH 5/9] Add integration tests --- .github/workflows/test.yml | 20 ++++++++ composer.json | 5 +- phpunit.xml.dist | 5 +- src/Client.php | 16 ++++++ tests/Integration/ClientTest.php | 83 ++++++++++++++++++++++++++++++++ tests/{ => Unit}/ClientTest.php | 0 6 files changed, 127 insertions(+), 2 deletions(-) create mode 100644 tests/Integration/ClientTest.php rename tests/{ => Unit}/ClientTest.php (100%) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 799cd49..5c44de1 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -9,9 +9,25 @@ jobs: strategy: matrix: + unifi-version: [ stable-5, stable-6, v7.0, v7.1, v7.2, v7.3 ] php-version: [ 7.2, 7.3, 7.4, "8.0", 8.1, 8.2 ] + 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@v3 @@ -39,3 +55,7 @@ jobs: - name: Unit tests run: | composer run-script test + + - name: Integration tests + run: | + composer run-script integration-test diff --git a/composer.json b/composer.json index 6cc454e..a0ff5a1 100755 --- a/composer.json +++ b/composer.json @@ -34,7 +34,10 @@ }, "scripts": { "test" : [ - "vendor/bin/phpunit --testdox" + "vendor/bin/phpunit --testdox --testsuite \"Unit tests\"" + ], + "integration-test" : [ + "vendor/bin/phpunit --testdox --testsuite \"Integration tests\"" ] } } diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 6894478..e5eeffb 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -4,7 +4,10 @@ colors="true"> - tests + tests/Unit + + + tests/Integration diff --git a/src/Client.php b/src/Client.php index 277a9ac..4fd33ad 100755 --- a/src/Client.php +++ b/src/Client.php @@ -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 * diff --git a/tests/Integration/ClientTest.php b/tests/Integration/ClientTest.php new file mode 100644 index 0000000..c4ee3f7 --- /dev/null +++ b/tests/Integration/ClientTest.php @@ -0,0 +1,83 @@ +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); + } + } +} diff --git a/tests/ClientTest.php b/tests/Unit/ClientTest.php similarity index 100% rename from tests/ClientTest.php rename to tests/Unit/ClientTest.php From 33d0ddd3163e92616942ab20d7955d236c597e45 Mon Sep 17 00:00:00 2001 From: Sebastian Grodzicki Date: Sun, 11 Feb 2024 18:20:00 +0100 Subject: [PATCH 6/9] Drop PHP <7.4 --- .github/workflows/test.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 5c44de1..e8edd44 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -10,7 +10,7 @@ jobs: strategy: matrix: unifi-version: [ stable-5, stable-6, v7.0, v7.1, v7.2, v7.3 ] - php-version: [ 7.2, 7.3, 7.4, "8.0", 8.1, 8.2 ] + php-version: [ 7.4, "8.0", 8.1, 8.2 ] mongo-version: [ 3.6 ] os: [ ubuntu-latest ] From 8ac38f8a0a2bd9d0ecf17fbf35f522652723419a Mon Sep 17 00:00:00 2001 From: Sebastian Grodzicki Date: Sun, 11 Feb 2024 18:23:40 +0100 Subject: [PATCH 7/9] Add PHP 8.3 --- .github/workflows/test.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index e8edd44..57f59dc 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -10,7 +10,7 @@ jobs: strategy: matrix: unifi-version: [ stable-5, stable-6, v7.0, v7.1, v7.2, v7.3 ] - php-version: [ 7.4, "8.0", 8.1, 8.2 ] + php-version: [ 7.4, "8.0", 8.1, 8.2, 8.3 ] mongo-version: [ 3.6 ] os: [ ubuntu-latest ] From 0dad74b8ee7685d8f9f2c0f1b24a0e98bc6b6fb2 Mon Sep 17 00:00:00 2001 From: Sebastian Grodzicki Date: Sun, 11 Feb 2024 18:28:40 +0100 Subject: [PATCH 8/9] Add UniFi 7.4/7.5/8.0 --- .github/workflows/test.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 57f59dc..3799cbe 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -9,7 +9,7 @@ jobs: strategy: matrix: - unifi-version: [ stable-5, stable-6, v7.0, v7.1, v7.2, v7.3 ] + 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 ] From 006953899f177f3b8f23b6cc0a9503c28e87e982 Mon Sep 17 00:00:00 2001 From: Sebastian Grodzicki Date: Sun, 11 Feb 2024 18:33:22 +0100 Subject: [PATCH 9/9] Update GitHub Actions Node.js 16 actions are deprecated --- .github/workflows/test.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 3799cbe..682f991 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -30,7 +30,7 @@ jobs: steps: - name: Checkout - uses: actions/checkout@v3 + uses: actions/checkout@v4 - name: Use PHP ${{ matrix.php-version }} uses: shivammathur/setup-php@v2 @@ -42,7 +42,7 @@ jobs: id: composercache run: echo "dir=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT - name: Cache dependencies - uses: actions/cache@v3 + uses: actions/cache@v4 with: path: ${{ steps.composercache.outputs.dir }} key: ${{ runner.os }}-php-${{ matrix.php-version }}-${{ hashFiles('**/composer.json') }}