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

标签 unit-testing cakephp testing

在 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/

相关文章:

oracle - Oracle PL/SQL测试如何实现测试隔离?

unit-testing - node.js:以任何方式导出文件中的所有函数(例如,启用单元测试),而不是一个一个

php - CakePHP 蛋糕 shell 错误 "A Notice: Uninitialized string offset: 0 in"

电子邮件上的 Cakephp 2.0 SMTP 设置不起作用

testing - TDD/BDD 积极影响统计

testing - 如何使用 saucelabs 隧道运行 testcafe

java - 如何在 Java/Groovy 中测试 MongoDB 过滤器 (BSON) 的相等性?

angular - 如何在 Angular 5 的测试主机中访问指令的属性?

php - PHPSpec 中的单元测试 UploadedFile.php

php - 为 10000 多条记录分配随机顺序