Test essentials for writing testable code that interacts with Magento core modules

Overview

Essentials for testing Magento 2 modules

Using mocking frameworks for testing Magento 2 modules is counterproductive as you replicate line by line your actual calls to a magento implementation.

Binding your tests to the details of implementation leads to very fragile test suite that no-one wants to work with afterwards as a small change in underlying code like extraction of the class requires complete rewrite of the test case.

This package solves this problem by providing fake objects for most common operations you might want to interact with core.

As well as set of fake objects there is an ObjectManagerInterface implementation that automatically instantiates dependencies if you use create method and allows specifying custom factories for creation of deep dependency.

Each fake object is covered by automated tests to make sure that behaviour is correct your tests can test your code specific behaviour by using different scenarios.

Installation

composer require --dev ecomdev/magento2-test-essentials

Examples

Testing functionality that tries to resolve a default store

use EcomDev\Magento2TestEssentials\ObjectManager;
use EcomDev\Magento2TestEssentials\Store\StoreManager;
use EcomDev\Magento2TestEssentials\Store\Store;
use Magento\Store\Model\StoreManagerInterface;
use Magento\Catalog\Model\Product;

class YourTestedClass 
{
    private $storeManager;
    
    public function __construct(StoreManagerInterface $storeManager) 
    {
        $this->storeManager = $storeManager;
    }
    
    public function applyCurrentStoreToProduct(Product $product) 
    {
        $product->setStoreId($this->storeManager->getStore()->getId());
    }
}

class YourTestedClassTest extends \PHPUnit\Framework\TestCase
{
    /** @test */
    public function appliesStoreIdToProduct() 
    { 
        $applier = new YourTestedClass(
            StoreManager::new()
                ->withStore(Store::new(3, 'store_three'))
                ->setCurrentStore('store_three')
        );
        
        // Creates product without any constructor but if you rely on data fields, it works great
        $product = ObjectManager::new()->get(Product::class); 
        
        $applier->applyCurrentStoreToProduct(
            $product
        );
        
        $this->assertEquals(3, $product->getStoreId());
    }
}

Features

  • ObjectManagerInterface implementation which mimics platform's behaviour
  • StoreInterface implementation as a simple data object for testing store related behaviour
  • GroupInterface implementation as a simple data object for testing store group related behaviour
  • WebsiteInterface implementation as a simple data object for testing website related behaviour
  • ScopeConfigurationInterface implementation for testing configuration dependent functionality
  • ShoppingCartInterface implementation for testing shopping cart related functionalities
  • CustomerInterface implementation for testing customer related functionalities
  • Customer/Session implementation for testing functionalities that depend on current customer data
  • Checkout/Session implementation for testing functionalities that depend on current checkout data
You might also like...
Magento 2 Italian Language Pack is special gift for Magento 2 Community Edition. The data of the translation is supplied by Magento 2 Translation Project of Crowdin site, on which you will contribute into the project and download the zip file to install on your own store. This knowledge base will include full of the clear guides that supports you to convert the default language (English) into Italian language on your entire store.
A tool that allows to quickly export data from Magento 1 and Magento 2 store and import it back into Magento 2

Simple Import / Export tool A tool that allows to quickly export data from Magento 1 and Magento 2 store and import it back into Magento 2. Table data

PHP OOP interface for writing Slack Block Kit messages and modals
PHP OOP interface for writing Slack Block Kit messages and modals

Slack Block Kit for PHP 👉 For formatting messages and modals for Slack using their Block Kit syntax via an OOP interface 👈 By Jeremy Lindblom (@jere

PHP implementation for reading and writing Apache Parquet files/streams

php-parquet This is the first parquet file format reader/writer implementation in PHP, based on the Thrift sources provided by the Apache Foundation.

A pure PHP library for reading and writing presentations documents
A pure PHP library for reading and writing presentations documents

Branch Master : Branch Develop : PHPPresentation is a library written in pure PHP that provides a set of classes to write to different presentation fi

Library for check dependency between modules inside projects

PHP Dependency analyzer PHP DA is tool for check and support dependencies inside your project clear. For example: You have project with 3 root namespa

Experimenting with Zabbix frontend modules
Experimenting with Zabbix frontend modules

Modules Request for new example can be left as issue for this repository, will do my best to implement if it is possible. Extending host popup menu Ac

Simple way to auto load modules without complicate definition of routes

Phalcon-autorouter Version 2.0 Example full application with various libraries included in this example Change log Structure have been changed and upd

WordPress plugin which contains a collection of modules to apply theme-agnostic front-end modifications

Soil A WordPress plugin which contains a collection of modules to apply theme-agnostic front-end modifications. Soil is a commercial plugin available

Owner
EcomDev B.V.
Providing High Performance Web-shops since 2010
EcomDev B.V.
LDFx is the ultimate essentials plugin for PM4!

LDFx LDFx is the ultimate essentials plugin for PM4! Does it work? As it everything on the todo & implemented list is work Todo & Implemented NickName

null 6 Oct 7, 2022
A library for reading and writing DNA test kit files in PHP.

php-dna Requirements php-dna 1.0+ requires PHP 8.0 (or later). Installation There are two ways of installing php-dna. Composer To install php-dna in y

Family Tree 365 4 Aug 31, 2022
Magento-composer-installer - Composer installer for Magento modules

!!! support the maintainer of this project via Patreon: https://www.patreon.com/Flyingmana Magento Composer Installer The purpose of this project is t

null 213 Sep 24, 2022
Result of our code-along meetup writing PHP 8.1 code

PHP 8.1 Demo Code This code demonstrates various PHP 8.0 and 8.1 features in a realistic, functional (but incomplete) codebase. The code is part of so

azPHP 2 Nov 14, 2021
Reference for writing clear PHP code

clearPHP Reference for writing clear PHP code It is difficult to know when one's code is well written. There are recommendations for writing PHP code

Seguy Damien 947 Dec 22, 2022
Check modules in app/code and vendor for PHP 8 compatibility status - PHP_CodeSniffer & php-compatibility standard

M2 PHP version compatibility check How To use Requires PHP 7.3+ | PHP 8 This app will run PHP_CodeSniffer with phpcompatibility/php-compatibility on t

William Tran 24 Oct 13, 2022
Performance fixes for magento 2 core.

magento2-performance-fixes Performance fixes for magento 2 core. Problem and solution's concept - briefly PHP / Magento doesn't support concurency req

Mariusz Łopuch 48 Dec 30, 2022
Allow multiple options for Magento 2 checkout layout. Provides capabilities to AB test checkout changes and more.

Aimes_CheckoutDesigns Features Please note: This module is currently still considered a proof of concept. This module provides the ability to change c

Rob Aimes 30 Aug 8, 2022
Quickly and easily preview and test your Magento 2 order confirmation page, without hacks or spending time placing new order each time

Preview Order Confirmation Page for Magento 2 For Magento 2.0.x, 2.1.x, 2.2.x and 2.3.x Styling and testing Magento's order confirmation page can be a

MagePal :: Magento Extensions 71 Aug 12, 2022