This commit is contained in:
Javier Casares 2026-01-30 18:51:40 +00:00
commit 443dceb8ca
21 changed files with 566609 additions and 230 deletions

View file

@ -0,0 +1,53 @@
<?php
namespace WP_Since\Tests;
use PHPUnit\Framework\TestCase;
use WP_Since\Checker\CompatibilityChecker;
class CompatibilityCheckerTest extends TestCase
{
public function testDetectsIncompatibleSymbols()
{
$sinceMap = [
'register_setting' => ['since' => '5.5.0'],
'WP_Query' => ['since' => '3.0.0'],
'MyClass::boot' => ['since' => '6.2.0'],
'custom_hook' => ['since' => '6.0.0'],
];
$symbols = [
'register_setting',
'WP_Query',
'MyClass::boot',
'custom_hook',
];
$declaredVersion = '5.5';
$checker = new CompatibilityChecker($sinceMap);
$incompatible = $checker->check($symbols, $declaredVersion);
$this->assertArrayHasKey('MyClass::boot', $incompatible);
$this->assertArrayHasKey('custom_hook', $incompatible);
$this->assertArrayNotHasKey('register_setting', $incompatible);
$this->assertArrayNotHasKey('WP_Query', $incompatible);
$this->assertEquals('6.2.0', $incompatible['MyClass::boot']);
}
public function testAllSymbolsCompatible()
{
$sinceMap = [
'function_one' => ['since' => '5.1.0'],
'function_two' => ['since' => '5.0.0'],
];
$symbols = ['function_one', 'function_two'];
$declaredVersion = '5.5.0';
$checker = new CompatibilityChecker($sinceMap);
$incompatible = $checker->check($symbols, $declaredVersion);
$this->assertEmpty($incompatible);
}
}

View file

@ -0,0 +1,52 @@
<?php
namespace WP_Since\Tests\Integration;
use PHPUnit\Framework\TestCase;
use WP_Since\Resolver\VersionResolver;
use WP_Since\Scanner\PluginScanner;
use WP_Since\Checker\CompatibilityChecker;
class FullCompatibilityFlowTest extends TestCase
{
public function testDetectsAllTypesOfSymbolsCorrectly()
{
$pluginPath = __DIR__ . '/../fixtures/plugin-full-test';
$declaredVersion = VersionResolver::resolve($pluginPath);
$symbols = PluginScanner::scan($pluginPath);
$this->assertNotNull($declaredVersion, 'Declared version should not be null');
$this->assertEquals('5.5', $declaredVersion['version']);
$sinceMap = [
'add_option' => ['since' => '2.0.0'],
'WP_Query' => ['since' => '3.0.0'],
'WP_Filesystem::get_contents' => ['since' => '5.1.0'],
'WP_User::add_cap' => ['since' => '5.7.0'],
'my_custom_hook' => ['since' => '6.0.0'],
'my_filter_hook' => ['since' => '5.3.0'],
];
$checker = new CompatibilityChecker($sinceMap);
$incompatible = $checker->check($symbols, $declaredVersion['version']);
$this->assertArrayHasKey('WP_User::add_cap', $incompatible);
$this->assertArrayHasKey('my_custom_hook', $incompatible);
$this->assertArrayNotHasKey('add_option', $incompatible);
$this->assertArrayNotHasKey('WP_Query', $incompatible);
$this->assertArrayNotHasKey('WP_Filesystem::get_contents', $incompatible);
$this->assertArrayNotHasKey('my_filter_hook', $incompatible);
$expected = array_keys($sinceMap);
foreach ($expected as $symbol) {
$this->assertContains($symbol, $symbols, "Missing symbol: {$symbol}");
// phpcs:disable Generic.Files.LineLength.TooLong
$this->assertNotContains('some_ignored_func_folder', $symbols, 'Should ignore folder from /ignored-folder/');
$this->assertNotContains('some_ignored_func_file', $symbols, 'Should ignore specific file /ignore-this.php');
$this->assertNotContains('some_ignored_func_noslash', $symbols, 'Should ignore folder from ignored-no-slash/');
}
}
}

View file

