lostkobrakai / pw-test-helper
ProcessWire 测试的辅助类/函数
0.0.7
2016-12-22 09:23 UTC
Requires
- behat/mink: ^1.7
- behat/mink-zombie-driver: dev-master@dev
This package is not auto-updated.
Last update: 2024-09-14 19:53:12 UTC
README
提供辅助类以启用 ProcessWire 的自动测试数据库设置和浏览器测试。核心类应由任何测试框架使用,但当前唯一的实际实现是使用 kahlan/kahlan
。
要安装,请在终端中运行以下命令
composer require --dev lostkobrakai/pw-test-helper
Kahlan
通过 composer 安装 Kahlan
composer require --dev kahlan/kahlan
创建或更新你的 kahlan-config.php
<?php use LostKobrakai\TestHelpers\Kahlan\SetupInclude; // Create a unique db name for the testruns $dbName = 'pw_' . md5(__DIR__ . time()); // Path to the ProcessWire root folder $path = __DIR__; // Browsertest settings $browserSettings = [ // Send db name as request header 'database' => $dbName, // Allow for relative urls in tests 'rootUrl' => 'http://db_tests.valet/' ]; // Add DB integration /** @noinspection PhpIncludeInspection */ include SetupInclude::db(); // Add Browsertest integration /** @noinspection PhpIncludeInspection */ include SetupInclude::browser();
数据库部分设置测试数据库,并在 kahlan 中包含一个启动的 ProcessWire 实例。在测试中可以像这样访问
it('should find the processwire homepage', function() { expect($this->processwire->pages->get('/')->id)->toBe(1); });
浏览器测试是通过使用 Behat/Mink
实现的,因此只有一些启动功能的辅助函数。否则 Mink 的 API 已在 Mink 包中进行文档记录。当前驱动程序是 zombie
。这是一个基于节点的无头驱动程序,运行速度相当快。它需要 npm i zombie --save-dev
来安装。
it('should be able to load the page for inspection', function() { $this->browser->visit('/'); // Check the page for specific text or elements expect($this->browser->element('h1')->getText())->toBe('Home'); // Kahlan does have support for async expectations // It waits until an optional timeout to see the text waitsFor(function() { return $this->browser->page(); })->toContain('Home'); });
当使用 browsertesting 时,默认情况下请求不知道有关临时测试数据库的任何信息。因此,以下需要在您的 config.php 中实际数据库配置下方。这样,请求就可以让 ProcessWire 使用测试数据库而不是您的正常数据库。
/** * Change db for browser testings */ if(isset($_SERVER['HTTP_X_TEST_WITH_DB']) && $_SERVER['HTTP_X_TEST_WITH_DB']){ $config->dbName = $_SERVER['HTTP_X_TEST_WITH_DB']; }
当使用 InnoDB 时,除了已经的临时测试数据库外,还可以使用数据库事务来避免测试中的副作用。以下数据库更改在每次测试后都被撤销。
describe('…', function() { beforeEach(function() { $this->processwire->database->beginTransaction(); }); afterEach(function() { $this->processwire->database->rollBack(); }); […] });