0

I am trying to execute my first unit test with PHPUnit 9.0.0 and Symfony 5.1.8. This test must to pass if the HTTP response is 200.

<?php declare(strict_types=1);

namespace Tests\Infrastructure\Api\Controller;

use PHPUnit\Framework\TestCase;

class ControllerTests extends TestCase
{
    /** @test */
     public function route(): void
    {
        $response = $this->get('/');

        $response->assertStatus(200);
    
    }

}

I obtain the error:

There was 1 error:

  1. Tests\Infrastructure\Api\Controller\SupplierControllerTests::route Error: Call to undefined method Tests\Infrastructure\Api\Controller\SupplierControllerTests::get()

I thought the Get method was a default method, imported by PHPUnit\Framework\TestCase, but it do not look that.

Have I to add a Get method into my class ControllerTests? How can i develop it to test the HTTP response?

Thanks in advance

1 Answer 1

3

You need to extend the WebTestCase provided by Symfony, not the default TestCase provided by PHPUnit.

Here is an example for a similar test you are trying to write:

namespace App\Tests\Controller;

use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

class PostControllerTest extends WebTestCase
{
    public function testShowPost()
    {
        $client = static::createClient();

        $client->request('GET', '/post/hello-world');

        $this->assertEquals(200, $client->getResponse()->getStatusCode());
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

So i have to use another command to throw the test? (Actually, i was used ./vendor/bin/phpunit)
I said because i obtain the error: " Tests\Infrastructure\Api\Controller\ControllerTests::route RuntimeException: Class "App\Kernel" doesn't exist or cannot be autoloaded. Check that the KERNEL_CLASS value in phpunit.xml matches the fully-qualified class name of your Kernel or override the "Tests\Infrastructure\Api\Controller\ControllerTests::createKernel()" method.". I am looking the reason but i do not find that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.