- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在 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 测试用例并查找示例:
您可能需要手动调用 Controller::constructClasses();
以正确初始化组件和模型。
关于unit-testing - CakePHP 在测试用例中使用事务 (getDataSource),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15796896/
我最近购买了《C 编程语言》并尝试了 Ex 1-8这是代码 #include #include #include /* * */ int main() { int nl,nt,nb;
早上好!我有一个变量“var”,可能为 0。我检查该变量是否为空,如果不是,我将该变量保存在 php session 中,然后调用另一个页面。在这个新页面中,我检查我创建的 session 是否为空,
我正在努力完成 Learn Python the Hard Way ex.25,但我无法理解某些事情。这是脚本: def break_words(stuff): """this functio
我是一名优秀的程序员,十分优秀!