hjerichen / prophecy-php
预测PHP函数。
1.0.0
2021-02-09 21:03 UTC
Requires
- php: ^7.4 | ^8.0
- phpspec/prophecy: ^1.8
- phpunit/php-text-template: ^2.0
Requires (Dev)
- phpspec/prophecy-phpunit: ^2.0
- phpunit/phpunit: ^9.0
- roave/security-advisories: dev-master
README
Prophecy-PHP
使用Prophecy风格在PHPUnit中模拟内置PHP函数。
安装
使用 Composer
composer require --dev hjerichen/prophecy-php
使用方法
在PHPUnit测试用例中使用PHPProphetTrait特性。
<?php namespace Some\Space; use PHPUnit\Framework\TestCase; use HJerichen\ProphecyPHP\PHPProphetTrait; use HJerichen\ProphecyPHP\NamespaceProphecy; class SomeTest extends TestCase { use PHPProphetTrait; /** @var NamespaceProphecy */ private $php; public function setUp(): void { parent::setUp(); $this->php = $this->prophesizePHP(__NAMESPACE__); } public function testSomething(): void { $this->php->time()->willReturn(2); $this->php->reveal(); self::assertEquals(2, time()); } }
一切工作方式都与您从Prophecy所知相同
<?php $this->php->time()->willReturn(1234, 1235, 1236); $this->php->date('Y', 1234)->willReturn('1970'); $this->php->date('Y', 1234000)->willReturn('1971'); $this->php->file_put_contents('/to/foo.txt', 'some content')->shouldBeCalledOnce(); $this->php->file_put_contents('/to/foo.txt2', 'some content')->shouldBeCalledOnce(); $this->php->file_put_contents('/to/foo.txt', \Prophecy\Argument::any())->shouldNotBeCalled(); $this->php->reveal(); //Only with this call the above functions will be mocked.
限制
只有命名空间上下文中的 未限定 函数调用可以被模拟。
已知问题
由于PHP错误 64346,模拟可能不起作用。这是因为模拟之前在命名空间中调用原始函数。在这种情况下,您可以在 "setUp" 方法中尝试使用 "prepare"。
<?php $this->php->prepare('time', 'date'); //If you have problems with the time and date functions.
许可证和作者
本项目是免费的,受MIT许可证约束。本项目由Heiko Jerichen负责(heiko@jerichen.de)。