zizaco / testcases-laravel
一些类,帮助编写laravel 4应用程序的测试
Requires
- php: >=5.3.0
- alexandresalome/php-selenium: 1.0.1
- illuminate/support: 4.*
Suggests
- GuilhermeGuitte/behat-laravel: A solution test your application using the BDD methodology
- mockery/mockery: Mockery is a simple yet flexible PHP mock object framework
This package is auto-updated.
Last update: 2024-08-29 04:08:55 UTC
README
一组类,旨在通过使用PHPUnit为构建在Laravel 4上的应用程序准备和编写测试。
功能
当前
- 模拟邮件发送以加快测试速度
- 更简单、更灵活的控制器和请求测试
- 针对控制器测试的特定断言
- 使用Selenium进行前端测试(带有selenium集成)
- 针对前端测试的特定断言
- 易于使用!
如果您在寻找BDD或Behat,请参阅behat-laravel
快速入门
所需设置
在composer.json
文件的require
键中添加以下内容
"zizaco/testcases-laravel": "dev-master"
运行Composer更新命令
$ composer update
测试模型和库
要为模型和其他类编写测试,只需扩展Zizaco\TestCases\TestCase
。例如
<?php
class StuffTest extends Zizaco\TestCases\TestCase
{
/**
* Clean collection between every test
*/
public function setUp()
{
foreach( Stuff::all() as $s ) { $s->delete(); }
parent::setUp(); // Don't forget this if you overwrite the setUp() method
}
/* Your tests here :) */
}
测试非模型类
<?php
use Mockery as m;
class StuffRepositoryTest extends Zizaco\TestCases\TestCase
{
public function testShouldCreateNew()
{
$repo = new StuffRepository;
/* Something and some assertion */
}
/* Other tests */
}
测试控制器
要为控制器编写测试,扩展Zizaco\TestCases\ControllerTestCase
。例如
<?php
use Mockery as m;
class StuffsControllerTest extends Zizaco\TestCases\ControllerTestCase
{
/**
* Create action should always return 200
*
*/
public function testShouldCreate(){
// Make request to action
$this->requestAction('GET', 'StuffsControllerTest@create');
// Asserts if the response code is 200
$this->assertRequestOk();
// $this->assertStatusCode( 200 ); // Is the same as assertRequestOk()
}
/**
* Store action should redirect to index if success
*
*/
public function testShouldStoreValidContent(){
// Mocks the StuffRepository to make sure that the storeNew
// method will be called at least once
$stuffRepo = m::mock(new StuffRepository);
$stuffRepo->shouldReceive('storeNew')->once()->passthru();
App::instance("StuffRepository", $stuffRepo);
$input = array('name'=>'Something nice', 'slug'=>'something_nice');
// Method chaining ;)
$this->withInput($input)->requestAction('POST', 'StuffsController@store');
// Asserts if the page was redirected to the correct place
$this->assertRedirection(URL::action('StuffsController@index'));
// Asserts if the session key "flash" has "success" word somewhere
$this->assertSessionHas('flash','success');
}
/* Other tests and stuff */
}
前端/集成测试
TestCases-Laravel使编写集成测试变得非常简单。首先,下载Selenium Server(以前称为Selenium RC Server)并将其放置在您主目录中的.selenium
目录中。例如:~/.selenium/selenium-server-standalone-7.7.7.jar
。IntegrationTestCase
将自动启动服务器,启动php构建在web服务器上并运行测试。
要编写集成测试,扩展Zizaco\TestCases\IntegrationTestCase
类。在文件开头使用use Selenium\Locator as l;
也是一个好主意,以便轻松使用定位器
<?php
use Selenium\Locator as l;
class ManageContentTest extends Zizaco\TestCases\IntegrationTestCase
{
public function testCreateAStuff()
{
$this->browser
->open(URL::action('StuffController@index')) // Visits the 'stuff' index
->click(l::id('btn-create-new-stuff')) // Click in the new button
->waitForPageToLoad(1000) // Wait for the page to load
->type(l::IdOrName('name'), 'Something nice') // Fill name
->type(l::IdOrName('slug'), 'something_nice') // Fill slug
->select(l::IdOrName('importance'), 'High') // Select a value in a selector/combobox
->click(l::css('.btn-primary')) // Click in the button
->waitForPageToLoad(1000); // Wait for page to load
// Asserts if at the end the user is at the stuff index again
$this->assertLocation( URL::action('StuffController@index') );
// Asserts if inside the element #stuff-index there is the text "Something nice"
// what means that the "stuff" has been created sucessfully :)
$this->assertElementHasText(l::id('stuff-index'), 'Something nice');
}
/* Other fancy tests */
}
详细用法
Zizaco\TestCases\TestCase
- 这是
app/tests
目录中默认TestCase的替代品 - 在
setUp()
方法中简单地加载应用程序。这允许您的应用程序为每个测试做好准备 - 用于测试模型、库、仓库和其他不是控制器的类。
Zizaco\TestCases\ControllerTestCase
- 建立在
Zizaco\TestCases\TestCase
之上,这个类旨在简化控制器测试的编写。 - 通过方法链,您将以简单易读的方式编写请求:
$this->withInput($someArray)->requestUrl($url)->assertRedirection($otherUrl);
- 包含一组自定义断言(
assertStatusCode
、assertRequestOk
、assertRedirection
、assertSessionHas
、assertBodyHasHtml
、assertBodyHasText
),这些断言可以帮助您评估控制器是否正常工作。
Zizaco\TestCases\IntegrationTestCase
- 建立在
Zizaco\TestCases\TestCase
之上,这个类旨在简化使用Selenium编写的测试。 - 检查Selenium服务器是否在端口4444上运行。如果没有,将尝试在您的主目录中的
.selenium
目录内找到并运行selenium .jar文件。 - 使用alexandresalome/PHP-Selenium与selenium交互。所以这些是您将能够使用的命令,这些是可能的定位器选项。
故障排除
PHP致命错误:在您的/app/directory/some/File.php的第20行找不到类'Something'
如果您在测试中覆盖了setUp()
方法,请确保调用parent::setUp()
public function setUp(){
parent::setUp() // Don't forget this
// Your stuff
}
错误:未找到Selenium。请运行Selenium服务器(端口4444)或将selenium .jar文件放置在'.selenium'目录下
这意味着Selenium服务器未找到。Zizaco\TestCases\IntegrationTestCase
将检查Selenium服务器是否在端口4444上运行。如果没有,将尝试在您的home路径下的.selenium
目录中查找并运行selenium .jar文件。
您可以从Selenium服务器(以前称为Selenium RC服务器)下载并将其放置在您的home目录下的.selenium
目录中,例如:~/.selenium/selenium-server-standalone-7.7.7.jar
以正确运行集成测试。
许可证
TestCases-Laravel是免费软件,根据MIT许可证条款分发
附加信息
有任何问题,请随时联系我或在这里提问
如有任何问题,请在此处报告