简体   繁体   English

如何测试在 PHP 中扩展另一个 class 的 class?

[英]How can I test a class that extends another class in PHP?

I am new to PHPUnit, and I am currently trying to test a function that authenticates a user on the system.我是 PHPUnit 的新手,我目前正在尝试测试一个对系统上的用户进行身份验证的 function。 The function is called authenticate() and is located inside the Account class. function 称为authenticate() ,位于Account class 内。

Test测试

public function passwordAreNotTheSame_Test()
{
    require 'login/app/models/Account.php';
    require 'login/app/controllers/Pages.php';
    require 'login/lib/Controller.php';

    $pages = new Pages();
    $account = new Account($pages);
    $username = "test_name";
    $password = "test_password";
    $cpassword = "invalid_password";
    $email = "email@test.com";
    $Expected = "Passwords do not match!";
    $Received = $account->register($username, $password, $cpassword, $email);
    
    $this->assertEquals($Expected, $Received);
}

However, my issue is that the constructor in the Account class also requires a controller called Pages .但是,我的问题是Account class 中的构造函数还需要一个名为Pages的 controller 。 My issue is that the class Pages, located in the file Pages.php extends the class Controller .我的问题是位于文件Pages.php中的 class Pages,扩展了 class Controller . Like this:像这样:

class Pages extends Controller {
    // some code
}

So when I run my test, I get the following issue.因此,当我运行测试时,我遇到了以下问题。

Error: Class "Controller" not found C:\xampp\htdocs\apex-management\login\app\controllers\Pages.php:3 C:\xampp\htdocs\apex-management\tests\Unit\RegisterAccountTests.php:13 Time: 00:00.019, Memory: 8.00 MB Error: Class "Controller" not found C:\xampp\htdocs\apex-management\login\app\controllers\Pages.php:3 C:\xampp\htdocs\apex-management\tests\Unit\RegisterAccountTests.php:13时间:00:00.019,Memory:8.00 MB

ERRORS: Tests, 1: Assertions, 0: Errors.错误:测试,1:断言,0:错误。 1. Process finished with exit code 2 1. 进程以退出代码 2 结束

How could I make this test work?我怎样才能使这个测试工作? I want to learn how to use PHPUnit, but I've found no documentation online for that type of issue.我想学习如何使用 PHPUnit,但我没有找到有关此类问题的在线文档。

In php oop, it is supporing 'extends' keyword.在 php oop 中,它支持“扩展”关键字。 Here is the example for that这是一个例子

<?php
class parent{
   //parent class definition
}

class child extends parent{
   //child class declarations
}

?> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM