mpndev / d8tdd
帮助进行 Drupal 8 TDD...
Requires
- php: >=7.2.0
- fzaninotto/faker: ^1.9@dev
README
这是一项非常具有挑战性的任务!对于每个测试,您都需要编写大量的准备代码。
以下是一些有用的工具!
通过 composer 安装
composer require mpndev/t8tdd
让我们从您的模块的内核测试开始。
首先,您需要创建一个继承自 KernelTestBase 类的抽象类。KernelTestBase 类位于这个库中。
- 从终端,转到您的 Drupal 安装根目录;
- 假设您有一个名为 "my_module" 的模块。执行以下命令
使用您模块的 "snake_case" 名称
php ./vendor/mpndev/d8tdd/src/generate.php make:kerneltest my_module或您模块的 "PascalCase" 名称
php ./vendor/mpndev/d8tdd/src/generate.php make:kerneltest MyModule这将为您生成抽象类。
- 每个要创建内核测试的测试类都必须继承自 "MyModuleKernelTestBase";
- 这将为您提供 "factory" 和 "jsonRequest" 功能的权力
$this->factory(Node::class)->make(); $this->jsonRequest('https:///some/endpoint') ->using('POST') ->send();
(下面我们有如何使用这些工具的示例。)
工厂
假设我们需要从 "project" 包中获取节点实例。首先,我们需要为默认的 "project" 实现定义一个工厂。
打开 MyModuleKernelTestBase.php 并在 setUp 方法中添加以下内容
$this->factory(Node::class)->define('project', [ 'type' => 'project', 'title' => 'Some Project Title' ]);
在这里您可以访问 "Faker" 库。 点击这里 查看可用的方法列表。
$this->factory(Node::class)->define('project', [ 'type' => 'project', 'title' => $this->faker->name, ]);
如果您需要在测试中创建 "project" 实例,可以这样做
$project = $this->factory(Node::class)->make('project'); $project->bundle(); // will return 'project' $project->get('title')->getString(); // will return 'Some Project Title'
$project 将是 "project" 包的全新内容类型
您可以在运行时覆盖并添加字段
$project = $this->factory(Node::class)->make('project', [ 'title' => 'Some Another Title' 'field_something' => 'Something' ]); $project->bundle(); // will return 'project' $project->get('title')->getString(); // will return 'Some Another Title' $project->get('field_something')->getString(); // will return 'Something'
这将覆盖 "project" 包的默认 "factory" 行为
您也可以这样创建多个 "project" 实例
$projects = $this->factory(Node::class, 15)->make('project');
这将给出一个包含 15 个 "project" 的数组
如果您使用 "create" 而不是 "make",工厂将把 "project" 保存到数据库中
$this->factory(Node::class)->create('project');
您可以使用 "make" 创建 "project",稍后保存它(正常的 Drupal 事务)
$project = $this->factory(Node::class)->make('project'); //Do something... $project->save()
您可以通过在 "make" 或 "create" 方法的第三个参数中使用闭包来访问 "project" 实例。通过这样做,您可以修改 "project" 实例,例如通过引用字段添加一些内容
/** * make */ $project = $this->factory(Node::class)->make('project', [], function($project_instance){ $project_instance->get('field_environments')->appendItem($this->factory(Paragraph::class)->create('environment')); return $project_instance; }); /** * create */ $project = $this->factory(Node::class)->create('project', [], function($project_instance){ $project_instance->get('field_environments')->appendItem($this->factory(Paragraph::class)->create('environment')); return $project_instance; });
重要!!!
- 在闭包中,总是返回传入的实例变量!(在我们的例子中 $project_instance);
- appendItem() 方法将期望已保存的实例在数据库中,以便附加项目!(在我们的例子中 "environment",这是通过工厂在运行时创建的,再次使用工厂);
HttpRequest
助手,提供测试端点的请求。
请求不会离开。相反,它将由 Drupal 内核处理,可以检查响应。不错吧?
$response = $this->httpRequest('https:///some/endpoint') ->using('GET') ->send();
$response = $this->httpRequest('https:///some/endpoint') ->using('POST') ->send();
$response = $this->httpRequest('https:///some/endpoint') ->using('PUT') ->send();
$response = $this->httpRequest('https:///some/endpoint') ->using('PATCH') ->send();
$response = $this->httpRequest('https:///some/endpoint') ->using('DELETE') ->send();
附加 cookie
$response = $this->httpRequest('https:///some/endpoint') ->using('POST') ->withCookie($some_cookie) ->send();
附加内容
$response = $this->httpRequest('https:///some/endpoint') ->using('POST') ->withContent($some_content) ->send();
附加服务器
$response = $this->httpRequest('https:///some/endpoint') ->using('POST') ->withServer($some_server) ->send();
附加文件
$response = $this->httpRequest('https:///some/endpoint') ->using('POST') ->withFiles($some_files) ->send();
JsonRequest
助手,提供用于测试端点的 JSON 请求(带有 application/json 标头)。
请求不会离开。相反,它将由 Drupal 内核处理,可以检查响应。和 HttpRequest 助手一样。
$response = $this->jsonRequest('https:///some/endpoint') ->using('GET') ->send();
$response = $this->jsonRequest('https:///some/endpoint') ->using('POST') ->send();
$response = $this->jsonRequest('https:///some/endpoint') ->using('PUT') ->send();
$response = $this->jsonRequest('https:///some/endpoint') ->using('PATCH') ->send();
$response = $this->jsonRequest('https:///some/endpoint') ->using('DELETE') ->send();
附加 cookie
$response = $this->jsonRequest('https:///some/endpoint') ->using('POST') ->withCookie($some_cookie) ->send();
附加内容
$response = $this->jsonRequest('https:///some/endpoint') ->using('POST') ->withContent($some_content) ->send();
附加服务器
$response = $this->jsonRequest('https:///some/endpoint') ->using('POST') ->withServer($some_server) ->send();
附加文件
$response = $this->jsonRequest('https:///some/endpoint') ->using('POST') ->withFiles($some_files) ->send();