@ -0,0 +1,44 @@
<?php
namespace WP_Since\Tests;
use PHPUnit\Framework\TestCase;
use WP_Since\Scanner\PluginScanner;
final class PluginScannerTest extends TestCase
{
public function testDetectsAllSymbols()
{
$pluginPath = __DIR__ . '/fixtures/plugin-full-test';
$symbols = PluginScanner::scan($pluginPath);
$expected = [
'add_option',
'WP_Query',
'WP_Filesystem::get_contents',
'WP_User::add_cap',
'my_custom_hook',
'my_filter_hook',
];
foreach ($expected as $symbol) {
$this->assertContains($symbol, $symbols, "Missing: $symbol");
}
}
public function testIgnoresSymbolsMarkedWithIgnoreComment()
{
$path = __DIR__ . '/fixtures/plugin-ignore-comment';
$symbols = PluginScanner::scan($path);
$this->assertNotContains('add_option', $symbols, 'Should ignore symbol with @wp-since ignore');
$this->assertNotContains('should_be_ignored', $symbols, 'Should ignore symbolwith @wp-since ignore');
$this->assertNotContains('wp_is_block_theme', $symbols, 'Should ignore symbol with @wp-since ignore');
$this->assertNotContains('should_be_ignored_space', $symbols, 'Should ignore symbol with @wp-since ignore');
$this->assertContains('do_action', $symbols, 'Should detect function call without ignore comment');
$this->assertContains('my_custom_hook', $symbols, 'Should detect function call without ignore comment');
$this->assertContains('register_setting', $symbols, 'Should detect function call without ignore comment');
$this->assertContains('wp_detected_function', $symbols, 'Should detect function call without ignore comment');
$this->assertContains('need_detect', $symbols, 'Should detect function call without ignore comment');
}
}

View file

@ -0,0 +1,42 @@
<?php
namespace WP_Since\Tests\Resolver;
use PHPUnit\Framework\TestCase;
use WP_Since\Resolver\IgnoreRulesResolver;
class IgnoreRulesResolverTest extends TestCase
{
public function testShouldIgnoreExactMatch()
{
$ignorePaths = ['tests/', 'assets/', 'example.php'];
$this->assertTrue(IgnoreRulesResolver::shouldIgnore('tests/helper.php', $ignorePaths));
$this->assertTrue(IgnoreRulesResolver::shouldIgnore('assets/img/logo.png', $ignorePaths));
$this->assertTrue(IgnoreRulesResolver::shouldIgnore('example.php', $ignorePaths));
}
public function testShouldNotIgnoreNonMatchingPath()
{
$ignorePaths = ['tests/', 'vendor/', 'docs/readme.txt'];
$this->assertFalse(IgnoreRulesResolver::shouldIgnore('src/Plugin.php', $ignorePaths));
$this->assertFalse(IgnoreRulesResolver::shouldIgnore('includes/functions.php', $ignorePaths));
}
public function testShouldIgnoreNestedPaths()
{
$ignorePaths = ['admin/'];
$this->assertTrue(IgnoreRulesResolver::shouldIgnore('admin/settings/page.php', $ignorePaths));
$this->assertTrue(IgnoreRulesResolver::shouldIgnore('admin/page.php', $ignorePaths));
}
public function testShouldIgnoreWithOrWithoutLeadingSlash()
{
$ignorePaths = ['/build/', 'temp/'];
$this->assertTrue(IgnoreRulesResolver::shouldIgnore('build/bundle.js', $ignorePaths));
$this->assertTrue(IgnoreRulesResolver::shouldIgnore('temp/cache.php', $ignorePaths));
}
}

View file

@ -0,0 +1,29 @@
<?php
namespace WP_Since\Tests;
use PHPUnit\Framework\TestCase;
use WP_Since\Utils\TablePrinter;
class TablePrinterTest extends TestCase
{
public function testRenderOutputsCorrectTable()
{
$headers = ['Name', 'Version'];
$rows = [
['register_setting', '5.5.0'],
['some_function', '6.0.0'],
];
ob_start();
TablePrinter::render($rows, $headers);
$output = ob_get_clean();
$this->assertStringContainsString('register_setting', $output);
$this->assertStringContainsString('some_function', $output);
$this->assertStringContainsString('┌', $output);
$this->assertStringContainsString('┴', $output);
$this->assertStringContainsString('│ Name', $output);
$this->assertStringContainsString('│ Version', $output);
}
}

View file

@ -0,0 +1,24 @@
<?php
namespace WP_Since\Tests;
use PHPUnit\Framework\TestCase;
use WP_Since\Utils\VersionHelper;
class VersionHelperTest extends TestCase
{
public function testNormalizeShortVersions()
{
$this->assertEquals('5.5.0', VersionHelper::normalize('5.5'));
$this->assertEquals('5.0.0', VersionHelper::normalize('5'));
$this->assertEquals('6.1.2', VersionHelper::normalize('6.1.2'));
}
public function testCompareVersions()
{
$this->assertSame(0, VersionHelper::compare('5.5', '5.5.0'));
$this->assertSame(0, VersionHelper::compare('6.1.0', '6.1'));
$this->assertSame(1, VersionHelper::compare('6.2', '6.1.5'));
$this->assertSame(-1, VersionHelper::compare('5.9', '6.0'));
}
}

View file

