In Magento 2, the Web API testing allows us to test Magento 2 Web API from the client application perspective. Magento Web API framework provides integrators, and lets developers utilize web services that communicate with the Magento system. The Web API tests can be utilized with either REST or SOAP. The adapter of REST or SOAP runs those tests specified in the configuration of  PHPUnit.

This article is basedon Web API functional testing, the Web API functional testing depends on the testing framework and reuses of classes implemented there.

Let’s start with Web API functional testing!

Create a New Test

The web API functional test should inherit from the generic test case caseMagento/TestFramework/TestCase/WebapiAbstract. This defines the _webApiCall() method, which should be utilized to perform Web API calls from tests. Clients of _webApiCall() are ignorant of which adapter will be utilized to perform the remote call.

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’], “retrieved unsuccessfully”);
} ?>

The above test is able to test SOAP and REST API depending on adapter which is currently used by the testing framework.


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);

In above test $serviceInfo format is defined by Web API client adapter interface.

Run the Tests

Before starting, make sure you fulfil all the requirements mentioned below:

  1. PHP Soap extension has installed. Copy php_soap.dll file to your PHP extensions directory and then edit your php.ini file and enable the PHP extension by deleting the leading semicolon in front of extension. And then restart your Apache service.
  1. Before running the functional tests, make sure to clear your Magento 2 cache.

Now you are ready to run the tests.

Go to your Magento 2 root directory and copy /dev/tests/api-functional/phpunit.xml.dist to /dev/tests/api-functional/phpunit.xml.

  • Specify your Magento 2 instance URL in phpunit file as a value of Tests_Base_URL
  • You must choose the required API adapter, rest or soap for use and specify it in TESTS_WEB_API_ADAPTER

Then, Copy /dev/tests/api-functional/config/install-config-mysql.php.dist to /dev/tests/api-functional/config/install-config-mysql.php. Configure your Database connection and install setting in your /dev/tests/api-functional/config/install-config-mysql.php file.

Specify the Magento 2 database is in install-config-mysql.php file. Base URL to access this Magento 2 instance must be defined in phpunit.xml file.

And after this, the last step you need to do is run phpunit using the /dev/tests/api-functional/phpunit.xml configuration file.

Conclusion: So now we’re done with Web API functional testing in Magento 2. By following this article, you may test your Magento 2 framework and reuses of classes. If you faced any problem, feel free leave your comments in below comment section.


Why Magestore? We believe in building a meaningful & long-term relationship with you.

1 Comment

  1. David Stephan Reply

    Hi! Thanks for this information. I am a Magento Freelancer. This is a great help. Hope you will come up with such great code helps. Thanks again!!

Write A Comment