james cowie - magento...• software architecture within php • tdd • phpspec - 2011 • bdd •...

57

Upload: others

Post on 26-Jul-2020

16 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: James Cowie - Magento...• Software Architecture within PHP • TDD • PHPSpec - 2011 • BDD • Behat - 2011 • DDD Installing Magento How did we install Magento 1 • Large scale
Page 2: James Cowie - Magento...• Software Architecture within PHP • TDD • PHPSpec - 2011 • BDD • Behat - 2011 • DDD Installing Magento How did we install Magento 1 • Large scale

James CowieSoftware Engineer

Session Digital

@Jcowie

Page 3: James Cowie - Magento...• Software Architecture within PHP • TDD • PHPSpec - 2011 • BDD • Behat - 2011 • DDD Installing Magento How did we install Magento 1 • Large scale

Getting ready for Magento 2

• How to prepare for a new e-

commerce system

Page 4: James Cowie - Magento...• Software Architecture within PHP • TDD • PHPSpec - 2011 • BDD • Behat - 2011 • DDD Installing Magento How did we install Magento 1 • Large scale

Whats changed since Magento 1 ?

Page 5: James Cowie - Magento...• Software Architecture within PHP • TDD • PHPSpec - 2011 • BDD • Behat - 2011 • DDD Installing Magento How did we install Magento 1 • Large scale

PHP has changed

Page 6: James Cowie - Magento...• Software Architecture within PHP • TDD • PHPSpec - 2011 • BDD • Behat - 2011 • DDD Installing Magento How did we install Magento 1 • Large scale

PHP since Magento 1

• Composer - 2012

• Namespaces - PHP 5.3.0 - 2009

• Traits - PHP 5.3.0 - 2009

• Generators - PHP 5.5.0 - 2013

• Better Interface support PHP 5.3.9 2009

• Type Hinting - PHP 5.4 - 2012

• ….

Page 7: James Cowie - Magento...• Software Architecture within PHP • TDD • PHPSpec - 2011 • BDD • Behat - 2011 • DDD Installing Magento How did we install Magento 1 • Large scale

The world of engineering has changed

Page 8: James Cowie - Magento...• Software Architecture within PHP • TDD • PHPSpec - 2011 • BDD • Behat - 2011 • DDD Installing Magento How did we install Magento 1 • Large scale

Software Engineering

• Server Architecture

• Cloud

• Docker

• VPS

• Software Architecture within PHP

• TDD

• PHPSpec - 2011

• BDD

• Behat - 2011

• DDD

Page 9: James Cowie - Magento...• Software Architecture within PHP • TDD • PHPSpec - 2011 • BDD • Behat - 2011 • DDD Installing Magento How did we install Magento 1 • Large scale

Installing Magento

Page 10: James Cowie - Magento...• Software Architecture within PHP • TDD • PHPSpec - 2011 • BDD • Behat - 2011 • DDD Installing Magento How did we install Magento 1 • Large scale

How did we install Magento 1

• Large scale bundled installations per versions.

Page 11: James Cowie - Magento...• Software Architecture within PHP • TDD • PHPSpec - 2011 • BDD • Behat - 2011 • DDD Installing Magento How did we install Magento 1 • Large scale

Along came composer

Page 12: James Cowie - Magento...• Software Architecture within PHP • TDD • PHPSpec - 2011 • BDD • Behat - 2011 • DDD Installing Magento How did we install Magento 1 • Large scale

Installing Magento 2 with composer

composer create-project--stability=beta--no-installmagento/project-community-editionM2Test

Page 13: James Cowie - Magento...• Software Architecture within PHP • TDD • PHPSpec - 2011 • BDD • Behat - 2011 • DDD Installing Magento How did we install Magento 1 • Large scale

Whats this created

{"name": "magento/project-community-edition","type": "project","require": {

"magento/product-community-edition": "0.74.0-beta12"

},"require-dev": {}

}

Page 14: James Cowie - Magento...• Software Architecture within PHP • TDD • PHPSpec - 2011 • BDD • Behat - 2011 • DDD Installing Magento How did we install Magento 1 • Large scale

