1

I have created an API and I want to write test files for that API. How can it be done in Magento 2.3.5? I have referred the link - https://devdocs.magento.com/guides/v2.4/get-started/web-api-functional-testing.html and also saw the video by a famous Russian Magento Developer. I am unable to run the tests and this is the error I am getting if I run the test.

The command I ran from my Magento root directory is

vendor/bin/phpunit --config dev/tests/api-functional/phpunit_rest.xml

The error is:

Could not read "dev/tests/api-functional/phpunit_rest.xml".

Also, in PHPStorm, I am unable to configure the Unit Test version. I am getting this error.

phpunit.phar error

If I use the autoloader.php I am getting the following error:

Composer Auto Loader error

When I run the following command I am getting this error.

vendor/phpunit/phpunit/phpunit app/code/<Directory>

Command Error

I need help in setting up the API testing in PHPStorm and the correct working code to test a POST API

Any help is highly appreciated.

1
  • If you just want to test api, use postman application to test api much easily. – Hamendra Sunthwal Aug 29 '20 at 5:31
1
+50

Step 1: Verifiy PHP SOAP extension is installed —> at the top of index.php that is used to serve the Magento site, replace the first line <?php by <?php phpinfo(); die(); And refresh the site, check PHH_SOAP extension appears as per the screenshot below

enter image description here

Step 2: as per devdocs documentation 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.

Step 3: create an API user Go to System/Integration, add an API user and assign all the roles (at least initially until the first call is made)

Validate the above steps by running a default Magento API call in Postman for isntance. Or using a php script like the one below (the bearer line has to be adjusted with your token access key)

<?php

// \Magento\Webapi\Model\ServiceMetadata::getServicesConfig to debug the available webapi services
$params = ['order_id' => null];
foreach ($argv as $arg) {
    if (preg_match("%^--(.*?)=(.*?)$%", $arg, $m)) {
        $params[$m[1]] = $m[2];
    }
}

if (!$params['order_id'])
    exit("Specify order id (as --order_id=_ID_ parameter)\n");

$orderId   = (int)$params['order_id'];
$url = 'http://magecertif.test/index.php/rest/V1/orders/' . $orderId;

$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
        'Content-Type: application/json',
        'Authorization: Bearer 75koeeydi18xzclhmdib8ou65h9qk8cx' // this is the access token for the API user
    )
);

//curl_setopt($ch,CURLOPT_POSTFIELDS, $str);

$result = curl_exec($ch);
curl_close($ch);

the result of this script should be like the screenshot below:

enter image description here

Step 4:

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

replace your database details and host with your system details.

Step 5: Finally to setup PHPStorm, if your xdebug setup works for general web page browsing, then the command-line for the webapi key should be working:

for me, I had the same error as you initially. I have to change the command-line

php vendor/bin/phpunit --config dev/tests/api-functional/phpunit_rest.xml

with

php vendor/bin/phpunit --config /data/macbook/mage-cert2/dev/tests/api-functional/phpunit_rest.xml

And then, my API ran eventually, see result in screenshot below: enter image description here

enter image description here

6
  • Hi Thanks for the reply. I did everything as you instructed. When I click on the run icon, I got the same error that said that the PHPUNIT version is not specified. But the console provided a command, so I copied that and replaced the phpunit with the one given by Magento. – Mathanagopal S Aug 31 '20 at 8:27
  • The test ran successfully, and I was even able to fwrite the response. Kudos to that, but it would be helpful if you know how to clear the PHPUNIT version not found error in the PHPSTORM. – Mathanagopal S Aug 31 '20 at 8:28
  • We need to have some steps: you accept the answer so that we are all clear with the question as it was when I answered it. Then I can help giving you pointers on this part after this – Herve Tribouilloy Aug 31 '20 at 9:13
  • Answer Accepted. – Mathanagopal S Aug 31 '20 at 9:53
  • My answer has a different path for phpunit than in your question. Also, my path matches devdocs path. So why can’t you use same path as devdocs? And what version of Magento do you use? – Herve Tribouilloy Aug 31 '20 at 10:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.