Magento Sandboxes

Magento sandboxes

It is always good when you can test your features fast. But sometimes it’s literally hard and takes a lot of time to test something in Magento. When you make one little change in code and want to test it, it can take much more time to test it rather than make the change itself. Nevertheless it’s a common situation taking into account the complexity of Magento.

There is also a good news. Magento is a modular system and we can power it up with almost anything that we want. By default, Magento Core Team has created a complex store. Many lines of code are executed after every user request. Which is not very useful when creating and testing new functionality. We, developers, can build something much smaller that runs fast and can help us create and test features in no time. We can create our own developer sandbox!

There is a high probability that you’ve already met scripts like this one in Magento 1 projects or even used one:

<?php
/**
 * @author Atwix Team
 * @copyright Copyright (c) 2016 Atwix (https://www.atwix.com/)
 * @package sandboxes
 */

require_once "app/Mage.php";

Mage::app();
umask(0);
ini_set('display_errors', 1);

// test your stuffs

$productModel = Mage::getModel("catalog/product");

A file with this code can be placed in the directory containing index.php. Then you can request it whenever you need to test anything. You can place this file into another directory, but you will have to change the path to Mage.php file that we’ve used to include it.

Well, it is easy, but what about Magento 2? It is more complicated, but we still want to have a tool to test any feature. Is there a way to build the same kind of developer sandbox in this case? Certainly, there is!

<?php
/**
* @author Atwix Team
* @copyright Copyright (c) 2016 Atwix (https://www.atwix.com/)
* @package sandboxes
*/

require __DIR__ . "/app/bootstrap.php";

$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);
$objectManager = $bootstrap->getObjectManager();

/** Configure the Object Manager **/
$configurations = $objectManager->get(\Magento\Framework\App\ObjectManager\ConfigLoader::class)->load("frontend");
$objectManager->configure($configurations);

/**
* @var \Magento\Framework\App\State $appState
*/
$appState = $objectManager->get(\Magento\Framework\App\State::class);
$appState->setAreaCode("frontend");

/**
* Test your stuffs here
* @var \Magento\Sales\Api\OrderRepositoryInterface $orderRepo
*/
$orderRepo = $objectManager->create(\Magento\Sales\Api\OrderRepositoryInterface::class);
$order = $orderRepo->get(3);

It’s quite simple, isn’t it? We need to create an instance of Magento\Framework\App\Bootstrap class. It can create an object manager instance for us. Then we need to configure the current application state. According to this, we’ve created a singleton instance of Magento\Framework\App\State and then we’ve configured application area that is used to load configuration files (it can be frontend or adminhtml areas). Now feel free to test your features!

Here you go. Develop more and spend less time with any Magento version!

See the source code of sandbox files on GitHub.

Clients

Smart Brands Choose Us.

From Mark Cuban and Sir Richard Branson backed startups, to Inc 500 US Fastest Growing Companies and Global Brands choose us to deliver and support unparalleled eСommerce experience for their customers.