Skip to content

Commit 936502d

Browse files
authored
Added some initial tests to verify configuration (#1)
1 parent 75600b3 commit 936502d

File tree

5 files changed

+93
-4
lines changed

5 files changed

+93
-4
lines changed

.github/workflows/testing.yml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ jobs:
1212
runs-on: ubuntu-latest
1313
strategy:
1414
matrix:
15-
php-version: ['8.2', '8.0', '7.4']
15+
php-version: ["8.2", "8.0", "7.4"]
1616
services:
1717
database:
1818
image: mysql:latest
@@ -25,10 +25,13 @@ jobs:
2525
- name: Check out source code
2626
uses: actions/checkout@v4
2727

28+
- name: Install SVN
29+
run: sudo apt-get update && sudo apt-get install -y subversion
30+
2831
- name: Setup PHP
2932
uses: shivammathur/setup-php@v2
3033
with:
31-
php-version: $
34+
php-version: ${{ matrix.php-version }}
3235
tools: phpunit-polyfills:1.1
3336

3437
- name: Setup tests

.phpunit.result.cache

Lines changed: 0 additions & 1 deletion
This file was deleted.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"test:php": "npm-run-all lint:php test:unit:php",
2222
"test:unit:php:setup": "wp-env --config .wp-env.test.json start",
2323
"test:unit:php:setup:debug": "wp-env --config .wp-env.test.json start --xdebug",
24-
"test:unit:php:base": "wp-env --config .wp-env.test.json run --env-cwd='wp-content/plugins/rsscloud' wordpress vendor/bin/phpunit -c phpunit.xml.dist --verbose",
24+
"test:unit:php:base": "wp-env --config .wp-env.test.json run --env-cwd='wp-content/plugins/rsscloud' wordpress vendor/bin/phpunit -c phpunit.xml.dist --verbose --testdox",
2525
"test:unit:php": "npm-run-all test:unit:php:setup test:unit:php:base",
2626
"test:unit:php:debug": "npm-run-all test:unit:php:setup:debug test:unit:php:base"
2727
}

tests/test-data-storage.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
/**
3+
* Tests for data storage functions.
4+
*
5+
* @package Rsscloud
6+
*/
7+
8+
class DataStorageTest extends WP_UnitTestCase {
9+
10+
public function test_get_hub_notifications_returns_false_when_not_set() {
11+
$this->assertFalse( rsscloud_get_hub_notifications() );
12+
}
13+
14+
public function test_update_hub_notifications_stores_data() {
15+
$data = array(
16+
'http://example.com/feed' => array(
17+
'http://example.com/rpc' => array(
18+
'protocol' => 'http-post',
19+
'status' => 'active',
20+
'failure_count' => 0,
21+
),
22+
),
23+
);
24+
25+
$this->assertTrue( rsscloud_update_hub_notifications( $data ) );
26+
$this->assertSame( $data, rsscloud_get_hub_notifications() );
27+
}
28+
29+
public function test_update_hub_notifications_casts_to_array() {
30+
rsscloud_update_hub_notifications( 'not-an-array' );
31+
$this->assertSame( array( 'not-an-array' ), rsscloud_get_hub_notifications() );
32+
}
33+
34+
public function test_update_hub_notifications_overwrites_previous() {
35+
$first = array( 'first' => 'value' );
36+
$second = array( 'second' => 'value' );
37+
38+
rsscloud_update_hub_notifications( $first );
39+
rsscloud_update_hub_notifications( $second );
40+
41+
$this->assertSame( $second, rsscloud_get_hub_notifications() );
42+
}
43+
44+
/**
45+
* This test must run after tests that write data.
46+
* It verifies that WP_UnitTestCase database isolation is working
47+
* (each test gets a clean slate via transaction rollback).
48+
*/
49+
public function test_database_isolation_between_tests() {
50+
$this->assertFalse( rsscloud_get_hub_notifications() );
51+
}
52+
}

tests/test-rsscloud.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
/**
3+
* Tests for main plugin functions.
4+
*
5+
* @package Rsscloud
6+
*/
7+
8+
class RsscloudTest extends WP_UnitTestCase {
9+
10+
public function test_generate_challenge_returns_string_of_expected_length() {
11+
$challenge = rsscloud_generate_challenge();
12+
13+
$this->assertIsString( $challenge );
14+
$this->assertSame( 30, strlen( $challenge ) );
15+
}
16+
17+
public function test_generate_challenge_respects_custom_length() {
18+
$this->assertSame( 50, strlen( rsscloud_generate_challenge( 50 ) ) );
19+
}
20+
21+
public function test_generate_challenge_returns_unique_values() {
22+
$first = rsscloud_generate_challenge();
23+
$second = rsscloud_generate_challenge();
24+
25+
$this->assertNotSame( $first, $second );
26+
}
27+
28+
public function test_query_vars_adds_rsscloud() {
29+
$vars = array( 'existing_var' );
30+
$result = rsscloud_query_vars( $vars );
31+
32+
$this->assertContains( 'existing_var', $result );
33+
$this->assertContains( 'rsscloud', $result );
34+
}
35+
}

0 commit comments

Comments
 (0)