Skip to content

Commit

Permalink
add simple CI pipeline with basic PHP test runner (#823)
Browse files Browse the repository at this point in the history
* add initial PHP test workflow

test matrix contains all currently released and supported PHP versions

* actually run the tests as described in tests/README

* tests: fix PHPUnit imports

* tests: fix missing constant S9Y_INCLUDE_PATH

* tests: fix PHPUnit deprecation warning: DataProviders must be static

```
2 tests triggered 2 PHPUnit deprecations:

1) functionsTest::test_serendipity_serverOffsetHour
Data Provider method functionsTest::serverOffsetHourDataProvider() is not static

/home/runner/work/Serendipity/Serendipity/tests/include/functionsTest.php:17

2) functionsTest::test_serendipity_serverOffsetHourWithTimestampNull
Data Provider method functionsTest::serverOffsetHourWithTimestampNullDataProvider() is not static

/home/runner/work/Serendipity/Serendipity/tests/include/functionsTest.php:46
```

* tests: migrate PHPUnit XML configuration to version 11.0

```
There was 1 PHPUnit test runner deprecation:

1) Your XML configuration validates against a deprecated schema. Migrate your XML configuration using "--migrate-configuration"!
```

* tests: disable code coverage for now

* tests: migrate doc-comments to Attributes

```
There were 3 PHPUnit test runner deprecations:

1) Metadata found in doc-comment for method functionsConfigTest::test_serendipity_getTemplateFile(). Metadata in doc-comments is deprecated and will no longer be supported in PHPUnit 12. Update your test code to use attributes instead.

2) Metadata found in doc-comment for method functionsTest::test_serendipity_serverOffsetHour(). Metadata in doc-comments is deprecated and will no longer be supported in PHPUnit 12. Update your test code to use attributes instead.

3) Metadata found in doc-comment for method functionsTest::test_serendipity_serverOffsetHourWithTimestampNull(). Metadata in doc-comments is deprecated and will no longer be supported in PHPUnit 12. Update your test code to use attributes instead.

```

* tests: fix assertions

`assertContains()` is used for arrays etc., but we only have a string.
Use `assertStringEndsWith()` instead, the _ends with_ also makes the
test more robust.

* tests: also run on PHP 7.4
  • Loading branch information
mmitch authored Feb 20, 2024
1 parent 3b05b82 commit 78d4d10
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 64 deletions.
39 changes: 39 additions & 0 deletions .github/workflows/php_tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# This workflow will run the tests on various versions of PHP.

name: PHP Tests

on:
push:
pull_request:
types: [opened]
# TODO: enable to run once a week regardless of commits, but this will expire if repo is inactive for a longer time
# schedule:
# - cron: '12 3 4 * *'

jobs:
test-php:
name: Run PHP Tests

runs-on: ubuntu-22.04

# for available versions see https://github.com/marketplace/actions/setup-php-action#tada-php-support
strategy:
matrix:
phpversion: ['7.4', '8.1', '8.2', '8.3']

steps:
- uses: actions/checkout@v4
- name: Setup PHP and dependencies
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.phpversion }}
extensions: pdo-sqlite
tools: phpunit
# TODO: enable coverage check
coverage: none
env:
fail-fast: true
- name: Run Tests
run: |
cd tests
phpunit --configuration config.xml.dist include/*
31 changes: 15 additions & 16 deletions tests/config.xml.dist
Original file line number Diff line number Diff line change
@@ -1,20 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>

<phpunit>
<filter>
<whitelist addUncoveredFilesFromWhitelist="false">
<directory>../include</directory>
<directory>../plugins</directory>
<exclude>
<file>../include/db/mysql.inc.php</file>
<file>../include/db/mysqli.inc.php</file>
<file>../include/db/pdo-postgres.inc.php</file>
<file>../include/db/postgres.inc.php</file>
<file>../include/db/mysql.inc.php</file>
<file>../include/db/sqlrelay.inc.php</file>
</exclude>
</whitelist>
</filter>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/11.0/phpunit.xsd" cacheDirectory=".phpunit.cache">
<php>
<var name="S9Y_INSTALLDIR" value="http://localhost/s9y"/>
<var name="S9Y_LANG" value="en"/>
Expand All @@ -23,4 +8,18 @@
<var name="S9Y_SELENIUM_PORT" value="4444"/>
<var name="S9Y_SELENIUM_BROWSER" value="*firefox /usr/lib/firefox/firefox-bin"/>
</php>
<source>
<include>
<directory>../include</directory>
<directory>../plugins</directory>
</include>
<exclude>
<file>../include/db/mysql.inc.php</file>
<file>../include/db/mysqli.inc.php</file>
<file>../include/db/pdo-postgres.inc.php</file>
<file>../include/db/postgres.inc.php</file>
<file>../include/db/mysql.inc.php</file>
<file>../include/db/sqlrelay.inc.php</file>
</exclude>
</source>
</phpunit>
19 changes: 9 additions & 10 deletions tests/include/functionsConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
define('IN_serendipity', true);
require_once dirname(__FILE__) . '/../../include/functions_config.inc.php';

use PHPUnit\Framework\Attributes\Test;

/**
* Class functionsTest
*/
class functionsConfigTest extends PHPUnit_Framework_TestCase
class functionsConfigTest extends PHPUnit\Framework\TestCase
{
/**
* @test
*/
#[Test]
public function test_serendipity_getTemplateFile()
{
global $serendipity;
Expand All @@ -22,12 +22,11 @@ public function test_serendipity_getTemplateFile()
$serendipity['template_backend'] = '2k11';
$serendipity['serendipityPath'] = realpath('../');
$serendipity['serendipityHTTPPath'] = realpath('/');
$this->assertContains('next/index.tpl', serendipity_getTemplateFile('index.tpl'));

$this->assertStringEndsWith('next/index.tpl', serendipity_getTemplateFile('index.tpl'));
define('IN_serendipity_admin', true);
$this->assertContains('2k11/admin/index.tpl', serendipity_getTemplateFile('admin/index.tpl'));
$this->assertContains('next/index.tpl', serendipity_getTemplateFile('index.tpl', 'serendipityHTTPPath', true));
$this->assertStringEndsWith('2k11/admin/index.tpl', serendipity_getTemplateFile('admin/index.tpl'));
$this->assertStringEndsWith('next/index.tpl', serendipity_getTemplateFile('index.tpl', 'serendipityHTTPPath', true));
}


}
}
22 changes: 11 additions & 11 deletions tests/include/functionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,19 @@

