olmer/magento-unit-tests-generator
Composer 安装命令:
composer require olmer/magento-unit-tests-generator
包简介
Unit Tests Generator
README 文档
README
Sometimes writing new unit test for class with multiple dependencies may be tedious, so this package is intended to simplify magento2 unit tests creation. Command reads source file and generates basic unit test structure for specified class. If unit test already exists - nothing will happen. Test class is placed into test object class' module under app/code/Vendor/Module/Test/Unit/...
How to install
composer require olmer/magento-unit-tests-generator --dev php bin/magento cache:clean php bin/magento setup:di:compile
How to generate a unit test for a specific class
php bin/magento dev:tests:generate-unit /app/code/Vendor/Module/path/to/file.php
Examples
Source class
<?php declare(strict_types=1); namespace Vendor\Reorder\Helper; class Reorder { public function __construct( Context $context, OrderRepositoryInterface $orderRepository, SearchCriteriaFactory $criteria, FilterGroupFactory $filterGroup, FilterFactory $filter, OrderFactory $orderFactory, SortOrderFactory $sortOrderFactory ) { $this->orderRepo = $orderRepository; $this->searchCriteriaFactory = $criteria; $this->filterGroupFactory = $filterGroup; $this->filterFactory = $filter; $this->orderFactory = $orderFactory; $this->sortOrderFactory = $sortOrderFactory; } public function getFilename() { ... } public function getLastShippedOrder() { ... } }
Generated test class
<?php namespace Vendor\Reorder\Test\Unit\Helper; use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; use PHPUnit\Framework\TestCase; use PHPUnit\Framework\MockObject\MockObject; /** * @covers \Vendor\Reorder\Helper\Reorder */ class ReorderTest extends TestCase { /** * Mock context * * @var \Magento\Framework\App\Helper\Context|PHPUnit\Framework\MockObject\MockObject */ private $context; /** * Mock orderRepository * * @var \Magento\Sales\Api\OrderRepositoryInterface|PHPUnit\Framework\MockObject\MockObject */ private $orderRepository; /** * Mock criteria * * @var \Magento\Framework\Api\SearchCriteriaFactory|PHPUnit\Framework\MockObject\MockObject */ private $criteria; /** * Mock filterGroup * * @var \Magento\Framework\Api\Search\FilterGroupFactory|PHPUnit\Framework\MockObject\MockObject */ private $filterGroup; /** * Mock filter * * @var \Magento\Framework\Api\FilterFactory|PHPUnit\Framework\MockObject\MockObject */ private $filter; /** * Mock orderFactory * * @var \Magento\Sales\Model\OrderFactory|PHPUnit\Framework\MockObject\MockObject */ private $orderFactory; /** * Mock sortOrderFactory * * @var \Magento\Framework\Api\SortOrderFactory|PHPUnit\Framework\MockObject\MockObject */ private $sortOrderFactory; /** * Object Manager instance * * @var \Magento\Framework\ObjectManagerInterface */ private $objectManager; /** * Object to test * * @var \Vendor\Reorder\Helper\Reorder */ private $testObject; /** * Main set up method */ public function setUp() : void { $this->objectManager = new ObjectManager($this); $this->context = $this->createMock(\Magento\Framework\App\Helper\Context::class); $this->orderRepository = $this->createMock(\Magento\Sales\Api\OrderRepositoryInterface::class); $this->criteria = $this->createMock(\Magento\Framework\Api\SearchCriteriaFactory::class); $this->filterGroup = $this->createMock(\Magento\Framework\Api\Search\FilterGroupFactory::class); $this->filter = $this->createMock(\Magento\Framework\Api\FilterFactory::class); $this->orderFactory = $this->createMock(\Magento\Sales\Model\OrderFactory::class); $this->sortOrderFactory = $this->createMock(\Magento\Framework\Api\SortOrderFactory::class); $this->testObject = $this->objectManager->getObject( \Vendor\Reorder\Helper\Reorder::class, [ 'context' => $this->context, 'orderRepository' => $this->orderRepository, 'criteria' => $this->criteria, 'filterGroup' => $this->filterGroup, 'filter' => $this->filter, 'orderFactory' => $this->orderFactory, 'sortOrderFactory' => $this->sortOrderFactory, ] ); } /** * @return array */ public function dataProviderForTestGetLastShippedOrder() { return [ 'Testcase 1' => [ 'prerequisites' => ['param' => 1], 'expectedResult' => ['param' => 1] ] ]; } /** * @dataProvider dataProviderForTestGetLastShippedOrder */ public function testGetLastShippedOrder(array $prerequisites, array $expectedResult) { $this->assertEquals($expectedResult['param'], $prerequisites['param']); } /** * @return array */ public function dataProviderForTestIsModuleOutputEnabled() { return [ 'Testcase 1' => [ 'prerequisites' => ['param' => 1], 'expectedResult' => ['param' => 1] ] ]; } /** * @dataProvider dataProviderForTestIsModuleOutputEnabled */ public function testIsModuleOutputEnabled(array $prerequisites, array $expectedResult) { $this->assertEquals($expectedResult['param'], $prerequisites['param']); } }
olmer/magento-unit-tests-generator 适用场景与选型建议
olmer/magento-unit-tests-generator 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 64.9k 次下载、GitHub Stars 达 47, 最近一次更新时间为 2018 年 06 月 29 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 olmer/magento-unit-tests-generator 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 olmer/magento-unit-tests-generator 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 64.9k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 48
- 点击次数: 5
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2018-06-29