Skip to content

Prepare the work for FunctionalTesting using Symfony's WebTestCase #19093

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

Pierstoval
Copy link
Contributor

@Pierstoval Pierstoval commented Mar 1, 2025

Checklist before requesting a review

Please delete options that are not relevant.

  • I have read the CONTRIBUTING document.
  • I have performed a self-review of my code.
  • I have added tests that prove my fix is effective or that my feature works.
  • This change requires a documentation update.

This code is the very first step towards easing testing by using existing Symfony-related tooling.

This code allows doing things like this that can work (because it's not yet fully tested on my machine and I only have a few amount of uncommitted test cases related to this):

<?php

namespace tests\units\Glpi\Controller;

use GLPIWebTestCase;

class IndexControllerTest extends GLPIWebTestCase
{
    public function testNonAuthenticated(): void
    {
        $client = static::createClient();

        $client->request('GET', '/Helpdesk');

        self::assertResponseStatusCodeSame(302);
    }

    public function testAuthenticated(): void
    {
        $client = static::createAuthenticatedClient();

        $client->request('GET', '/Helpdesk');

        self::assertResponseStatusCodeSame(200);
    }
}

In the future, this will make it much easier to create functional tests by just spawning a kernel request call and asserting on HTML, XML or JSON output because Symfony already has tons of cool assertions for that purpose 👌

Another advantage is that some specific tests will need existing data in the database, and for that purpose, we can spawn some HTTP requests that will fill some forms or run the API to actually do the data addition (and even extract this in some utility Traits for reusability), and roll the changes back after the request, by wrapping the entire process in a database transaction and calling "rollback" on it at test's teardown.

A few small blockers though: all code using StreamedResponse won't (yet) be completely compatible because these streamed responses are harder to test with PHPUnit.
Not impossible, but you have to manually make the call to the internal closure and wrap it in an output buffer on which you'll make your assertions, so it's all manual stuff and cannot benefit from Symfony's assertions.

Comment on lines +86 to +87
\restore_error_handler();
\restore_exception_handler();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems to breaks tests that checks that expected messages are logged.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants