如何在Symfony中为此编写测试? [英] How do I write test(s) for this in Symfony?

查看:64
本文介绍了如何在Symfony中为此编写测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将要研究PHP世界中的测试,并且我有一些疑问.我有一个处理贷款申请的控制器.然后将大部分工作委托给ProcessLoanApplication类.

I'm about to delve into test in the PHP world and I have some questions. I have a controller which handles a loan application. The bulk of the work is then delegated to a ProcessLoanApplication class.

ApplyController

class ApplyController extends Controller
{
  public function indexAction(Request $request)
  {
    $form = $this->createForm(new LoanApplication());

    if($request->getMethod() == 'POST') {
        $form->bind($request);

        if($form->isValid()) {

            $session = $this->getRequest()->getSession();

            $loan_app_processor = new Tasks\ProcessLoanApplication($form, $session);
            $loan_app_processor->process();

            return $this->redirect($this->generateUrl('apply_thanks'));
        }
    }

Tasks \ ProcessLoanApplication

class ProcessLoanApplication
{
  private $_QuickBaseModels;
  private $_session;
  private $_app; // submitted form data
  private $_existingApp = false; // holds existing application in QB, if it exists

  public function __construct(Form $form, Session $session)
  {
    $this->_app = $form->getNormData();
    $this->_session = $session;

    // save the form data to a session
    $session->set('application', $this->_app);

    // create the quickbase table models objects
    $this->_QuickBaseModels['GenFnHome\Application'] = new GenFnHome\Application();
    $this->_QuickBaseModels['GenFnHome\SSN'] = new GenFnHome\SSN();
  }

  public function process()
  {
    $this->_existingApp = $this->_getExistingApplication();

    $app_status = $this->_existingApp[GenFnHome\SSN::LogInApplicationStatus];

    if(!$this->_existingApp || ($this->_existingApp && ($app_status !== 'PENDING' && $app_status !== 'OPEN' && $app_status !== 'EXPIRED')))
      return $this->_saveNewLoanApplication();

    if($app_status == 'EXPIRED') $this->_reOpenApplication();
  }

这里有很多事情,所以我先概述一下:

There's a lot going on here, so I will outline it first:

  • 用户对应用程序进行请求
  • 申请表已通过验证
  • 如果有效,请处理贷款申请
  • 检查用户是否已经有应用程序(如果有)-如果没有,则执行X;
  • 该应用程序保留在一个在线数据库"(QuickBase)中,我的应用程序通过HTTP上的XML通过XML与之通信(换句话说,没有真正的数据库)

我对社区的提问:

  • 这里应该测试什么?我知道这在很大程度上取决于我,但是也许社区可以推荐一些应该定义的基准测试.我应该测试控制器,处理器类和QuickBase类吗?
  • 我的测试是否应该相互独立-意思是,我应该分别测试每个组件,而不是拥有一个大型的testApplication来完成indexAction所做的所有工作,并只寻找设置的预期会话变量?
  • 最后,如何测试一个API调用(请求/响应)而不实际发出真正的请求(我正在使用PHPUnit).
  • 还有什么我应该知道的吗?

谢谢!

推荐答案

这里应该测试什么?我知道这很大程度上取决于我,但是也许社区可以推荐一些应该定义的基准测试.我应该测试控制器,处理器类和QuickBase类吗?

What should be tested here? I know it is largely up to me, but perhaps the community can recommend some baseline tests that should def be written. Should I be testing the controller, the processor class, and the QuickBase class?

我建议测试您构建的每个类.如果您使用的是测试驱动开发",则测试将声明您要构建的内容,而无需测试也无需代码.

I recommend to test every class you build. If you are using Test Driven Development, the test declares what you are building, no test no code.

我的测试应该相互独立吗-意思是,我应该分别测试每个组件,而不是拥有一个大型的testApplication来完成indexAction所做的所有工作,并且只查找设置的预期会话变量?

Should my tests be independent of one another - meaning, I should test each component individually, rather than have one massive testApplication that does everything the indexAction does and just looks for the expected sessions vars that get set?

每个单元测试都应该隔离,并且只能测试要测试的类.如果一个对象依赖于另一个对象,则应使用Mock对象(将PHPunit模拟库或其他第三方库用作Mockery).

Every Unit test should be isolated and should only test the Class that you are testing. You should use Mock object (use the PHPunit mock library or other 3th party libraries as Mockery) if one object dependences on another object.

最后,如何测试一个API调用(请求/响应)而不实际发出真正的请求(我正在使用PHPUnit).

Finally, how does one test API calls (request / response) without actually making real request (I'm using PHPUnit).

您可以使用Symfony的WebTestCase提供简单的方法来模仿浏览器请求,并在

You can use the Symfony's WebTestCase that provides easy methods to imitate a browser request, learn more about that in the documentation. We call that Functional Testing.

这通常是单元测试之后的阶段.在单元测试中,您将测试每个单独的类(

This is usually the stage after Unit Testing. In Unit Testing you will test each individual class (it's a good practice to unit test your controller) and after that you write your Functional Tests which combines everything and tests if it works like expected.

这篇关于如何在Symfony中为此编写测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