Web API 功能测试
Web API 功能测试
Web API测试框架允许你的API的客户端应用Magento Web测试。该测试可用于REST或SOAP. REST或SOAP适配器运行试验是PHPUnit配置指定的。查看如何运行测试 更多信息。
实施细则
Web API功能测试框架取决于集成测试框架和重用类的实现。
如何创建新的测试
<?php
namespace Magento\Webapi\Routing;
class CoreRoutingTest extends \Magento\TestFramework\TestCase\WebapiAbstract
{
    public function testBasicRoutingExplicitPath()
    {
        $itemId = 1;
        $serviceInfo = [
            'rest' => [
                'resourcePath' => '/V1/testmodule1/' . $itemId,
                'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET,
            ],
            'soap' => [
                'service' => 'testModule1AllSoapAndRestV1',
                'operation' => 'testModule1AllSoapAndRestV1Item',
            ],
        ];
        $requestData = ['itemId' => $itemId];
        $item = $this->_webApiCall($serviceInfo, $requestData);
        $this->assertEquals('testProduct1', $item['name'], "Item was retrieved unsuccessfully");
    }
} ?>
          <?php
namespace Magento\TestFramework\TestCase\Webapi;
interface AdapterInterface
{
    /**
     * Perform call to the specified service method.
     *
     * @param array $serviceInfo <pre>
     * array(
     *     'rest' => array(
     *         'resourcePath' => $resourcePath, // e.g. /products/:id
     *         'httpMethod' => $httpMethod,     // e.g. GET
     *         'token' => '21hasbtlaqy8t3mj73kjh71cxxkqj4aq'    // optional : for token based Authentication. Will
     *                                                             override default Oauth based authentication provided
     *                                                             by test framework
     *     ),
     *     'soap' => array(
     *         'service' => $soapService,    // soap service name with Version suffix e.g. catalogProductV1, customerV2
     *         'operation' => $operation     // soap operation name e.g. catalogProductCreate
     *     )
     * );
     * </pre>
     * @param array $arguments
     * @param string|null $storeCode if store code not provided, default store code will be used
     * @param \Magento\Integration\Model\Integration|null $integration
     * @return array|string|int|float|bool
     */
    public function call($serviceInfo, $arguments = [], $storeCode = null, $integration = null);
}
 ?>
          如何运行测试
先决条件
- 
              
安装PHP SOAP扩展。
复制
php_soap.dll或php_soap.so到你的PHP扩展目录。编辑php.ini文件和应用PHP Soap 扩展。extension=php_soap.dll - 
              
在运行功能测试之前,您需要清除缓存。 现在你已经准备好运行测试。
 
运行测试
- 
              
复制
/dev/tests/api-functional/phpunit.xml.dist到/dev/tests/api-functional/phpunit.xmla.指定您的Magento实例的URL作为一个值TESTS_BASE_URL在phpunit.xml。 b.选择所需的web API适配器,rest或soap, 被使用并指定在TESTS_WEB_API_ADAPTER. - 
              
复制
/dev/tests/api-functional/config/install-config-mysql.php.dist到/dev/tests/api-functional/config/install-config-mysql.php. - 
              
配置您的DB连接和安装设置
/dev/tests/api-functional/config/install-config-mysql.php. 指定的Magento数据库。基URL来访问此Magento的实例必须指定在phpunit.xml. - 
              
运行
phpunit使用/dev/tests/api-functional/phpunit.xml配置文件。