# 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($exception)` - 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.
Note: If the method is called several times, you can use the following syntax to return different
values for each call:
```php
$prophecy->read('123')->willReturn(1, 2, 3);
```
This feature is actually not recommended for most cases. Relying on the order of
calls for the same arguments tends to make test fragile, as adding one more call
can break everything.
#### 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'
没有合适的资源?快使用搜索试试~ 我知道了~
国际多语言出海商城返佣产品自动匹配订单源码.zip
共2044个文件
png:469个
xml:233个
html:222个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 116 浏览量
2024-05-08
10:22:38
上传
评论
收藏 34.57MB ZIP 举报
温馨提示
国际多语言出海商城返佣产品自动匹配订单源码出售搭建 8国多语言出海拼单商城 此网站是很多巴西客户定制的原型,已投放运营符合当地本地化 多语言商城返利返佣投资理财派单自带余额宝, 采取全新支付端口,后台语音提醒,客服中心改造豪华页面,赠送客服系统 后台釆取全新框架, 余额宝分为定数时间,一天,七天,一个月,一年,合约到就会产生收益 1.会员自动匹配订单,获得佣金收入。 2.添加了巴西葡语+英文+代理后台+修复已知BUG+优化系统内核+打开速度更快,运行更顺畅 具体功能以演示站为准。 3.好友扫描你的二维码注册完成即可成为你的下级、会员3级代理模式分享下级获得下级返佣收入 4.邀请会员注册奖励机制,邀请会员充值奖励机制,全部自动发放 5.三级分销,每个推广链接带独立客服,等级优先,充值提现优先完美后台查询功能 源码仅供爱好者学习交流研究使用,不得使用于非法用途,购买以后用作他用附带的一切法律责任后果都由购买者承担于本网站无任何关系!
资源推荐
资源详情
资源评论
收起资源包目录
国际多语言出海商城返佣产品自动匹配订单源码.zip (2044个子文件)
83ZkmNoxT0KzqfwjarcPcTFc_S4N8tjwCVjQvOE5r30 87B
cityjson 84B
app.3227f3b635185d55fe635aae11c7880e.css 725KB
app.7b22fa66c2af28f12bf32977d4b82694.css 725KB
mobiscroll.custom-3.0.0-beta6.min.css 237KB
bootstrap.css 195KB
bootstrap.min.css 158KB
bootstrap.min.css 156KB
light7.css 129KB
style.css 123KB
bootstrap.min.css 118KB
layui.css 71KB
bootstrap-grid.css 66KB
bootstrap-grid.min.css 49KB
editor_ie8.css 49KB
editor_iequirks.css 48KB
editor_ie.css 48KB
font.css 48KB
editor_gecko.css 47KB
editor.css 47KB
hui.css 45KB
userstyle.css 36KB
fonts.css 30KB
index_style.css 30KB
console.css 25KB
intlTelInput.css 25KB
user.css 25KB
light7-swiper.css 22KB
share.css 20KB
intlTelInput.min.css 19KB
swiper.min.css 19KB
base.css 17KB
base.css 17KB
public.css 16KB
dialog_ie8.css 15KB
dialog_iequirks.css 14KB
dialog_ie.css 14KB
layer.css 14KB
swiper-bundle.min.css 14KB
dialog.css 13KB
swiper.min.css 13KB
layui.mobile.css 10KB
funding.css 9KB
funding.css 9KB
nv.d3.min.css 9KB
console.custom.css 9KB
deposit.css 8KB
my.css 8KB
laydate.css 7KB
register-login.css 7KB
reg.css 7KB
style2.css 7KB
index.css 7KB
unlock.css 6KB
mobile.css 6KB
zTreeStyle.css 6KB
team-reports.css 6KB
grab.css 6KB
michat.css 5KB
account.css 5KB
layer.css 5KB
deposit-record.css 5KB
login.css 5KB
bootstrap-reboot.css 5KB
index.css 5KB
account-details.css 4KB
console.layout.css 4KB
record.css 4KB
bootstrap-reboot.min.css 4KB
withdrawal.css 4KB
theme.css 4KB
mobileSelect.css 3KB
accesssub.css 3KB
console.form.css 3KB
lixibao.css 3KB
iconfont.css 3KB
access.css 3KB
contents.css 3KB
icon-font.css 3KB
phone.css 3KB
record.css 3KB
common.css 3KB
console.layui.css 2KB
login.css 2KB
recharge.css 2KB
rolldate.css 2KB
personal.css 2KB
style.css 2KB
style.css 2KB
index.css 2KB
templates.css 2KB
copyformatting.css 1KB
autocompleter.css 1KB
invitation.css 1KB
wsc.css 1KB
toolbar.css 1KB
wsc.css 1KB
order.css 1KB
notice.css 1KB
ui.css 1KB
共 2044 条
- 1
- 2
- 3
- 4
- 5
- 6
- 21
资源评论
智慧浩海
- 粉丝: 1w+
- 资源: 5175
下载权益
C知道特权
VIP文章
课程特权
开通VIP
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功