$serendipity['dbType'] = 'pdo-sqlite';
define('IN_serendipity', true);
define('S9Y_INCLUDE_PATH', dirname(__FILE__) . '/../../');
require_once dirname(__FILE__) . '/../../include/functions.inc.php';

use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Test;

/**
* Class functionsTest
*/
class functionsTest extends PHPUnit_Framework_TestCase
class functionsTest extends PHPUnit\Framework\TestCase
{
/**
* @test
* @dataProvider serverOffsetHourDataProvider
*/
#[Test]
#[DataProvider("serverOffsetHourDataProvider")]
public function test_serendipity_serverOffsetHour($serverOffsetHours, $timestamp, $negative, $expected)
{
global $serendipity;
Expand All @@ -24,7 +26,7 @@ public function test_serendipity_serverOffsetHour($serverOffsetHours, $timestamp
/**
* @return array
*/
public function serverOffsetHourDataProvider()
public static function serverOffsetHourDataProvider()
{
return array(
array(0, 0, false, 0),
Expand All @@ -38,10 +40,8 @@ public function serverOffsetHourDataProvider()
);
}

/**
* @test
* @dataProvider serverOffsetHourWithTimestampNullDataProvider
*/
#[Test]
#[DataProvider("serverOffsetHourWithTimestampNullDataProvider")]
public function test_serendipity_serverOffsetHourWithTimestampNull($serverOffsetHours, $negative)
{
global $serendipity;
Expand All @@ -62,7 +62,7 @@ public function test_serendipity_serverOffsetHourWithTimestampNull($serverOffset
/**
* @return array
*/
public function serverOffsetHourWithTimestampNullDataProvider()
public static function serverOffsetHourWithTimestampNullDataProvider()
{
return array(
array(null, false),
Expand Down
50 changes: 23 additions & 27 deletions tests/phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,28 +1,24 @@
<?xml version="1.0" encoding="UTF-8" ?>
<phpunit bootstrap="bootstrap.php"
backupGlobals="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
colors="true">
<php>
<const name="IN_serendipity" value="true"/>
<!--<const name="IS_installed" value="true"/>-->
<!--<const name="USERLEVEL_ADMIN" value="255"/>-->
<!--<const name="USERLEVEL_CHIEF" value="1"/>-->
<!--<const name="USERLEVEL_EDITOR" value="0"/>-->
<!--<const name="DB_DSN" value="sqlite:dbname=s9y_test;host=localhost" />-->
<!--<const name="DB_HOST" value="localhost" />-->
<!--<const name="DB_TYPE" value="pdo-sqlite" />-->
<!--<const name="DB_USER" value="s9y_test" />-->
<!--<const name="DB_PASSWD" value="s9y_test" />-->
<!--<const name="DB_DBNAME" value="s9y_test" />-->
<!--<const name="TEST_DB" value="test.db" />-->
<!--<const name="PATH_SMARTY_COMPILE" value="templates_c" />-->
</php>
<testsuites>
<testsuite name="include">
<directory>../tests/include</directory>
</testsuite>
</testsuites>
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" bootstrap="bootstrap.php" backupGlobals="true" colors="true" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/11.0/phpunit.xsd" cacheDirectory=".phpunit.cache">
<php>
<const name="IN_serendipity" value="true"/>
<!--<const name="IS_installed" value="true"/>-->
<!--<const name="USERLEVEL_ADMIN" value="255"/>-->
<!--<const name="USERLEVEL_CHIEF" value="1"/>-->
<!--<const name="USERLEVEL_EDITOR" value="0"/>-->
<!--<const name="DB_DSN" value="sqlite:dbname=s9y_test;host=localhost" />-->
<!--<const name="DB_HOST" value="localhost" />-->
<!--<const name="DB_TYPE" value="pdo-sqlite" />-->
<!--<const name="DB_USER" value="s9y_test" />-->
<!--<const name="DB_PASSWD" value="s9y_test" />-->
<!--<const name="DB_DBNAME" value="s9y_test" />-->
<!--<const name="TEST_DB" value="test.db" />-->
<!--<const name="PATH_SMARTY_COMPILE" value="templates_c" />-->
</php>
<testsuites>
<testsuite name="include">
<directory>../tests/include</directory>
</testsuite>
</testsuites>
<source/>
</phpunit>

0 comments on commit 78d4d10

Please sign in to comment.