# Prophecy
[![Stable release](https://poser.pugx.org/phpspec/prophecy/version.svg)](https://packagist.org/packages/phpspec/prophecy)
[![Build Status](https://travis-ci.org/phpspec/prophecy.svg?branch=master)](https://travis-ci.org/phpspec/prophecy)
Prophecy is a highly opinionated yet very powerful and flexible PHP object mocking
framework. Though initially it was created to fulfil phpspec2 needs, it is flexible
enough to be used inside any testing framework out there with minimal effort.
## A simple example
```php
<?php
class UserTest extends PHPUnit_Framework_TestCase
{
private $prophet;
public function testPasswordHashing()
{
$hasher = $this->prophet->prophesize('App\Security\Hasher');
$user = new App\Entity\User($hasher->reveal());
$hasher->generateHash($user, 'qwerty')->willReturn('hashed_pass');
$user->setPassword('qwerty');
$this->assertEquals('hashed_pass', $user->getPassword());
}
protected function setup()
{
$this->prophet = new \Prophecy\Prophet;
}
protected function tearDown()
{
$this->prophet->checkPredictions();
}
}
```
## Installation
### Prerequisites
Prophecy requires PHP 5.3.3 or greater.
### Setup through composer
First, add Prophecy to the list of dependencies inside your `composer.json`:
```json
{
"require-dev": {
"phpspec/prophecy": "~1.0"
}
}
```
Then simply install it with composer:
```bash
$> composer install --prefer-dist
```
You can read more about Composer on its [official webpage](http://getcomposer.org).
## How to use it
First of all, in Prophecy every word has a logical meaning, even the name of the library
itself (Prophecy). When you start feeling that, you'll become very fluid with this
tool.
For example, Prophecy has been named that way because it concentrates on describing the future
behavior of objects with very limited knowledge about them. But as with any other prophecy,
those object prophecies can't create themselves - there should be a Prophet:
```php
$prophet = new Prophecy\Prophet;
```
The Prophet creates prophecies by *prophesizing* them:
```php
$prophecy = $prophet->prophesize();
```
The result of the `prophesize()` method call is a new object of class `ObjectProphecy`. Yes,
that's your specific object prophecy, which describes how your object would behave
in the near future. But first, you need to specify which object you're talking about,
right?
```php
$prophecy->willExtend('stdClass');
$prophecy->willImplement('SessionHandlerInterface');
```
There are 2 interesting calls - `willExtend` and `willImplement`. The first one tells
object prophecy that our object should extend specific class, the second one says that
it should implement some interface. Obviously, objects in PHP can implement multiple
interfaces, but extend only one parent class.
### Dummies
Ok, now we have our object prophecy. What can we do with it? First of all, we can get
our object *dummy* by revealing its prophecy:
```php
$dummy = $prophecy->reveal();
```
The `$dummy` variable now holds a special dummy object. Dummy objects are objects that extend
and/or implement preset classes/interfaces by overriding all their public methods. The key
point about dummies is that they do not hold any logic - they just do nothing. Any method
of the dummy will always return `null` and the dummy will never throw any exceptions.
Dummy is your friend if you don't care about the actual behavior of this double and just need
a token object to satisfy a method typehint.
You need to understand one thing - a dummy is not a prophecy. Your object prophecy is still
assigned to `$prophecy` variable and in order to manipulate with your expectations, you
should work with it. `$dummy` is a dummy - a simple php object that tries to fulfil your
prophecy.
### Stubs
Ok, now we know how to create basic prophecies and reveal dummies from them. That's
awesome if we don't care about our _doubles_ (objects that reflect originals)
interactions. If we do, we need to use *stubs* or *mocks*.
A stub is an object double, which doesn't have any expectations about the object behavior,
but when put in specific environment, behaves in specific way. Ok, I know, it's cryptic,
but bear with me for a minute. Simply put, a stub is a dummy, which depending on the called
method signature does different things (has logic). To create stubs in Prophecy:
```php
$prophecy->read('123')->willReturn('value');
```
Oh wow. We've just made an arbitrary call on the object prophecy? Yes, we did. And this
call returned us a new object instance of class `MethodProphecy`. Yep, that's a specific
method with arguments prophecy. Method prophecies give you the ability to create method
promises or predictions. We'll talk about method predictions later in the _Mocks_ section.
#### Promises
Promises are logical blocks, that represent your fictional methods in prophecy terms
and they are handled by the `MethodProphecy::will(PromiseInterface $promise)` method.
As a matter of fact, the call that we made earlier (`willReturn('value')`) is a simple
shortcut to:
```php
$prophecy->read('123')->will(new Prophecy\Promise\ReturnPromise(array('value')));
```
This promise will cause any call to our double's `read()` method with exactly one
argument - `'123'` to always return `'value'`. But that's only for this
promise, there's plenty others you can use:
- `ReturnPromise` or `->willReturn(1)` - returns a value from a method call
- `ReturnArgumentPromise` or `->willReturnArgument($index)` - returns the nth method argument from call
- `ThrowPromise` or `->willThrow` - causes the method to throw specific exception
- `CallbackPromise` or `->will($callback)` - gives you a quick way to define your own custom logic
Keep in mind, that you can always add even more promises by implementing
`Prophecy\Promise\PromiseInterface`.
#### Method prophecies idempotency
Prophecy enforces same method prophecies and, as a consequence, same promises and
predictions for the same method calls with the same arguments. This means:
```php
$methodProphecy1 = $prophecy->read('123');
$methodProphecy2 = $prophecy->read('123');
$methodProphecy3 = $prophecy->read('321');
$methodProphecy1 === $methodProphecy2;
$methodProphecy1 !== $methodProphecy3;
```
That's interesting, right? Now you might ask me how would you define more complex
behaviors where some method call changes behavior of others. In PHPUnit or Mockery
you do that by predicting how many times your method will be called. In Prophecy,
you'll use promises for that:
```php
$user->getName()->willReturn(null);
// For PHP 5.4
$user->setName('everzet')->will(function () {
$this->getName()->willReturn('everzet');
});
// For PHP 5.3
$user->setName('everzet')->will(function ($args, $user) {
$user->getName()->willReturn('everzet');
});
// Or
$user->setName('everzet')->will(function ($args) use ($user) {
$user->getName()->willReturn('everzet');
});
```
And now it doesn't matter how many times or in which order your methods are called.
What matters is their behaviors and how well you faked it.
#### Arguments wildcarding
The previous example is awesome (at least I hope it is for you), but that's not
optimal enough. We hardcoded `'everzet'` in our expectation. Isn't there a better
way? In fact there is, but it involves understanding what this `'everzet'`
actually is.
You see, even if method arguments used during method prophecy creation look
like simple method arguments, in reality they are not. They are argument token
wildcards. As a matter of fact, `->setName('everzet')` looks like a simple call just
because Prophecy automatically transforms it under the hood into:
```php
$user->setName(new Prophecy\Argument\Token\Exac
没有合适的资源?快使用搜索试试~ 我知道了~
最新精品游戏电竞网站源码
共2009个文件
php:496个
png:290个
gif:187个
0 下载量 130 浏览量
2023-10-31
09:36:49
上传
评论 1
收藏 119.4MB ZIP 举报
温馨提示
该平台的测试环境为php7.2+mysql5.6,需要修改数据库配置文件 applicationdatabase.php,替换为自己的域名103.252.19.226,并修改数据库sql文件中的信息。将public设置为运行目录,并设置thinkphp伪静态,后台账号为admin,密码为admin888。 此外,平台还有后台通用模板常用说明,如layui数据表格、阿里云单一模板短信、腾讯短信、小程序授权和公众号授权等。接口部署说明为:环境nginx+php7.0以上,将项目解压后部署,运行目录为解压后的public下,将对应的数据库yx_codyapp_cn.sql导入数据库,并修改config文件夹下的database.php中的信息。 前端部署为纯静态部署,只需将接口地址替换为自己部署的接口地址即可。接口地址更换为kumiaostaticinit.js
资源推荐
资源详情
资源评论
收起资源包目录
最新精品游戏电竞网站源码 (2009个子文件)
weuix.css 210KB
bootstrap.min.css 115KB
bootstrap.min.css 115KB
bootstrap.min.css 112KB
layui.css 73KB
weui.css 58KB
ueditor.css 43KB
font-awesome.css 37KB
ueditor.min.css 34KB
font-awesome.min.css 30KB
video-js.css 21KB
bootstrap-theme.min.css 20KB
image.css 18KB
swiper.min.css 15KB
video.css 15KB
layer.css 14KB
attachment.css 14KB
video-js.min.css 11KB
layui.mobile.css 10KB
nv.d3.min.css 9KB
nv.d3.min.css 9KB
shCoreEclipse.css 8KB
shCoreDjango.css 7KB
shCoreFadeToGrey.css 7KB
shCoreDefault.css 7KB
shCoreMidnight.css 7KB
shCoreRDark.css 7KB
laydate.css 7KB
shCoreEmacs.css 7KB
shCoreMDUltra.css 7KB
shCoreDefault.css 7KB
sami.css 7KB
ztree.css 6KB
app.css 6KB
ztree.css 5KB
shCore.css 5KB
ztree.css 5KB
scrawl.css 4KB
demo.css 3KB
pink.css 3KB
codemirror.css 3KB
green.css 3KB
blue.1.css 3KB
orange.css 3KB
red.css 3KB
charts.css 3KB
shThemeEclipse.css 3KB
background.css 2KB
shThemeDjango.css 2KB
shThemeFadeToGrey.css 2KB
shThemeVisualStudio.css 2KB
shThemeDefault.css 2KB
shThemeMidnight.css 2KB
shThemeRDark.css 2KB
shThemeEmacs.css 2KB
shThemeMDUltra.css 2KB
component.css 2KB
style.css 2KB
style.css 2KB
normalize.css 2KB
shThemeAppleScript.css 2KB
emotion.css 2KB
dialogbase.css 2KB
music.css 2KB
light.css 2KB
blue.css 2KB
login.css 1KB
choumei.min.css 1KB
nprogress.css 1KB
edittable.css 1KB
treeselect.css 1KB
code.css 1KB
template.css 1KB
message.css 964B
index.css 878B
auth.css 727B
center.css 688B
main.css 621B
webuploader.css 515B
push.css 467B
footer.css 421B
doc.css 405B
help.css 389B
my.css 365B
game_account.css 254B
lead.css 235B
default.css 168B
admin.css 93B
iframe.css 41B
index.css 38B
dashboard.html.dist 7KB
file.html.dist 3KB
TestCaseMethod.tpl.dist 3KB
directory.html.dist 2KB
file_item.html.dist 871B
directory_item.html.dist 821B
phpunit.xml.dist 681B
method_item.html.dist 632B
coverage_bar.html.dist 305B
.DS_Store 10KB
共 2009 条
- 1
- 2
- 3
- 4
- 5
- 6
- 21
资源评论
行动之上
- 粉丝: 2192
- 资源: 928
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功