PHPUnit框架保持未找到返回类

问题描述 投票:0回答:1

PHPUnit框架不断返回此消息

tests \ IWPNoncesTest :: testCreateNonce错误:找不到类“ tests \ 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. 然后创建包含主代码的classes文件夹,并在classes文件夹中,我

createIWPNonces.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 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');   
    }*/
}
  1. 里面

composer.json

文件我有这个

"autoload": {
        "psr-4": {
            "WPNoncesFunction\\": "classes",
            "tests\\": "tests"
        }
    },
php phpunit php-7
1个回答
0
投票

欢迎加入!

© www.soinside.com 2019 - 2024. All rights reserved.