需要编写 php 测试文件来测试我的 PHP 控制器类的帮助

发布于 2024-08-21 09:57:02 字数 1605 浏览 9 评论 0原文

跟随我之前的网站开发,我们构建了几个与我们网站开发中将出现的实体相对应的控制器类。我们的网站非常简单:只是一个购物网站,供客户预订旅行巴士。

现在我被分配了一个具有挑战性的任务需要完成,那就是,对于单个控制器类,我需要编写一个测试用例来测试它是否正常工作。

例如,我们有一个名为“JobsController”的控制器类,如下所示:

models = array( 'jobs' => new Jobs() ); } function __destruct() { parent::__destruct(); } function addJob( $name , $desc ) { if( $name == '' ) { return false; }; $params = array( 'name' => $name , 'description' => $desc ); return $this->models['jobs']->add( $params ); } function modifyJob( $jid , $name , $desc ) { if( $jid == '' || $name == '' ) { return false; }; $params = array( 'name' => $name , 'description' => $desc ); return $this->models['jobs']->modify( $jid , $params ); } function removeJob( $jid ) { if( $jid == '' ) { return false; }; return $this->models['jobs']->remove( $jid ); } function getJob( $jid ) { return $this->models['jobs']->getInfo( $jid ); } function getAllJobs() { return $this->models['jobs']->getAll(); } } ?>

和所有其他控制器类几乎相同,只是名称更改。

现在我真的需要你们帮助我如何编写一个简单的 php 文件来测试这个控制器类是否正常工作。在研究了你们的代码之后,我可以亲自使用它来与其余控制器一起使用。

非常感谢!

followed by my previous website development,we have built several controller classs corresponding to entities that will appear in our website development.and our website is very simple:just a shopping website for customers to book bus for travel.

Now I am assigned a challenging task that need to be done,that is ,for a single controller class,I need to write a test case to test whether it is working properly.

For example,we have a controller class called "JobsController",like:

models = array( 'jobs' => new Jobs() );
}

function __destruct()
{
parent::__destruct();
}

function addJob( $name , $desc )
{
if( $name == '' )
{
return false;
};

$params = array( 'name' => $name ,
'description' => $desc );

return $this->models['jobs']->add( $params );
}

function modifyJob( $jid , $name , $desc )
{
if( $jid == '' || $name == '' )
{
return false;
};

$params = array( 'name' => $name ,
'description' => $desc );

return $this->models['jobs']->modify( $jid , $params );
}

function removeJob( $jid )
{
if( $jid == '' )
{
return false;
};

return $this->models['jobs']->remove( $jid );
}

function getJob( $jid )
{
return $this->models['jobs']->getInfo( $jid );
}

function getAllJobs()
{
return $this->models['jobs']->getAll();
}

}

?>

and all other Controller class are almost the same,just with name changes.

Now I really need you guys help me a bit how on to write a simple php file to test whether this Controller Class is working properly.After studying your code,I could get my hands on it to work with the rest controllers myself.

Many thank!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

固执像三岁 2024-08-28 09:57:02

在编写测试用例时,您基本上是在测试之前和之后的场景。在运行测试之前,您需要初始化一个干净的环境(每次)以确保您知道会发生什么。接下来,您将运行您的方法并测试之后的环境,确认您采取的操作是否达到了预期的效果。

例如,要测试您的modifyJob方法,您想要创建一个包含作业的环境,请使用测试值调用modifyJob方法,然后对同一作业调用getJob,并确保返回值的值与您传递给modifyJob的值匹配:

function testModifyJob() {
  // create clean, known environment
  $controller = new JobsController();
  $job_id = $controller->addJob('name', 'description');

  // run the action
  $controller->modifyJob($job_id, 'new name', 'new description');

  //test the results
  $job = $controller->getJob($job_id);
  $result = $job['name'] == 'new name' && $job['description'] == 'new description';

  return $result;
}

对于测试您的控制器时,您需要为该类提供的每种方法编写类似上面的测试用例,以确保该对象在您的系统中是可信的。

在编写测试用例时,您很可能需要在控制器上为您的测试创建新方法,请继续创建这些方法(如果您的分配允许)。测试用例提出这一点是很常见的,并且通常通过揭示这些细微差别来帮助完善类的可用功能(这是测试驱动开发拥有如此强大追随者的原因之一)。一个例子是创建一个 hasJob($job_id) 方法,该方法将测试您的作业集合是否包含特定作业,从而允许您测试诸如 deleteJob() 之类的函数。

When writing test cases you are basically testing a before and after scenario. Before the test is run you want to initialize a clean environment (every time) to ensure you know what to expect. Next you'll run your methods and test what the environment is like afterward, confirming whether or not the action you took had the desired affect.

For example to test your modifyJob method you want to create an environment with a job, call the modifyJob method with test values, and then call getJob on the same job and ensure that the return value has values matching what you passed in to modifyJob:

function testModifyJob() {
  // create clean, known environment
  $controller = new JobsController();
  $job_id = $controller->addJob('name', 'description');

  // run the action
  $controller->modifyJob($job_id, 'new name', 'new description');

  //test the results
  $job = $controller->getJob($job_id);
  $result = $job['name'] == 'new name' && $job['description'] == 'new description';

  return $result;
}

For testing your controller you're going to want to write test cases like the one above for each of the methods the class provides to ensure that the object can be trusted within your system.

When writing your test cases you'll most likely have a need to create new methods on your controller just for your tests, go ahead and create these (if your assignment permits). It's very common for test cases to bring this up and usually helps round out the available functionality of your classes by bringing these nuances to light (one of the reasons test-driven development has such a strong following). An example of this would be creating a hasJob($job_id) method that would test if your jobs collection contains a specific job, allowing you to test functions such as deleteJob().

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文