codeception/stub
Composer 安装命令:
composer require codeception/stub
包简介
Flexible Stub wrapper for PHPUnit's Mock Builder
README 文档
README
Library on top of PHPUnit's mock builder providing a highly simplified syntax:
Reference
- Stub - creating stub classes using static methods
- Stub Trait - creating stubs and mocks using trait
- Expected - defining expectations for mocks
Install
Enabled by default in Codeception. For PHPUnit install this package:
composer require codeception/stub --dev
Stubs
Stubs can be constructed with Codeception\Stub static calls:
<?php // create a stub with find method replaced $userRepository = Stub::make(UserRepository::class, ['find' => new User]); $userRepository->find(1); // => User // create a dummy $userRepository = Stub::makeEmpty(UserRepository::class); // create a stub with all methods replaced except one $user = Stub::makeEmptyExcept(User::class, 'validate'); $user->validate($data); // create a stub by calling constructor and replacing a method $user = Stub::construct(User::class, ['name' => 'davert'], ['save' => false]); // create a stub by calling constructor with empty methods $user = Stub::constructEmpty(User::class, ['name' => 'davert']); // create a stub by calling constructor with empty methods $user = Stub::constructEmptyExcept(User::class, 'getName', ['name' => 'davert']); $user->getName(); // => davert $user->setName('jane'); // => this method is empty $user->getName(); // => davert
Alternatively, stubs can be created by using Codeception\Test\Feature\Stub trait:
<?php $this->make(UserRepositry::class); $this->makeEmpty(UserRepositry::class); $this->construct(UserRepositry::class); $this->constructEmpty(UserRepositry::class); // ...
Mocks
Mocks should be created by including Codeception\Test\Feature\Stub trait into a test case.
Execution expectation are set with Codeception\Stub\Expected:
<?php // find should be never called $userRepository = $this->make(UserRepository::class, [ 'find' => Codeception\Stub\Expected::never() ]); // find should be called once and return a new user $userRepository = $this->make(UserRepository::class, [ 'find' => Codeception\Stub\Expected::once(new User) ]);
License
MIT
codeception/stub 适用场景与选型建议
codeception/stub 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 82.11M 次下载、GitHub Stars 达 301, 最近一次更新时间为 2018 年 01 月 10 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 codeception/stub 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 codeception/stub 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 82.11M
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 302
- 点击次数: 13
- 依赖项目数: 51
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2018-01-10