ecomdev / magento2-test-essentials
Magento 2 测试基础
dev-main
2021-06-22 19:53 UTC
Requires
- php: ~7.2|~8.0
- magento/framework: ~102.0|~103.0
- magento/module-catalog: ~103.0|~104.0
- magento/module-customer: ~102.0|~103.0
- magento/module-quote: ~101.0
- magento/module-store: ~101.0
Requires (Dev)
- phpunit/phpunit: ~9.5
- squizlabs/php_codesniffer: ^3.6.0
This package is auto-updated.
Last update: 2024-09-22 04:17:39 UTC
README
使用模拟框架进行测试对 Magento 2 模块的测试是无效的,因为你一行一行地复制了实际的调用到 Magento 实现。
将测试绑定到实现的细节会导致非常脆弱的测试套件,没有人愿意在之后使用它,因为底层代码的微小变化,如类的提取,就需要完全重写测试用例。
本软件包通过提供与核心交互的常见操作所需的 模拟对象
来解决这个问题。
除了模拟对象集合之外,还有一个 ObjectManagerInterface
实现可以自动实例化依赖关系,如果你使用 create
方法,并且允许指定用于创建深层依赖关系的自定义工厂。
每个模拟对象都由自动化测试覆盖,以确保行为正确,你的测试可以通过使用不同的场景来测试你的代码特定的行为。
安装
composer require --dev ecomdev/magento2-test-essentials
示例
测试尝试解决默认商店的功能
use EcomDev\Magento2TestEssentials\ObjectManager; use EcomDev\Magento2TestEssentials\Store\StoreManager; use EcomDev\Magento2TestEssentials\Store\Store; use Magento\Store\Model\StoreManagerInterface; use Magento\Catalog\Model\Product; class YourTestedClass { private $storeManager; public function __construct(StoreManagerInterface $storeManager) { $this->storeManager = $storeManager; } public function applyCurrentStoreToProduct(Product $product) { $product->setStoreId($this->storeManager->getStore()->getId()); } } class YourTestedClassTest extends \PHPUnit\Framework\TestCase { /** @test */ public function appliesStoreIdToProduct() { $applier = new YourTestedClass( StoreManager::new() ->withStore(Store::new(3, 'store_three')) ->setCurrentStore('store_three') ); // Creates product without any constructor but if you rely on data fields, it works great $product = ObjectManager::new()->get(Product::class); $applier->applyCurrentStoreToProduct( $product ); $this->assertEquals(3, $product->getStoreId()); } }
特性
-
ObjectManagerInterface
实现,模仿平台行为 -
StoreInterface
实现作为测试商店相关行为的简单数据对象 -
GroupInterface
实现作为测试商店组相关行为的简单数据对象 -
WebsiteInterface
实现作为测试网站相关行为的简单数据对象 -
ScopeConfigurationInterface
实现用于测试依赖于配置的功能 -
ShoppingCartInterface
实现用于测试购物车相关功能 -
CustomerInterface
实现用于测试客户相关功能 -
Customer/Session
实现用于测试依赖于当前客户数据的功能 -
Checkout/Session
实现用于测试依赖于当前结账数据的功能