viraj/cakephp-integrated

3.0.3 2020-11-30 08:40 UTC

This package is auto-updated.

Last update: 2024-08-27 02:50:29 UTC


README

使用 CakePHP 进行更优的集成测试。为 CakePHP 应用程序的集成测试提供直观的 API。

步骤 1:安装 & 设置

使用 Composer 安装此包

composer require viraj/cakephp-integrated --dev

您还需要为应用程序设置一个 baseUrl。默认情况下,它设置为 "https://",但您可能需要更改它。通过在测试类中添加一个 $baseUrl 来实现。

protected $baseUrl = 'http://your-dev-url';

此包包含 TestDummy 包。建议使用工厂和 DatabaseMigrations 特性代替 fixtures 以获得此包的最佳生产力。您可以在 TestDummy 包的官方文档中了解更多信息。

步骤 2:扩展基本类

在 CakePHP 3.4.1 之后

class DemoTest extends CakeTestCase
{
}

在 CakePHP 3.4.1 之前

class DemoTest extends LegacyTestCase
{
}

步骤 3:编写测试 ;)

这两个类的 API 将相同。我们将使用 CakeTestCase 作为示例。以下是一个示例测试,以帮助您了解其工作原理

class DemoTest extends CakeTestCase
{
    use DatabaseMigrations;
    
    protected $baseUrl = "http://local.yourapp.dev";

    /** @test */
    public function unauthenticated_user_cannot_see_the_add_posts_page()
    {
        $this->openPage('/posts/add')
             ->canSeePageUrlContains('/users/login');
    }

    /** @test */
    public function authenticated_user_can_add_a_new_post()
    {
        $user = factory('Users')->create();

        $this->actingAs($user)
             ->openPage('/posts/add')
             ->fillInField('title', 'My first post')
             ->fillInField('author', 'Viraj Khatavkar')
             ->fillInField('body', 'My Post body')
             ->check('published')
             ->press('Submit')
             ->canSeePageIs('/posts')
             ->seeText('My first post');
    }
}

API

以下是此包的 API,可用于编写测试

$this->fillInField($elementName, $text)

在以元素名称命名的输入字段中填充文本

$this->fillInField('name', 'Viraj Khatavkar');

$this->check($elementName)

检查以名称命名的复选框

$this->check('agree_to_terms');

$this->uncheck($elementName)

取消选中以名称命名的复选框

$this->uncheck('agree_to_terms');

$this->select($elementName, $option)

选择以名称命名的单选按钮或下拉字段中的选项

//Dropdown
$this->select('state', 'Pennsylvania');

//Radio
$this->select('gender', 'M');

$this->press($buttonText)

按下具有提供名称或文本的按钮。

//Text of the button
$this->press('Submit');


//Name of the button
$this->press('submit');

$this->canSeePageIs($url)

断言页面 URI 与给定的 URL 匹配。

$this->canSeePageIs('/posts');

$this->canSeePageUrlContains($url)

断言页面 URI 包含给定的 URL。

$this->canSeePageUrlContains('/po');

$this->actingAs($user)

设置应用程序的当前登录用户。

$user = factory('Users')->create();

$this->actingAs($user)->openPage('/posts/add');

寻找全面指南以实现 TDD 吗?

我正在写一本关于在现实世界的 CakePHP 应用程序中实现 TDD 的书!如果您在编写实际应用程序的测试方面遇到困难,请查看