# 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
没有合适的资源?快使用搜索试试~ 我知道了~
PHP农产品防伪追溯系统源码/产品防伪追溯源码
共5609个文件
svg:3053个
php:1034个
js:252个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
5星 · 超过95%的资源 3 下载量 19 浏览量
2022-03-27
20:12:22
上传
评论 2
收藏 30.58MB ZIP 举报
温馨提示
php农产品防伪追溯系统源码,该系统采用最简单易用的php+MySQL进行搭建,拥有完善的网站前后台,通过对每件产品生产线上的单品、二级包装、等各级包装赋予唯一的监管条码,在生产线上将此数据和经销商信息一起上传至服务器。 1、基本资料管理:用于出货时选择参数,必须先设定、客户资料:添加、修改、删除、管理代理商资料;产品资料:添加、修改、删除、管理产品资料。条码对应关系导入套标数据,建立标签关系。 2、产品管理:出货扫描:设定产品出货地区、出货代理商、产品名称等信息扫描出货。支持批量导入条码数据出货。支持无线扫描枪、手持终端、采集器等设备出货。导入出货资料:可以利用采集器或手持终端扫描数据后批量导入。退货扫描:扫描产品直接退货,未出货产品不能出货。支持批量导入条码数据退货。支持无线扫描枪、手持终端、采集器等设备退货。导入退货资料:可以利用采集器或手持终端扫描数据后批量导入。 3、报表与查询:出货记录明细:可按时间、代理商、条码、批号等查询出货情况。出货统计:可按时间、地区、代理商等查询出货数量。退货记录明细:可按时间、代理商、条码、批号等查询退货情况。退货统计 4、系统管理
资源推荐
资源详情
资源评论
收起资源包目录
PHP农产品防伪追溯系统源码/产品防伪追溯源码 (5609个子文件)
ui.css 327KB
ui.css 326KB
bootstrap.css 123KB
public.css 122KB
public.css 120KB
public1.css 120KB
public1.css 120KB
components.css 116KB
bootstrap.min.css 115KB
bootstrap.min.css 106KB
bootstrap.min.css 106KB
bootstrap.min.css 106KB
bootstrap.min.css 101KB
style.css 88KB
components.min.css 85KB
material-design-iconic-font.css 83KB
material-design-iconic-font.min.css 69KB
flag-icon.css 37KB
font-awesome.css 37KB
ie7.css 35KB
flag-icon.min.css 33KB
font-awesome.min.css 32KB
style.css 28KB
style.css 28KB
style.css 27KB
wap.css 24KB
wap.css 24KB
wap.css 23KB
default.css 22KB
bootstrap-theme.css 19KB
bootstrap-theme.min.css 17KB
swiper.min.css 17KB
swiper.min.css 17KB
swiper.min.css 17KB
themify-icons.css 16KB
home.css 15KB
home.css 15KB
layer.css 14KB
layer.css 14KB
layer.css 14KB
layer.css 14KB
user_register_v2.css 12KB
user_forgot_password_v2.css 12KB
user_register_v1.css 12KB
user_forgot_password_v1.css 12KB
left_icon_menu_layout.css 10KB
left_menu_layout.css 10KB
pc.css 10KB
user_lockscreen_v2.css 9KB
nv.d3.min.css 9KB
user_lockscreen_v1.css 9KB
style-responsive.css 9KB
user_login_v1.css 9KB
user_register_v2.min.css 9KB
user_login_v2.css 8KB
user_forgot_password_v2.min.css 8KB
gallery.css 8KB
user_register_v1.min.css 8KB
user_forgot_password_v1.min.css 8KB
pc.css 8KB
style.css 8KB
left_icon_menu_layout.min.css 8KB
left_menu_layout.min.css 8KB
user_lockscreen_v2.min.css 7KB
maintenance.css 7KB
user_lockscreen_v1.min.css 7KB
bootstrap-reset.css 7KB
css.css 6KB
user_login_v2.min.css 6KB
user_login_v1.min.css 6KB
laydate.css 6KB
core.css 6KB
core.css 6KB
style.css 5KB
layer.css 5KB
layer.css 5KB
layer.css 5KB
layer.css 5KB
layer.css 5KB
maintenance.min.css 5KB
qqlist.css 5KB
style.css 4KB
laydate.css 3KB
laydate.css 3KB
laydate.css 3KB
laydate.css 3KB
laydate.css 3KB
laydate.css 3KB
qq.css 3KB
table-responsive.css 3KB
common.css 3KB
wap.css 3KB
fw.css 3KB
fwcss.css 3KB
fw.css 3KB
fw.css 3KB
fwcss.css 3KB
jquery.searchableSelect.css 2KB
user_error.css 2KB
simple.css 2KB
共 5609 条
- 1
- 2
- 3
- 4
- 5
- 6
- 57
资源评论
- weixin_417396832022-06-07用户下载后在一定时间内未进行评价,系统默认好评。
- m0_748440552023-04-24资源有一定的参考价值,与资源描述一致,很实用,能够借鉴的部分挺多的,值得下载。
- m0_598177542024-10-30资源很好用,有较大的参考价值,资源不错,支持一下。
办公模板库素材蛙
- 粉丝: 1678
- 资源: 2301
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- qimo_text.zip
- 3CDaemon-FTP、syslog、TFTP服务器模拟程序
- 2024年企业级聊天机器人应用与优化指南
- 新能源汽车行业2025年度策略:行业触底回升,新技术加速落地.pdf
- 中国银河-钢铁行业深度报告:供需格局改善,行业产能优化强者更强.pdf
- 电力设备及新能源行业2025年年度投资策略:行业触底,复苏在即.pdf
- OTA行业深度报告:春暖花开,奔赴山海.pdf
- AI深度洞察系列报告(三):Scale up与Scaleout组网变化趋势如何看?.pdf
- 玛莎拉蒂年会活动方案.pdf
- 提升企业开源开发有效性和影响力的路线图 .pdf
- 推动应用创新的九大 AI 趋势.pdf
- 欧洲的开源成熟度:2024年的里程碑、机遇与路径研究报告(英文版).pdf
- 2024年量子技术研究报告:投资于拐点(英文版).pdf
- 2024年地中海南部和东部(SEMED)新就业形态与平台工作研究报告(英文版).pdf
- 2024年环境经济核算体系-生态系统核算报告(英文版).pdf
- 2024年东南亚的可持续航空燃料基于生物的解决办法的区域视角报告(英文版).pdf
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功