laracasts/behat-laravel-extension 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

laracasts/behat-laravel-extension

Composer 安装命令:

composer require laracasts/behat-laravel-extension

包简介

Laravel extension for Behat

README 文档

README

This extension offers an incredibly simple (and fast) way to begin testing and driving your Laravel applications with Behat. Some benefits include:

  • Fast: It doesn't depend on anything like Goutte, so it offers a super-fast way to test your UI. You don't even need to setup a host to run your tests.
  • Refresh: Laravel is automatically rebooted after each scenario (so nothing like user sessions will be persisted).
  • Environments: Specifying custom environment files (like the .env one) for different app environments is a little tricky in Laravel 5. This extension handles that for you automatically. By default, it'll look for a .env.behat file in your project root.
  • Access Laravel: You instantly have access to Laravel (things like facades and such) from your FeatureContext file.
  • Workflow: A number of useful traits are available, which will speed up your workflow.

To get started, you only need to follow a few steps:

Prefer a video walk-through? See this lesson from Laracasts.

1. Install Dependencies

As always, we need to pull in some dependencies through Composer.

composer require behat/behat behat/mink behat/mink-extension laracasts/behat-laravel-extension --dev

This will give us access to Behat, Mink, and, of course, the Laravel extension.

2. Create the Behat.yml Configuration File

Next, within your project root, create a behat.yml file, and add:

default:
    extensions:
        Laracasts\Behat:
            # env_path: .env.behat
        Behat\MinkExtension:
            default_session: laravel
            laravel: ~

Here, is where we reference the Laravel extension, and tell Behat to use it as our default session. You may pass an optional parameter, env_path (currently commented out above) to specify the name of the environment file that should be referenced from your tests. By default, it'll look for a .env.behat file.

This file should, like the standard .env file in your project root, contain any special environment variables for your tests (such as a special acceptance test-specific database).

3. Setting up FeatureContext

Run, from the root of your app

vendor/bin/behat --init 

It should set

features/bootstrap/FeatureContext.php

At this point you should set it to extend MinkContext.


<?php

use Behat\Behat\Hook\Scope\AfterStepScope;
use Behat\Behat\Tester\Exception\PendingException;
use Behat\Behat\Context\Context;
use Behat\Behat\Context\SnippetAcceptingContext;
use Behat\Gherkin\Node\PyStringNode;
use Behat\Gherkin\Node\TableNode;
#This will be needed if you require "behat/mink-selenium2-driver"
#use Behat\Mink\Driver\Selenium2Driver;
use Behat\MinkExtension\Context\MinkContext;

/**
 * Defines application features from the specific context.
 */
class FeatureContext extends MinkContext implements Context, SnippetAcceptingContext
{

4. Write Some Features

example

You're all set to go! Start writing some features. If you want a quick dummy example to get you started, refer to this project.

Note: if you want to leverage some of the Mink helpers in your FeatureContext file, then be sure to extend Behat\MinkExtension\Context\MinkContext.

Feature Context Traits

As a convenience, this package also includes a number of traits to streamline common tasks, such as migrating your database, or using database transactions, or even testing mail.

Migrator

Often, you'll find yourself in situations where you want to migrate your test database before a scenario. Easy! Just pull in the Laracasts\Behat\Context\Migrator trait into your FeatureContext, like so:

// ...

use Laracasts\Behat\Context\Migrator;

class FeatureContext extends MinkContext implements Context, SnippetAcceptingContext
{
    use Migrator;

}

That's it! The trait will do the rest. Before each scenario runs, if your database needs to be migrated, it will be!

Database Transactions

On the other hand, you might prefer to run all of your tests through database transactions. You'll get a nice speed boost out of the deal, as your data will never actually be saved to the database. To take advantage of this, once again, pull in the Laracasts\Behat\Context\DatabaseTransactions trait, like so:

// ...

use Laracasts\Behat\Context\DatabaseTransactions;

class FeatureContext extends MinkContext implements Context, SnippetAcceptingContext
{
    use DatabaseTransactions;

}

Once you pull in this trait, before each scenario runs, it'll begin a new transaction. And when the scenario completes, we'll roll it back for you.

Service: MailTrap

Especially when functional testing, it can be beneficial to test your mail against a real test server (rather than mocking it out, and hoping that things were formatted correctly). If you're a fan of MailTrap (highly recommended), this extension can help you!

If you haven't already, create a quick account (free) and inbox at MailTrap.io. Next, update either your config/mail.php to use the settings that MailTrap provides you, or modify your .env.behat variables to reference them. Once you've configured your app to use MailTrap, the only other thing we need is your MailTrap API key, and the default inbox id that we should use. Makes these available in config/services.php. Here's an example:

'mailtrap' => [
    'secret' => 'YOUR API KEY',
    'default_inbox' => 'ID OF THE MAILTRAP INBOX YOU CREATED'
]

That should do it! Now, just pull in the trait, and you're ready to go! Let me show you:

<?php

// ...

use Laracasts\Behat\Context\Services\MailTrap;
use PHPUnit_Framework_Assert as PHPUnit;

class DmcaContext extends MinkContext implements SnippetAcceptingContext
{
    use MailTrap;

    /**
     * @Then an email should be sent to YouTube
     */
    public function anEmailShouldBeSentToYoutube()
    {
        $lastEmail = $this->fetchInbox()[0];
        $stub = file_get_contents(__DIR__ . '/../stubs/dmca-complete.txt');

        PHPUnit::assertEquals('DMCA Notice', $lastEmail['subject']);
        PHPUnit::assertContains($stub, $lastEmail['text_body']);
    }

}

Notice that call to fetchInbox()? That will send an API request to MailTrap, which will return to you an array of all the messages/emails in your inbox. As such, if you want to write some assertions against the most recently received email in your MailTrap inbox, you can do:

$lastEmail = $this->fetchInbox()[0];

If working along, you can dump that variable to see all of the various fields that you may write assertions against. In the example above, we're ensuring that the subject was set correctly, and the body of the email matches a stub that we've created.

Even better, after each scenario completes, we'll go ahead and empty out your MailTrap inbox for convenience by adding the @mail tag to the scenario or right above the Feature

  @mail
  Scenario: User will get notified
    Given there are jobs that have been started by this user
    Then when the report is done the user will get an email to let them know it is done

FAQ

I'm getting a "PHP Fatal error: Maximum function nesting level of '100' reached, aborting!" error.

Sounds like you're using Xdebug. Increase the max nesting level.

laracasts/behat-laravel-extension 适用场景与选型建议

laracasts/behat-laravel-extension 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.28M 次下载、GitHub Stars 达 261, 最近一次更新时间为 2015 年 01 月 10 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「BDD」 「Behat」 「extension」 「laravel」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 laracasts/behat-laravel-extension 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 laracasts/behat-laravel-extension 我们能提供哪些服务?
定制开发 / 二次开发

基于 laracasts/behat-laravel-extension 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 1.28M
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 262
  • 点击次数: 24
  • 依赖项目数: 12
  • 推荐数: 0

GitHub 信息

  • Stars: 261
  • Watchers: 7
  • Forks: 72
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-01-10