4

At the company I work with, our modules are individual repositories. We add those as composer packages to our Magento 2 installation by adding GitHub as a repository in our composer.json:

"require": {
    "vendor/custom-module": "dev-master"
},
"repositories": [
    {
      "type": "git",
      "url": "[email protected]:CompanyName/custom-module.git"
    }
],
"autoload": {
    "files": [
        "registration.php"
    ],
    "psr-4": {
        "Happy\\PrivateProducts\\": ""
    }
},

This works fine, no questions there.

The next thing we want to do is set these modules up as individual testable modules using a Continuous Integration provider (CircleCI in our case). So I've added dependencies like phpunit and phpcs to the composer.json of our module:

"require-dev": {
    "phpunit/phpunit": "5.3.5",
    "squizlabs/php_codesniffer": "1.5.3"
}

We setup our master-branch to be protected and do our work in the development-branch. When we want to merge the changes we send a pull request to our master-branch. This is the moment where CircleCI kicks in.

Everything gets installed properly on our CI servers' container that is spawned. However, when I try to run phpunit or phpcs from the command line (using vender/bin/phpunit or vendor/bin/phpcs for example), I get a PHP error that all kind of Magento dependencies are missing:

PHP Fatal error:  Uncaught Error: Class 'Magento\Framework\Component\ComponentRegistrar' not found in /home/ubuntu/custom-module/registration.php:6
Stack trace:
#0 /home/ubuntu/custom-module/vendor/composer/autoload_real.php(66): require()
#1 /home/ubuntu/custom-module/vendor/composer/autoload_real.php(56): composerRequiref6475a93b986d5b841accb13703f079a('bdaf00c9b7defe0...', '/home/ubuntu/ma...')
#2 /home/ubuntu/custom-module/vendor/autoload.php(7): ComposerAutoloaderInitf6475a93b986d5b841accb13703f079a::getLoader()
#3 /home/ubuntu/custom-module/vendor/phpunit/phpunit/phpunit(45): require('/home/ubuntu/ma...')
#4 {main}

thrown in /home/ubuntu/mage2-private-products/registration.php on line 6

My question

I have a fairly good idea of why I'm getting this error. After all, I have the registration.php in my composer.json autoload-property. However, I am wondering what's best practice here?

  • Should I omit the registration.php from my composer.json-file?
  • Should I include the complete Magento framework as dev-dependency? (I don't think so because my unit tests should mock everything).
  • Should I wrap my register function with a if class_exists()?
  • Some other solution / suggestion?

What are other peoples experiences / best practices when it comes to individual module testing / CI processes with Magento 2?

Edit: I found out that if I omit my registration.php autoload in my composer.json, that Magento 2 will not see my module (which makes sense). For now I've included the Magento Framework (magento/framework) as a development dependency and added an auth.json for the credentials. I guess that's the best way to do this, but I'm still interested in other peoples' experiences.

1 Answer 1

2

In your composer.json your are autoloading registration.php, which requires Magento\Framework\Component\ComponentRegistrar. So you have a dependency on the magento/framework package. If you add it to your composer.json you should be good to go.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.