@ -0,0 +1,35 @@
<?php
namespace WP_Since\Tests;
use PHPUnit\Framework\TestCase;
use WP_Since\Resolver\VersionResolver;
class VersionResolverTest extends TestCase
{
public function testExtractsVersionFromMainPluginFile()
{
$path = __DIR__ . '/fixtures/plugin-with-header';
$resolved = VersionResolver::resolve($path);
$this->assertEquals('6.2', $resolved['version']);
$this->assertEquals('main plugin file header', $resolved['source']);
}
public function testExtractsVersionFromReadmeIfNoHeader()
{
$path = __DIR__ . '/fixtures/plugin-with-readme-only';
$resolved = VersionResolver::resolve($path);
$this->assertEquals('5.8', $resolved['version']);
$this->assertEquals('readme', $resolved['source']);
}
public function testReturnsNullIfVersionNotFoundAnywhere()
{
$path = __DIR__ . '/fixtures/plugin-without-version';
$resolved = VersionResolver::resolve($path);
$this->assertNull($resolved);
}
}

View file

@ -0,0 +1,5 @@
<?php
do_action('init');
register_setting('group', 'option');
new WP_Query();

View file

@ -0,0 +1,4 @@
<?php
apply_filters('custom_filter', $value);
MyClass::boot();

View file

@ -0,0 +1,3 @@
/ignored-folder/
/ignore-this.php
ignored-no-slash/

View file

@ -0,0 +1,6 @@
<?php
function ignored_function_file()
{
some_ignored_func_file();
}

View file

@ -0,0 +1,6 @@
<?php
function ignored_function_folder()
{
some_ignored_func_folder();
}

View file

@ -0,0 +1,6 @@
<?php
function ignored_function_noslash()
{
some_ignored_func_noslash();
}

View file

@ -0,0 +1,28 @@
<?php
/**
* Plugin Name: My plugin
* Plugin URI: https://myddelivery.com/
* Description: Simple test plugin.
* Author: EduardoVillao.me
* Author URI: https://eduardovillao.me/
* Version: 1.0
* Requires PHP: 7.4
* Requires at least: 5.5
* Text Domain: test-plugin
* Domain Path: /languages
* License: GPL-2.0+
* License URI: https://www.gnu.org/licenses/gpl-2.0.txt
*/
add_option('foo', 'bar');
$query = new WP_Query();
WP_Filesystem::get_contents('/some/path');
$user = new WP_User();
$user->add_cap('edit_posts');
do_action('my_custom_hook', 'param');
apply_filters('my_filter_hook', 'value');

View file

@ -0,0 +1,10 @@
=== Test Plugin ===
Contributors: evcode
Donate link: https://eduardovillao.me/
Tags: delivery, wordpress delivery, delivery whatsapp
Requires at least: 5.5
Tested up to: 6.7
Stable tag: 2.0
Requires PHP: 7.4
License: GPLv2License
URI:https://www.gnu.org/licenses/gpl-2.0.html

View file

@ -0,0 +1,29 @@
<?php
add_option('should_be_ignored'); // @wp-since ignore
do_action('should_be_ignored_space'); // @wp-since ignore
do_action('need_detect'); // simple comment
do_action('my_custom_hook');
register_setting('mygroup', 'myoption');
function isBlockTheme()
{
if (function_exists( 'wp_is_block_theme')) {
return wp_is_block_theme(); // @wp-since ignore
}
return false;
}
function exampleWithoutIgnore()
{
if (function_exists('wp_detected_function')) {
return wp_detected_function();
}
return false;
}

View file

@ -0,0 +1,16 @@
<?php
/**
* Plugin Name: My plugin
* Plugin URI: https://myddelivery.com/
* Description: Simple test plugin.
* Author: EduardoVillao.me
* Author URI: https://eduardovillao.me/
* Version: 1.0
* Requires PHP: 7.4
* Requires at least: 6.2
* Text Domain: test-plugin
* Domain Path: /languages
* License: GPL-2.0+
* License URI: https://www.gnu.org/licenses/gpl-2.0.txt
*/

View file

@ -0,0 +1,15 @@
<?php
/**
* Plugin Name: My plugin
* Plugin URI: https://myddelivery.com/
* Description: Simple test plugin.
* Author: EduardoVillao.me
* Author URI: https://eduardovillao.me/
* Version: 1.0
* Requires PHP: 7.4
* Text Domain: test-plugin
* Domain Path: /languages
* License: GPL-2.0+
* License URI: https://www.gnu.org/licenses/gpl-2.0.txt
*/

View file

@ -0,0 +1,10 @@
=== My Plugin ===
Contributors: evcode
Donate link: https://eduardovillao.me/
Tags: test plugin
Requires at least: 5.8
Tested up to: 6.7
Stable tag: 1.0
Requires PHP: 7.4
License: GPLv2License
URI:https://www.gnu.org/licenses/gpl-2.0.html

View file

@ -0,0 +1,15 @@
<?php
/**
* Plugin Name: My plugin
* Plugin URI: https://myddelivery.com/
* Description: Simple test plugin.
* Author: EduardoVillao.me
* Author URI: https://eduardovillao.me/
* Version: 1.0
* Requires PHP: 7.4
* Text Domain: test-plugin
* Domain Path: /languages
* License: GPL-2.0+
* License URI: https://www.gnu.org/licenses/gpl-2.0.txt
*/

Some files were not shown because too many files have changed in this diff Show more