Skip to content

[Question] Getting the live (non-test) module activation state during integration tests #937

Closed
@Vinai

Description

@Vinai

This is a question for help while studying the Magento 2 testing frameworks.

In a test I want to check for module activation state in an integration test.
I managed to get to it (see below), but I wonder if there is a more elegant way, as it seems like an awful lot of setup for such a small thing.

Here is the code I used:

<?php

namespace Namespace\Module;

use Magento\TestFramework\Helper\Bootstrap;
use Magento\Framework\Module\ModuleList;
use Magento\Framework\App\DeploymentConfig;
use Magento\Framework\App\DeploymentConfig\Reader as ConfigReader;
use Magento\Framework\App\Filesystem\DirectoryList;

class ModuleTest extends \PHPUnit_Framework_TestCase
{
    /**
     * @var string
     */
    private $moduleName = 'Namespace_Module';

    /**
     * @test
     * @return void
     */
    public function itShouldBeKnownToMagento()
    {
        $this->assertTrue(
           $this->getNonIntegrationTestModuleList()->has($this->moduleName),
            sprintf('The module "%s" is not known', $this->moduleName)
        );
    }

    /**
     * @return ModuleList
     */
    private function getNonIntegrationTestModuleList()
    {
        return Bootstrap::getObjectManager()->create(
            ModuleList::class,
            ['config' => $this->getNonIntegrationTestDeploymentConfig()]
        );
    }

    /**
     * @return DeploymentConfig
     */
    private function getNonIntegrationTestDeploymentConfig()
    {
        return Bootstrap::getObjectManager()->create(
            DeploymentConfig::class,
            ['reader' => $this->getNonIntegrationTestConfigReader()]
        );
    }

    /**
     * @return ConfigReader
     */
    private function getNonIntegrationTestConfigReader()
    {
        return Bootstrap::getObjectManager()->create(
            ConfigReader::class,
            ['dirList' => $this->getNonIntegrationTestDirectoryList()]
        );
    }

    /**
     * @return DirectoryList
     */
    private function getNonIntegrationTestDirectoryList()
    {
        return Bootstrap::getObjectManager()->create(
            DirectoryList::class,
            ['root' => $this->getBaseDirectoryPath()]
        );
    }

    /**
     * @return string
     */
    private function getBaseDirectoryPath()
    {
        return Bootstrap::getObjectManager()
            ->get(\Magento\Framework\Filesystem::class)
            ->getDirectoryRead(DirectoryList::ROOT)
            ->getAbsolutePath();
    }
}

PS: 🔔 @maksek :)

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions