【问题标题】:Setting up package migrations with Behat 3 and Laravel使用 Behat 3 和 Laravel 设置包迁移
【发布时间】:2014-10-12 20:09:19
【问题描述】:

我正在努力将 Behat 3 包含在一个基于 Laravel 的新 API 中,并且我正在使用 lucadegasperi/oauth2-server-laravel 包来处理身份验证。

一直在努力让 Behat 设置正常工作(现在是这样)。我不明白为什么,但似乎我需要在 beforeSuite 和 beforeFeature 挂钩中迁移包。似乎很愚蠢,因为我只需要在所有功能运行之前迁移一次..?

我只想在套件加载之前迁移一次,否则随着测试数量的增加,运行时间可能会变长。

我一直在编写 Behat 2.* 的示例以在版本 3 中工作。

我的 FeatureContext 类目前看起来像这样:

use Behat\Behat\Context\SnippetAcceptingContext;
use Behat\Gherkin\Node\PyStringNode;
use Behat\Gherkin\Node\TableNode;

/**
 * Behat context class.
 */
class FeatureContext implements SnippetAcceptingContext
{

    private $_restObject        = null;
    private $_restObjectType    = null;
    private $_restObjectMethod  = 'get';
    private $_client            = null;
    private $_response          = null;
    private $_requestUrl        = null;

    private $_baseUrl           = null;

    /**
     * Initializes context.
     *
     * Every scenario gets its own context object.
     * You can also pass arbitrary arguments to the context constructor through behat.yml.
     */
    public function __construct($baseUrl)
    {
        $this->_restObject  = new stdClass();
        $this->_client      = new GuzzleHttp\Client();
        $this->_baseUrl = $baseUrl;
    }

    /**
    * @static
    * @beforeSuite
    */
    public static function bootstrapLaravel()
    {
        $unitTesting = true;
        $testEnvironment = 'testing';
        // This assumes the FeatureContext.php class is within app/tests/features/bootstrap
        $app = require_once __DIR__.'/../../../../bootstrap/start.php';

        $app->boot();

        Mail::pretend(true);
    }

    /**
    * @static
    * @beforeSuite
    */
    public static function setUpDb()
    {
        Artisan::call('migrate');

        self::migratePackages();

        /* do seeding */
        $seeders = array(
            "OAuthTestSeeder",
            "RolesPermissionsSeeder"
        );

        foreach ($seeders as $seedClass)
        {
            Artisan::call("db:seed", array("--class" => $seedClass));
        }        
    }
    /**
    * @static
    * @beforeFeature
    */
    public static function prepDb()
    {
        Artisan::call('migrate:refresh');
        self::migratePackages();

        Artisan::call('db:seed');


    }

    public static function migratePackages() 
    {
        $packages = array(
            "lucadegasperi/oauth2-server-laravel",
        );        

        foreach ($packages as $packageName)
        {
            Artisan::call("migrate",
                array("--package" => $packageName, "--env" => "testing"));
            echo 'migrating package: '.$packageName;
        }   

    }
/* feature specs here... 
*/ 
}

【问题讨论】:

  • 如果我知道您在问什么,我会提供帮助 :) 您是否尝试在所有测试开始前只迁移一次数据库?
  • 谢谢@IanBytchek 我用更清晰的问题更新了问题!是的,回答你的问题。还想知道我是否以理智/有效的方式来做这件事,因为很难找到与 Behat 3 兼容的示例。
  • 你最后解决了吗?

标签: php laravel behat


【解决方案1】:

是的,文档和示例并不是 Behat 最强大的部分,但不要害怕。与其他一些测试软件不同,Behat 并没有为您提供在加载整个程序时运行引导程序的单一选项,尽管它提供了许多其他很酷的功能。你几乎是在正确的道路上。您需要做的就是有一个静态标志来指示引导程序是否完成了这项工作。就我而言,我通过添加存储状态值的 TestUtility 类和包含在每个上下文中的 SetUpTrait 来完成这项工作。

final class TestUtility extends AbstractUtility
{

    protected static $bootstrapped = false;

    public static function isBootstrapped()
    {
        return self::$bootstrapped;
    }

    public static function bootstrap()
    {
        if (self::isBootstrapped()) {
            return;
        }

        include_once(__DIR__ . './../bootstrap.php');
        self::$bootstrapped = true;
    }
}

trait SetUpTrait
{

    public static function setUpSuite(BeforeSuiteScope $scope)
    {
        TestUtility::bootstrap();
    }
}

class My extends RawMinkContext
{
    use SetUpTrait;
}

我设法将应用程序设置为对应用程序本身、PHPUnit 和 Behat 测试使用单个引导文件。它带有几行额外的代码,但所有内容都有一个条目,而且非常容易维护——绝对比拥有 3 个引导文件更容易。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-11-22
    • 2015-02-17
    • 2016-07-18
    • 2018-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多