WIP - phpstan and phpcs - tests failing currently

This commit is contained in:
2026-01-24 01:20:17 -08:00
parent 31650e55c2
commit 37b7bca3be
17 changed files with 888 additions and 822 deletions

View File

@@ -4,42 +4,74 @@ 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
{
class ContactUsComponentTest extends TestCase {
protected ContactUsComponent $component;
protected Controller $controller;
/**
* Test subject
* Fixtures
*
* @var \CakeContactUs\Controller\Component\ContactUsComponent
* @var array<string>
*/
protected $ContactUs;
/**
protected array $fixtures = [
'plugin.CakeContactUs.ContactUsFormSubmissions',
];
/**
* setUp method
*
* @return void
*/
protected function setUp(): void
{
parent::setUp();
$registry = new ComponentRegistry();
$this->ContactUs = new ContactUsComponent($registry);
}
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->ContactUs);
protected function tearDown(): void {
unset($this->component);
parent::tearDown();
parent::tearDown();
}
public function testNewContactUsFormWithoutCaptcha()
{
$this->assertInstanceOf(ContactUsFormSubmission::class, $this->component->newContactUsForm());
}
public function testNewContactUsFormWithCaptcha()
{
$this->component->setConfig('requireCaptcha', true);
$this->assertInstanceOf(ContactUsFormSubmission::class, $this->component->newContactUsForm());
}
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);
}
}

View File

