[英]Setting up package migrations with Behat 3 and Laravel
我正在将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...
*/
}
是的,文档和示例不是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个引导文件更容易。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.