如何在控制台中忽略不影响测试的 PHPUnit 错误?
How to ignore PHPUnit errors, which do not affect tests, in console?
当我 运行 PHP 单元测试时,我的控制台中显示了一些错误,即使这些错误不影响我的测试(它们都通过了)。看到所有这些错误非常烦人
displayed errors
我尝试在 phpunit.xml 文件中添加以下行:
convertErrorsToExceptions="false"
convertNoticesToExceptions="false"
convertWarningsToExceptions="false"
但这并没有帮助。我正在使用:
PHP单位:9.5
Symfony:6.0
PHP: 8.1
我的测试是这样的:
/**
* @test
* @dataProvider editItemInvalidDataProvider
*/
public function should_not_edit_item_and_not_redirect_when_failure(
?string $itemName,
?string $itemSellIn,
?string $itemQuality
): void {
$this->loginUser();
$crawler = $this->client->request('POST', '/item/1/edit');
$button = $crawler->selectButton('edit-item');
$form = $button->form();
$form['item[name]']->setValue($itemName);
$form['item[sellIn]']->setValue($itemSellIn);
$form['item[quality]']->setValue($itemQuality);
$this->client->submit($form);
$this->assertResponseNotHasHeader('location');
}
/** @test */
public function should_return_403_when_not_logged_in_and_reaching_edit_page(): void
{
$this->client->request('GET', '/item/1/edit');
$this->assertResponseStatusCodeSame(403);
}
控制器:
#[Route('/item/{id}/edit', name: 'item_edit')]
#[ParamConverter('item', class: Item::class)]
#[IsGranted('ROLE_ADMIN', statusCode: 403)]
public function edit(
?Item $item,
Request $request,
): Response {
if (!$item) {
$this->addFlash('error', 'No item found');
return $this->redirectToRoute('item_list');
}
$form = $this->createForm(ItemType::class, $item);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$this->entityManager->persist($item);
$this->entityManager->flush();
$this->addFlash('success', 'Item successfully edited!');
return $this->redirectToRoute('item_list');
}
return $this->render('item/edit.html.twig', [
'form' => $form->createView(),
]);
}
关于如何抑制这些错误的任何想法?
您可以通过以下配置告诉客户端不要跟随任何重定向:
$this->client->request('POST', '/item/1/edit', [
'max_redirects' => 0,
]);
这应该可以防止您的一些(也许是全部)错误。
当我 运行 PHP 单元测试时,我的控制台中显示了一些错误,即使这些错误不影响我的测试(它们都通过了)。看到所有这些错误非常烦人 displayed errors
我尝试在 phpunit.xml 文件中添加以下行:
convertErrorsToExceptions="false"
convertNoticesToExceptions="false"
convertWarningsToExceptions="false"
但这并没有帮助。我正在使用:
PHP单位:9.5
Symfony:6.0
PHP: 8.1
我的测试是这样的:
/**
* @test
* @dataProvider editItemInvalidDataProvider
*/
public function should_not_edit_item_and_not_redirect_when_failure(
?string $itemName,
?string $itemSellIn,
?string $itemQuality
): void {
$this->loginUser();
$crawler = $this->client->request('POST', '/item/1/edit');
$button = $crawler->selectButton('edit-item');
$form = $button->form();
$form['item[name]']->setValue($itemName);
$form['item[sellIn]']->setValue($itemSellIn);
$form['item[quality]']->setValue($itemQuality);
$this->client->submit($form);
$this->assertResponseNotHasHeader('location');
}
/** @test */
public function should_return_403_when_not_logged_in_and_reaching_edit_page(): void
{
$this->client->request('GET', '/item/1/edit');
$this->assertResponseStatusCodeSame(403);
}
控制器:
#[Route('/item/{id}/edit', name: 'item_edit')]
#[ParamConverter('item', class: Item::class)]
#[IsGranted('ROLE_ADMIN', statusCode: 403)]
public function edit(
?Item $item,
Request $request,
): Response {
if (!$item) {
$this->addFlash('error', 'No item found');
return $this->redirectToRoute('item_list');
}
$form = $this->createForm(ItemType::class, $item);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$this->entityManager->persist($item);
$this->entityManager->flush();
$this->addFlash('success', 'Item successfully edited!');
return $this->redirectToRoute('item_list');
}
return $this->render('item/edit.html.twig', [
'form' => $form->createView(),
]);
}
关于如何抑制这些错误的任何想法?
您可以通过以下配置告诉客户端不要跟随任何重定向:
$this->client->request('POST', '/item/1/edit', [
'max_redirects' => 0,
]);
这应该可以防止您的一些(也许是全部)错误。