2017 © Pedro Peláez
 

library phpspec-magento-di-adapter

An adapter for DI features of Magento\Framework to write easier your examples with PHPSpec. Does not use ObjectManager, only emulates some of its features

image

ecomdev/phpspec-magento-di-adapter

An adapter for DI features of Magento\Framework to write easier your examples with PHPSpec. Does not use ObjectManager, only emulates some of its features

  • Thursday, September 15, 2016
  • by IvanChepurnyi
  • Repository
  • 1 Watchers
  • 6 Stars
  • 19 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 5 Versions
  • 0 % Grown

The README.md

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., (*1)

Why?

Reasons why not to use ObjectManager in PHPSpec examples:, (*2)

  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, (*3)

    composer require --dev ecomdev/phpspec-magento-di-adapter
    
  2. Add to your phpspec.yml, (*4)

    extensions:
       - EcomDev\PHPSpec\MagentoDiAdapter\Extension
    

Usage

Make sure that when you write examples to specify fully qualified class name for auto-generated class., (*5)

<?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., (*6)

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

<?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, (*8)

The Versions

15/09 2016

dev-develop

dev-develop

An adapter for DI features of Magento\Framework to write easier your examples with PHPSpec. Does not use ObjectManager, only emulates some of its features

  Sources   Download

OSL-3.0

The Requires

 

The Development Requires

phpspec magento

15/09 2016

dev-master

9999999-dev

An adapter for DI features of Magento\Framework to write easier your examples with PHPSpec. Does not use ObjectManager, only emulates some of its features

  Sources   Download

OSL-3.0

The Requires

 

The Development Requires

phpspec magento

15/09 2016

1.0.0

1.0.0.0

An adapter for DI features of Magento\Framework to write easier your examples with PHPSpec. Does not use ObjectManager, only emulates some of its features

  Sources   Download

OSL-3.0

The Requires

 

The Development Requires

phpspec magento

24/05 2016

0.1.1

0.1.1.0

An adapter for DI features of Magento\Framework to write easier your examples with PHPSpec. Does not use ObjectManager, only emulates some of its features

  Sources   Download

OSL-3.0

The Requires

 

The Development Requires

phpspec magento

24/05 2016

0.1.0

0.1.0.0

An adapter for DI features of Magento\Framework to write easier your examples with PHPSpec. Does not use ObjectManager, only emulates some of its features

  Sources   Download

OSL-3.0

The Requires

 

The Development Requires

phpspec magento