Skip to content

Commit

Permalink
Upgrade composer dependencies
Browse files Browse the repository at this point in the history
Migrate tests to Pest
Remove support for PHP 7.4, 8.0
Add support for PHP 8.3
Antispambot: update phpdoc
  • Loading branch information
fab120 committed Mar 1, 2024
1 parent c1be3ba commit 97db183
Show file tree
Hide file tree
Showing 11 changed files with 157 additions and 114 deletions.
20 changes: 14 additions & 6 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,23 @@ on: [push, pull_request]

jobs:
test:
runs-on: ubuntu-latest
runs-on: ${{ matrix.os }}
strategy:
fail-fast: true
matrix:
php: [7.4, '8.0', 8.1, 8.2]
os: [ubuntu-latest]
php: [8.1, 8.2, 8.3]
laravel: [9.*, 10.*, 11.*]
stability: [prefer-lowest, prefer-stable]
exclude:
- php: 8.1
laravel: 11.*

name: PHP${{ matrix.php }}
name: P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.stability }} - ${{ matrix.os }}

steps:
- name: Checkout code
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: Setup PHP
uses: shivammathur/setup-php@v2
Expand All @@ -28,7 +34,9 @@ jobs:
echo "::add-matcher::${{ runner.tool_cache }}/phpunit.json"
- name: Install dependencies
run: composer update --prefer-dist --no-interaction
run: |
composer require "illuminate/support:${{ matrix.laravel }}" --no-interaction --no-update
composer update --${{ matrix.stability }} --prefer-dist --no-interaction
- name: Execute tests
run: vendor/bin/phpunit
run: vendor/bin/pest
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/vendor
composer.lock
.php-cs-fixer.cache
.phpunit.result.cache
4 changes: 2 additions & 2 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Webnuvola

Copyright 2019-2023
Copyright 2019-2024

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
Expand All @@ -19,7 +19,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.

WordPress - Web publishing software

