The PHPUnit framework keeps returning this message
tests\IWPNoncesTest::testCreateNonce Error: Class 'tests\IWPNonces' not found
C:\Users\user pc\Desktop\inpsyde\tests\IWPNoncesTest.php:25
To start with, Am new to unit testing and I don't really know how it works. I tried following the offical tutorial on the first page of the website [https://phpunit.de/getting-started/phpunit-8.html][1] but that too doesn't work as it also complain that the
Email class does not exits or so.
My question is how do I work with the PHPUnit framework? And personally, this is the steps I already took 1. Create
phpunit.xml
file in the root directory and this is the code there
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
syntaxCheck="false"
bootstrap="vendor/autoload.php"
>
<testsuites>
<testsuite name="WPNonces Test Suite">
<directory>./tests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory>./classes</directory>
</whitelist>
</filter>
</phpunit>
- Then create the classes folder that house the main code and in the classes folder, I
createIWPNonces.php
and this is the code there
<?php
declare(strict_types=1);
namespace IWPNoncesFunctions;
class IWPNonces{
public function IWPCreateNonce(string $action): string {
return wp_create_nonce($action);
}
public function IWPNonceURL(string $url, string $action): string {
return wp_nonce_url($url, $action);
}
public function IWPNonceField(string $action, string $field): string {
return wp_nonce_field($action, $field);
}
public function IWPVerifyNonce(string $action, string $field, string $type): string{
switch($type){
if('field' == $type){
if(!isset($_REQUEST['field']) || !wp_verify_nonce(['field'], 'action')){
return false;
}else{
return wp_verify_nonce(['field'], 'action');
}
}
break;
if('url' == $type){
if ( ! empty( $_REQUEST ) && check_admin_referer( 'action', 'field' ) ){
return check_admin_referer( 'action', 'field' );
}
}
break;
if('ajax' == $type){
return check_ajax_referer('$action', $security);
}
break;
default:
return 'Error, the type name does not exist in wp nonce verifier. Please, use a valid verifier';
break;
}
}
public function IWPVerifyURLNonce(string $url, string $action): string {
if ( ! empty( $_REQUEST ) && check_admin_referer( 'action', 'field' ) ){
return check_admin_referer( 'action', 'field' );
}
}
public function IWPVerifyFieldNonce(string $action, string $field): string {
if(!isset($_REQUEST['field']) || !wp_verify_nonce(['field'], 'action')){
return false;
}else{
return wp_verify_nonce(['field'], 'action');
}
}
public function IWPVerifyAjaxNonce(string $url, string $action): string {
if('ajax' == $type){
return check_ajax_referer('$action', $security);
}
}
}
- After this, I created the
tests/IWPNoncesTest folder
that house the script that PHPUnit will test and this is the script written for the test
<?php
declare(strict_types=1);
// Created by : Gbenga Ogunbule
// Location : Ijebu Ode
// Date : 18/07/19
// Time : 21:44
namespace tests;
use IWPNoncesFunctions;
use PHPUnit\Framework\TestCase;;
class IWPNoncesTest extends TestCase{
/*private $createNonce;
public function setUp() {
$this->createNonce = new IWPNonces('action');
#$this->createNonce = IWPCreateNonce('action');
}*/
public function testCreateNonce(){
$createNonce = new IWPNonces();
$createNonce->IWPCreateNonce('action');
$this->assertSame(
'action',
$this->createNonce,
'The verify Nonce(WPNonce) is different from the one created above'
);
}
/*public function testVerifyURLNonce(){
$verifyURLNonce = new IWPNonces();
$verifyURLNonce->IWPVerifyURLNonce('localhost/index.php', 'action');
$this->assertSame('action', $verifyURLNonce, 'Verified not');
}
public function testVerifyFieldNonce(){
$verifyFieldNonce = new IWPNonces();
$verifyFieldNonce->IWPVerifyFieldNonce('action', 'action');
$this->assertSame('action', $verifyFieldNonce, 'Failed');
}
public function testVerifyAjaxNonce(){
$verifyAjaxNonce = new IWPNonces();
$verifyAjaxNonce->IWPVerifyFieldNonce('localhost/index.php', 'action');
$this->assertSame('action', $verifyAjaxNonce, 'Failure');
}*/
}
- Inside the
composer.json
file I have this
"autoload": {
"psr-4": {
"WPNoncesFunction\\": "classes",
"tests\\": "tests"
}
},