Magento 2 API testing
-
14-04-2021 - |
質問
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.
If I use the autoloader.php I am getting the following error:
When I run the following command I am getting this error.
vendor/phpunit/phpunit/phpunit app/code/<Directory>
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.
解決
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
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:
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: