Magento 2 API 테스트

Aug 25 2020

API를 만들었고 해당 API에 대한 테스트 파일을 작성하고 싶습니다. Magento 2.3.5에서 어떻게 할 수 있습니까? 나는 링크를 참조했다-https://devdocs.magento.com/guides/v2.4/get-started/web-api-functional-testing.html유명한 러시아 Magento 개발자의 비디오도 보았습니다. 테스트를 실행할 수 없으며 테스트를 실행하면 오류가 발생합니다.

내 Magento 루트 디렉터리에서 실행 한 명령은 다음과 같습니다.

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

오류는 다음과 같습니다.

"dev / tests / api-functional / phpunit_rest.xml"을 읽을 수 없습니다.

또한 PHPStorm에서 단위 테스트 버전을 구성 할 수 없습니다. 이 오류가 발생합니다.

autoloader.php를 사용하면 다음 오류가 발생합니다.

다음 명령을 실행할 때이 오류가 발생합니다.

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

PHPStorm에서 API 테스트를 설정하는 데 도움이 필요하고 POST API를 테스트하기위한 올바른 작업 코드가 필요합니다.

어떤 도움이라도 대단히 감사합니다.

답변

HerveTribouilloy Aug 30 2020 at 18:27

1 단계 : Verifiy PHP SOAP 확장이 설치되어 있습니다. —> Magento 사이트를 제공하는 데 사용되는 index.php의 맨 위에 첫 번째 줄을 다음 <?php으로 교체 <?php phpinfo(); die();하고 사이트를 새로 고침하고 PHH_SOAP 확장이 아래 스크린 샷과 같이 나타나는지 확인합니다.

2 단계 : devdocs 문서에 따라 /dev/tests/api-functional/phpunit_rest.xml.dist 및 phpunit_soap.xml.dist를 /dev/tests/api-functional/phpunit_rest.xml 및 phpunit_soap.xml에 복사합니다.

3 단계 : API 사용자 만들기로 이동하여 API 사용자를 System/Integration추가하고 모든 역할을 할당합니다 (적어도 처음에는 첫 번째 호출이 이루어질 때까지).

isntance를 위해 Postman에서 기본 Magento API 호출을 실행하여 위의 단계를 검증하십시오. 또는 아래와 같은 PHP 스크립트를 사용합니다 (베어러 라인은로 조정해야합니다 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);

이 스크립트의 결과는 아래 스크린 샷과 같아야합니다.

4 단계 :

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

데이터베이스 세부 정보 및 호스트를 시스템 세부 정보로 바꿉니다.

5 단계 : 마지막으로 PHPStorm을 설정하기 위해 xdebug 설정이 일반 웹 페이지 탐색에 작동하는 경우 webapi 키에 대한 명령 줄이 작동해야합니다.

저에게는 처음에 당신과 같은 오류가있었습니다. 명령 줄을 변경해야합니다

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

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

그런 다음 내 API가 결국 실행되었으며 아래 스크린 샷에서 결과를 확인하십시오.