But wait there is more composer offers

Page 15: James Cowie - Magento...• Software Architecture within PHP • TDD • PHPSpec - 2011 • BDD • Behat - 2011 • DDD Installing Magento How did we install Magento 1 • Large scale

More Composer

• Packagist + packages.magento.com

• Package versioning

• Dependency Management

Page 16: James Cowie - Magento...• Software Architecture within PHP • TDD • PHPSpec - 2011 • BDD • Behat - 2011 • DDD Installing Magento How did we install Magento 1 • Large scale

Show me the use case !

composer require "league/period"

Page 17: James Cowie - Magento...• Software Architecture within PHP • TDD • PHPSpec - 2011 • BDD • Behat - 2011 • DDD Installing Magento How did we install Magento 1 • Large scale

1 /** 2 * @var League\Period\Period; 3 */4 protected $_datePeriod;5 6 /** 7 * @param \League\Period\Period; $datePeriod 8 */9 public function __construct(\League\Period\Period $datePeriod)

10 {11 $this->_datePeriod = $datePeriod;12 }

Page 18: James Cowie - Magento...• Software Architecture within PHP • TDD • PHPSpec - 2011 • BDD • Behat - 2011 • DDD Installing Magento How did we install Magento 1 • Large scale

Composer 101

Versioning In Composer

Page 19: James Cowie - Magento...• Software Architecture within PHP • TDD • PHPSpec - 2011 • BDD • Behat - 2011 • DDD Installing Magento How did we install Magento 1 • Large scale

Composer 101

Exact version: “1.4.2″, “v1.4.2″.

"magento/product-community-edition": “1.0.1”

Page 20: James Cowie - Magento...• Software Architecture within PHP • TDD • PHPSpec - 2011 • BDD • Behat - 2011 • DDD Installing Magento How did we install Magento 1 • Large scale

Composer 101

Ranges: >=1.0, <2.0,

"magento/product-community-edition": “>=1.0, <2”

Page 21: James Cowie - Magento...• Software Architecture within PHP • TDD • PHPSpec - 2011 • BDD • Behat - 2011 • DDD Installing Magento How did we install Magento 1 • Large scale

Composer 101

Wildcard: “1.0.*”,

“1.*”

"magento/product-community-edition": “1.*”

"magento/product-community-edition": “1.0.*”

Page 22: James Cowie - Magento...• Software Architecture within PHP • TDD • PHPSpec - 2011 • BDD • Behat - 2011 • DDD Installing Magento How did we install Magento 1 • Large scale

Composer 101

Next Significant Release: “~1.2″ is equivalent to “>=1.2,<2.0″.

"magento/product-community-edition": “~1.2”

Page 23: James Cowie - Magento...• Software Architecture within PHP • TDD • PHPSpec - 2011 • BDD • Behat - 2011 • DDD Installing Magento How did we install Magento 1 • Large scale

A note on semver

Page 24: James Cowie - Magento...• Software Architecture within PHP • TDD • PHPSpec - 2011 • BDD • Behat - 2011 • DDD Installing Magento How did we install Magento 1 • Large scale

Decoupling modules

Page 25: James Cowie - Magento...• Software Architecture within PHP • TDD • PHPSpec - 2011 • BDD • Behat - 2011 • DDD Installing Magento How did we install Magento 1 • Large scale

Better Module Architecture

Page 26: James Cowie - Magento...• Software Architecture within PHP • TDD • PHPSpec - 2011 • BDD • Behat - 2011 • DDD Installing Magento How did we install Magento 1 • Large scale

We can and should apply best software practice to Magento

Page 27: James Cowie - Magento...• Software Architecture within PHP • TDD • PHPSpec - 2011 • BDD • Behat - 2011 • DDD Installing Magento How did we install Magento 1 • Large scale

Benefits of decoupling modules ?

• Clean Code

• Reusable packages

• Testable

• Easier to read

• Easier to maintain

Page 28: James Cowie - Magento...• Software Architecture within PHP • TDD • PHPSpec - 2011 • BDD • Behat - 2011 • DDD Installing Magento How did we install Magento 1 • Large scale

