Edit in GitHubLog an issue

Web API Functional Testing

The Web API testing framework allows you to test Web APIs from the client application point of view. The tests can be used with either REST or SOAP. The REST or SOAP adapter that runs the tests is specified in PHPUnit configuration. See How to Run the Tests for more information.

To run Web API tests for GraphQl, see GraphQL functional testing.

Implementation Details#

The Web API functional testing framework depends on the integration testing framework and reuses most of classes implemented there.

Custom Annotations for Data Fixtures#

In the Web API functional tests only, the custom annotation @magentoApiDataFixture is available for declaring fixtures. The difference of this annotation from @magentoDataFixture is that the fixture will be committed and accessible during HTTP requests made within the test body. The usage rules of @magentoApiDataFixture are the same as @magentoDataFixture usage rules.

Do not define fixtures in dev/tests/api-functional. Instead, they must be taken from dev/tests/integration. The integration framework defines most necessary fixtures, and they should be reused during Web API functional testing. If the existing set of fixtures is insufficient, add new fixtures under dev/tests/integration. The fixtures will then be available for both testing frameworks.

To keep your test environment clean, clear all entities created in fixture files or within tests itself from the DB after test execution. This can be done either directly in tearDown or by a corresponding rollback for the fixture file. This file should be named the same as a fixture, but with _rollback suffix.

How to Create a New Test#

All Web API functional tests should inherit from the generic test case Magento\TestFramework\TestCase\WebapiAbstract. It defines the _webApiCall() method, which should be used to perform Web API calls from tests. Clients of _webApiCall() are unaware of which adapter will be used to perform the remote call.

Copied to your clipboard
1namespace Magento\Webapi\Routing;
2
3class CoreRoutingTest extends \Magento\TestFramework\TestCase\WebapiAbstract
4{
5 public function testBasicRoutingExplicitPath()
6 {
7 $itemId = 1;
8 $serviceInfo = [
9 'rest' => [
10 'resourcePath' => '/V1/testmodule1/' . $itemId,
11 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET,
12 ],
13 'soap' => [
14 'service' => 'testModule1AllSoapAndRestV1',
15 'operation' => 'testModule1AllSoapAndRestV1Item',
16 ],
17 ];
18 $requestData = ['itemId' => $itemId];
19 $item = $this->_webApiCall($serviceInfo, $requestData);
20 $this->assertEquals('testProduct1', $item['name'], "Item was retrieved unsuccessfully");
21 }
22}

The test above should be able to test SOAP and REST, depending on what adapter is currently used by the testing framework. The format of $serviceInfo is defined by the Web API client adapter interface:

Copied to your clipboard
1namespace Magento\TestFramework\TestCase\Webapi;
2
3interface AdapterInterface
4{
5 /**
6 * Perform call to the specified service method.
7 *
8 * @param array $serviceInfo <pre>
9 * array(
10 * 'rest' => array(
11 * 'resourcePath' => $resourcePath, // e.g. /products/:id
12 * 'httpMethod' => $httpMethod, // e.g. GET
13 * 'token' => '21hasbtlaqy8t3mj73kjh71cxxkqj4aq' // optional : for token based Authentication. Will
14 * override default OAuth based authentication provided
15 * by test framework
16 * ),
17 * 'soap' => array(
18 * 'service' => $soapService, // soap service name with Version suffix e.g. catalogProductV1, customerV2
19 * 'operation' => $operation // soap operation name e.g. catalogProductCreate
20 * )
21 * );
22 * </pre>
23 * @param array $arguments
24 * @param string|null $storeCode if store code not provided, default store code will be used
25 * @param \Magento\Integration\Model\Integration|null $integration
26 * @return array|string|int|float|bool
27 */
28 public function call($serviceInfo, $arguments = [], $storeCode = null, $integration = null);
29}

How to Run the Tests#

Prerequisites#

  1. Install the PHP Soap extension.

    Copy php_soap.dll or php_soap.so to your PHP extensions directory. Edit your php.ini file and enable the PHP Soap extension. Usually this means deleting the leading semi-colon in front of the extension. Then restart Apache.

    extension=php_soap.dll

  2. Before running the functional tests you need to clear your cache. Now you are ready to run the tests.

Running the Tests#

  1. Copy dev/tests/api-functional/phpunit_rest.xml.dist and phpunit_soap.xml.dist to dev/tests/api-functional/phpunit_rest.xml and phpunit_soap.xml.

  2. Define the Commerce instance URL as a value of TESTS_BASE_URL, Test Webservice User as value of TESTS_WEBSERVICE_USER and Test Webservice API key as value of TESTS_WEBSERVICE_APIKEY in copied file i.e. phpunit_rest.xml or phpunit_soap.xml.

  3. Copy dev/tests/api-functional/config/install-config-mysql.php.dist to dev/tests/api-functional/config/install-config-mysql.php.

  4. Configure your DB connection and install settings in dev/tests/api-functional/config/install-config-mysql.php. Specify the Commerce database. The base URL to access this instance must be the same specified in the phpunit_rest.xml or phpunit_soap.xml file.

  5. Run phpunit using the dev/tests/api-functional/phpunit_rest.xml or dev/tests/api-functional/phpunit_soap.xml configuration file::

    Copied to your clipboard
    vendor/bin/phpunit --configuration <full xml file path>

    or

    Copied to your clipboard
    vendor/bin/phpunit -c <full xml file path>
  • Privacy
  • Terms of Use
  • Do not sell my personal information
  • AdChoices
Copyright © 2022 Adobe. All rights reserved.