tddwizard/magento2-fixtures

用于 Magento 2 集成测试的固定库

安装次数: 372,942

依赖关系: 13

建议者: 0

安全性: 0

星星: 141

关注者: 10

分支: 28

开放问题: 18

类型:magento2-module

v1.1.2 2022-05-24 15:31 UTC

README

Latest Version on Packagist Software License Build Status Coverage Status Quality Score Code Climate

由 Fabian Schmengler 编写的 Magento 2 固定库

🧙🏻‍♂ https://tddwizard.com/

这是什么?

是 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)。有关更多信息,请参阅 许可证文件