Code Monkey home page Code Monkey logo

phpspec-magento-di-adapter's Introduction

PHPSpec Magento 2.0 DI Adapter Build Status Coverage Status

This small PHPSpec extension allows you to test Magento 2.0 modules much more easier by utilizing generators of Magento\Framework\ObjectManager.

Why?

Reasons why not to use ObjectManager in PHPSpec examples:

  1. It is heavy and requires stubbing full file system in order to run a simple spec example.
  2. Depending on ObjectManager is a bad idea, as you don't want to test some-else DI overrides.
  3. Simple modules that do not require database do not need fully functional object manager
  4. Adapting your business logic to another framework will require from you only to materialize generated classes, instead of depending on the whole ObjectManager library.

Supported Generators

  • Factory
  • Repository
  • Converter
  • Persistor
  • Mapper
  • SearchResults

Installation

  1. Install via composer

    composer require --dev ecomdev/phpspec-magento-di-adapter
  2. Add to your phpspec.yml

    extensions:
       - EcomDev\PHPSpec\MagentoDiAdapter\Extension

Usage

Make sure that when you write examples to specify fully qualified class name for auto-generated class.

<?php

namespace spec\Acme\CustomMagentoModule\Model;

use Magento\Catalog\Model\Product;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;

class ProductManagerSpec extends ObjectBehavior
{
    private $productFactory; 
    
    function let(ProductFactory $factory) {
        $this->productFactory = $factory;    
        $this->beConstructedWith($factory);
    }
    
    function it_creates_items_via_product_factory(Product $product)
    {
        $this->productFactory->create()->willReturn($product)->shouldBeCalled();
        $this->someCreationLogic();
    }
}

This approach will not get you a desired result, as PHP by default looks for undefined classes within the same namespace. So instead of Magento\Catalog\Model\ProductFactory it will generate a class spec\Acme\CustomMagentoModule\Model\ProductFactory, that is definitely not a desired behavior.

In order to fix that make sure to specify fully qualified name in method signature or via use operator in the file header.

<?php

namespace spec\Acme\CustomMagentoModule\Model;

use Magento\Catalog\Model\Product;
use Magento\Catalog\Model\ProductFactory; // This class will be automatically generated
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;

class ProductManagerSpec extends ObjectBehavior
{
    private $productFactory; 
    
    function let(ProductFactory $factory) {
        $this->productFactory = $factory;    
        $this->beConstructedWith($factory);
    }
    
    function it_creates_items_via_product_factory(Product $product)
    {
        $this->productFactory->create()->willReturn($product)->shouldBeCalled();
        $this->someCreationLogic();
    }
}

Contribution

Make a pull request based on develop branch

phpspec-magento-di-adapter's People

Contributors

ivanchepurnyi avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

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.