0

I am trying to create a integration test to check if my module is enabled or not.

Following is the code in my test class

<?php

namespace Anshu\Learning\Test\Integration;

use Magento\Framework\App\DeploymentConfig;
use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\Component\ComponentRegistrar;
use Magento\Framework\Module\ModuleList;
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
use PHPUnit\Framework\TestCase;

class LearningConfigTest extends TestCase
{
    private $moduleName = 'Anshu_Learning';

    public function testIsModuleConfiguredAndEnabled()
    {
        /**
         * @var ObjectManager $objectManager
         */
        $objectManager = new ObjectManager($this);

        /**
         * @var DirectoryList $dirList
         */
        $dirList = $objectManager->getObject(DirectoryList::class, ['root' => BP]);

        /**
         * @var DeploymentConfig\Reader $configReader
         */
        $configReader = $objectManager->getObject(DeploymentConfig\Reader::class, ['dirList' => $dirList]);

        /**
         * @var DeploymentConfig $deploymentConfig
         */
        $deploymentConfig = $objectManager->getObject(DeploymentConfig::class, ['reader' => $configReader]);

        /**
         * @var ModuleList $moduleList
         */
        $moduleList = $objectManager->getObject(ModuleList::class, ['config' => $deploymentConfig]);

        $this->assertTrue($moduleList->has($this->moduleName), 'Module is not enabled');
    }
}

While running the test I am getting error message Use of undefined constant BP - assumed 'BP' (this will throw an Error in a future version of PHP)

And when I am changing BP to DirectoryList::ROOT then I am getting following error message

array_keys() expects parameter 1 to be array, null given
 vendor/magento/framework/App/DeploymentConfig/Reader.php:113
 vendor/magento/framework/App/DeploymentConfig.php:140
 vendor/magento/framework/App/DeploymentConfig.php:70
 vendor/magento/framework/Module/ModuleList.php:143
 vendor/magento/framework/Module/ModuleList.php:114
 app/code/Anshu/Learning/Test/Integration/LearningConfigTest.php:49

PHP version is 7.2

May someone can help me to figure out and fix the issue.

1 Answer 1

0

Try to import this class Magento\TestFramework\ObjectManager instead of Magento\Framework\TestFramework\Unit\Helper\ObjectManager.

Then initialize ObjectManager by this:

$objectManager = ObjectManager::getInstance();
2

Your Answer

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

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