benconstable/phpspec-laravel
Composer 安装命令:
composer require --dev benconstable/phpspec-laravel
包简介
Test your Laravel applications with PhpSpec
README 文档
README
Versions
Depending on the version of Laravel and/or Phpspec you're using, you'll want to make sure that you're using the version of this package that's right for you. Use the table below to pick the right one.
| PHP Version | Package Version | Laravel Version | Phpspec Version |
|---|---|---|---|
>=5.3.0 |
^v1.2 |
^v4.1 |
^v2.0 |
>=5.4.0 |
^v2.0 |
v5.0-v5.3 |
^v2.1 |
>=5.6.0 |
^v3.0 |
^v5.1-v5.4 |
^v3.0 |
>=7.0.0 |
^v4.0 |
^v5.4 |
^v4.0 |
Installation
Install the package with composer:
composer require --dev "benconstable/phpspec-laravel:~4.0"
then add this to your phpspec.yml:
extensions: PhpSpec\Laravel\Extension\LaravelExtension: ~
You can take a look at example.phpspec.yml
for a good set of sensible phpspec defaults for a Laravel project.
Why this extension?
This extension provides you with a bootstrapped Laravel environment when writing your phpspec tests.
It allows you to make use of some of the nice features that Laravel provides, like class aliases and helper functions, without being hindered by your testing framework.
This extension is not a swap-in replacement for Laravel's built in PHPUnit setup. If you'd like integration and/or functional tests, please use that, Behat, or Codeception.
Configuration
Testing environment
By default, the extension bootstraps Laravel in the testing environment. You
can change this to production (or whatever you like) by setting:
extensions: PhpSpec\Laravel\Extension\LaravelExtension: testing_environment: "production"
in your phpspec.yml.
App bootstrap path
By default, the extension will bootstrap your app by looking for bootstrap/app.php
in the directory above vendor/. This is the default location that Laravel
provides.
You can manually specify the path to the bootstrap file if you're using a non-standard installation, like so:
extensions: PhpSpec\Laravel\Extension\LaravelExtension: framework_path: "/non/standard/laravel/setup/app.php"
You can specify either an absolute path (use leading slash), or a path relative
to the vendor/ directory.
Usage
Testing without Laravel
If you're not using any code specific to the Laravel environment, then you don't need to do anything differently. Just write your phpspec tests as normal!
Testing with Laravel
If you want to take advantage of Laravel's aliases, or use some of its
helper functions, extend your specs
from PhpSpec\Laravel\LaravelObjectBehavior. This will prevent errors when
testing.
For example, this class uses an alias:
<?php namespace App; use Inspiring; class MyInspiring extends Inspiring { public function quoteBackwards() { return strrev(parent::quote()); } }
and without extending from PhpSpec\Laravel\LaravelObjectBehavior:
<?php namespace spec\App; use PhpSpec\ObjectBehavior; class MyInspiringSpec extends ObjectBehavior { function it_inspires_backwards() { $this->quoteBackwards()->shouldBeString(); } }
you'll get Fatal error: Class 'Inspiring' not found.... But extending from PhpSpec\Laravel\LaravelObjectBehavior:
<?php namespace spec\App; use PhpSpec\Laravel\LaravelObjectBehavior; class MyInspiringSpec extends LaravelObjectBehavior { function it_inspires_backwards() { $this->quoteBackwards()->shouldBeString(); } }
you'll get ✔ inspires backwards.
and this class uses a helper function:
<?php namespace App; class MyEncryptor { public function encrypt($arg) { return bcrypt($arg); } }
and without extending from PhpSpec\Laravel\LaravelObjectBehavior:
<?php namespace spec\App; use PhpSpec\ObjectBehavior; class MyEncryptor extends ObjectBehavior { function it_encrypts_a_string() { $this->encrypt()->shouldBeString(); } }
you'll get Fatal error: Call to a member function make() on a non-object....
But extending from PhpSpec\Laravel\LaravelObjectBehavior:
<?php namespace spec\App; use PhpSpec\Laravel\LaravelObjectBehavior; class MyEncryptor extends LaravelObjectBehavior { function it_encrypts_a_string() { $this->encrypt()->shouldBeString(); } }
you'll get ✔ encrypts a string.
Accessing the IoC container
If you need to access the Service Container
in your specs, just use the app() helper!
Learning more about phpspec and Laravel
Laracasts has some great guides on phpspec and Laravel. 'Laravel, phpspec and refactoring' is a good starting point; it shows how you should use phpspec with Laravel, and covers the basics of writing tests (and it's free!).
Contributing
See CONTRIBUTING.md.
License
MIT © Ben Constable. See LICENSE for more info.
Thanks
Thanks to...
- @obrignoni for their great work in getting this extension working with Laravel 5
- @Sam-Burns for their great work in getting this extension working with Phpspec v3 and v4
- All of the other contributors and to everyone that's reported issues and bugs with the project
benconstable/phpspec-laravel 适用场景与选型建议
benconstable/phpspec-laravel 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 351.65k 次下载、GitHub Stars 达 144, 最近一次更新时间为 2014 年 03 月 10 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「testing」 「test」 「laravel」 「phpspec」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 benconstable/phpspec-laravel 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 benconstable/phpspec-laravel 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 benconstable/phpspec-laravel 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Testing Suite For Lumen like Laravel does.
The PHP SDK for Checkmango
Specification-oriented BDD helpers for PHPUnit
KissTest is command-line free, fast, simple and beautiful unit test framework.
PWWEB Artomator
Alfabank REST API integration
统计信息
- 总下载量: 351.65k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 145
- 点击次数: 9
- 依赖项目数: 17
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2014-03-10