Cakephp集成测试登录后如何post请求?
Cakephp integration test how I will post request after login?
我正在尝试在 cakephp IntegrationTest 中发出 post 请求,其中 post 请求,我必须在登录后发送 admin前缀目录.
我的数据库 table 名称是 admin_users
我创建了一个 AdminUsersFixture,并且 Fixture 有 3 条记录
第一个记录是:
$this->records = [
[
'id' => 1,
'name' => 'jone_due',
'email' => 'jondue@test.com',
'password' => 'password',
'status' => 1,
'modified' => date('Y-m-d H:i:s'),
'created' => date('Y-m-d H:i:s'),
],
]
我写了AdminUsersTestController.php
<?php
class AdminUsersTest extends TestCase{
use IntegrationTestTrait;
public $fixtures = [
'app.AdminUsers'
];
public function testAddAuthenticated(): void
{
$query = $this->AdminUsers->find()->all();
$this->assertEquals(3, $query->count());
}
}
在 运行 测试脚本后我得到了响应
OK (1 test, 1 assertion)
然后尝试通过 post 请求登录,下面是我在操作中编写的更改 testAddAuthenticated()
public function testAddAuthenticated(): void
{
$query = $this->AdminUsers->find()->all();
$this->assertEquals(3, $query->count());
$user = [
'email' => 'jondue@test.com',
'password' => 'password'
];
$this->post('/admin?type=admin',$user);
debug($this->_response);
$this->get('/admin/adminUsers/add');
$this->assertResponseOk();
}
在调试中我总是收到电子邮件和密码错误消息。下面是我从 html
登录
<form method="post" accept-charset="utf-8" action="/admin?type=admin">
<input type="hidden" name="_method" value="POST">
<input type="email" name="email">
<input type="password" name="password">
<button name="redirect" type="submit">ログイン</button>
</form>
这是我在PHP单元结果
中得到的输出
There was 1 failure:
1) App\Test\TestCase\Controller\Admin\AdminUsersTest::testAddAuthenticated
Failed asserting that 302 is between 200 and 204.
修复程序中是否存在哈希密码问题?我走对路了吗?如何在登录 post 请求后访问 link?
您的设备必须包含散列密码,因为数据将被插入数据库 as-is,它不会通过 ORM,您的实体及其密码散列机制将被触发。
另请注意,您不能像这样连续发出两个请求,因为在集成测试中没有实际的状态处理。第二个请求将有一个空 session,这意味着您不会通过身份验证。
通常只需检查您的登录请求 returns 预期的响应就足够了,例如有效的 session cookie (assertCookie()
),预期的 session 数据 (assertSession()
),以及位置 header (assertRedirect()
).
用完全相同的方法进一步测试登录流程没有太大意义,因为它将依赖于您手动为下一个请求填充 session 数据,这意味着如果响应session 数据有效,那么使用该 session 数据的请求应该可以正常工作。
无论如何,您都会相应地对其他 non-login 端点进行测试,您将在 session 中手动填充有效身份验证状态所需的预期数据。
另见
我正在尝试在 cakephp IntegrationTest 中发出 post 请求,其中 post 请求,我必须在登录后发送 admin前缀目录.
我的数据库 table 名称是 admin_users
我创建了一个 AdminUsersFixture,并且 Fixture 有 3 条记录
第一个记录是:
$this->records = [
[
'id' => 1,
'name' => 'jone_due',
'email' => 'jondue@test.com',
'password' => 'password',
'status' => 1,
'modified' => date('Y-m-d H:i:s'),
'created' => date('Y-m-d H:i:s'),
],
]
我写了AdminUsersTestController.php
<?php
class AdminUsersTest extends TestCase{
use IntegrationTestTrait;
public $fixtures = [
'app.AdminUsers'
];
public function testAddAuthenticated(): void
{
$query = $this->AdminUsers->find()->all();
$this->assertEquals(3, $query->count());
}
}
在 运行 测试脚本后我得到了响应
OK (1 test, 1 assertion)
然后尝试通过 post 请求登录,下面是我在操作中编写的更改 testAddAuthenticated()
public function testAddAuthenticated(): void
{
$query = $this->AdminUsers->find()->all();
$this->assertEquals(3, $query->count());
$user = [
'email' => 'jondue@test.com',
'password' => 'password'
];
$this->post('/admin?type=admin',$user);
debug($this->_response);
$this->get('/admin/adminUsers/add');
$this->assertResponseOk();
}
在调试中我总是收到电子邮件和密码错误消息。下面是我从 html
登录<form method="post" accept-charset="utf-8" action="/admin?type=admin">
<input type="hidden" name="_method" value="POST">
<input type="email" name="email">
<input type="password" name="password">
<button name="redirect" type="submit">ログイン</button>
</form>
这是我在PHP单元结果
中得到的输出There was 1 failure:
1) App\Test\TestCase\Controller\Admin\AdminUsersTest::testAddAuthenticated
Failed asserting that 302 is between 200 and 204.
修复程序中是否存在哈希密码问题?我走对路了吗?如何在登录 post 请求后访问 link?
您的设备必须包含散列密码,因为数据将被插入数据库 as-is,它不会通过 ORM,您的实体及其密码散列机制将被触发。
另请注意,您不能像这样连续发出两个请求,因为在集成测试中没有实际的状态处理。第二个请求将有一个空 session,这意味着您不会通过身份验证。
通常只需检查您的登录请求 returns 预期的响应就足够了,例如有效的 session cookie (assertCookie()
),预期的 session 数据 (assertSession()
),以及位置 header (assertRedirect()
).
用完全相同的方法进一步测试登录流程没有太大意义,因为它将依赖于您手动为下一个请求填充 session 数据,这意味着如果响应session 数据有效,那么使用该 session 数据的请求应该可以正常工作。
无论如何,您都会相应地对其他 non-login 端点进行测试,您将在 session 中手动填充有效身份验证状态所需的预期数据。
另见