Copyright 2011-2023 by the contributors
Copyright 2011-2024 by the contributors

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
Expand Down
15 changes: 9 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@
}
],
"require": {
"php": "^7.4 || ^8.0"
"php": "^8.1"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^3.14",
"illuminate/support": "^8.0 || ^9.0 || ^10.0",
"phpunit/phpunit": "^9.6.4"
"illuminate/support": "^9.0 || ^10.0 || ^11.0",
"pestphp/pest": "^2.34"
},
"autoload": {
"files": [
Expand All @@ -31,14 +31,17 @@
}
},
"suggest": {
"illuminate/support": "Required to use antispambot_html function (^9.0||^10.0)"
"illuminate/support": "Required to use antispambot_html function (^9.0||^10.0||^11.0)"
},
"scripts": {
"test": "phpunit --testdox"
},
"config": {
"sort-packages": true
"sort-packages": true,
"allow-plugins": {
"pestphp/pest-plugin": true
}
},
"minimum-stability": "stable",
"minimum-stability": "dev",
"prefer-stable": true
}
16 changes: 8 additions & 8 deletions phpunit.xml.dist → phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpunit/phpunit.xsd"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.3/phpunit.xsd"
bootstrap="vendor/autoload.php"
colors="true"
>
<coverage>
<include>
<directory suffix=".php">./src</directory>
</include>
</coverage>
<testsuites>
<testsuite name="Antispambot tests">
<directory>tests</directory>
<testsuite name="Test Suite">
<directory suffix="Test.php">./tests</directory>
</testsuite>
</testsuites>
<source>
<include>
<directory suffix=".php">./src</directory>
</include>
</source>
</phpunit>
19 changes: 2 additions & 17 deletions src/Antispambot.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,6 @@ class Antispambot
{
/**
* Converts email addresses characters to HTML entities to block spam bots.
* Based on WordPress function antispambot.
*
* @param string $email_address
* @param bool $hex_encoding
* @return string
*
* @see https://developer.wordpress.org/reference/functions/antispambot/
*/
Expand All @@ -35,19 +30,9 @@ public static function antispambot(string $email_address, bool $hex_encoding = f
/**
* Add leading zeros when necessary.
*
* If you set the threshold to '4' and the number is '10', then you will get
* back '0010'. If you set the threshold to '4' and the number is '5000', then you
* will get back '5000'.
*
* Uses sprintf to append the amount of zeros based on the $threshold parameter
* and the size of the number. If the number is large enough, then no zeros will
* be appended.
*
* @param int|string $number
* @param int $threshold
* @return string
* @see https://developer.wordpress.org/reference/functions/zeroise/
*/
protected static function zeroise($number, int $threshold): string
protected static function zeroise(int|string $number, int $threshold): string
{
return sprintf('%0' . $threshold . 's', $number);
}
Expand Down
74 changes: 0 additions & 74 deletions tests/AntispambotTest.php

This file was deleted.

9 changes: 9 additions & 0 deletions tests/Datasets/Emails.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

dataset('emails', [
'info@example.com',
'dot.in.name@example.com',
'info@subdomain.example.com',
'test+plus@example.com',
'another+plus+test@example.com',
]);
45 changes: 45 additions & 0 deletions tests/Pest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

/*
|--------------------------------------------------------------------------
| Test Case
|--------------------------------------------------------------------------
|
| The closure you provide to your test functions is always bound to a specific PHPUnit test
| case class. By default, that class is "PHPUnit\Framework\TestCase". Of course, you may
| need to change it using the "uses()" function to bind a different classes or traits.
|
*/

// uses(Tests\TestCase::class)->in('Feature');

/*
|--------------------------------------------------------------------------
| Expectations
|--------------------------------------------------------------------------
|
| When you're writing tests, you often need to check that values meet certain conditions. The
| "expect()" function gives you access to a set of "expectations" methods that you can use
| to assert different things. Of course, you may extend the Expectation API at any time.
|
*/

expect()->extend('toBeOne', function () {
return $this->toBe(1);
});

/*
|--------------------------------------------------------------------------
| Functions
|--------------------------------------------------------------------------
|
| While Pest is very powerful out-of-the-box, you may have some testing code specific to your
| project that you don't want to repeat in every file. Here you can also expose helpers as
| global functions to help you to reduce the number of lines of code in your test files.
|
*/

function something()
{
// ..
}
10 changes: 10 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Webnuvola\Antispambot\Tests;

use PHPUnit\Framework\TestCase as BaseTestCase;

abstract class TestCase extends BaseTestCase
{
//
}
58 changes: 58 additions & 0 deletions tests/Unit/AntispambotTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

use Illuminate\Support\HtmlString;
use Webnuvola\Antispambot\Antispambot;

function decode_email(string $encodedEmail): string
{
return html_entity_decode($encodedEmail, ENT_HTML5);
}

it('can obfuscate email', function (string $email) {
$antispambot = Antispambot::antispambot($email);

expect($antispambot)
->not->toBe($email)
->and(decode_email($antispambot))->toBe($email);
})->with('emails');

it('can obfuscate email with hex encoding', function (string $email) {
$antispambot = Antispambot::antispambot($email, true);

expect($antispambot)
->not->toBe($email)
->and(html_entity_decode(urldecode(str_replace('+', '&#43;', $antispambot)), ENT_HTML5))->toBe($email);
})->with('emails');

test('antispambot function return value', function (string $email) {
$result = antispambot($email);
$return = preg_match('/"([^"]+)"/', $result, $matches);
expect($return)
->toBe(1)
->and(decode_email($matches[1]))->toBe('mailto:' . $email)
->and(decode_email(strip_tags($result)))->toBe($email);

$result = antispambot($email, 'Contact us');
$return = preg_match('/"([^"]+)"/', $result, $matches);
expect($return)
->toBe(1)
->and(decode_email($matches[1]))->toBe('mailto:' . $email)
->and(strip_tags($result))->toBe('Contact us');

$result = antispambot($email, 'Contact us', ['class' => 'text-white', 'target' => '_blank']);
$return = preg_match('/"([^"]+)"/', $result, $matches);
expect($return)
->toBe(1)
->and(decode_email($matches[1]))->toBe('mailto:' . $email)
->and(strip_tags($result))->toBe('Contact us')
->and(str_contains($result, 'class="text-white"'))->toBeTrue()
->and(str_contains($result, 'target="_blank"'))->toBeTrue();
})->with(['demo@example.com']);

test('antispambot_html function return value', function (string $email) {
$antispambotHtml = antispambot_html($email);

expect($antispambotHtml)
->toBeInstanceOf(HtmlString::class)
->and(decode_email(strip_tags((string) $antispambotHtml)))->toBe($email);
})->with(['demo@example.com']);

0 comments on commit 97db183

Please sign in to comment.