Yea but in Magento 1 we could not fully do this..

Page 29: James Cowie - Magento...• Software Architecture within PHP • TDD • PHPSpec - 2011 • BDD • Behat - 2011 • DDD Installing Magento How did we install Magento 1 • Large scale

Decoupling in Magento 1

• No Dependency Injection container.

• Mage god class

• Hard to test

• Hard to decouple logic

Page 30: James Cowie - Magento...• Software Architecture within PHP • TDD • PHPSpec - 2011 • BDD • Behat - 2011 • DDD Installing Magento How did we install Magento 1 • Large scale

Dependency Injection in Magento 2

Page 31: James Cowie - Magento...• Software Architecture within PHP • TDD • PHPSpec - 2011 • BDD • Behat - 2011 • DDD Installing Magento How did we install Magento 1 • Large scale
Page 32: James Cowie - Magento...• Software Architecture within PHP • TDD • PHPSpec - 2011 • BDD • Behat - 2011 • DDD Installing Magento How did we install Magento 1 • Large scale

public function __construct(\Magento\Framework\App\Action\Context $context,\Magento\Catalog\Model\Design $catalogDesign,\Magento\Catalog\Model\Session $catalogSession,\Magento\Framework\Registry $coreRegistry,\Magento\Store\Model\StoreManagerInterface $storeManager,\Magento\CatalogUrlRewrite\Model\CategoryUrlPathGenerator $categoryUrlPathGenerator,PageFactory $resultPageFactory,\Magento\Framework\Controller\Result\ForwardFactory $resultForwardFactory,Resolver $layerResolver,CategoryRepositoryInterface $categoryRepository\Magento\Framework\App\Action\Context $context,\Magento\Catalog\Model\Design $catalogDesign,\Magento\Catalog\Model\Session $catalogSession,\Magento\Framework\Registry $coreRegistry,\Magento\Store\Model\StoreManagerInterface $storeManager,\Magento\CatalogUrlRewrite\Model\CategoryUrlPathGenerator

Page 33: James Cowie - Magento...• Software Architecture within PHP • TDD • PHPSpec - 2011 • BDD • Behat - 2011 • DDD Installing Magento How did we install Magento 1 • Large scale

public function __construct(\Magento\Framework\App\Action\Context $context,\Magento\Catalog\Model\Design $catalogDesign,\Magento\Catalog\Model\Session $catalogSession,\Magento\Framework\Registry $coreRegistry,\Magento\Store\Model\StoreManagerInterface $storeManager,\Magento\CatalogUrlRewrite\Model\CategoryUrlPathGenerator $categoryUrlPathGenerator,PageFactory $resultPageFactory,\Magento\Framework\Controller\Result\ForwardFactory $resultForwardFactory,Resolver $layerResolver,CategoryRepositoryInterface $categoryRepository\Magento\Framework\App\Action\Context $context,\Magento\Catalog\Model\Design $catalogDesign,\Magento\Catalog\Model\Session $catalogSession,\Magento\Framework\Registry $coreRegistry,\Magento\Store\Model\StoreManagerInterface $storeManager,\Magento\CatalogUrlRewrite\Model\CategoryUrlPathGenerator

Code Smell

Page 34: James Cowie - Magento...• Software Architecture within PHP • TDD • PHPSpec - 2011 • BDD • Behat - 2011 • DDD Installing Magento How did we install Magento 1 • Large scale

Information on DI

• Dependency Injection replaces Mage:: god class

• Dependency Injection can be overused.

• Dependency Injection enables composition.

Page 35: James Cowie - Magento...• Software Architecture within PHP • TDD • PHPSpec - 2011 • BDD • Behat - 2011 • DDD Installing Magento How did we install Magento 1 • Large scale

How we manage dependencies without Injection

1 <?php2 class Sample3 {4 protected $logger;5 6 public function doSomething()7 {8 $this->logger = new \Logger();9 $logger->doSomething();10 }11 }

Page 36: James Cowie - Magento...• Software Architecture within PHP • TDD • PHPSpec - 2011 • BDD • Behat - 2011 • DDD Installing Magento How did we install Magento 1 • Large scale

How we manage dependencies without Injection

1 <?php2 class Sample3 {4 protected $logger;5 6 public function doSomething()7 {8 $this->logger = new \Logger();9 $logger->doSomething();10 }11 }

Page 37: James Cowie - Magento...• Software Architecture within PHP • TDD • PHPSpec - 2011 • BDD • Behat - 2011 • DDD Installing Magento How did we install Magento 1 • Large scale

Basic Dependency Injection

1 <?php2 class SampleDi {3 protected $logger;4 public function __construct(\Logger $logger) {5 $this->logger = $logger;6 }7 8 public function doSomething() {9 $this->logger->doSomething();10 }11 }

Page 38: James Cowie - Magento...• Software Architecture within PHP • TDD • PHPSpec - 2011 • BDD • Behat - 2011 • DDD Installing Magento How did we install Magento 1 • Large scale

Basic Dependency Injection

1 <?php2 class SampleDi {3 protected $logger;4 public function __construct(\Logger $logger) {5 $this->logger = $logger;6 }7 8 public function doSomething() {9 $this->logger->doSomething();10 }11 }

Page 39: James Cowie - Magento...• Software Architecture within PHP • TDD • PHPSpec - 2011 • BDD • Behat - 2011 • DDD Installing Magento How did we install Magento 1 • Large scale

Basic Dependency Injection

1 <?php2 class SampleDi {3 protected $logger;4 public function __construct(\Logger $logger) {5 $this->logger = $logger;6 }7 8 public function doSomething() {9 $this->logger->doSomething();10 }11 }

Page 40: James Cowie - Magento...• Software Architecture within PHP • TDD • PHPSpec - 2011 • BDD • Behat - 2011 • DDD Installing Magento How did we install Magento 1 • Large scale

What are the benefits of DI ?

• Creating a new instance of logger is removed from the class.

• Concrete implementation of Logger can be swapped out via

container configuration

• When running tests we can Mock the dependency

Page 41: James Cowie - Magento...• Software Architecture within PHP • TDD • PHPSpec - 2011 • BDD • Behat - 2011 • DDD Installing Magento How did we install Magento 1 • Large scale

How does this look within Magento 2

Page 42: James Cowie - Magento...• Software Architecture within PHP • TDD • PHPSpec - 2011 • BDD • Behat - 2011 • DDD Installing Magento How did we install Magento 1 • Large scale

/** * @param \Magento\Framework\View\Element\Context $context */public function __construct(

\Magento\Framework\View\Element\Context$context

) {parent::__construct($context, $data);

}

Page 43: James Cowie - Magento...• Software Architecture within PHP • TDD • PHPSpec - 2011 • BDD • Behat - 2011 • DDD Installing Magento How did we install Magento 1 • Large scale

DI Continued

Its not only Objects that can be injected.

Page 44: James Cowie - Magento...• Software Architecture within PHP • TDD • PHPSpec - 2011 • BDD • Behat - 2011 • DDD Installing Magento How did we install Magento 1 • Large scale

<type name="Magento\Cms\Model\Wysiwyg\Images\Storage"><arguments><argument name="extensions" xsi:type="array">

<item name="allowed" xsi:type="array"><item name="jpg" xsi:type="number">1</item><item name="jpeg" xsi:type="number">1</item><item name="png" xsi:type="number">1</item><item name="gif" xsi:type="number">1</item></item>

</argument></arguments>

Page 45: James Cowie - Magento...• Software Architecture within PHP • TDD • PHPSpec - 2011 • BDD • Behat - 2011 • DDD Installing Magento How did we install Magento 1 • Large scale

<type name="Magento\Cms\Model\Wysiwyg\Images\Storage"><arguments><argument name="extensions" xsi:type="array">

<item name="allowed" xsi:type="array"><item name="jpg" xsi:type="number">1</item><item name="jpeg" xsi:type="number">1</item><item name="png" xsi:type="number">1</item><item name="gif" xsi:type="number">1</item></item>

</argument></arguments>

Page 46: James Cowie - Magento...• Software Architecture within PHP • TDD • PHPSpec - 2011 • BDD • Behat - 2011 • DDD Installing Magento How did we install Magento 1 • Large scale

<type name="Magento\Cms\Model\Wysiwyg\Images\Storage"><arguments><argument name="extensions" xsi:type="array">

<item name="allowed" xsi:type="array"><item name="jpg" xsi:type="number">1</item><item name="jpeg" xsi:type="number">1</item><item name="png" xsi:type="number">1</item><item name="gif" xsi:type="number">1</item></item>

</argument></arguments>

Page 47: James Cowie - Magento...• Software Architecture within PHP • TDD • PHPSpec - 2011 • BDD • Behat - 2011 • DDD Installing Magento How did we install Magento 1 • Large scale

<type name="Magento\Cms\Model\Wysiwyg\Images\Storage"><arguments><argument name="extensions" xsi:type="array">

<item name="allowed" xsi:type="array"><item name="jpg" xsi:type="number">1</item><item name="jpeg" xsi:type="number">1</item><item name="png" xsi:type="number">1</item><item name="gif" xsi:type="number">1</item></item>

</argument></arguments>

Page 48: James Cowie - Magento...• Software Architecture within PHP • TDD • PHPSpec - 2011 • BDD • Behat - 2011 • DDD Installing Magento How did we install Magento 1 • Large scale

protected $_extensions;

public function __construct(array $extensions = []

) {$this->_extensions = $extensions;

}

Page 49: James Cowie - Magento...• Software Architecture within PHP • TDD • PHPSpec - 2011 • BDD • Behat - 2011 • DDD Installing Magento How did we install Magento 1 • Large scale

$allowed = $this->_extensions["{$type}_allowed"];

Page 50: James Cowie - Magento...• Software Architecture within PHP • TDD • PHPSpec - 2011 • BDD • Behat - 2011 • DDD Installing Magento How did we install Magento 1 • Large scale

Inversion of Control

• Separating instantiation of objects from the object

• Easier to test

• Aids with Separation of concerns

Page 51: James Cowie - Magento...• Software Architecture within PHP • TDD • PHPSpec - 2011 • BDD • Behat - 2011 • DDD Installing Magento How did we install Magento 1 • Large scale

How can we test this ?

Page 52: James Cowie - Magento...• Software Architecture within PHP • TDD • PHPSpec - 2011 • BDD • Behat - 2011 • DDD Installing Magento How did we install Magento 1 • Large scale
Page 53: James Cowie - Magento...• Software Architecture within PHP • TDD • PHPSpec - 2011 • BDD • Behat - 2011 • DDD Installing Magento How did we install Magento 1 • Large scale
Page 54: James Cowie - Magento...• Software Architecture within PHP • TDD • PHPSpec - 2011 • BDD • Behat - 2011 • DDD Installing Magento How did we install Magento 1 • Large scale

function it_should_create_a_new_cached_image($filesystem){

$filesystem->beADoubleOf(\Magento\Framework\Filesystem $fileSystem);

$this->createCachedFile($filesystem)->shouldReturnTrue();

}

Page 55: James Cowie - Magento...• Software Architecture within PHP • TDD • PHPSpec - 2011 • BDD • Behat - 2011 • DDD Installing Magento How did we install Magento 1 • Large scale

Closing thoughts

Page 56: James Cowie - Magento...• Software Architecture within PHP • TDD • PHPSpec - 2011 • BDD • Behat - 2011 • DDD Installing Magento How did we install Magento 1 • Large scale

• Composer can help create reusable packages

• Dependency Injection is a must,

• Yet be careful on how many dependencies are injected

• Testing has become easier

• Decoupling of modules is now easier

• Magento 2 service contracts / Design by contract ( Interfaces )

Page 57: James Cowie - Magento...• Software Architecture within PHP • TDD • PHPSpec - 2011 • BDD • Behat - 2011 • DDD Installing Magento How did we install Magento 1 • Large scale

Thank-you and any questions ?