ecomdev / magento2-test-essentials
Magento 2 Test Essentials
Package info
github.com/EcomDev/magento2-test-essentials
pkg:composer/ecomdev/magento2-test-essentials
Requires
- php: ~8.4.0 || ~8.5.0 || ~8.6.0
- magento/framework: 103.*
- magento/module-catalog: ~104.0
- magento/module-customer: ~103.0
- magento/module-quote: ~101.0
- magento/module-store: ~101.0
Requires (Dev)
- brianium/paratest: ^7.29.0
- ecomdev/testcontainers-magento-data: ^2.1
- phpstan/phpstan: ^2.3.0
- phpunit/phpunit: ^13.4
- squizlabs/php_codesniffer: ^4.0
Suggests
- ext-pdo: For database connections with ResourceConnection implementation
- ecomdev/testcontainers-magento-data: Database Containers pre-populated with various data
Provides
None
Conflicts
None
Replaces
None
README
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-backedObjectManagerInterfacewith immutable builders and composableObjectManager\Configurationpresets- Store & Config Fakes —
StoreManager,Website,StoreGroup,Store,ScopeConfig(withfromDatabase()live DB hydration),DeploymentConfig,ResourceConnectionConfig - Event Spy —
Event\EventManagerimplementingMagento\Framework\Event\ManagerInterface - Database Utilities —
DatabaseUtilitywith typed table reads, bulk seeding, transaction rollback isolation, FK toggle helper,TableSnapshot/TableDiff,QueryPlan(EXPLAIN), andQueryLogger - ResourceModels & Collections —
ResourceModelConfigurationwiring Magento'sAbstractDbandAbstractCollectiondependencies - Declarative Schema & Patches —
SchemaUtility,ModuleUtility, andPatchUtility(PatchConfiguration) for runningdb_schema.xmldeclarations 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.