我在集成测试中做了一些工作,这是我到目前为止发现的。
基本上,我遵循了与Fooman所说的相似的步骤,但有一些区别,以便使集成测试成为我模块的一部分。
这些是我遵循的步骤:
1-将您的集成测试放在 app/code/Vendor/CustomModule/Test/Integration
2-复制dev/tests/integration/phpunit.dist.xml
到dev/tests/integration/phpunit.xml
并更换
<testsuite name="Magento Integration Tests">
<directory suffix="Test.php">testsuite</directory>
<directory suffix="Test.php">../../../update/dev/tests/integration/testsuite</directory>
<exclude>testsuite/Magento/Test/Integrity</exclude>
<exclude>testsuite/Magento/MemoryUsageTest.php</exclude>
</testsuite>
与
<testsuite name="Magento Integration Tests">
<directory suffix="Test.php">../../../app/code/Vendor/CustomModule/Test/Integration</directory>
</testsuite>
3-然后我使用CLI工具运行它 bin/magento dev:test:run integration
您应该牢记Fooman关于“ TESTS_CLEANUP”的说法,以及如果启用了清除功能,则需要设置集成测试所需的时间。
在这里,我添加了一个功能示例以供进一步参考。您将看到如何访问对象管理器,如何生成Magento类的实例以及如何使用Magento固定装置。
应用/代码/供应商/ CustomModule /控制器/Order/Info.php
namespace Vendor\CustomModule\Controller\Order;
use Magento\Framework\Controller\ResultFactory;
class Info
extends \Magento\Framework\App\Action\Action
{
/**
* @param \Magento\Framework\App\Action\Context $context
* @param \Magento\Sales\Api\OrderRepositoryInterface $orderRepository
*/
public function __construct(
\Magento\Framework\App\Action\Context $context,
\Magento\Sales\Api\OrderRepositoryInterface $orderRepository
)
{
$this->orderRepository = $orderRepository;
parent::__construct($context);
}
/**
* Return Json OrderInfo
*
* @return \Magento\Framework\Controller\Result\Json $this
*/
public function execute()
{
$orderId = $this->getRequest()->getParam('id');
$order = $this->orderRepository->get($orderId);
$orderInfo = [
'total' => $order->getBaseGrandTotal()
];
/** @var \Magento\Framework\Controller\Result\Json $result */
$result = $this->resultFactory->create(ResultFactory::TYPE_JSON);
return $result->setData($orderInfo);
}
}
app / code / Vendor / CustomModule / etc / frontend / routes.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
<router id="standard">
<route id="vendor_custommodule" frontName="vendor_custommodule">
<module name="Vendor_CustomModule"/>
</route>
</router>
</config>
app / code / Vendor / CustomModule / etc / module.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Vendor_CustomModule" setup_version="1.0.0">
<sequence>
<module name="Magento_Sales" />
</sequence>
</module>
</config>
应用/代码/供应商/ CustomModule /测试/集成/控制器/订单/InfoTest.php
namespace Vendor\CustomModule\Controller\Order;
use Magento\TestFramework\TestCase\AbstractController;
class InfoTest extends AbstractController
{
public function getOrderInfoActionDataProvider()
{
return [
'order with one simple item' => [
'incrementId' => '100000001',
'contentType' => 'application/json',
'orderTotal' => 100
]
];
}
/**
* @dataProvider getOrderInfoActionDataProvider
* @magentoDataFixture Magento/Sales/_files/order.php
*/
public function testOrderInfoAction($incrementId, $expectedContentType, $expectedOrderTotal)
{
/** @var $objectManager \Magento\TestFramework\ObjectManager */
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
/** @var \Magento\Sales\Model\OrderFactory $orderFactory */
$orderFactory = $objectManager->get('Magento\Sales\Model\OrderFactory');
$order = $orderFactory->create();
$order->loadByIncrementId($incrementId);
$this->dispatch("vendor_custommodule/order/info/id/{$order->getId()}");
$contentType = $this->getResponse()->getHeader('Content-Type');
$this->assertEquals($expectedContentType, $contentType->getFieldValue());
$responseJson = $this->getResponse()->getBody();
$responseArray = json_decode($responseJson, true);
$this->assertEquals($expectedOrderTotal, $responseArray['total']);
}
}
应用/代码/供应商/CustomModule/registration.php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'Vendor_CustomModule',
__DIR__
);