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

@@ -9,7 +9,12 @@
}, },
"require-dev": { "require-dev": {
"phpunit/phpunit": "^10.1", "phpunit/phpunit": "^10.1",
"cakephp/migrations": "^4.0.0" "cakephp/migrations": "^4.0.0",
"phpstan/phpstan": "^2.1",
"dereuromark/composer-prefer-lowest": "^0.1.10",
"cakedc/cakephp-phpstan": "^4.1",
"dereuromark/cakephp-captcha": "^2.0",
"fig-r/psr2r-sniffer": "^2.7"
}, },
"suggest": { "suggest": {
"dereuromark/cakephp-captcha": "^2.0" "dereuromark/cakephp-captcha": "^2.0"
@@ -25,5 +30,17 @@
"Cake\\Test\\": "vendor/cakephp/cakephp/tests/", "Cake\\Test\\": "vendor/cakephp/cakephp/tests/",
"TestApp\\": "tests/test_app/src/" "TestApp\\": "tests/test_app/src/"
} }
},
"scripts": {
"cs-check": "vendor/bin/phpcs --colors --parallel=16",
"cs-fix": "vendor/bin/phpcbf --colors --parallel=16",
"lowest": "validate-prefer-lowest",
"lowest-setup": "composer update --prefer-lowest --prefer-stable --prefer-dist --no-interaction && cp composer.json composer.backup && composer require --dev dereuromark/composer-prefer-lowest && mv composer.backup composer.json",
"stan": "phpstan analyze"
},
"config": {
"allow-plugins": {
"dealerdirect/phpcodesniffer-composer-installer": false
}
} }
} }

View File

@@ -3,8 +3,8 @@ declare(strict_types=1);
use Migrations\AbstractMigration; use Migrations\AbstractMigration;
class CreateContactUsFormSubmissions extends AbstractMigration class CreateContactUsFormSubmissions extends AbstractMigration {
{
/** /**
* Change Method. * Change Method.
* *
@@ -12,8 +12,7 @@ class CreateContactUsFormSubmissions extends AbstractMigration
* https://book.cakephp.org/phinx/0/en/migrations.html#the-change-method * https://book.cakephp.org/phinx/0/en/migrations.html#the-change-method
* @return void * @return void
*/ */
public function change(): void public function change(): void {
{
$table = $this->table('contact_us_form_submissions', ['id' => false, 'primary_key' => ['id']]); $table = $this->table('contact_us_form_submissions', ['id' => false, 'primary_key' => ['id']]);
$table->addColumn('id', 'uuid', [ $table->addColumn('id', 'uuid', [
@@ -58,4 +57,5 @@ class CreateContactUsFormSubmissions extends AbstractMigration
]); ]);
$table->create(); $table->create();
} }
} }

21
phpcs.xml Normal file
View File

