new design with nice layout
This commit is contained in:
@@ -0,0 +1,115 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature;
|
||||
|
||||
use App\Livewire\ContactForm;
|
||||
use App\Models\ContactEnquiry;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Illuminate\Support\Facades\Mail;
|
||||
use Livewire\Livewire;
|
||||
use Mockery;
|
||||
use Symfony\Component\Mailer\Exception\TransportException;
|
||||
use Symfony\Component\Mailer\Transport\TransportInterface;
|
||||
use Tests\TestCase;
|
||||
|
||||
class ContactEnquiryNotificationTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
/** Verify the saved enquiry and the message handed to the mail transport. */
|
||||
public function test_saves_enquiry_and_sends_to_configured_recipient(): void
|
||||
{
|
||||
config([
|
||||
'mail.contact_recipient' => '[email protected]',
|
||||
'mail.from.address' => '[email protected]',
|
||||
]);
|
||||
|
||||
$this->submitEnquiry();
|
||||
$this->assertDatabaseHas('contact_enquiries', [
|
||||
'email' => '[email protected]',
|
||||
'is_read' => false,
|
||||
]);
|
||||
|
||||
$messages = Mail::mailer()->getSymfonyTransport()->messages();
|
||||
$this->assertCount(1, $messages);
|
||||
$message = $messages->first()->getOriginalMessage();
|
||||
$this->assertSame('[email protected]', $message->getTo()[0]->getAddress());
|
||||
$this->assertSame('[email protected]', $message->getFrom()[0]->getAddress());
|
||||
$this->assertSame('[email protected]', $message->getReplyTo()[0]->getAddress());
|
||||
$this->assertStringContainsString(
|
||||
"Name: Test Visitor\nEmail: [email protected]\nCompany: Example Ltd\nService: Consultancy\n\nPlease tell me about Laravel support.",
|
||||
$message->getTextBody(),
|
||||
);
|
||||
}
|
||||
|
||||
/** Verify that a transport failure keeps the enquiry and logs safe diagnostics. */
|
||||
public function test_transport_failure_preserves_enquiry_and_logs_its_id(): void
|
||||
{
|
||||
config(['mail.contact_recipient' => '[email protected]']);
|
||||
Log::spy();
|
||||
$transport = Mockery::mock(TransportInterface::class);
|
||||
$transport->shouldReceive('send')->once()->andReturnUsing(function (): never {
|
||||
$this->assertDatabaseHas('contact_enquiries', ['email' => '[email protected]']);
|
||||
|
||||
throw new TransportException('Provider error with sensitive connection details.');
|
||||
});
|
||||
Mail::mailer()->setSymfonyTransport($transport);
|
||||
|
||||
$this->submitEnquiry();
|
||||
|
||||
$enquiry = ContactEnquiry::sole();
|
||||
Log::shouldHaveReceived('error')->once()->with('Contact enquiry notification failed.', [
|
||||
'enquiry_id' => $enquiry->getKey(),
|
||||
'exception_type' => TransportException::class,
|
||||
]);
|
||||
}
|
||||
|
||||
/** Verify that missing configuration is reported without sending a message. */
|
||||
public function test_missing_recipient_preserves_enquiry_and_logs_warning(): void
|
||||
{
|
||||
config(['mail.contact_recipient' => null]);
|
||||
Log::spy();
|
||||
|
||||
$this->submitEnquiry();
|
||||
|
||||
$enquiry = ContactEnquiry::sole();
|
||||
$this->assertCount(0, Mail::mailer()->getSymfonyTransport()->messages());
|
||||
Log::shouldHaveReceived('warning')->once()->with(
|
||||
'Contact enquiry notification skipped: configure CONTACT_ENQUIRY_TO.',
|
||||
['enquiry_id' => $enquiry->getKey()],
|
||||
);
|
||||
}
|
||||
|
||||
/** Verify that validation rejects spam before saving or sending. */
|
||||
public function test_rejected_submission_sends_no_notification(): void
|
||||
{
|
||||
config(['mail.contact_recipient' => '[email protected]']);
|
||||
|
||||
Livewire::test(ContactForm::class)
|
||||
->set('name', 'Test Visitor')
|
||||
->set('email', '[email protected]')
|
||||
->set('message', 'Please tell me about Laravel support.')
|
||||
->set('website', 'spam')
|
||||
->call('submit')
|
||||
->assertHasErrors(['website']);
|
||||
|
||||
$this->assertDatabaseCount('contact_enquiries', 0);
|
||||
$this->assertCount(0, Mail::mailer()->getSymfonyTransport()->messages());
|
||||
}
|
||||
|
||||
/** Submit valid data and verify the confirmation shown to the visitor. */
|
||||
private function submitEnquiry(): void
|
||||
{
|
||||
Livewire::test(ContactForm::class)
|
||||
->set('name', 'Test Visitor')
|
||||
->set('email', '[email protected]')
|
||||
->set('company', 'Example Ltd')
|
||||
->set('service', 'Consultancy')
|
||||
->set('message', 'Please tell me about Laravel support.')
|
||||
->call('submit')
|
||||
->assertHasNoErrors()
|
||||
->assertSet('submitted', true)
|
||||
->assertSee('your enquiry has been received.');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user