Testing
WebTestCase setup
Functional tests boot a small copy of your app and visit pages like a browser. The base class for them is called WebTestCase.
Extend it in a test class and create a client inside each test. The client holds cookies and follows the app config.
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; class HomeTest extends WebTestCase { public function testHome(): void { $client = static::createClient(); $this->assertNotNull($client); } }
Request and assert
Ask the client for a page with the request method and an address. Then check the answer with a response assert.
The test below opens the home page and expects success. When the page breaks, this test turns red first.
class HomeTest extends WebTestCase { public function testHome(): void { $client = static::createClient(); $client->request("GET", "/"); $this->assertResponseIsSuccessful(); } }
Test database note
Tests must not touch the real database. Point the test setup at a separate test database and rebuild it before the suite runs.
With a fresh base each run, tests stay repeatable. A test that passes alone must also pass with the full suite.
Run phpunit
Run the whole suite with the PHPUnit binary from the project root. One command runs every test class in order.
Green output means the app still behaves. Read the counts at the end to see how many checks ran.
php bin/phpunit
OK (4 tests, 9 assertions)
Next: Fixtures
Article author: Arthur Isaev