@@ -3,317 +3,304 @@ declare(strict_types=1);
namespace CakeContactUs\Test\TestCase\Controller;
use App\Model\Table\ContactUsFormSubmissionsTable;
use Cake\Controller\Controller;
use Cake\Core\Configure;
use Cake\Event\EventList;
use Cake\Event\EventManager;
use Cake\Http\ServerRequest;
use Cake\Mailer\Transport\DebugTransport;
use Cake\ORM\Table;
use Cake\TestSuite\EmailTrait;
use Cake\TestSuite\IntegrationTestTrait;
use Cake\TestSuite\TestCase;
use CakeContactUs\CakeContactUsPlugin;
use CakeContactUs\Controller\ContactUsFormSubmissionsController;
use PHPUnit\Exception;
/**
* CakeContactUs\Controller\ContactUsFormSubmissionsController Test Case
*
* @uses ContactUsFormSubmissionsController
*/
class ContactUsFormSubmissionsControllerTest extends TestCase
{
use IntegrationTestTrait;
use EmailTrait;
class ContactUsFormSubmissionsControllerTest extends TestCase {
/**
* @var ContactUsFormSubmissionsTable|Table
use IntegrationTestTrait;
use EmailTrait;
/**
* @var \App\Model\Table\ContactUsFormSubmissionsTable|\Cake\ORM\Table
*/
protected $ContactUsFormSubmissions;
protected $ContactUsFormSubmissions;
/**
/**
* Fixtures
*
* @var array<string>
*/
protected array $fixtures = [
'plugin.CakeContactUs.ContactUsFormSubmissions',
];
protected array $fixtures = [
'plugin.CakeContactUs.ContactUsFormSubmissions',
];
/**
/**
* setUp method
*
* @return void
*/
protected function setUp(): void
{
parent::setUp();
$this->ContactUsFormSubmissions = $this->getTableLocator()->get('ContactUsFormSubmissions');
protected function setUp(): void {
parent::setUp();
$this->ContactUsFormSubmissions = $this->getTableLocator()->get('ContactUsFormSubmissions');
EventManager::instance()->setEventList(new EventList());
}
EventManager::instance()->setEventList(new EventList());
}
/**
/**
* tearDown method
*
* @return void
*/
protected function tearDown(): void
{
unset($this->ContactUsFormSubmissions);
protected function tearDown(): void {
unset($this->ContactUsFormSubmissions);
parent::tearDown();
}
parent::tearDown();
}
/**
/**
* Test add method
*
* Tests the add action with a logged in user
*
* @return void
* @uses ContactUsFormSubmissionsController::add
* @throws Exception
*
* @uses ContactUsFormSubmissionsController::add
* @return void
*/
public function testAddGet(): void
{
$cntBefore = $this->ContactUsFormSubmissions->find()->count();
public function testAddGet(): void {
$cntBefore = $this->ContactUsFormSubmissions->find()->count();
$url = [
'plugin' => 'CakeContactUs',
'controller' => 'ContactUsFormSubmissions',
'action' => 'add',
];
$this->get($url);
$this->assertResponseCode(200);
$url = [
'plugin' => 'CakeContactUs',
'controller' => 'ContactUsFormSubmissions',
'action' => 'add',
];
$this->get($url);
$this->assertResponseCode(200);
$cntAfter = $this->ContactUsFormSubmissions->find()->count();
$this->assertEquals($cntBefore, $cntAfter);
$cntAfter = $this->ContactUsFormSubmissions->find()->count();
$this->assertEquals($cntBefore, $cntAfter);
$this->assertNoMailSent();
}
$this->assertNoMailSent();
}
/**
/**
* Test add method
*
* Tests a POST request to the add action with a logged in user
*
* @uses ContactUsFormSubmissionsController::add
* @throws Exception
*
* @return void
* @throws Exception
*
* @uses ContactUsFormSubmissionsController::add
*/
public function testAddPostSuccessNoEmail(): void
{
$cntBefore = $this->ContactUsFormSubmissions->find()->count();
public function testAddPostSuccessNoEmail(): void {
$cntBefore = $this->ContactUsFormSubmissions->find()->count();
$url = [
'plugin' => 'CakeContactUs',
'controller' => 'ContactUsFormSubmissions',
'action' => 'add',
];
$data = [
'name' => 'valid name',
'email' => 'valid_email@test.com',
'message' => 'valid message goes here',
];
$this->post($url, $data);
$this->assertResponseCode(302);
$cntAfter = $this->ContactUsFormSubmissions->find()->count();
$this->assertEquals($cntBefore + 1, $cntAfter);
$this->assertEventFired(CakeContactUsPlugin::EVENT_AFTER_CONTACT_US_FORM_SAVED);
$this->assertNoMailSent();
}
$url = [
'plugin' => 'CakeContactUs',
'controller' => 'ContactUsFormSubmissions',
'action' => 'add',
];
$data = [
'name' => 'valid name',
'email' => 'valid_email@test.com',
'message' => 'valid message goes here',
];
$this->post($url, $data);
$this->assertResponseCode(302);
$cntAfter = $this->ContactUsFormSubmissions->find()->count();
$this->assertEquals($cntBefore + 1, $cntAfter);
$this->assertEventFired(CakeContactUsPlugin::EVENT_AFTER_CONTACT_US_FORM_SAVED);
$this->assertNoMailSent();
}
/**
/**
* Test add method
*
* Tests a POST request to the add action with a logged in user
*
* @return void
* @uses ContactUsFormSubmissionsController::add
* @throws Exception
*
* @uses ContactUsFormSubmissionsController::add
* @return void
*/
public function testAddPostSuccessBothEmailsSent(): void
{
Configure::write('ContactUs', [
'fields' => [
'captcha' => false,
'email' => true,
],
'email' => [
'mailerClass' => 'CakeContactUs.ContactUsFormSubmissions',
'confirmation' => true, // true or false
'backend' => [ // array with enabled and the to/cc/bcc/ fields as strings or arrays
'enabled' => true,
'to' => 'test@example.com',
],
],
]);
Configure::write('EmailTransport', [
'default' => [
'className' => DebugTransport::class,
]
]);
public function testAddPostSuccessBothEmailsSent(): void {
Configure::write('ContactUs', [
'fields' => [
'captcha' => false,
'email' => true,
],
'email' => [
'mailerClass' => 'CakeContactUs.ContactUsFormSubmissions',
'confirmation' => true, // true or false
'backend' => [ // array with enabled and the to/cc/bcc/ fields as strings or arrays
'enabled' => true,
'to' => 'test@example.com',
],
],
]);
Configure::write('EmailTransport', [
'default' => [
'className' => DebugTransport::class,
],
]);
$cntBefore = $this->ContactUsFormSubmissions->find()->count();
$cntBefore = $this->ContactUsFormSubmissions->find()->count();
$url = [
'plugin' => 'CakeContactUs',
'controller' => 'ContactUsFormSubmissions',
'action' => 'add',
];
$data = [
'name' => 'valid name',
'email' => 'valid_email@test.com',
'message' => 'valid message goes here',
];
$this->post($url, $data);
$this->assertResponseCode(302);
$url = [
'plugin' => 'CakeContactUs',
'controller' => 'ContactUsFormSubmissions',
'action' => 'add',
];
$data = [
'name' => 'valid name',
'email' => 'valid_email@test.com',
'message' => 'valid message goes here',
];
$this->post($url, $data);
$this->assertResponseCode(302);
$cntAfter = $this->ContactUsFormSubmissions->find()->count();
$this->assertEquals($cntBefore + 1, $cntAfter);
$this->assertEventFired(CakeContactUsPlugin::EVENT_AFTER_CONTACT_US_FORM_SAVED);
$cntAfter = $this->ContactUsFormSubmissions->find()->count();
$this->assertEquals($cntBefore + 1, $cntAfter);
$this->assertEventFired(CakeContactUsPlugin::EVENT_AFTER_CONTACT_US_FORM_SAVED);
$this->assertMailCount(2); // confirmation + backend emails
}
$this->assertMailCount(2); // confirmation + backend emails
}
/**
/**
* Test add method
*
* Tests a POST request to the add action with a logged in user
*
* @return void
* @uses ContactUsFormSubmissionsController::add
* @throws Exception
*
* @uses ContactUsFormSubmissionsController::add
* @return void
*/
public function testAddPostSuccessConfirmationEmailSent(): void
{
Configure::write('ContactUs', [
'fields' => [
'captcha' => false,
'email' => true,
],
'email' => [
'mailerClass' => 'CakeContactUs.ContactUsFormSubmissions',
'confirmation' => true, // true or false
'backend' => [ // array with enabled and the to/cc/bcc/ fields as strings or arrays
'enabled' => false,
'to' => 'test@example.com',
],
],
]);
$cntBefore = $this->ContactUsFormSubmissions->find()->count();
public function testAddPostSuccessConfirmationEmailSent(): void {
Configure::write('ContactUs', [
'fields' => [
'captcha' => false,
'email' => true,
],
'email' => [
'mailerClass' => 'CakeContactUs.ContactUsFormSubmissions',
'confirmation' => true, // true or false
'backend' => [ // array with enabled and the to/cc/bcc/ fields as strings or arrays
'enabled' => false,
'to' => 'test@example.com',
],
],
]);
$cntBefore = $this->ContactUsFormSubmissions->find()->count();
$url = [
'plugin' => 'CakeContactUs',
'controller' => 'ContactUsFormSubmissions',
'action' => 'add',
];
$data = [
'name' => 'valid name',
'email' => 'valid_email@test.com',
'message' => 'valid message goes here',
];
$this->post($url, $data);
$this->assertResponseCode(302);
$url = [
'plugin' => 'CakeContactUs',
'controller' => 'ContactUsFormSubmissions',
'action' => 'add',
];
$data = [
'name' => 'valid name',
'email' => 'valid_email@test.com',
'message' => 'valid message goes here',
];
$this->post($url, $data);
$this->assertResponseCode(302);
$cntAfter = $this->ContactUsFormSubmissions->find()->count();
$this->assertEquals($cntBefore + 1, $cntAfter);
$this->assertEventFired(CakeContactUsPlugin::EVENT_AFTER_CONTACT_US_FORM_SAVED);
$cntAfter = $this->ContactUsFormSubmissions->find()->count();
$this->assertEquals($cntBefore + 1, $cntAfter);
$this->assertEventFired(CakeContactUsPlugin::EVENT_AFTER_CONTACT_US_FORM_SAVED);
$this->assertMailCount(1); // confirmation only
}
$this->assertMailCount(1); // confirmation only
}
/**
/**
* Test add method
*
* Tests a POST request to the add action with a logged in user
*
* @return void
* @uses ContactUsFormSubmissionsController::add
* @throws Exception
*
* @uses ContactUsFormSubmissionsController::add
* @return void
*/
public function testAddPostSuccessBackendEmailSent(): void
{
Configure::write('ContactUs', [
'fields' => [
'captcha' => false,
'email' => true,
],
'email' => [
'mailerClass' => 'CakeContactUs.ContactUsFormSubmissions',
'confirmation' => false, // true or false
'backend' => [ // array with enabled and the to/cc/bcc/ fields as strings or arrays
'enabled' => true,
'to' => 'test@example.com',
],
],
]);
public function testAddPostSuccessBackendEmailSent(): void {
Configure::write('ContactUs', [
'fields' => [
'captcha' => false,
'email' => true,
],
'email' => [
'mailerClass' => 'CakeContactUs.ContactUsFormSubmissions',
'confirmation' => false, // true or false
'backend' => [ // array with enabled and the to/cc/bcc/ fields as strings or arrays
'enabled' => true,
'to' => 'test@example.com',
],
],
]);
$cntBefore = $this->ContactUsFormSubmissions->find()->count();
$cntBefore = $this->ContactUsFormSubmissions->find()->count();
$url = [
'plugin' => 'CakeContactUs',
'controller' => 'ContactUsFormSubmissions',
'action' => 'add',
];
$data = [
'name' => 'valid name',
'email' => 'valid_email@test.com',
'message' => 'valid message goes here',
];
$this->post($url, $data);
$responseBody = (string)$this->_response->getBody();
$tmpLog = fopen(TMP . 'phpunit.log', 'w');
if ($tmpLog) {
fwrite($tmpLog, $responseBody);
}
$this->assertResponseCode(302);
$url = [
'plugin' => 'CakeContactUs',
'controller' => 'ContactUsFormSubmissions',
'action' => 'add',
];
$data = [
'name' => 'valid name',
'email' => 'valid_email@test.com',
'message' => 'valid message goes here',
];
$this->post($url, $data);
$responseBody = (string)$this->_response->getBody();
$tmpLog = fopen(TMP . 'phpunit.log', 'w');
if ($tmpLog) {
fwrite($tmpLog, $responseBody);
}
$this->assertResponseCode(302);
$cntAfter = $this->ContactUsFormSubmissions->find()->count();
$this->assertEquals($cntBefore + 1, $cntAfter);
$this->assertEventFired(CakeContactUsPlugin::EVENT_AFTER_CONTACT_US_FORM_SAVED);
$cntAfter = $this->ContactUsFormSubmissions->find()->count();
$this->assertEquals($cntBefore + 1, $cntAfter);
$this->assertEventFired(CakeContactUsPlugin::EVENT_AFTER_CONTACT_US_FORM_SAVED);
$this->assertMailCount(1); // backend email only
}
$this->assertMailCount(1); // backend email only
}
/**
/**
* Test add method
*
* Tests a POST request to the add action with a logged in user
*
* @return void
* @uses ContactUsFormSubmissionsController::add
* @throws Exception
*
* @uses ContactUsFormSubmissionsController::add
* @return void
*/
public function testAddPostLoggedInFailure(): void
{
$cntBefore = $this->ContactUsFormSubmissions->find()->count();
public function testAddPostLoggedInFailure(): void {
$cntBefore = $this->ContactUsFormSubmissions->find()->count();
// $this->loginUserByRole('admin');
$url = [
'plugin' => 'CakeContactUs',
'controller' => 'ContactUsFormSubmissions',
'action' => 'add',
];
$data = [
'name' => 'valid name',
'email' => 'not_valid_email',
'message' => 'this is a valid message ',
];
$this->post($url, $data);
$this->assertResponseCode(200);
// $this->loginUserByRole('admin');
$url = [
'plugin' => 'CakeContactUs',
'controller' => 'ContactUsFormSubmissions',
'action' => 'add',
];
$data = [
'name' => 'valid name',
'email' => 'not_valid_email',
'message' => 'this is a valid message ',
];
$this->post($url, $data);
$this->assertResponseCode(200);
$cntAfter = $this->ContactUsFormSubmissions->find()->count();
$this->assertEquals($cntBefore, $cntAfter);
$this->assertEventFired(CakeContactUsPlugin::EVENT_BEFORE_CONTACT_US_FORM_SAVED);
$this->assertNoMailSent();
}
$cntAfter = $this->ContactUsFormSubmissions->find()->count();
$this->assertEquals($cntBefore, $cntAfter);
$this->assertEventFired(CakeContactUsPlugin::EVENT_BEFORE_CONTACT_US_FORM_SAVED);
$this->assertNoMailSent();
}
}