talentrydev/prophecy-phpunit

将 Prophecy 模拟库集成到 PHPUnit 测试用例中

2.0.3 2023-11-30 09:12 UTC

This package is auto-updated.

Last update: 2024-08-30 01:38:41 UTC


README

Build Status

Prophecy PhpUnit 将 Prophecy 模拟库与 PHPUnit 集成,以便在测试套件中更轻松地进行模拟。

安装

先决条件

Prophecy PhpUnit 需要 PHP 7.3 或更高版本。Prophecy PhpUnit 需要 PHPUnit 9.1 或更高版本。较旧版本的 PHPUnit 已经提供了 Prophecy 集成。

通过 Composer 安装

composer require --dev phpspec/prophecy-phpunit

您可以在其 官方网页 上了解更多有关 Composer 的信息。

如何使用

特质 ProphecyTrait 提供了一个名为 prophesize($classOrInterface = null) 的方法来使用 Prophecy。有关 Prophecy 双重使用的详细信息,请参阅 Prophecy 文档

以下是一个使用示例

<?php

namespace App;

use PHPUnit\Framework\TestCase;
use Prophecy\PhpUnit\ProphecyTrait;
use App\Security\Hasher;
use App\Entity\User;

class UserTest extends TestCase
{
    use ProphecyTrait;

    public function testPasswordHashing()
    {
        $hasher = $this->prophesize(Hasher::class);
        $user   = new User($hasher->reveal());

        $hasher->generateHash($user, 'qwerty')->willReturn('hashed_pass');

        $user->setPassword('qwerty');

        $this->assertEquals('hashed_pass', $user->getPassword());
    }
}