GithubHelp home page GithubHelp logo

m2-unit-test-helpers's Introduction

Magento 2 Unit Test Helpers

Unit testing helpers for Magento 2 code bases.

Installation

$ composer config repositories.m2-unit-test-helpers vcs [email protected]:WeareJH/m2-unit-test-helpers.git
$ composer require --dev wearejh/m2-unit-test-helpers

Object Helper

Creates mock objects using prophecy and automatically pulls in the object's dependencies

Example Usage

<?php

use PHPUnit\Framework\TestCase;
use Magento\Framework\App\Action\Context;
use Magento\Framework\App\Request\Http;
use Magento\Framework\Message\ManagerInterface;
use Jh\UnitTestHelpers\ObjectHelper;

/**
 * @author [email protected]
 */
class ExampleTest extends TestCase
{
    use ObjectHelper;
    
    private $request;
    private $messages;
    
    public function setup()
    {
        $this->request  = $this->createPartialMock(Http::class, []);
        $this->messages = $this->prophesize(ManagerInterface::class);
        
        $redirectFactory   = $this->prophesize(RedirectFactory::class);
        $redirectFactory->create()->willReturn($this->redirect->reveal());
        
        $context = $this->getObject(Context::class, [
            'request'               => $this->request,
            'messageManager'        => $this->messages->reveal(),
            'resultRedirectFactory' => $redirectFactory->reveal()
        ]);
    }
}

Note that you can pass your own arguments to the object you want to create, in-case you want to pass real instances or your own mocks. You pass them as a key value array where the key is the object constructor's parameter name you want to override and the value is the actual value.

getObject will read the object constructor parameters and automatically create mock objects using the class type of the parameter. It will then create the object you asked for passing in all the mocks via the constructor.

You can also retrieve one of mocked dependencies after the object was created like so:

<?php

use PHPUnit\Framework\TestCase;
use Magento\Framework\App\Action\Context;
use Magento\Framework\App\Request\Http;
use Magento\Framework\Message\ManagerInterface;
use Jh\UnitTestHelpers\ObjectHelper;

/**
 * @author [email protected]
 */
class ExampleTest extends TestCase
{
    use ObjectHelper;

    
    public function setup()
    {
        
        $context = $this->getObject(Context::class);
        
        $request = $this->retrieveChildMock(Context::class, 'request');
    }
}

Where request is the constructor parameter name in Context::class - this method will return you the object prophecy so you can create expectations on it.

m2-unit-test-helpers's People

Contributors

aydinhassan avatar bartoszkubicki avatar pknap avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Forkers

beejhuff

m2-unit-test-helpers's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.