You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

自定义Shopware集成测试中创建虚拟订单的便捷方法咨询

Clean Alternatives to Copy-Pasting Order Creation Logic in Shopware Tests

Great question! Copy-pasting test helper methods is a total pain—let’s break down cleaner, maintainable ways to generate dummy orders for your custom integration tests without duplicating code:

1. Use Shopware’s Built-in Test Traits

Shopware’s core test suite includes reusable traits designed to handle common test operations, and there’s a perfect one for order creation: \Shopware\Core\Checkout\Test\Order\OrderTestTrait.

Instead of copying the createOrder method, just import this trait into your test class and use its pre-built methods. For example:

use Shopware\Core\Checkout\Test\Order\OrderTestTrait;
use Shopware\Core\Test\TestCaseBase\IntegrationTestBehaviour;

class YourCustomIntegrationTest extends TestCase
{
    use IntegrationTestBehaviour;
    use OrderTestTrait;

    public function testYourOrderScenario()
    {
        // Create a basic test order with default values
        $order = $this->createTestOrder();
        
        // Or customize it with additional data (like initial tracking number placeholders)
        $customOrder = $this->createTestOrder([
            'trackingCodes' => ['TEMP-TRACKING-123']
        ]);
        
        // Rest of your test logic
    }
}

This trait wraps up all the boilerplate for creating orders, including associating customers, addresses, and line items—no need to replicate that code yourself.

2. Build a Custom Test Helper Trait/Class

If the built-in trait doesn’t fit your specific needs (like needing to set module-specific default values for tracking numbers), create a project-level helper trait or class to centralize your order creation logic.

Example Custom Trait:

// tests/Helper/OrderTestHelperTrait.php
namespace Tests\Helper;

use Shopware\Core\Checkout\Order\OrderEntity;
use Shopware\Core\Framework\Context;

trait OrderTestHelperTrait
{
    private function createDummyOrderForModule(string $initialTrackingNumber = 'MODULE-TEMP-001'): OrderEntity
    {
        // Reuse Shopware's core services to build the order
        $orderFactory = $this->getContainer()->get(\Shopware\Core\Checkout\Order\SalesChannel\OrderFactory::class);
        
        // Build your order with default values tailored to your module
        $orderData = [
            'trackingCodes' => [$initialTrackingNumber],
            // Add other module-specific defaults here
        ];
        
        $order = $orderFactory->create($orderData, Context::createDefaultContext());
        
        // Persist the order if needed
        $this->getContainer()->get(\Shopware\Core\Checkout\Order\OrderRepository::class)->upsert([$order->jsonSerialize()], Context::createDefaultContext());
        
        return $order;
    }
}

Then use it in any test class:

use Tests\Helper\OrderTestHelperTrait;
use Shopware\Core\Test\TestCaseBase\IntegrationTestBehaviour;

class YourModuleIntegrationTest extends TestCase
{
    use IntegrationTestBehaviour;
    use OrderTestHelperTrait;

    public function testOrderTrackingUpdate()
    {
        $dummyOrder = $this->createDummyOrderForModule();
        
        // Update tracking number later in your test
        // ...
    }
}

This way, you only maintain one copy of the order creation logic, and you can update it once for all tests that need it.

3. Use Test Fixtures

If you need consistent, pre-defined orders for multiple tests, create a fixture file (JSON or PHP) that defines your dummy order structure, then use Shopware’s fixture loader to load and create the order in your tests.

For example, create tests/Fixtures/dummy_order.json with your order data, then load it:

$fixtureLoader = $this->getContainer()->get(\Shopware\Core\Test\Fixture\FixtureLoader::class);
$fixtureLoader->load('dummy_order', Context::createDefaultContext());

// Fetch the created order from the repository
$order = $this->getContainer()->get(\Shopware\Core\Checkout\Order\OrderRepository::class)
    ->search(new Criteria([$fixtureData['id']]), Context::createDefaultContext())
    ->first();

This works great for scenarios where you need identical order structures across multiple test cases.


内容的提问来源于stack exchange,提问作者Alex

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.28 22:19:05