PHPUnit 框架保持未找到返回类



PHPUnit 框架不断返回此消息

tests\IWPNoncesTest::testCreateNonce 错误:找不到类"测试\IWPNonces">

C:\Users\user pc\Desktop\inpsyde\tests\IWPNoncesTest.php:25

首先,我是单元测试的新手,我真的不知道它是如何工作的。我尝试遵循网站第一页上的官方教程 [https://phpunit.de/getting-started/phpunit-8.html][1],但这也不起作用,因为它也抱怨

电子邮件类不退出左右。

我的问题是如何使用 PHPUnit 框架?就个人而言,这是我已经采取的步骤 1. 创建

phpunit.xml

根目录中的文件 这是那里的代码

<?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>
  1. 然后创建包含主代码的类文件夹,在类文件夹中,I

创建IWPNonces.php

这是那里的代码

<?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);
}
}
}
  1. 在此之后,我创建了

tests/IWPNoncesTest 文件夹

它包含 PHPUnit 将测试的脚本,这是为测试编写的脚本

<?php
declare(strict_types=1);

// Created by : Gbenga Ogunbule
// Location : Ijebu Ode
// Date : 18/07/19
// Time : 21:44
namespace tests;
use IWPNoncesFunctions;
use PHPUnitFrameworkTestCase;;
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');   
}*/
}
  1. 里面

作曲家.json

文件 我有这个

"autoload": {
"psr-4": {
"WPNoncesFunction\": "classes",
"tests\": "tests"
}
},

欢迎来到SO!

更改测试中的使用声明:

use IWPNoncesFunctions;

use IWPNoncesFunctionsIWPNonces;

请务必了解相对路径和绝对路径之间的区别 https://www.php.net/manual/language.namespaces.basics.php

最新更新