tddwizard / magento2-fixtures
用于 Magento 2 集成测试的固定库
Requires
- php: ^7.1|^8.0
- fakerphp/faker: ^1.9.1
- magento/framework: ^102.0|^103.0
- magento/module-catalog: ^103.0|^104.0
- magento/module-catalog-inventory: ^100.3
- magento/module-checkout: ^100.3
- magento/module-customer: ^102.0|^103.0
- magento/module-directory: ^100.3
- magento/module-indexer: ^100.3
- magento/module-payment: ^100.3
- magento/module-quote: ^101.1
- magento/module-sales: ^102.0|^103.0
- magento/zendframework1: ^1.14
Requires (Dev)
- ext-json: *
- magento/module-store: ^101.0
- pds/skeleton: ^1.0
- phpro/grumphp: ^0.19.0
- phpstan/phpstan: ^0.12.0
- phpunit/phpunit: ^6.0|^9.0
- squizlabs/php_codesniffer: ^3.3.1
- dev-master
- v1.1.2
- v1.1.1
- 1.1.0
- 1.1.0-rc1
- v1.0.0
- v0.12.1
- v0.12.0
- v0.11.0
- v0.10.0
- v0.9.0
- v0.8.0
- v0.7.0
- v0.6.0
- v0.5.0
- v0.4.0
- v0.3.1
- v0.3.0
- v0.2.0
- v0.1.1
- v0.1.0
- dev-dependabot/github_actions/dot-github/workflows/actions/download-artifact-4.1.7
- dev-php81
- dev-use-faker-fork
- dev-replace-faker
- dev-multi-source-inventory
- dev-graceful-rollback
- dev-theme-fixture
- dev-fixing-stuff
- dev-config-fixture
- dev-travis
- dev-virtual-products
- dev-analysis
- dev-address-builder-extension-fix-conflicts
- dev-lfolco-backorders
- dev-develop
This package is not auto-updated.
Last update: 2024-09-11 02:31:33 UTC
README
由 Fabian Schmengler 编写的 Magento 2 固定库
这是什么?
是 Magento 2 集成测试中基于过程的脚本固定件的替代品。
它的目标是
- 可扩展的
- 表达性的
- 易于使用
安装
使用 composer 将其安装到您的 Magento 2 项目中
composer require --dev tddwizard/magento2-fixtures
要求
- Magento 2.3 或 Magento 2.4
- PHP 7.3 或 7.4 (7.1 和 7.2 通过 composer 允许用于完整的 Magento 2.3 兼容性,但不再进行测试)
使用示例
客户
如果您需要一个没有特定数据的客户,只需这样做
protected function setUp(): void { $this->customerFixture = new CustomerFixture( CustomerBuilder::aCustomer()->build() ); } protected function tearDown(): void { $this->customerFixture->rollback(); }
它使用默认示例数据和随机电子邮件地址。如果您需要在测试中使用 ID 或电子邮件地址,则 CustomerFixture
可提供访问权限
$this->customerFixture->getId(); $this->customerFixture->getEmail();
您可以使用属性配置构建器
CustomerBuilder::aCustomer() ->withEmail('test@example.com') ->withCustomAttributes( [ 'my_custom_attribute' => 42 ] ) ->build()
您可以为客户添加地址
CustomerBuilder::aCustomer() ->withAddresses( AddressBuilder::anAddress()->asDefaultBilling(), AddressBuilder::anAddress()->asDefaultShipping(), AddressBuilder::anAddress() ) ->build()
或者只添加一个
CustomerBuilder::aCustomer() ->withAddresses( AddressBuilder::anAddress()->asDefaultBilling()->asDefaultShipping() ) ->build()
CustomerFixture
还有一个创建客户会话的快捷方式
$this->customerFixture->login();
地址
类似于客户构建器,您也可以使用自定义属性配置地址构建器
AddressBuilder::anAddress() ->withCountryId('DE') ->withCity('Aachen') ->withPostcode('52078') ->withCustomAttributes( [ 'my_custom_attribute' => 42 ] ) ->asDefaultShipping()
产品
产品固定件与客户固定件类似工作
protected function setUp(): void { $this->productFixture = new ProductFixture( ProductBuilder::aSimpleProduct() ->withPrice(10) ->withCustomAttributes( [ 'my_custom_attribute' => 42 ] ) ->build() ); } protected function tearDown(): void { $this->productFixture->rollback(); }
SKU 是随机生成的,可以通过 ProductFixture
访问,就像 ID 一样
$this->productFixture->getSku(); $this->productFixture->getId();
购物车/结账
要创建报价,请使用与产品固定件一起的 CartBuilder
$cart = CartBuilder::forCurrentSession() ->withSimpleProduct( $productFixture1->getSku() ) ->withSimpleProduct( $productFixture2->getSku(), 10 // optional qty parameter ) ->build() $quote = $cart->getQuote();
结账支持已登录客户。要创建订单,您可以对具有默认的收货和账单地址的客户固定件以及产品固定件进行以下结账模拟:
$customerFixture = new CustomerFixture(CustomerBuilder::aCustomer()->withAddresses( AddressBuilder::anAddress()->asDefaultBilling(), AddressBuilder::anAddress()->asDefaultShipping() )->build()); $customerFixture->login(); $checkout = CustomerCheckout::fromCart( CartBuilder::forCurrentSession() ->withProductRequest(ProductBuilder::aVirtualProduct()->build()->getSku()) ->build() ); $order = $checkout->placeOrder();
它将尝试选择默认地址以及第一个可用的运货和支付方式。
您也可以明确选择它们
$order = $checkout ->withShippingMethodCode('freeshipping_freeshipping') ->withPaymentMethodCode('checkmo') ->withCustomerBillingAddressId($this->customerFixture->getOtherAddressId()) ->withCustomerShippingAddressId($this->customerFixture->getOtherAddressId()) ->placeOrder();
订单
OrderBuilder
是结账模拟的快捷方式。
$order = OrderBuilder::anOrder()->build();
已登录客户、产品和购物车商品数量将内部生成,除非需要更多控制。
$order = OrderBuilder::anOrder() ->withProducts( // prepare catalog product fixtures ProductBuilder::aSimpleProduct()->withSku('foo'), ProductBuilder::aSimpleProduct()->withSku('bar') )->withCart( // define cart item quantities CartBuilder::forCurrentSession()->withSimpleProduct('foo', 2)->withSimpleProduct('bar', 3) )->build();
发货
可以完全或部分发货订单,可选地附带跟踪信息。
$order = OrderBuilder::anOrder()->build(); // ship everything $shipment = ShipmentBuilder::forOrder($order)->build(); // ship only given order items, add tracks $shipment = ShipmentBuilder::forOrder($order) ->withItem($fooItemId, $fooQtyToShip) ->withItem($barItemId, $barQtyToShip) ->withTrackingNumbers('123-FOO', '456-BAR') ->build();
发票
可以完全或部分开具发票。
$order = OrderBuilder::anOrder()->build(); // invoice everything $invoice = InvoiceBuilder::forOrder($order)->build(); // invoice only given order items $invoice = InvoiceBuilder::forOrder($order) ->withItem($fooItemId, $fooQtyToInvoice) ->withItem($barItemId, $barQtyToInvoice) ->build();
信用备忘录
可以为所订的全部或部分商品创建信用备忘录。将内部创建用于退款发票。
$order = OrderBuilder::anOrder()->build(); // refund everything $creditmemo = CreditmemoBuilder::forOrder($order)->build(); // refund only given order items $creditmemo = CreditmemoBuilder::forOrder($order) ->withItem($fooItemId, $fooQtyToRefund) ->withItem($barItemId, $barQtyToRefund) ->build();
固定池
要管理多个固定件,为所有实体引入了 固定池
使用 ProductFixturePool
展示了使用示例
protected function setUp()
{
$this->productFixtures = new ProductFixturePool;
}
protected function tearDown()
{
$this->productFixtures->rollback();
}
public function testSomethingWithMultipleProducts()
{
$this->productFixtures->add(ProductBuilder::aSimpleProduct()->build());
$this->productFixtures->add(ProductBuilder::aSimpleProduct()->build(), 'foo');
$this->productFixtures->add(ProductBuilder::aSimpleProduct()->build());
$this->productFixtures->get(); // returns ProductFixture object for last added product
$this->productFixtures->get('foo'); // returns ProductFixture object for product added with specific key 'foo'
$this->productFixtures->get(0); // returns ProductFixture object for first product added without specific key (numeric array index)
}
配置固定件
使用配置固定件可以全局设置配置值,即它将确保它不仅设置在默认作用域中,而且在所有商店作用域中
ConfigFixture::setGlobal('general/store_information/name', 'Ye Olde Wizard Shop');
它使用 MutableScopeConfigInterface
,因此配置不会持久化到数据库中。在您的测试中使用 @magentoAppIsolation enabled
确保更改在后续测试中被撤销。
您还可以使用 ConfigFixture::setForStore()
为具有特定商店的配置值设置显式值
致谢
许可证
MIT 许可证(MIT)。有关更多信息,请参阅 许可证文件