@@ -0,0 +1,21 @@
<?xml version="1.0"?>
<ruleset name="plugin">
<arg value="nps"/>
<file>src/</file>
<file>config/</file>
<file>tests/</file>
<exclude-pattern>/tests/test_files/</exclude-pattern>
<exclude-pattern>/tests/test_app/</exclude-pattern>
<rule ref="vendor/fig-r/psr2r-sniffer/PSR2R/ruleset.xml"/>
<rule ref="PSR1.Classes.ClassDeclaration.MissingNamespace">
<exclude-pattern>*/config/Migrations/*</exclude-pattern>
</rule>
<rule ref="PhpCollective.Classes.ClassFileName.NoMatch">
<exclude-pattern>*/config/Migrations/*</exclude-pattern>
</rule>
</ruleset>

10
phpstan.neon Normal file
View File

@@ -0,0 +1,10 @@
includes:
- vendor/cakedc/cakephp-phpstan/extension.neon
parameters:
level: 4
paths:
- src
bootstrapFiles:
- tests/bootstrap.php
treatPhpDocTypesAsCertain: false

View File

@@ -13,10 +13,21 @@ use Cake\Routing\RouteBuilder;
/** /**
* Plugin for CakeContactUs * Plugin for CakeContactUs
*/ */
class CakeContactUsPlugin extends BasePlugin class CakeContactUsPlugin extends BasePlugin {
{
/**
* @var string
*/
public const EVENT_BEFORE_CONTACT_US_FORM_SAVED = 'ContactUs.Global.beforeRegister'; public const EVENT_BEFORE_CONTACT_US_FORM_SAVED = 'ContactUs.Global.beforeRegister';
/**
* @var string
*/
public const EVENT_AFTER_CONTACT_US_FORM_SAVED = 'ContactUs.Global.afterRegister'; public const EVENT_AFTER_CONTACT_US_FORM_SAVED = 'ContactUs.Global.afterRegister';
/**
* @var string
*/
public const CAKE_CONTACT_US_MYSQL_DATETIME = 'Y-m-d H:i:s'; public const CAKE_CONTACT_US_MYSQL_DATETIME = 'Y-m-d H:i:s';
/** /**
* Load all the plugin configuration and bootstrap logic. * Load all the plugin configuration and bootstrap logic.
@@ -27,8 +38,7 @@ class CakeContactUsPlugin extends BasePlugin
* @param \Cake\Core\PluginApplicationInterface $app The host application * @param \Cake\Core\PluginApplicationInterface $app The host application
* @return void * @return void
*/ */
public function bootstrap(PluginApplicationInterface $app): void public function bootstrap(PluginApplicationInterface $app): void {
{
} }
/** /**
@@ -40,8 +50,7 @@ class CakeContactUsPlugin extends BasePlugin
* @param \Cake\Routing\RouteBuilder $routes The route builder to update. * @param \Cake\Routing\RouteBuilder $routes The route builder to update.
* @return void * @return void
*/ */
public function routes(RouteBuilder $routes): void public function routes(RouteBuilder $routes): void {
{
$routes->prefix('Admin', function (RouteBuilder $routes): void { $routes->prefix('Admin', function (RouteBuilder $routes): void {
$routes->plugin('CakeContactUs', function (RouteBuilder $routes): void { $routes->plugin('CakeContactUs', function (RouteBuilder $routes): void {
$routes->connect('/', ['controller' => 'ContactUsFormSubmissions', 'action' => 'index']); $routes->connect('/', ['controller' => 'ContactUsFormSubmissions', 'action' => 'index']);
@@ -62,8 +71,7 @@ class CakeContactUsPlugin extends BasePlugin
* @param \Cake\Http\MiddlewareQueue $middlewareQueue The middleware queue to update. * @param \Cake\Http\MiddlewareQueue $middlewareQueue The middleware queue to update.
* @return \Cake\Http\MiddlewareQueue * @return \Cake\Http\MiddlewareQueue
*/ */
public function middleware(MiddlewareQueue $middlewareQueue): MiddlewareQueue public function middleware(MiddlewareQueue $middlewareQueue): MiddlewareQueue {
{
// Add your middlewares here // Add your middlewares here
return $middlewareQueue; return $middlewareQueue;
@@ -75,8 +83,7 @@ class CakeContactUsPlugin extends BasePlugin
* @param \Cake\Console\CommandCollection $commands The command collection to update. * @param \Cake\Console\CommandCollection $commands The command collection to update.
* @return \Cake\Console\CommandCollection * @return \Cake\Console\CommandCollection
*/ */
public function console(CommandCollection $commands): CommandCollection public function console(CommandCollection $commands): CommandCollection {
{
// Add your commands here // Add your commands here
$commands = parent::console($commands); $commands = parent::console($commands);
@@ -87,12 +94,12 @@ class CakeContactUsPlugin extends BasePlugin
/** /**
* Register application container services. * Register application container services.
* *
* @link https://book.cakephp.org/4/en/development/dependency-injection.html#dependency-injection
* @param \Cake\Core\ContainerInterface $container The Container to update. * @param \Cake\Core\ContainerInterface $container The Container to update.
* @return void * @return void
* @link https://book.cakephp.org/4/en/development/dependency-injection.html#dependency-injection
*/ */
public function services(ContainerInterface $container): void public function services(ContainerInterface $container): void {
{
// Add your services here // Add your services here
} }
} }

View File

@@ -10,10 +10,9 @@ use App\Controller\AppController;
* *
* @property \CakeContactUs\Model\Table\ContactUsFormSubmissionsTable $ContactUsFormSubmissions * @property \CakeContactUs\Model\Table\ContactUsFormSubmissionsTable $ContactUsFormSubmissions
*/ */
class ContactUsFormSubmissionsController extends AppController class ContactUsFormSubmissionsController extends AppController {
{
public function initialize(): void public function initialize(): void {
{
parent::initialize(); // TODO: Change the autogenerated stub parent::initialize(); // TODO: Change the autogenerated stub
} }
@@ -22,8 +21,7 @@ class ContactUsFormSubmissionsController extends AppController
* *
* @return \Cake\Http\Response|null|void Renders view * @return \Cake\Http\Response|null|void Renders view
*/ */
public function index() public function index() {
{
$query = $this->ContactUsFormSubmissions->find(); $query = $this->ContactUsFormSubmissions->find();
$contactUsFormSubmissions = $this->paginate($query); $contactUsFormSubmissions = $this->paginate($query);
@@ -34,25 +32,22 @@ class ContactUsFormSubmissionsController extends AppController
* View method * View method
* *
* @param string|null $id Contact Us Form Submission id. * @param string|null $id Contact Us Form Submission id.
* @return \Cake\Http\Response|null|void Renders view
* @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found. * @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
* @return \Cake\Http\Response|null|void Renders view
*/ */
public function view($id = null) public function view($id = null) {
{
$contactUsFormSubmission = $this->ContactUsFormSubmissions->get($id, contain: []); $contactUsFormSubmission = $this->ContactUsFormSubmissions->get($id, contain: []);
$this->set(compact('contactUsFormSubmission')); $this->set(compact('contactUsFormSubmission'));
} }
/** /**
* Edit method * Edit method
* *
* @param string|null $id Contact Us Form Submission id. * @param string|null $id Contact Us Form Submission id.
* @return \Cake\Http\Response|null|void Redirects on successful edit, renders view otherwise.
* @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found. * @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
* @return \Cake\Http\Response|null|void Redirects on successful edit, renders view otherwise.
*/ */
public function edit($id = null) public function edit($id = null) {
{
$contactUsFormSubmission = $this->ContactUsFormSubmissions->get($id, contain: []); $contactUsFormSubmission = $this->ContactUsFormSubmissions->get($id, contain: []);
if ($this->request->is(['patch', 'post', 'put'])) { if ($this->request->is(['patch', 'post', 'put'])) {
$contactUsFormSubmission = $this->ContactUsFormSubmissions->patchEntity($contactUsFormSubmission, $this->request->getData()); $contactUsFormSubmission = $this->ContactUsFormSubmissions->patchEntity($contactUsFormSubmission, $this->request->getData());
@@ -70,11 +65,10 @@ class ContactUsFormSubmissionsController extends AppController
* Delete method * Delete method
* *
* @param string|null $id Contact Us Form Submission id. * @param string|null $id Contact Us Form Submission id.
* @return \Cake\Http\Response|null Redirects to index.
* @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found. * @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
* @return \Cake\Http\Response|null Redirects to index.
*/ */
public function delete($id = null) public function delete($id = null) {
{
$this->request->allowMethod(['post', 'delete']); $this->request->allowMethod(['post', 'delete']);
$contactUsFormSubmission = $this->ContactUsFormSubmissions->get($id); $contactUsFormSubmission = $this->ContactUsFormSubmissions->get($id);
if ($this->ContactUsFormSubmissions->delete($contactUsFormSubmission)) { if ($this->ContactUsFormSubmissions->delete($contactUsFormSubmission)) {
@@ -85,4 +79,5 @@ class ContactUsFormSubmissionsController extends AppController
return $this->redirect(['action' => 'index']); return $this->redirect(['action' => 'index']);
} }
} }

View File

@@ -4,7 +4,6 @@ declare(strict_types=1);
namespace CakeContactUs\Controller\Component; namespace CakeContactUs\Controller\Component;
use Cake\Controller\Component; use Cake\Controller\Component;
use Cake\Controller\ComponentRegistry;
use Cake\Core\Configure; use Cake\Core\Configure;
use Cake\Datasource\EntityInterface; use Cake\Datasource\EntityInterface;
use Cake\Http\Response; use Cake\Http\Response;
@@ -12,18 +11,18 @@ use Cake\I18n\DateTime;
use Cake\Log\Log; use Cake\Log\Log;
use Cake\ORM\Table; use Cake\ORM\Table;
use Cake\ORM\TableRegistry; use Cake\ORM\TableRegistry;
use CakeContactUs\Model\Entity\ContactUsFormSubmission;
use CakeContactUs\Model\Table\ContactUsFormSubmissionsTable;
use Exception;
use CakeContactUs\CakeContactUsPlugin; use CakeContactUs\CakeContactUsPlugin;
use CakeContactUs\Model\Table\ContactUsFormSubmissionsTable;
/** /**
* ContactUs component * ContactUs component
*
* @property \Cake\Controller\Component\FlashComponent $Flash
*/ */
class ContactUsComponent extends Component class ContactUsComponent extends Component {
{
/** /**
* @var ContactUsFormSubmissionsTable|Table * @var \CakeContactUs\Model\Table\ContactUsFormSubmissionsTable|\Cake\ORM\Table
*/ */
protected ContactUsFormSubmissionsTable|Table $ContactUsFormSubmissions; protected ContactUsFormSubmissionsTable|Table $ContactUsFormSubmissions;
@@ -41,12 +40,11 @@ class ContactUsComponent extends Component
/** /**
* @param array $config * @param array $config
* @throws \Exception
* @return void * @return void
* *
* @throws Exception
*/ */
public function initialize(array $config): void public function initialize(array $config): void {
{
parent::initialize($config); // TODO: Change the autogenerated stub parent::initialize($config); // TODO: Change the autogenerated stub
$this->ContactUsFormSubmissions = TableRegistry::getTableLocator()->get('CakeContactUs.ContactUsFormSubmissions'); $this->ContactUsFormSubmissions = TableRegistry::getTableLocator()->get('CakeContactUs.ContactUsFormSubmissions');
@@ -63,10 +61,9 @@ class ContactUsComponent extends Component
} }
/** /**
* @return EntityInterface|ContactUsFormSubmission * @return \Cake\Datasource\EntityInterface|\CakeContactUs\Model\Entity\ContactUsFormSubmission
*/ */
public function newContactUsForm() public function newContactUsForm() {
{
if ($this->getConfig('requireCaptcha')) { if ($this->getConfig('requireCaptcha')) {
$this->ContactUsFormSubmissions->addBehavior('Captcha.Captcha'); $this->ContactUsFormSubmissions->addBehavior('Captcha.Captcha');
} }
@@ -75,10 +72,9 @@ class ContactUsComponent extends Component
} }
/** /**
* @return EntityInterface|ContactUsFormSubmission * @return \Cake\Http\Response|null|void
*/ */
public function processContactUsForm(EntityInterface $contactUsFormSubmission, array|null $postData) public function processContactUsForm(EntityInterface $contactUsFormSubmission, array|null $postData) {
{
if (!isset($postData)) { if (!isset($postData)) {
$postData = $this->getController()->getRequest()->getData(); $postData = $this->getController()->getRequest()->getData();
} }
@@ -126,7 +122,6 @@ class ContactUsComponent extends Component
Log::debug(print_r($contactUsFormSubmission->getErrors(), true)); Log::debug(print_r($contactUsFormSubmission->getErrors(), true));
} }
$contactUsFormSubmissionSaved = $this->ContactUsFormSubmissions->save($contactUsFormSubmission); $contactUsFormSubmissionSaved = $this->ContactUsFormSubmissions->save($contactUsFormSubmission);
if ($contactUsFormSubmissionSaved) { if ($contactUsFormSubmissionSaved) {
@@ -143,11 +138,10 @@ class ContactUsComponent extends Component
/** /**
* Prepare flash messages after registration, and dispatch afterRegister event * Prepare flash messages after registration, and dispatch afterRegister event
* *
* @param \Cake\Datasource\EntityInterface|ContactUsFormSubmission $contactUsFormSubmissionSaved Contact us form submission entity * @param \Cake\Datasource\EntityInterface|\CakeContactUs\Model\Entity\ContactUsFormSubmission $contactUsFormSubmissionSaved Contact us form submission entity
* @return \Cake\Http\Response * @return \Cake\Http\Response
*/ */
protected function _afterFormSaved(EntityInterface $contactUsFormSubmissionSaved) protected function _afterFormSaved(EntityInterface $contactUsFormSubmissionSaved) {
{
$message = __d('cake_contact_us', 'Message received, thank you. We will be in touch soon.'); $message = __d('cake_contact_us', 'Message received, thank you. We will be in touch soon.');
$event = $this->getController()->dispatchEvent(CakeContactUsPlugin::EVENT_AFTER_CONTACT_US_FORM_SAVED, [ $event = $this->getController()->dispatchEvent(CakeContactUsPlugin::EVENT_AFTER_CONTACT_US_FORM_SAVED, [
'contactUsFormSubmission' => $contactUsFormSubmissionSaved, 'contactUsFormSubmission' => $contactUsFormSubmissionSaved,
@@ -164,6 +158,8 @@ class ContactUsComponent extends Component
if ($this->getConfig('addIdToRedirect')) { if ($this->getConfig('addIdToRedirect')) {
$redirectUrl[] = $contactUsFormSubmissionSaved->get($this->ContactUsFormSubmissions->getPrimaryKey()); $redirectUrl[] = $contactUsFormSubmissionSaved->get($this->ContactUsFormSubmissions->getPrimaryKey());
} }
return $this->getController()->redirect($redirectUrl); return $this->getController()->redirect($redirectUrl);
} }
} }

View File

@@ -8,12 +8,12 @@ use App\Controller\AppController;
/** /**
* ContactUsFormSubmissions Controller * ContactUsFormSubmissions Controller
* *
* @property \CakeContactUs\Controller\Component\ContactUsComponent $ContactUs
* @property \CakeContactUs\Model\Table\ContactUsFormSubmissionsTable $ContactUsFormSubmissions * @property \CakeContactUs\Model\Table\ContactUsFormSubmissionsTable $ContactUsFormSubmissions
*/ */
class ContactUsFormSubmissionsController extends AppController class ContactUsFormSubmissionsController extends AppController {
{
public function initialize(): void public function initialize(): void {
{
parent::initialize(); // TODO: Change the autogenerated stub parent::initialize(); // TODO: Change the autogenerated stub
$this->loadComponent('CakeContactUs.ContactUs'); $this->loadComponent('CakeContactUs.ContactUs');
} }
@@ -23,12 +23,12 @@ class ContactUsFormSubmissionsController extends AppController
* *
* @return \Cake\Http\Response|null|void Redirects on successful add, renders view otherwise. * @return \Cake\Http\Response|null|void Redirects on successful add, renders view otherwise.
*/ */
public function add() public function add() {
{
$contactUsFormSubmission = $this->ContactUs->newContactUsForm(); $contactUsFormSubmission = $this->ContactUs->newContactUsForm();
if ($this->request->is('post')) { if ($this->request->is('post')) {
return $this->ContactUs->processContactUsForm($contactUsFormSubmission, $this->request->getData()); return $this->ContactUs->processContactUsForm($contactUsFormSubmission, $this->request->getData());
} }
$this->set(compact('contactUsFormSubmission')); $this->set(compact('contactUsFormSubmission'));
} }
} }

View File

@@ -4,28 +4,28 @@ declare(strict_types=1);
namespace CakeContactUs\Mailer; namespace CakeContactUs\Mailer;
use Cake\Core\Configure; use Cake\Core\Configure;
use Cake\Datasource\EntityInterface;
use Cake\Mailer\Mailer; use Cake\Mailer\Mailer;
use Cake\Mailer\Message; use Cake\Mailer\Message;
use CakeContactUs\Model\Entity\ContactUsFormSubmission;
/** /**
* User Mailer * User Mailer
*/ */
class ContactUsFormSubmissionsMailer extends Mailer class ContactUsFormSubmissionsMailer extends Mailer {
{
/** /**
* Send the templated email to the user * Send the confirmation email to the user
* *
* @param \Cake\Datasource\EntityInterface $user User entity * @param \CakeContactUs\Model\Entity\ContactUsFormSubmission $contactUsFormSubmission contact us form submission entity
* @param array $options
* @return void * @return void
*/ */
protected function confirmation(EntityInterface $contactUsFormSubmission, array $options = []) protected function confirmation(ContactUsFormSubmission $contactUsFormSubmission, array $options = []) {
{ $name = isset($contactUsFormSubmission->name) ? $contactUsFormSubmission->name . ', ' : '';
$name = isset($contactUsFormSubmission['name']) ? $contactUsFormSubmission['name'] . ', ' : '';
$subject = __d('cake_contact_us', 'We have received your message'); $subject = __d('cake_contact_us', 'We have received your message');
$this $this
->setTo($contactUsFormSubmission['email']) ->setTo($contactUsFormSubmission->email)
->setSubject($name . $subject) ->setSubject($name . $subject)
->setEmailFormat(Message::MESSAGE_BOTH) ->setEmailFormat(Message::MESSAGE_BOTH)
->setViewVars([ ->setViewVars([
@@ -39,13 +39,13 @@ class ContactUsFormSubmissionsMailer extends Mailer
/** /**
* Send to backoffice to take action * Send to backoffice to take action
* *
* @param \Cake\Datasource\EntityInterface $user User entity * @param \CakeContactUs\Model\Entity\ContactUsFormSubmission $contactUsFormSubmission contact us form submission entity
* @param array $options
* @return void * @return void
*/ */
protected function backend(EntityInterface $contactUsFormSubmission, array $options = []) protected function backend(ContactUsFormSubmission $contactUsFormSubmission, array $options = []) {
{
$subject = __d('cake_contact_us', 'Contact Us Form Submitted'); $subject = __d('cake_contact_us', 'Contact Us Form Submitted');
$name = isset($contactUsFormSubmission['name']) ? ' by ' . $contactUsFormSubmission['name'] : ''; $name = isset($contactUsFormSubmission->name) ? ' by ' . $contactUsFormSubmission->name : '';
$to = Configure::readOrFail('ContactUs.email.backend.to'); $to = Configure::readOrFail('ContactUs.email.backend.to');
$cc = Configure::read('ContactUs.email.backend.cc', []); $cc = Configure::read('ContactUs.email.backend.cc', []);
@@ -72,4 +72,5 @@ class ContactUsFormSubmissionsMailer extends Mailer
$this->viewBuilder() $this->viewBuilder()
->setTemplate('CakeContactUs.backend'); ->setTemplate('CakeContactUs.backend');
} }
} }

View File

@@ -18,8 +18,8 @@ use Cake\ORM\Entity;
* @property \Cake\I18n\DateTime|null $confirm_email_sent * @property \Cake\I18n\DateTime|null $confirm_email_sent
* @property \Cake\I18n\DateTime|null $backend_email_sent * @property \Cake\I18n\DateTime|null $backend_email_sent
*/ */
class ContactUsFormSubmission extends Entity class ContactUsFormSubmission extends Entity {
{
/** /**
* Fields that can be mass assigned using newEntity() or patchEntity(). * Fields that can be mass assigned using newEntity() or patchEntity().
* *
@@ -39,4 +39,5 @@ class ContactUsFormSubmission extends Entity
'confirm_email_sent' => true, 'confirm_email_sent' => true,
'backend_email_sent' => true, 'backend_email_sent' => true,
]; ];
} }

View File

@@ -3,42 +3,37 @@ declare(strict_types=1);
namespace CakeContactUs\Model\Table; namespace CakeContactUs\Model\Table;
use ArrayObject;
use Cake\Core\Configure; use Cake\Core\Configure;
use Cake\Core\Exception\CakeException; use Cake\Core\Exception\CakeException;
use Cake\Datasource\EntityInterface; use Cake\Datasource\EntityInterface;
use Cake\Datasource\ResultSetInterface;
use Cake\Event\EventInterface; use Cake\Event\EventInterface;
use Cake\I18n\DateTime; use Cake\I18n\DateTime;
use Cake\Log\Log;
use Cake\Mailer\MailerAwareTrait; use Cake\Mailer\MailerAwareTrait;
use Cake\ORM\Query\SelectQuery;
use Cake\ORM\RulesChecker;
use Cake\ORM\Table; use Cake\ORM\Table;
use Cake\Validation\Validator; use Cake\Validation\Validator;
use CakeContactUs\CakeContactUsPlugin; use CakeContactUs\CakeContactUsPlugin;
use CakeContactUs\Model\Entity\ContactUsFormSubmission; use CakeContactUs\Model\Entity\ContactUsFormSubmission;
use Closure;
use Psr\SimpleCache\CacheInterface;
/** /**
* ContactUsFormSubmissions Model * ContactUsFormSubmissions Model
* *
* @method ContactUsFormSubmission newEmptyEntity() * @method \CakeContactUs\Model\Entity\ContactUsFormSubmission newEmptyEntity()
* @method ContactUsFormSubmission newEntity(array $data, array $options = []) * @method \CakeContactUs\Model\Entity\ContactUsFormSubmission newEntity(array $data, array $options = [])
* @method array<ContactUsFormSubmission> newEntities(array $data, array $options = []) * @method array<ContactUsFormSubmission> newEntities(array $data, array $options = [])
* @method ContactUsFormSubmission get(mixed $primaryKey, array|string $finder = 'all', CacheInterface|string|null $cache = null, Closure|string|null $cacheKey = null, mixed ...$args) * @method \CakeContactUs\Model\Entity\ContactUsFormSubmission get(mixed $primaryKey, array|string $finder = 'all', \Psr\SimpleCache\CacheInterface|string|null $cache = null, \Closure|string|null $cacheKey = null, mixed ...$args)
* @method ContactUsFormSubmission findOrCreate($search, ?callable $callback = null, array $options = []) * @method \CakeContactUs\Model\Entity\ContactUsFormSubmission findOrCreate($search, ?callable $callback = null, array $options = [])
* @method ContactUsFormSubmission patchEntity(EntityInterface $entity, array $data, array $options = []) * @method \CakeContactUs\Model\Entity\ContactUsFormSubmission patchEntity(EntityInterface $entity, array $data, array $options = [])
* @method array<ContactUsFormSubmission> patchEntities(iterable $entities, array $data, array $options = []) * @method array<ContactUsFormSubmission> patchEntities(iterable $entities, array $data, array $options = [])
* @method ContactUsFormSubmission|false save(EntityInterface $entity, array $options = []) * @method ContactUsFormSubmission|false save(EntityInterface $entity, array $options = [])
* @method ContactUsFormSubmission saveOrFail(EntityInterface $entity, array $options = []) * @method \CakeContactUs\Model\Entity\ContactUsFormSubmission saveOrFail(\Cake\Datasource\EntityInterface $entity, array $options = [])
* @method iterable<ContactUsFormSubmission>|ResultSetInterface<ContactUsFormSubmission>|false saveMany(iterable $entities, array $options = []) * @method iterable<ContactUsFormSubmission>|\Cake\Datasource\ResultSetInterface<ContactUsFormSubmission>|false saveMany(iterable $entities, array $options = [])
* @method iterable<ContactUsFormSubmission>|ResultSetInterface<ContactUsFormSubmission> saveManyOrFail(iterable $entities, array $options = []) * @method iterable<ContactUsFormSubmission>|\Cake\Datasource\ResultSetInterface<ContactUsFormSubmission> saveManyOrFail(iterable $entities, array $options = [])
* @method iterable<ContactUsFormSubmission>|ResultSetInterface<ContactUsFormSubmission>|false deleteMany(iterable $entities, array $options = []) * @method iterable<ContactUsFormSubmission>|\Cake\Datasource\ResultSetInterface<ContactUsFormSubmission>|false deleteMany(iterable $entities, array $options = [])
* @method iterable<ContactUsFormSubmission>|ResultSetInterface<ContactUsFormSubmission> deleteManyOrFail(iterable $entities, array $options = []) * @method iterable<ContactUsFormSubmission>|\Cake\Datasource\ResultSetInterface<ContactUsFormSubmission> deleteManyOrFail(iterable $entities, array $options = [])
*/ */
class ContactUsFormSubmissionsTable extends Table class ContactUsFormSubmissionsTable extends Table {
{
use MailerAwareTrait; use MailerAwareTrait;
/** /**
@@ -47,8 +42,7 @@ class ContactUsFormSubmissionsTable extends Table
* @param array<string, mixed> $config The configuration for the Table. * @param array<string, mixed> $config The configuration for the Table.
* @return void * @return void
*/ */
public function initialize(array $config): void public function initialize(array $config): void {
{
parent::initialize($config); parent::initialize($config);
$this->setTable('contact_us_form_submissions'); $this->setTable('contact_us_form_submissions');
@@ -59,11 +53,10 @@ class ContactUsFormSubmissionsTable extends Table
/** /**
* Default validation rules. * Default validation rules.
* *
* @param Validator $validator Validator instance. * @param \Cake\Validation\Validator $validator Validator instance.
* @return Validator * @return \Cake\Validation\Validator
*/ */
public function validationDefault(Validator $validator): Validator public function validationDefault(Validator $validator): Validator {
{
$fields = Configure::readOrFail('ContactUs.fields'); $fields = Configure::readOrFail('ContactUs.fields');
$validator $validator
->dateTime('submitted_at') ->dateTime('submitted_at')
@@ -116,13 +109,12 @@ class ContactUsFormSubmissionsTable extends Table
} }
/** /**
* @param EventInterface $event * @param \Cake\Event\EventInterface $event
* @param EntityInterface|ContactUsFormSubmission $contactUsFormSubmission * @param \Cake\Datasource\EntityInterface|\CakeContactUs\Model\Entity\ContactUsFormSubmission $contactUsFormSubmission
* @param \ArrayObject $options * @param \ArrayObject $options
* @return void * @return void
*/ */
public function afterSave(EventInterface $event, EntityInterface|ContactUsFormSubmission $contactUsFormSubmission, \ArrayObject $options) public function afterSave(EventInterface $event, EntityInterface|ContactUsFormSubmission $contactUsFormSubmission, ArrayObject $options) {
{
if (!$contactUsFormSubmission->isNew()) { if (!$contactUsFormSubmission->isNew()) {
return; return;
} }
@@ -150,4 +142,5 @@ class ContactUsFormSubmissionsTable extends Table
$this->saveOrFail($contactUsFormSubmission); $this->saveOrFail($contactUsFormSubmission);
} }
} }
} }

View File

@@ -8,15 +8,14 @@ use Cake\TestSuite\Fixture\TestFixture;
/** /**
* ContactUsFormSubmissionsFixture * ContactUsFormSubmissionsFixture
*/ */
class ContactUsFormSubmissionsFixture extends TestFixture class ContactUsFormSubmissionsFixture extends TestFixture {
{
/** /**
* Init method * Init method
* *
* @return void * @return void
*/ */
public function init(): void public function init(): void {
{
$this->records = [ $this->records = [
[ [
'id' => '76fbe7fb-1949-4670-a3d2-c0b48eb98e8d', 'id' => '76fbe7fb-1949-4670-a3d2-c0b48eb98e8d',
@@ -32,4 +31,5 @@ class ContactUsFormSubmissionsFixture extends TestFixture
]; ];
parent::init(); parent::init();
} }
} }

View File

@@ -4,31 +4,41 @@ declare(strict_types=1);
namespace CakeContactUs\Test\TestCase\Controller\Component; namespace CakeContactUs\Test\TestCase\Controller\Component;
use Cake\Controller\ComponentRegistry; use Cake\Controller\ComponentRegistry;
use Cake\Controller\Controller;
use Cake\Http\ServerRequest;
use Cake\TestSuite\TestCase; use Cake\TestSuite\TestCase;
use CakeContactUs\Controller\Component\ContactUsComponent; use CakeContactUs\Controller\Component\ContactUsComponent;
use CakeContactUs\Model\Entity\ContactUsFormSubmission;
/** /**
* CakeContactUs\Controller\Component\ContactUsComponent Test Case * 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 * setUp method
* *
* @return void * @return void
*/ */
protected function setUp(): void protected function setUp(): void {
{
parent::setUp(); parent::setUp();
$registry = new ComponentRegistry(); $request = new ServerRequest();
$this->ContactUs = new ContactUsComponent($registry); $this->controller = new Controller($request);
$registry = new ComponentRegistry($this->controller);
$this->component = new ContactUsComponent($registry);
} }
/** /**
@@ -36,10 +46,32 @@ class ContactUsComponentTest extends TestCase
* *
* @return void * @return void
*/ */
protected function tearDown(): void protected function tearDown(): void {
{ unset($this->component);
unset($this->ContactUs);
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,33 +3,27 @@ declare(strict_types=1);
namespace CakeContactUs\Test\TestCase\Controller; namespace CakeContactUs\Test\TestCase\Controller;
use App\Model\Table\ContactUsFormSubmissionsTable;
use Cake\Controller\Controller;
use Cake\Core\Configure; use Cake\Core\Configure;
use Cake\Event\EventList; use Cake\Event\EventList;
use Cake\Event\EventManager; use Cake\Event\EventManager;
use Cake\Http\ServerRequest;
use Cake\Mailer\Transport\DebugTransport; use Cake\Mailer\Transport\DebugTransport;
use Cake\ORM\Table;
use Cake\TestSuite\EmailTrait; use Cake\TestSuite\EmailTrait;
use Cake\TestSuite\IntegrationTestTrait; use Cake\TestSuite\IntegrationTestTrait;
use Cake\TestSuite\TestCase; use Cake\TestSuite\TestCase;
use CakeContactUs\CakeContactUsPlugin; use CakeContactUs\CakeContactUsPlugin;
use CakeContactUs\Controller\ContactUsFormSubmissionsController;
use PHPUnit\Exception;
/** /**
* CakeContactUs\Controller\ContactUsFormSubmissionsController Test Case * CakeContactUs\Controller\ContactUsFormSubmissionsController Test Case
* *
* @uses ContactUsFormSubmissionsController * @uses ContactUsFormSubmissionsController
*/ */
class ContactUsFormSubmissionsControllerTest extends TestCase class ContactUsFormSubmissionsControllerTest extends TestCase {
{
use IntegrationTestTrait; use IntegrationTestTrait;
use EmailTrait; use EmailTrait;
/** /**
* @var ContactUsFormSubmissionsTable|Table * @var \App\Model\Table\ContactUsFormSubmissionsTable|\Cake\ORM\Table
*/ */
protected $ContactUsFormSubmissions; protected $ContactUsFormSubmissions;
@@ -47,8 +41,7 @@ class ContactUsFormSubmissionsControllerTest extends TestCase
* *
* @return void * @return void
*/ */
protected function setUp(): void protected function setUp(): void {
{
parent::setUp(); parent::setUp();
$this->ContactUsFormSubmissions = $this->getTableLocator()->get('ContactUsFormSubmissions'); $this->ContactUsFormSubmissions = $this->getTableLocator()->get('ContactUsFormSubmissions');
@@ -60,8 +53,7 @@ class ContactUsFormSubmissionsControllerTest extends TestCase
* *
* @return void * @return void
*/ */
protected function tearDown(): void protected function tearDown(): void {
{
unset($this->ContactUsFormSubmissions); unset($this->ContactUsFormSubmissions);
parent::tearDown(); parent::tearDown();
@@ -72,13 +64,12 @@ class ContactUsFormSubmissionsControllerTest extends TestCase
* *
* Tests the add action with a logged in user * Tests the add action with a logged in user
* *
* @return void * @uses ContactUsFormSubmissionsController::add
* @throws Exception * @throws Exception
* *
* @uses ContactUsFormSubmissionsController::add * @return void
*/ */
public function testAddGet(): void public function testAddGet(): void {
{
$cntBefore = $this->ContactUsFormSubmissions->find()->count(); $cntBefore = $this->ContactUsFormSubmissions->find()->count();
$url = [ $url = [
@@ -100,13 +91,12 @@ class ContactUsFormSubmissionsControllerTest extends TestCase
* *
* Tests a POST request to the add action with a logged in user * Tests a POST request to the add action with a logged in user
* *
* @return void * @uses ContactUsFormSubmissionsController::add
* @throws Exception * @throws Exception
* *
* @uses ContactUsFormSubmissionsController::add * @return void
*/ */
public function testAddPostSuccessNoEmail(): void public function testAddPostSuccessNoEmail(): void {
{
$cntBefore = $this->ContactUsFormSubmissions->find()->count(); $cntBefore = $this->ContactUsFormSubmissions->find()->count();
$url = [ $url = [
@@ -132,13 +122,12 @@ class ContactUsFormSubmissionsControllerTest extends TestCase
* *
* Tests a POST request to the add action with a logged in user * Tests a POST request to the add action with a logged in user
* *
* @return void * @uses ContactUsFormSubmissionsController::add
* @throws Exception * @throws Exception
* *
* @uses ContactUsFormSubmissionsController::add * @return void
*/ */
public function testAddPostSuccessBothEmailsSent(): void public function testAddPostSuccessBothEmailsSent(): void {
{
Configure::write('ContactUs', [ Configure::write('ContactUs', [
'fields' => [ 'fields' => [
'captcha' => false, 'captcha' => false,
@@ -156,7 +145,7 @@ class ContactUsFormSubmissionsControllerTest extends TestCase
Configure::write('EmailTransport', [ Configure::write('EmailTransport', [
'default' => [ 'default' => [
'className' => DebugTransport::class, 'className' => DebugTransport::class,
] ],
]); ]);
$cntBefore = $this->ContactUsFormSubmissions->find()->count(); $cntBefore = $this->ContactUsFormSubmissions->find()->count();
@@ -186,13 +175,12 @@ class ContactUsFormSubmissionsControllerTest extends TestCase
* *
* Tests a POST request to the add action with a logged in user * Tests a POST request to the add action with a logged in user
* *
* @return void * @uses ContactUsFormSubmissionsController::add
* @throws Exception * @throws Exception
* *
* @uses ContactUsFormSubmissionsController::add * @return void
*/ */
public function testAddPostSuccessConfirmationEmailSent(): void public function testAddPostSuccessConfirmationEmailSent(): void {
{
Configure::write('ContactUs', [ Configure::write('ContactUs', [
'fields' => [ 'fields' => [
'captcha' => false, 'captcha' => false,
@@ -234,13 +222,12 @@ class ContactUsFormSubmissionsControllerTest extends TestCase
* *
* Tests a POST request to the add action with a logged in user * Tests a POST request to the add action with a logged in user
* *
* @return void * @uses ContactUsFormSubmissionsController::add
* @throws Exception * @throws Exception
* *
* @uses ContactUsFormSubmissionsController::add * @return void
*/ */
public function testAddPostSuccessBackendEmailSent(): void public function testAddPostSuccessBackendEmailSent(): void {
{
Configure::write('ContactUs', [ Configure::write('ContactUs', [
'fields' => [ 'fields' => [
'captcha' => false, 'captcha' => false,
@@ -288,13 +275,12 @@ class ContactUsFormSubmissionsControllerTest extends TestCase
* *
* Tests a POST request to the add action with a logged in user * Tests a POST request to the add action with a logged in user
* *
* @return void * @uses ContactUsFormSubmissionsController::add
* @throws Exception * @throws Exception
* *
* @uses ContactUsFormSubmissionsController::add * @return void
*/ */
public function testAddPostLoggedInFailure(): void public function testAddPostLoggedInFailure(): void {
{
$cntBefore = $this->ContactUsFormSubmissions->find()->count(); $cntBefore = $this->ContactUsFormSubmissions->find()->count();
// $this->loginUserByRole('admin'); // $this->loginUserByRole('admin');
@@ -316,4 +302,5 @@ class ContactUsFormSubmissionsControllerTest extends TestCase
$this->assertEventFired(CakeContactUsPlugin::EVENT_BEFORE_CONTACT_US_FORM_SAVED); $this->assertEventFired(CakeContactUsPlugin::EVENT_BEFORE_CONTACT_US_FORM_SAVED);
$this->assertNoMailSent(); $this->assertNoMailSent();
} }
} }

View File

@@ -9,8 +9,8 @@ use CakeContactUs\Model\Table\ContactUsFormSubmissionsTable;
/** /**
* CakeContactUs\Model\Table\ContactUsFormSubmissionsTable Test Case * CakeContactUs\Model\Table\ContactUsFormSubmissionsTable Test Case
*/ */
class ContactUsFormSubmissionsTableTest extends TestCase class ContactUsFormSubmissionsTableTest extends TestCase {
{
/** /**
* Test subject * Test subject
* *
@@ -32,8 +32,7 @@ class ContactUsFormSubmissionsTableTest extends TestCase
* *
* @return void * @return void
*/ */
protected function setUp(): void protected function setUp(): void {
{
parent::setUp(); parent::setUp();
$config = $this->getTableLocator()->exists('ContactUsFormSubmissions') ? [] : ['className' => ContactUsFormSubmissionsTable::class]; $config = $this->getTableLocator()->exists('ContactUsFormSubmissions') ? [] : ['className' => ContactUsFormSubmissionsTable::class];
$this->ContactUsFormSubmissions = $this->getTableLocator()->get('ContactUsFormSubmissions', $config); $this->ContactUsFormSubmissions = $this->getTableLocator()->get('ContactUsFormSubmissions', $config);
@@ -44,8 +43,7 @@ class ContactUsFormSubmissionsTableTest extends TestCase
* *
* @return void * @return void
*/ */
protected function tearDown(): void protected function tearDown(): void {
{
unset($this->ContactUsFormSubmissions); unset($this->ContactUsFormSubmissions);
parent::tearDown(); parent::tearDown();
@@ -54,11 +52,10 @@ class ContactUsFormSubmissionsTableTest extends TestCase
/** /**
* TestInitialize method * TestInitialize method
* *
* @return void
* @uses \CakeContactUs\Model\Table\ContactUsFormSubmissionsTable::initialize() * @uses \CakeContactUs\Model\Table\ContactUsFormSubmissionsTable::initialize()
* @return void
*/ */
public function testInitialize(): void public function testInitialize(): void {
{
// verify all associations loaded // verify all associations loaded
$expectedAssociations = []; $expectedAssociations = [];
$associations = $this->ContactUsFormSubmissions->associations(); $associations = $this->ContactUsFormSubmissions->associations();
@@ -81,11 +78,11 @@ class ContactUsFormSubmissionsTableTest extends TestCase
/** /**
* Test validationDefault method * Test validationDefault method
* *
* @return void
* @uses \CakeContactUs\Model\Table\ContactUsFormSubmissionsTable::validationDefault() * @uses \CakeContactUs\Model\Table\ContactUsFormSubmissionsTable::validationDefault()
* @return void
*/ */
public function testValidationDefault(): void public function testValidationDefault(): void {
{
$this->markTestIncomplete('Not implemented yet.'); $this->markTestIncomplete('Not implemented yet.');
} }
} }

View File

@@ -10,7 +10,6 @@ use Cake\Datasource\ConnectionManager;
use Cake\Mailer\Mailer; use Cake\Mailer\Mailer;
use Cake\Mailer\Transport\DebugTransport; use Cake\Mailer\Transport\DebugTransport;
use Cake\Mailer\TransportFactory; use Cake\Mailer\TransportFactory;
use Cake\TestSuite\Fixture\SchemaLoader;
use CakeContactUs\CakeContactUsPlugin; use CakeContactUs\CakeContactUsPlugin;
use Migrations\TestSuite\Migrator; use Migrations\TestSuite\Migrator;
use TestApp\Controller\AppController; use TestApp\Controller\AppController;
@@ -77,7 +76,7 @@ Configure::write('ContactUs', [
Configure::write('EmailTransport', [ Configure::write('EmailTransport', [
'default' => [ 'default' => [
'className' => DebugTransport::class, 'className' => DebugTransport::class,
] ],
]); ]);
TransportFactory::setConfig('default', [ TransportFactory::setConfig('default', [
'className' => DebugTransport::class, 'className' => DebugTransport::class,
@@ -156,7 +155,17 @@ ConnectionManager::setConfig('test', [
*/ */
// Load a schema dump file. // Load a schema dump file.
//(new SchemaLoader())->loadSqlFiles('tests/schema.sql', 'test'); //(new SchemaLoader())->loadSqlFiles('tests/schema.sql', 'test');
if (!defined('SECOND')) {
define('SECOND', 1);
define('MINUTE', 60);
define('HOUR', 3600);
define('DAY', 86400);
define('WEEK', 604800);
define('MONTH', 2592000);
define('YEAR', 31536000);
}
$migrator = new Migrator(); $migrator = new Migrator();
$migrator->run(['plugin' => 'CakeContactUs']); $migrator->run(['plugin' => 'CakeContactUs']);
$migrator->run(['plugin' => 'Captcha']);