console-helpers/prophecy-phpunit

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

v3.0.0 2024-07-15 12:38 UTC

This package is auto-updated.

Last update: 2024-09-15 13:07:19 UTC


README

Build Status

Prophecy PhpUnit将Prophecy模拟库与PHPUnit集成,以便在测试套件中提供更简单的模拟功能。

安装

先决条件

Prophecy PhpUnit需要PHP 5.6或更高版本。Prophecy PhpUnit需要PHPUnit 5.0或更高版本。

通过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());
    }
}