Unit testing 在测试用例中使用事务(getDataSource)的CakePHP
在CakePHP 2.2.5中,有一个组件处理我的应用程序的付款。我可以通过浏览器/常规会话在组件内成功运行函数addPayment,但在测试用例中运行时失败 使用$this->controller->Product->getDataSource初始化事务时,我在尝试获取非对象的属性时出错;在组件内部 组件中似乎没有设置$this->controller或$this->controller->Product。有没有办法修复它,使$this->controller->Product->getDataSource;工作 应用程序/测试/案例/控制器/组件/财务组件: app/Controller/Components/FinanceComponent.php:Unit testing 在测试用例中使用事务(getDataSource)的CakePHP,unit-testing,cakephp,testing,Unit Testing,Cakephp,Testing,在CakePHP 2.2.5中,有一个组件处理我的应用程序的付款。我可以通过浏览器/常规会话在组件内成功运行函数addPayment,但在测试用例中运行时失败 使用$this->controller->Product->getDataSource初始化事务时,我在尝试获取非对象的属性时出错;在组件内部 组件中似乎没有设置$this->controller或$this->controller->Product。有没有办法修复它,使$this->controller->Product->getDat
CakePHP 2.x在很多情况下都使用“延迟加载”,因此可能无法加载模型: 可能值得一看PaginatorComponent的CakePHP测试用例,并寻找示例: 您可能需要手动调用Controller::constructClass;以便正确初始化部件和型号
App::uses('ComponentCollection', 'Controller');
App::uses('Component', 'Controller');
App::uses('FinanceComponent', 'Controller/Component');
class TestFinanceController extends Controller {
public $uses = array('Product');
}
class FinanceComponentTest extends CakeTestCase {
public $Finance = null;
public $Controller = null;
/**
* setUp method
*
* @return void
*/
public function setUp() {
parent::setUp();
$CakeRequest = new CakeRequest();
$CakeResponse = new CakeResponse();
$this->Controller = new TestFinanceController($CakeRequest, $CakeResponse);
$Collection = new ComponentCollection();
$this->Finance = new FinanceComponent($Collection);
$this->Finance->startup($this->Controller);
}
/**
* tearDown method
*
* @return void
*/
public function tearDown() {
unset($this->Finance);
unset($this->Controller);
parent::tearDown();
}
/**
* testAddPayment method
*
* @return void
*/
public function testAddPayment() {
// Get products
$products = $this->Controller->Product->find('all', array(
'limit' => 2,
'offset' => rand(2,6)
));
$this->assertEquals(2, count($products));
// hidden code to shorten example...
// Test buying from main store
$values = array(
// hidden code to shorten example...
);
$payment = call_user_func_array(array($this->Finance, 'addPayment'), array_values($values));
}
}
class FinanceComponent extends Component {
/**
* Controller instance reference
*
* @var object
*/
public $controller;
public $dataSource;
public $mainUserId = 1;
public $mainStoreId = 1;
/**
* Constructor
*/
public function __construct(ComponentCollection $collection, $settings = array()) {
parent::__construct($collection, $settings);
$this->controller = $collection->getController();
}
public function addPayment($amount, $amountTax, $userId, $products, $storeId, $transactionId = null, $affiliateUserId = null) {
// Start Transaction
$this->__transactionStart();
try {
// hidden code to shorten example...
// Commit Transaction
$this->__transactionCommit();
return array(
'status' => 'success'
);
} catch (Exception $e) {
// Rollback Transaction
$this->__transactionRollback();
return array(
'status' => 'error',
'message' => 'Error ' . $e->getLine() . ': ' . $e->getMessage()
);
}
}
protected function __transactionStart() {
$this->dataSource = $this->controller->Product->getDataSource();
$this->dataSource->begin();
}
protected function __transactionRollback() {
$this->dataSource->rollback();
}
protected function __transactionCommit() {
$this->dataSource->commit();
}
}