gpt4 book ai didi

unit-testing - CakePHP 在测试用例中使用事务 (getDataSource)

转载 作者:行者123 更新时间:2023-11-28 20:56:24 25 4
gpt4 key购买 nike

在 CakePHP 2.2.5 中有一个组件可以处理我的应用程序的付款。我可以从浏览器/常规 session 在组件内成功运行函数“addPayment”,但是当我在测试用例中运行它时失败。

在组件内使用 $this->controller->Product->getDataSource(); 初始化事务时,出现错误“尝试获取非对象的属性”。

组件中似乎没有设置 $this->controller$this->controller->Product。有什么想法可以修复它,以便 $this->controller->Product->getDataSource(); 工作吗?

app/Test/Case/Controller/Compnent/FinanceComponent:

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));
}
}

app/Controller/Components/FinanceComponent.php:

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();
}

}

最佳答案

CakePHP 2.x 对很多东西都使用“延迟加载”,模型可能因此没有加载:

http://book.cakephp.org/2.0/en/appendices/2-0-migration-guide.html#models

可能值得查看 PaginatorComponent 的 CakePHP 测试用例并查找示例:

https://github.com/cakephp/cakephp/blob/2.2.5/lib/Cake/Test/Case/Controller/Component/PaginatorComponentTest.php#L334

您可能需要手动调用 Controller::constructClasses(); 以正确初始化组件和模型。

关于unit-testing - CakePHP 在测试用例中使用事务 (getDataSource),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15796896/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com