Kopiuj
<?php
namespace App\Tests\Controller;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Mime\Email;
use Symfony\Component\Mime\RawMessage;
use Symfony\Component\Mailer\Envelope;
final class ContactControllerTest extends WebTestCase
{
protected function setUp(): void
{
parent::setUp();
restore_exception_handler();
}
public function testInvalidRequestReturnsBadRequest(): void
{
$client = static::createClient();
$client->catchExceptions(false);
$client->request('POST', '/api/contact', [], [], [
'CONTENT_TYPE' => 'application/json'
], json_encode([
'name' => '',
'email' => '',
'message' => 'short',
]));
$response = $client->getResponse();
$this->assertSame(400, $response->getStatusCode());
$this->assertTrue($response->headers->contains('Content-Type', 'application/json'));
$this->assertJson($response->getContent());
$data = json_decode($response->getContent(), true);
$this->assertIsArray($data);
$this->assertArrayHasKey('status', $data);
$this->assertArrayHasKey('message', $data);
$this->assertArrayHasKey('errors', $data);
$this->assertSame('error', $data['status']);
$this->assertSame('Formularz zawiera błędy.', $data['message']);
}
public function testValidRequestSendsEmail(): void
{
$client = static::createClient();
$client->catchExceptions(false);
self::getContainer()->set(MailerInterface::class, new class implements MailerInterface {
public function send(RawMessage $message, ?Envelope $envelope = null): void {}
});
$client->request('POST', '/api/contact', [], [], [
'CONTENT_TYPE' => 'application/json'
], json_encode([
'name' => 'John Doe',
'email' => 'john@example.com',
'message' => str_repeat('Message body. ', 5),
]));
$response = $client->getResponse();
$this->assertSame(200, $response->getStatusCode());
$this->assertTrue($response->headers->contains('Content-Type', 'application/json'));
$this->assertJson($response->getContent());
$data = json_decode($response->getContent(), true);
$this->assertIsArray($data);
$this->assertArrayHasKey('status', $data);
$this->assertArrayHasKey('message', $data);
$this->assertSame('success', $data['status']);
$this->assertSame('Wiadomość została wysłana.', $data['message']);
}
}
Oto cała klasa z testem, tak jak mówiłem testy przechodza ale sa jakieś warningi wiec na moje oko coś z konfiguracją.
Używając phpunit --debug też ładnie przechodzi nie zwraca żadnych błędów, ale tez nie pokazuje informacji o żadnych warningach