Search by

ecomdev / magento2-test-essentials

IvanChepurnyi

Magento 2 Test Essentials

Package info

github.com/EcomDev/magento2-test-essentials

pkg:composer/ecomdev/magento2-test-essentials

Statistics

Installs: 199

Dependents: 1

Suggesters: 0

Stars: 18

Open Issues: 1

v2.2.0 2026-11-29 00:57 UTC

This package is auto-updated.

Last update: 2026-11-29 01:32:57 UTC


README

PHP Package

Using mocking frameworks for testing Magento 2 modules is counterproductive: you end up replicating your production calls line by line and coupling your tests to internal implementation details. A small refactor breaks dozens of mocks.

ecomdev/magento2-test-essentials solves this with faithful fake objects, a lightweight immutable ObjectManager with reusable DI configuration presets, and real MySQL testcontainer utilities for declarative schema, patches, ResourceModels, query plans, and snapshots — all verified with 100% test coverage.

📦 Requirements & Installation

  • PHP 8.4+
  • Magento Framework 103.*
composer require --dev ecomdev/magento2-test-essentials

For real database integration tests, install ecomdev/testcontainers-magento-data:

composer require --dev ecomdev/testcontainers-magento-data

🚀 Quick Start

1. Testing Services with Store & ScopeConfig Fakes

use EcomDev\Magento2TestEssentials\ObjectManager;
use EcomDev\Magento2TestEssentials\ScopeConfig;
use EcomDev\Magento2TestEssentials\Store\Store;
use EcomDev\Magento2TestEssentials\Store\StoreManager;
use Magento\Catalog\Model\Product;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Store\Model\StoreManagerInterface;

$storeManager = StoreManager::new()
    ->withStore(Store::new(1, 'store_one'))
    ->withStore(Store::new(3, 'store_three'));
$storeManager->setCurrentStore('store_three');

$objectManager = ObjectManager::new()
    ->withObject(StoreManagerInterface::class, $storeManager)
    ->withFactory(
        ScopeConfigInterface::class,
        static fn (ObjectManager $objectManager): ScopeConfig => ScopeConfig::new()
            ->withStoreManager($objectManager->get(StoreManagerInterface::class))
            ->withStoreValue('store_three', 'catalog/product/store_alias', 1)
    );

// Instantiates data models without triggering Magento's heavy constructor graph
$product = $objectManager->get(Product::class);
$service = $objectManager->create(YourService::class);
$service->applyCurrentStoreToProduct($product);

2. Real Database Integration Testing with Testcontainers

use EcomDev\Magento2TestEssentials\DatabaseUtility;
use EcomDev\Magento2TestEssentials\DeploymentConfig;
use EcomDev\Magento2TestEssentials\ObjectManager;
use EcomDev\Magento2TestEssentials\ObjectManager\DatabaseConfiguration;
use EcomDev\Magento2TestEssentials\ScopeConfig;
use EcomDev\Magento2TestEssentials\SchemaUtility;
use EcomDev\TestContainers\MagentoData\DbContainerBuilder;

$container = DbContainerBuilder::mysql()
    ->withMagentoVersion('2.4.9')
    ->build();
$connection = $container->getConnectionSettings();

$objectManager = ObjectManager::new()->withConfiguration(
    DatabaseConfiguration::new(
        DeploymentConfig::new()->withDatabaseConnection(
            $connection->host,
            $connection->user,
            $connection->password,
            $connection->database
        )
    )
);

// Apply module db_schema.xml without dropping unrelated tables
SchemaUtility::create($objectManager)->applyModuleSchema('Vendor_MyModule');

// Hydrate ScopeConfig + StoreManager from live DB, with in-memory overrides
$scopeConfig = ScopeConfig::fromDatabase($objectManager)
    ->withDefaultValue('my_module/general/enabled', '1');

// Seed, inspect, diff, and EXPLAIN real queries
$database = DatabaseUtility::create($objectManager);
$database->populateTable('my_custom_table', ['code', 'is_active'], [['alpha', 1]]);
$rows = $database->tableData('my_custom_table', ['code', 'is_active'], order: ['code ASC']);

📚 Documentation

Guide Covers
ObjectManager & Configuration Presets ObjectManager, get() vs create() vs withAutowire(), Configuration presets (DatabaseConfiguration, SelectConfiguration, ResourceModelConfiguration, SchemaConfiguration, PatchConfiguration), custom presets, IntegrationUtility
Stores, ScopeConfig, DeploymentConfig & Events StoreManager, Website, StoreGroup, Store, ScopeConfig, live DB hydration (::fromDatabase()), DeploymentConfig, ResourceConnectionConfig, EventManager spy
Database Testing, Snapshots, Query Logging & EXPLAIN DatabaseUtility, typed tableData(), inTransaction(), withoutForeignKeyChecks(), TableSnapshot & TableDiff, explain() & QueryPlan, QueryLogger, testing ResourceModels & Collections
Declarative Schema & Patches SchemaUtility (applySchemaFile, applyModuleSchema, schema inspection), ModuleUtility::etcFile(), PatchUtility & PatchConfiguration for Data/Schema patches

✨ Features

  • ObjectManager — Reflection-backed ObjectManagerInterface with immutable builders and composable ObjectManager\Configuration presets
  • Store & Config Fakes — StoreManager, Website, StoreGroup, Store, ScopeConfig (with fromDatabase() live DB hydration), DeploymentConfig, ResourceConnectionConfig
  • Event Spy — Event\EventManager implementing Magento\Framework\Event\ManagerInterface
  • Database Utilities — DatabaseUtility with typed table reads, bulk seeding, transaction rollback isolation, FK toggle helper, TableSnapshot/TableDiff, QueryPlan (EXPLAIN), and QueryLogger
  • ResourceModels & Collections — ResourceModelConfiguration wiring Magento's AbstractDb and AbstractCollection dependencies
  • Declarative Schema & Patches — SchemaUtility, ModuleUtility, and PatchUtility (PatchConfiguration) for running db_schema.xml declarations and Data/Schema patches against real MySQL

🎓 Using with AI Coding Agents

This package ships an Agent Skill in skills/magento2-module-testing/SKILL.md that teaches AI coding agents (Antigravity, Claude Code, Codex, Cursor, etc.) how to write mock-free Magento 2 module tests with magento2-test-essentials and testcontainers-magento-data.

Install it into your project from GitHub:

npx skills add EcomDev/magento2-test-essentials

🛠️ Development & Quality Checks

composer static   # PHPStan (level 8) + PHPCS
composer test     # ParaTest + PHPUnit 13 with Xdebug coverage report
composer format   # Auto-fix code style with PHPCBF

📜 License

This project is licensed under the MIT License. See the LICENSE file for more details.