Skip to content

Commit

Permalink
test: update test frameworks and restore unit tests in CI
Browse files Browse the repository at this point in the history
Behat dependencies have been updated, but it is not expected that the
tests will work.
  • Loading branch information
stklcode committed Mar 17, 2024
1 parent e5c4ebe commit ad387fd
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 103 deletions.
12 changes: 7 additions & 5 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
name: Tests, Behat, Coding Standards
on: push
jobs:
test:
test-unit:
runs-on: ubuntu-latest
strategy:
matrix:
php-versions: ['7.3', '7.2']
php-versions: ['8.3', '7.4']
steps:
- uses: actions/checkout@master
- name: Setup PHP
Expand All @@ -14,11 +14,13 @@ jobs:
php-version: ${{ matrix.php-versions }}
extensions: mbstring, intl
ini-values: post_max_size=256M, short_open_tag=On
tools: phpunit, composer
- name: Run tests
tools: composer
- name: Install
run: |
composer install
./vendor/bin/phpunit -c ./phpunit.xml.dist
- name: Run tests
run: |
composer test:unit
behat:
runs-on: ubuntu-latest
services:
Expand Down
20 changes: 9 additions & 11 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,15 @@
"php": ">=7.0"
},
"require-dev": {
"behat/mink": "1.7.1",
"behat/mink-goutte-driver": "^1.2",
"brain/monkey": "^1.5",
"composer/package-versions-deprecated": "^1.11",
"dmore/behat-chrome-extension": "^1.3",
"dealerdirect/phpcodesniffer-composer-installer": "^0.7.1",
"friends-of-behat/mink-extension": "^2.5",
"paulgibbs/behat-wordpress-extension": "^3.3",
"behat/mink": "^v1.11.0",
"behat/mink-browserkit-driver": "^v2.2.0",
"dmore/behat-chrome-extension": "^1.4.0",
"dealerdirect/phpcodesniffer-composer-installer": "^v1.0.0",
"friends-of-behat/mink-extension": "^v2.7.5",
"phpcompatibility/phpcompatibility-wp": "^2.1",
"phpunit/phpunit": "^4",
"wp-cli/wp-cli-bundle": "@stable",
"wp-coding-standards/wpcs": "^3.0"
"wp-coding-standards/wpcs": "^3.0.1",
"yoast/wp-test-utils": "^1.2.0"
},
"autoload": {
"psr-4": {
Expand All @@ -61,7 +58,8 @@
],
"lint-php": [
"phpcs --standard=phpcs.xml -s"
]
],
"test:unit": [ "phpunit -c phpunit.xml.dist --testsuite unit" ]
},
"config": {
"allow-plugins": {
Expand Down
47 changes: 23 additions & 24 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/4.8/phpunit.xsd"
backupGlobals="false"
backupStaticAttributes="false"
bootstrap="vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false">
<testsuites>
<testsuite name="unit">
<directory suffix=".php">tests/Unit</directory>
</testsuite>
<testsuite name="integration">
<directory suffix=".php">tests/Integration</directory>
</testsuite>
</testsuites>
<listeners>
<listener class="TestListener" file="tests/TestListener.php"/>
</listeners>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/9.2/phpunit.xsd"
backupGlobals="false"
bootstrap="./vendor/autoload.php"
colors="true"
beStrictAboutTestsThatDoNotTestAnything="true"
beStrictAboutOutputDuringTests="true"
convertErrorsToExceptions="true"
convertWarningsToExceptions="true"
convertNoticesToExceptions="true"
convertDeprecationsToExceptions="true">
<testsuites>
<testsuite name="unit">
<directory suffix=".php">tests/Unit</directory>
<exclude>tests/Unit/AntispamBeeTest.php</exclude>
</testsuite>
<testsuite name="integration">
<directory suffix=".php">tests/Integration</directory>
</testsuite>
</testsuites>
<listeners>
<listener class="TestListener" file="tests/TestListener.php"/>
</listeners>
</phpunit>
45 changes: 0 additions & 45 deletions tests/TestCase.php

This file was deleted.

12 changes: 9 additions & 3 deletions tests/TestListener.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
<?php

use PHPUnit\Framework\TestSuite;

use PHPUnit\Framework\TestListener as PHPUnitTestListener;
use PHPUnit\Framework\TestListenerDefaultImplementation;

/**
* Test listener implementation taking care of loading stubs for unit tests.
*
Expand All @@ -8,7 +13,8 @@
*
* @since 2.7.0
*/
class TestListener extends PHPUnit_Framework_BaseTestListener {
class TestListener implements PHPUnitTestListener {
use TestListenerDefaultImplementation;

/**
* Performs individual test-suite-specific actions.
Expand All @@ -17,9 +23,9 @@ class TestListener extends PHPUnit_Framework_BaseTestListener {
*
* @since 2.7.0
*
* @param PHPUnit_Framework_TestSuite $suite Test suite object.
* @param TestSuite $suite Test suite object.
*/
public function startTestSuite( PHPUnit_Framework_TestSuite $suite ) {
public function startTestSuite( TestSuite $suite ): void {
switch ( strtolower( $suite->getName() ) ) {
case 'unit':
$this->stub_functions();
Expand Down
32 changes: 17 additions & 15 deletions tests/Unit/AntispamBeeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@
namespace AntispamBee\Tests\Unit\Core;

use Antispam_Bee as Testee;
use AntispamBee\Tests\TestCase;
use Brain\Monkey\Functions;
use Yoast\WPTestUtils\BrainMonkey\TestCase;

use function Brain\Monkey\Functions\expect;
use function Brain\Monkey\Functions\when;

/**
* Test case for the factory class.
*
* TODO: This is a legacy test from 2.x. Update or remove.
*
* @since 2.7.0
*/
class FactoryTest extends TestCase {
Expand All @@ -18,20 +22,20 @@ class FactoryTest extends TestCase {
*
* @since 2.7.0
*/
protected function setUp() {
parent::setUp();
protected function set_up() {
parent::set_up();

Functions::when( 'get_bloginfo' )->justReturn( 'https://domain.com/' );
Functions::when( 'wp_parse_url' )->alias('parse_url');
Functions::when( 'is_admin' )->justReturn( false );
Functions::expect( 'wp_unslash' )
when( 'get_bloginfo' )->justReturn( 'https://domain.com/' );
when( 'wp_parse_url' )->alias('parse_url');
when( 'is_admin' )->justReturn( false );
expect( 'wp_unslash' )
->andReturnUsing(
function( $data ) {
return $data;
}
);

Functions::when( 'get_option' )->justReturn( $this->get_options() );
when( 'get_option' )->justReturn( $this->get_options() );

Testee::init();
}
Expand Down Expand Up @@ -81,18 +85,16 @@ public function test_gets_ip_address() {
*
* @covers Testee::handle_incoming_request()
*
* @param array $comment Comment overrides to use.
* @param string $reason Expected spam reason to catch.
*/
public function test_spam_reasons( $comment, $reason ) {
$comment = array_merge( $this->get_base_comment(), $comment );
public function test_spam_reasons($comment, $reason) {
$comment = array_merge($this->get_base_comment(), $comment);

$_SERVER['HTTP_CLIENT_IP'] = '12.23.34.45';
$_SERVER['REQUEST_URI'] = 'https://domain.com/wp-comments-post.php';
$_POST['comment'] = $comment;

// This is where we check for the spam reason that was detected.
Functions::expect( 'add_comment_meta' )->once()
expect( 'add_comment_meta' )->once()
->with(
1,
'antispam_bee_reason',
Expand Down Expand Up @@ -133,7 +135,7 @@ public function spam_reasons_data_provider() {
// @ToDo: static $_reason
array(
array(
'comment_content' => "this is a pharmacy, why does it work now?.",
'comment_content' => "this is a pharmacy, why does it work now?.",
'comment_author_email' => 'test@yandex.ru',
),
'regexp',
Expand Down
33 changes: 33 additions & 0 deletions tests/Unit/Rules/EmptyDataTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace AntispamBee\Tests\Rules;

use AntispamBee\Helpers\ContentTypeHelper;
use AntispamBee\Rules\EmptyData;
use Yoast\WPTestUtils\BrainMonkey\TestCase;

use function Brain\Monkey\Functions\when;

class EmptyDataTest extends TestCase {

public function test_verify_comment() {
$item = array( 'reaction_type' => ContentTypeHelper::COMMENT_TYPE );
self::assertSame( 999, EmptyData::verify( $item ), 'unexpected result for empty comment' );

$item['comment_content'] = 'This is a test.';
self::assertSame( 999, EmptyData::verify( $item ), 'unexpected result for empty author IP' );

$item['comment_author_IP'] = '192.0.2.91';
when( 'get_option' )->justReturn( false );
self::assertSame( 0, EmptyData::verify( $item ), 'unexpected result with no name required' );

when( 'get_option' )->justReturn( true );
self::assertSame( 999, EmptyData::verify( $item ), 'unexpected result without name and mail' );

$item['comment_author_email'] = 'comments@example.com';
self::assertSame( 999, EmptyData::verify( $item ), 'unexpected result without author name' );

$item['comment_author'] = 'A. Tester';
self::assertSame( 0, EmptyData::verify( $item ), 'unexpected result with name and mail' );
}
}

0 comments on commit ad387fd

Please sign in to comment.