rogervila/global-namespace

在命名空间下封装全局 PHP 函数

1.1.0 2021-01-25 10:54 UTC

This package is auto-updated.

Last update: 2024-09-10 11:09:14 UTC


README

Build Status Example Application Status StyleCI Quality Gate Status

Latest Stable Version Total Downloads License

Global Namespace

全局命名空间

这个库为像 PHP 内置函数这样的全局函数提供了一个命名空间。

这听起来可能有些奇怪,让我通过一个例子来展示它能做什么。

示例

假设你有一个使用 rand(0, 1) 的应用程序,你想断言它返回 01 的情况。

让我们构建这个类

class App
{
    public function run()
    {
        $result = rand(0, 1);

        if ($result == 0) {
            // do something when $result is 0
        } else {
            // do something else when $result is 1
        }
    }
}

如果你创建一个测试来断言结果,你会遇到一个障碍: 我们如何模拟 rand() 来强制它返回 0 或 1?。这个包为此问题提供了一个解决方案。

首先,在你的应用程序中引入这个包。

composer require rogervila/global-namespace

现在,更新你的应用程序,将 rand() 前缀为全局命名空间。

PHP::rand() 将简单地调用内置的 PHP 函数,所以你的应用程序的行为不会改变。

use PHP\PHP;

class App
{
    public function run()
    {
        $result = PHP::rand(0, 1);
        //...
    }
}

这按预期工作,但还不是完全可测试的,因为 PHP 依赖项是硬编码的,无法模拟。

让我们将其添加到构造函数中,这样我们就可以使用任何 PHP 实现。

use PHP\PHP;

class App
{
    private PHP $php;

    public function __construct(PHP $php)
    {
        $this->php = $php;
    }

    public function run()
    {
        $result = $this->php::rand(0, 1);
        //...
    }
}

很好。现在你可以使用 Mockery 模拟 rand() 并断言应用程序代码

final class AppTest extends \PHPUnit\Framework\TestCase
{
    public function test_rand_returns_one()
    {
        // Create a mock of the package class
        $php = Mockery::mock(\PHP\PHP::class);

        // Define its return value
        $php->shouldReceive('rand')
            ->once()
            ->andReturn(1);

        // Now rand() always returns 1
        $this->assertEquals(
            1,
            $php::rand()
        );

        // Create the instance with the mocked PHP implementation
        $app = new App($php);

        // Do your application assertions here
        // ...
    }
}

查看这个 示例应用程序 以获取更完整的示例

关于

这个库调用任何全局函数,而不仅仅是内置函数。

use PHP\PHP;

// PHP Built-in functions
PHP::http_build_query(['foo' => 'bar']); // returns 'foo=bar'
PHP::json_encode(['foo' => 'bar']); // returns '{"foo": "bar"}'
PHP::json_decode('{"foo": "bar"}'); // return ['foo' => 'bar']
// etc...


// For WordPress projects
PHP::wp_redirect('/') // redirects to home
// etc...

// Literaly, any function, built-in or not, could be listed here

辅助类

如果你不想使用 Mockery,或者如果你在一个不适合它的测试环境中工作,你可以使用这个包附带的一些辅助类

IgnoreMissing

它的工作方式相同,但如果函数不存在,它将忽略该函数。

如果你由于某种原因不能模拟某个函数,这可能很有用。

use PHP\IgnoreMissing as PHP;

PHP::http_build_query(['foo' => 'bar']); // returns 'foo=bar'

PHP::foo(); // returns null instead of triggering a "Call to undefined function" error

// missing functions return null. existing functions are called

IgnoreAlways

如果你出于某种原因想直接跳过所有函数调用,请使用它。

无论函数是否存在都无关紧要。

use PHP\IgnoreAlways as PHP;

PHP::http_build_query(['foo' => 'bar']); // returns null

PHP::foo(); // returns null

// all functions return null

作者

Roger Vilà 创建

许可

Global Namespace 是开源软件,根据 MIT 许可协议 许可。