beyondit/opencart-test-suite

OpenCart 开发测试套件

安装次数: 19,752

依赖: 2

推荐者: 0

安全: 0

星标: 67

关注者: 17

分支: 36

开放问题: 2

类型:opencart-extension

3.0.0 2017-07-21 16:25 UTC

This package is not auto-updated.

Last update: 2024-09-14 14:26:22 UTC


README

Build Status

OpenCart 测试套件

支持的 OpenCart 版本

动机

本项目的目的是提供一个简单的方法来设置自定义 OpenCart 开发的测试套件。

从头开始

  • 创建一个新的 OpenCart 实例(可能参考这个指南
  • opencart-test-suite 添加为依赖项 composer require beyondit/opencart-test-suite --dev
  • 对于 OpenCart 3.0,使用 composer require beyondit/opencart-test-suite:3.0 --dev
  • 创建一个 tests 文件夹并添加相应的测试(见以下示例)
  • 添加一个 phpunit.xml,其中包括测试套件(例如 admin 和 catalog)并将环境变量设置为 opencart 根目录(见下面的示例 phpunit.xml)
  • 现在可以通过 vendor/bin/phpunit --testsuite catalog-tests 命令运行测试

我们的OpenCart 项目模板可能会简化设置。

phpunit.xml 的示例

<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="vendor/autoload.php">
    <testsuites>
        <testsuite name="catalog-tests">
            <directory suffix="Test.php">./tests/catalog/</directory>
        </testsuite>
        <testsuite name="admin-tests">
            <directory suffix="AdminTest.php">./tests/admin/</directory>
        </testsuite>
    </testsuites>
    <php>
        <env name="OC_ROOT" value="/../opencart/root-folder" />
        <env name="HTTP_SERVER" value="https://:8080/" />
        <env name="TEST_CONFIG" value="test-config" />
    </php>
</phpunit>

测试示例

测试模型

namespace Tests;

class ModelCatalogManufacturerTest extends OpenCartTest
{
    public function testASpecificManufacturer()
    {
        // load the manufacturer model
        $model = $this->loadModel("catalog/manufacturer");
        $manufacturer = $model->getManufacturer(5);

        // test a specific assertion
        $this->assertEquals('HTC', $manufacturer['name']);

    }
}

测试控制器

namespace Tests;

class ControllerCheckoutCartTest extends OpenCartTest
{
    public function testAddingASpecificProductToTheCart()
    {
        $response = $this->dispatchAction('checkout/cart/add','POST',['product_id' => 28]);
        $output = json_decode($response->getOutput(),true);
        
        $this->assertTrue(isset($output['success']) && isset($output['total']));
        $this->assertRegExp('/HTC Touch HD/', $output['success']);
    }
}

测试登录顾客

class ControllerAccountEditTest extends OpenCartTest {  
    public function testEditAccountWithLoggedInCustomer() {

        $this->login('somebody@test.com','password');
        
        $response = $this->dispatchAction('account/edit');
        $this->assertRegExp('/Your Personal Details/',$response->getOutput());
        
        $this->logout();
        
    }   
}

在管理员界面内测试登录用户

为了测试管理员文件夹内的类,只需调用以 AdminTest 结尾的测试类,例如 ModelCatalogCategoryAdminTest

class ControllerCommonDashboardAdminTest extends OpenCartTest {  
    public function testShowDashboardWithLoggedInUser() {

        $this->login('admin','admin');
        
        $response = $this->dispatchAction('common/dashboard');
        $this->assertRegExp('/Total Sales/', $response->getOutput());
        
        $this->logout();
        
    }   
}