1

我正在尝试对带有一些遗留代码的应用程序进行一些集成测试。我要测试的是在应用程序中发生的某些事件链(由用户在前端触发,例如下订单、支付订单)之后,存在某些数据库条目。

目前,我建立了一个空数据库,然后为事件放入应用程序逻辑,然后查看 db.

有一个更好的方法吗?维护起来似乎很痛苦。

这是一些伪代码:

class someTests extends PHPUnit_Framework_TestCase {
function setupScenario1(){
    //create a new database, put in initial values
}

//test that an order is created properly
function testScenario1(){
    $this->setupScenario1();
    $this->loadOtherConfigs();

    //now do some application logic
    //setup an object to pass into a factory
    $OrderInfo->ID = 5;
    $OrderInfo->ProductID = 1;

    $Order = new OrderFactory->Create($OrderInfo);
    $PostOrderProcessor = new $PostOrderProcessor();
    $PostOrderProcessor->Process($Order);

    //assert that certain database entries were made
}

//test that an order is created properly, and then the payment is processed properly
function testScenario12(){
    $this->testScenario1();

    //hard coded from testScenario1
    $OrderID = 5;

    //setup payment stuff
    $PaymentInfoObj->Amount = 500;
    $PaymentInfoObj->Type = PaymentType::CREDIT_CARD;

    //now do some application logic
    $MockPaymentResult->Status = PaymentResult::APPROVED;
    $MockPaymentProcessor = $this->getMockBuilder('PaymentProcessor')->disableOriginalConstructor()->getMock();
    $MockPaymentProcessor->expects($this->once())
                        ->method('process')
                        ->will($this->returnValue($MockPaymentResult));

    $PaymentProcessingWrapper = new PaymentProcessingWrapper($AppraisalsDAO,$MockPaymentProcessor);
    $Result = $PaymentProcessingWrapper->TryToProcessPayment($OrderID,$PaymentInfoObj);

    //assert that certain database entries were made
}

}

4

1 回答 1

1

Codeception测试框架解决了大多数关于数据库断言的问题。它是 PHPUnit 之上的一个可靠的包装器。签出它的Db 模块

它还与流行的 ORM 和不同的 DBMS 集成。

唯一的限制是使用 DB 模块需要以特殊格式编写测试。这里简要介绍一下。

于 2012-04-10T21:44:03.617 回答