5

I am following Vinai Kopp's Series on Mage2CodeKata but it gives me an error message when I declare the $objectManager. After seeing these treads:

I want to try the ObjectManager in Magento\TestFramework\ObjectManager not the one in magento/framework/TestFramework/Unit/Helper/ObjectManager

Here is my code: before class definition:

use Magento\TestFramework\ObjectManager;

after class definition:

public function testTheModuleIsConfiguredAndEnabled()
{
    /** @var ObjectManager $objectManager */
    $objectManager = ObjectManager::getInstance();

    $moduleList = $objectManager->create(ModuleList::class);

    $this->assertTrue($moduleList->has($this->moduleName));
}

Here is the error message:

PHP Fatal error: Class 'Magento\TestFramework\ObjectManager' not found

4

For unit tests you should use Magento\Framework\TestFramework\Unit\Helper\ObjectManager which allows to create any object with needed arguments (if they aren't passed, mocks will be created instead).

The Magento\TestFramework\ObjectManager is used only by integration tests and you get the PHP Fatal error: Class 'Magento\TestFramework\ObjectManager' not found error because the different autoload.php file should be used.

For more details, please, see the testing section from official the Magento 2 dev docs.

  • The test in question is an integration test, not a unit test. – Vinai Apr 9 '17 at 11:03
2

Remember to use the integration phpunit.xml.dist as the alternative configuration file for your Run/Debug configuration as following:

PHPStorm configuration for integration tests

Probably you are using the unit phpunit.xml.dist configuration file as default in your settings.

1

I was not about to get @joni's fix working with the exact same code that @vinai uses in the video. Since i ran into the exact same problem, i wanted to post what i was able to get working for me.

The call to Magento\Framework\TestFramework\Unit\Helper\ObjectManager does correctly bring in the right ObjectManager, but this class works very differently then how the Magento\TestFramework\ObjectManager works. The outline of the difference is explained by @Marius here:

How to obtain the Object Manager in Unit-Tests?

For me i was able to get the tests working with this code:

public function testTheModuleIsConfiguredAndEnabled()
{

    $objectManager = new ObjectManager($this);

    $moduleList = $objectManager->getObject(ModuleList::class);

    $this->assertTrue($moduleList->has($this->moduleName));
}

The main difference here is that you are calling in the object manager and passing the current class into it with $this. You then get a list of all the modules in the same way, but with the method getObject instead of create. After these changes the test worked for me.

  • 1
    If the Magento 2 integration test framework has been properly bootstrapped by phpunit, instantiating the ObjectManager is not required. Instead just use ObjectManager::getInstance() to get the correctly configured object manager instance. I believe the original poster didn't execute phpunit correctly to use the dev/tests/integration/phpunit.xml file, which correctly initializes the autoloader. – Vinai Apr 9 '17 at 11:05
0

You have to just use below methods to use objectmanager in your testcase php file,

public function testTheModuleIsConfiguredAndEnabled()
{
   /** @var $objectManager \Magento\TestFramework\ObjectManager */
    $objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();   

    $moduleList = $objectManager->create(ModuleList::class);

    $this->assertTrue($moduleList->has($this->moduleName));
}

Remove from top use objectmanager and try with this syntax.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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