0

When I run PHPUnit tests, there are some errors displayed in my console, even though these errors do not affect my tests (they all pass). And it is very annoying to see all these errors displayed errors

I tried in phpunit.xml file add the following lines:

convertErrorsToExceptions="false"
convertNoticesToExceptions="false"
convertWarningsToExceptions="false"

but it didn't help. I am using:

PHPUNIT: 9.5

Symfony: 6.0

PHP: 8.1

My tests look like this:

/**
     * @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);
    }

And the controller:

#[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(),
        ]);
    }

Any ideas on how to suppress those errors?

2

1 Answer 1

0

You can tell the client to not follow any redirects with the following config:

$this->client->request('POST', '/item/1/edit', [
    'max_redirects' => 0,
]);

This should prevent some (maybe all) of your errors.

0

Not the answer you're looking for? Browse other questions tagged or ask your own question.