Files
cake-contact-us/tests/TestCase/Controller/Component/ContactUsComponentTest.php
2026-01-24 11:11:04 -08:00

86 lines
2.1 KiB
PHP

<?php
declare(strict_types=1);
namespace CakeContactUs\Test\TestCase\Controller\Component;
use Cake\Controller\ComponentRegistry;
use Cake\Controller\Controller;
use Cake\Http\ServerRequest;
use Cake\TestSuite\TestCase;
use CakeContactUs\Controller\Component\ContactUsComponent;
use CakeContactUs\Model\Entity\ContactUsFormSubmission;
/**
* CakeContactUs\Controller\Component\ContactUsComponent Test Case
*/
class ContactUsComponentTest extends TestCase {
protected ContactUsComponent $component;
protected Controller $controller;
/**
* Fixtures
*
* @var array<string>
*/
protected array $fixtures = [
'plugin.CakeContactUs.ContactUsFormSubmissions',
];
/**
* setUp method
*
* @return void
*/
protected function setUp(): void {
parent::setUp();
$request = new ServerRequest();
$this->controller = new Controller($request);
$registry = new ComponentRegistry($this->controller);
$this->component = new ContactUsComponent($registry);
}
/**
* tearDown method
*
* @return void
*/
protected function tearDown(): void {
unset($this->component);
parent::tearDown();
}
/**
* @return void
*/
public function testNewContactUsFormWithoutCaptcha() {
$this->assertInstanceOf(ContactUsFormSubmission::class, $this->component->newContactUsForm());
}
/**
* @return void
*/
public function testNewContactUsFormWithCaptcha() {
$this->component->setConfig('requireCaptcha', true);
$this->assertInstanceOf(ContactUsFormSubmission::class, $this->component->newContactUsForm());
}
/**
* @return void
*/
public function testProcessContactUsFormSaved() {
$numSubmissionsBefore = $this->fetchTable('CakeContactUs/ContactUsFormSubmissions')->find()->count();
$result = $this->component->processContactUsForm($this->component->newContactUsForm(), [
'name' => 'Jane Doe',
'email' => 'test@example.com',
]);
$this->assertNotInstanceOf(ContactUsFormSubmission::class, $result);
$numSubmissionsAfter = $this->fetchTable('CakeContactUs/ContactUsFormSubmissions')->find()->count();
$this->assertEquals($numSubmissionsBefore + 1, $numSubmissionsAfter);
}
}