ProcessLoanApplication

ProcessLoanApplication

本文介绍了如何在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).
  • 还有什么我应该知道的吗?

谢谢!

推荐答案

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

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.

每个单元测试都应该隔离,并且只能测试要测试的类.如果一个对象依赖于另一个对象,则应使用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.

您可以使用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中为此编写测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 10:42