使用PHPUnit进行集成测试(检查DB值)

时间:2012-04-09 17:30:05

标签: testing phpunit

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

目前,我设置了一个空数据库,然后为该事件放入应用程序逻辑,然后查看数据库。

有更好的方法吗?维持这似乎很痛苦。

这是一些伪代码:

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
}

}

1 个答案:

答案 0 :(得分:1)

数据库断言的大多数问题都是由Codeception测试框架解决的。 它是PHPUnit之上的一个可靠的包装器。结帐它是Db module

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

唯一的限制是使用DB模块需要以特殊格式编写测试。它简要描述了here