The very best way that I have found to figure out how to develop with Magento 2 is to write unit tests for a module. It not only requires you to figure out how to work with phpunit, but to also look at the existing codebase for examples of how the Magento 2 team works with the same objects and handles the same kinds of tasks. Once I wrote a full set of unit tests for my first Magento 2 module, I felt that I had a much better grasp of the workings of Magento 2 than I did before I started writing the unit tests.
When learning how to develop with Magento 1, it seemed that there weren’t that many publicly available resources for the experienced developer new to Magento development. In the end, I learned the most from going through Alan Storm’s (@alanstorm) excellent series on Magento 1.
However, when it comes to Magento 2, it definitely seems that there is an abundance of information available for how to do the extremely simple stuff, but not a lot available when it comes to some of the newer technologies that Magento 2 works with or some of the more obscure customizations that some stores require. Hearing that Magento 2’s architecture was completely different than what Magento 1 employed, and that the
Mage object, that had been the core of everything you do as a Magento 1 developer, was gone, a good source for re-training seemed important for a team of Magento 1 developers. With the newness of Magento 2, the first training material I checked out was Magento’s own training videos. While they explained a lot of the dependency injection newness and reading of the very slides that were visible in the video portion, the most memorable part of the training was listening to the instructor repeat multiple file paths that were not reproduced directly in the video, and seemed to appear only in the accompanying notes.
Having a slight grasp of the high-level changes in Magento 2, it was time to find a resource that went a bit more in-depth with some real-world examples of customizations that you might make on your own Magento 2 store. That brought me back to Alan Storm’s Magento 2 series, but I still didn’t have a thorough understanding of how the intricacies of the PHP framework worked. While there was some familiarity with the system, it was not perfect, but it was time to start development with Magento 2.
Working on the first real module in Magento 2, based upon the Magento 2 Sample Module was slow and required lots of research to try to figure out how to load data and parse it, and all the other things that had become so natural with Magento 1. Once I finally got it working the way I wanted it to, I started writing unit tests to make sure that others working on the same code in the future would have some idea as to whether the changes they were making made things better or worse. At this point, it became evident that you have to know quite a bit about how all the
factory objects work, and what they return when you call
$this->collectionFactory->create(); so that you can not only create mock objects for the factory, but also the objects that are created by the factory. The best way that I found to figure out what these objects were and what to mock was to look at other tests that shipped with Magento 2 and see how they handled the exact same objects. When I started working on the next module after finishing the unit tests for the first one, the time to complete development was greatly reduced, with much less need to look outside the existing Magento 2 codebase for examples of how to accomplish tasks.
After this experience, you can be sure that I will make sure to write as many unit tests as possible, as they have already saved me from shipping some obscure bugs in my code that I would not have found until much further down the line.