# 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\ExactValueToken('everzet'));
```
Those argument tokens are simple PHP classes, that implement
`Prophecy\Argument\Token\TokenInterface` and tell Prophecy how to compare real arguments
with your expectations. And yes, those cl
没有合适的资源?快使用搜索试试~ 我知道了~
众人帮任务帮威客兼职双端网页端 悬赏任务源码 蚂蚁聊天三级分销
共2000个文件
php:4172个
png:1150个
gif:997个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 180 浏览量
2022-04-09
09:19:35
上传
评论
收藏 40.23MB ZIP 举报
温馨提示
功能介绍 1、支持用户发布任务、置顶任务、发布任务红包、浏览任务、下架任务、分享任务、管理任务、暂停任务、任务加量、结束任务等 2、接单排行榜奖励、邀请排行、新手任务、每日任务(后台设置相应奖励奖励) 3、后台可对任务进行审核、拒绝任务可以填写拒绝原因、冻结任务、下架任务 4、支持每日签到(后台设置普通会员及会员每个签名赠送的奖金)任务置顶(后台设置普通会员及高级会员置顶费用-按小时收费置顶) 5、后台添加任务分类,可设置分类图片、发布任务的最低金额、会员领取任务的等级。 6、支持轮播图、公告、任务搜索、按人气、佣金、时间搜索等。 7、支持在线支付,对接支付宝付款、微信支付、微信登录(需要在APP打开)短信对接阿里云短信(如需不要短信注册可联系免费客服更换) 8、用户可与雇主聊天、对任务或雇主进行举报、维权,平台管理员介入等 9、支持三级分销功能、邀请海报、链接邀请、二维码邀请、自动识别并绑定邀请码(比如我分享给您链接,打开之后系统自动识别自动填写邀请码,无需手动填写) 10、支持会员功能、在线客服功能、粉丝关注功能、帮助中心、设置头像、修改密码、修改昵称。
资源推荐
资源详情
资源评论
收起资源包目录
众人帮任务帮威客兼职双端网页端 悬赏任务源码 蚂蚁聊天三级分销 (2000个子文件)
aaQYvpmpXh8-XhGjRashaFJ8pHbF4n_Cr2G6t9wCVD0 87B
EveryHour.bat 177B
EveryMinute.bat 172B
merge.bat 22B
test.bmp 0B
CHANGELOG 1KB
framework7.md.min.css 381KB
bootstrap.css 151KB
bootstrap.css 149KB
geren.css 144KB
AdminLTE.css 132KB
bootstrap.min.css 118KB
bootstrap.min.css 115KB
bootstrap.min.css 115KB
AdminLTE.min.css 104KB
AdminLTE-without-plugins.css 92KB
master.css 89KB
sm.min.css 85KB
mui.min.css 74KB
AdminLTE-without-plugins.min.css 72KB
facaeincon.css 71KB
workinfo_3.css 63KB
aui.css 63KB
ionicons.css 57KB
app.css 56KB
jquery-weui.min.css 55KB
mzui.css 54KB
ionicons.min.css 50KB
weui.min.css 50KB
_all-skins.css 49KB
ueditor.css 45KB
_all-skins.min.css 41KB
style.css 40KB
font-awesome.css 39KB
mobiscroll.custom-2.5.2.min.css 36KB
next_style.css 34KB
ueditor.min.css 34KB
new_style.css 30KB
font-awesome.min.css 30KB
layui.css 27KB
bootstrap-theme.css 26KB
new_page.css 26KB
font-awesome.min.css 23KB
bootstrap-theme.min.css 23KB
plugin.css 22KB
video-js.css 22KB
swiper.min.css 19KB
swiper.min.css 19KB
image.css 19KB
login.css 18KB
common.css 17KB
AdminLTE-bootstrap-social.css 16KB
dialog.css 16KB
agile.layout.css 15KB
video.css 15KB
attachment.css 15KB
swiper.min.css 15KB
layer.css 14KB
mobiscroll.animation-2.6.2.css 13KB
mobiscroll.animation-2.5.2.css 13KB
AdminLTE-bootstrap-social.min.css 12KB
video-js.min.css 11KB
bootstrap-datetimepicker.min.css 11KB
style.css 10KB
mobiscroll.wp-2.5.2.css 10KB
nv.d3.min.css 9KB
nv.d3.min.css 9KB
mobiscroll.ios-2.6.2.css 9KB
add_newpage.css 9KB
mobiscroll.core-2.6.2.css 9KB
mobiscroll.core-2.5.2.css 9KB
mobiscroll.ios-2.5.1.css 8KB
shCoreDefault.css 7KB
tmpl.css 7KB
jquery.magnify.css 7KB
home.css 6KB
daterangepicker.css 5KB
mobiscroll.android-ics-2.5.2.css 5KB
workinfo_1.css 5KB
spop.min.css 5KB
base64face.css 5KB
font_57295_m7uk4d3kbri.css 5KB
gubase.css 5KB
skin-black-light.css 5KB
jackpot.css 5KB
skin-blue-light.css 5KB
workinfo_2.css 4KB
skin-purple-light.css 4KB
skin-yellow-light.css 4KB
user.css 4KB
skin-green-light.css 4KB
skin-red-light.css 4KB
flexslider.min.css 4KB
skin-black.css 4KB
public.css 4KB
jquery.magnify.min.css 4KB
member.css 4KB
skin-black-light.min.css 4KB
skin-blue-light.min.css 4KB
scrawl.css 4KB
共 2000 条
- 1
- 2
- 3
- 4
- 5
- 6
- 20
资源评论
智慧浩海
- 粉丝: 1w+
- 资源: 5113
下载权益
C知道特权
VIP文章
课程特权
开通VIP
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- ### 词向量的介绍、使用技巧和优缺点的文章
- 基于STM32F103CBT6单片机GC65+MP2625+CC1101 GPSTrack模块板硬件(原理图+PCB)工程文件
- ### 通道处理过程模拟概念、优缺点和使用技巧
- ### MyBatis动态SQL介绍说明、使用技巧和优缺点
- 上传下载仿163网盘无刷新文件上传 for Jsp-fileupload-jsp.rar
- VMware Workstation业界非常稳定且安全的桌面虚拟机软件-计算机上运行多个操作系统,支持Windows、DOS等
- 基于STM8L101F3P6单片机+LY2508A33P+CC1100遥控器硬件(原理图+PCB)工程文件.zip
- 上传下载WAP图铃下载系统-unimg.rar
- YTX-0.1.0-Win
- 上传下载ExtJS 2.2 开源网络硬盘系统-dogdisk.rar
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功