Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
NotificationsYou must be signed in to change notification settings

yireo/Yireo_IntegrationTestHelper

Repository files navigation

This module adds various utilities to aid in creating integration tests for Magento 2.

Installation

Use the following commands to install:

composer require yireo/magento2-integration-test-helper --dev

Enable this module:

./bin/magento module:enable Yireo_IntegrationTestHelper./bin/magento setup:upgrade

Using this helper to enhance your tests

Parent classes:

  • \Yireo\IntegrationTestHelper\Test\Integration\AbstractTestCase
  • \Yireo\IntegrationTestHelper\Test\Integration\GraphQlTestCase

These classes offer some utility functions plus import numerous traits (seeTest/Integration/Traits/) with PHPUnit assertions. For instance, the following test checks for the proper registration of your module:

<?phpdeclare(strict_types=1);namespaceYireo\Example\Test\Integration;usePHPUnit\Framework\TestCase;useYireo\IntegrationTestHelper\Test\Integration\Traits\AssertModuleIsEnabled;useYireo\IntegrationTestHelper\Test\Integration\Traits\AssertModuleIsRegistered;useYireo\IntegrationTestHelper\Test\Integration\Traits\AssertModuleIsRegisteredForReal;class ModuleTestextends TestCase{use AssertModuleIsEnabled;use AssertModuleIsRegistered;use AssertModuleIsRegisteredForReal;publicfunctiontestIfModuleIsWorking()    {$this->assertModuleIsEnabled('Yireo_Example');$this->assertModuleIsRegistered('Yireo_Example');$this->assertModuleIsRegisteredForReal('Yireo_Example');    }}

Toggle TESTS_CLEANUP in integration tests configuration

When running integration tests, you probably want to frequently toggle the constantTESTS_CLEANUP fromdisabled toenabled todisabled. The following command-line easily allows for this (assuming the file is actuallydev/tests/integration/phpunit.xml cause you shouldn't modify the*.dist version):

bin/magento integration_tests:toggle_tests_cleanup

It is toggled. You can also set the value directly:

bin/magento integration_tests:toggle_tests_cleanup enabled

Generating theinstall-config-mysql.php return array

When installing Magento - as part of the procedure of running Integration Tests - the filedev/tests/integration/etc/install-config-mysql.php should return an array with all of your relevant settings, most importantly the database settings. By using the utility classYireo\IntegrationTestHelper\Utilities\InstallConfig you can quickly generate the relevant output, plus details like Redis and ElasticSearch:

<?phpdeclare(strict_types=1);useYireo\IntegrationTestHelper\Utilities\InstallConfig;return (newInstallConfig())    ->addDb('mysql80_tmpfs')    ->addRedis()    ->addElasticSearch('elasticsearch6')    ->get();

Disable modules when installing Magento

When installing Magento - as part of the procedure of running Integration Tests - the filedev/tests/integration/etc/install-config-mysql.php is modified to point to your test database. There is also a flagdisable-modules that allows you to disable specific Magento modules. Disabling modules is a benefit for performance. The utility classYireo\IntegrationTestHelper\Utilities\DisableModules allows you to generate a listing of modules to disable quicker.

In the following example, first all (!) modules that are listed in the globalapp/etc/config.php are disabled by default. But then all Magento core modules and the moduleYireo_GoogleTagManager2 are enabled (but only if they are marked as active in the global configuration):

<?phpdeclare(strict_types=1);useYireo\IntegrationTestHelper\Utilities\DisableModules;useYireo\IntegrationTestHelper\Utilities\InstallConfig;$disableModules = (newDisableModules())    ->disableAll()    ->enableMagento()    ->enableByName('Yireo_GoogleTagManager2')    ->toString();return (newInstallConfig())    ->setDisableModules($disableModules)    ->addDb('mysql80_tmpfs')    ->addRedis()    ->addElasticSearch('elasticsearch6')    ->get();

Instead of using a hard-coded value, you might also want to set an environment variableMAGENTO_MODULE - for instance, in theRun configuration in PHPStorm. This way, you can keep the sameinstall-config-mysql.php file while reusing it for variousRun configurations:

MAGENTO_MODULE=Yireo_Example

Note that if your module has dependencies, they need to be added to the same environment as well:

MAGENTO_MODULE=Yireo_Example,Yireo_Foobar

If you have a lot of requirements, you can also use theMAGENTO_MODULE_FOLDER variable instead, which parses your ownetc/module.xml and adds all declared modules to the whitelist:

MAGENTO_MODULE_FOLDER=app/code/Yireo/Example

Another example, all the Magento modules are enabled by default. But then MSI and GraphQL are disabled again, while all Yireo modules are enabled:

$disableModules = (newDisableModules())    ->disableAll()    ->enableMagento()    ->disableMagentoInventory()    ->disableGraphQl()    ->enableByPattern('Yireo_')    ->toString();

Note that if there would be a moduleYireo_ExampleGraphQl then this would be first disabled withdisableGraphQl() and then re-enabled again withenableByPattern('Yireo_'). The ordering of your methods matters!

Validating your configuration

The module also ships with a CLI command to easily check whether the currently returnedsetup:install flags make sense:

$ bin/magento integration_tests:check+--------------------+--------------------+| Setting| Value|+--------------------+--------------------+| TESTS_CLEANUP| enabled|| TESTS_MAGENTO_MODE| developer|| DB host| mysql57_production|| DB username| root|| DB password| root|| DB name| m2_test|| DB reachable| Yes|| ES host| localhost|| ES port| 9207|| ES reachable| Yes|| Redis host| 127.0.0.1|| Redis port| 6379|| Redis reachable| Yes|+--------------------+--------------------+

FAQ

Do I need ElasticSearch / OpenSearch for integration tests?

Yes, by default. No, with a little bit of work. You could just disable all Elasticsearch and Opensearch modules at installation time (see code sample below), but then Magento will still try to detect a valid search engine during the setup. This could be hacked with a modifiedsetup/src/Magento/Setup/Model/SearchConfig.php file, but it's not perfect.

$disableModules = (newDisableModules(__DIR__.'/../../../../'))    ->disableByPattern('Elasticsearch')    ->disableByPattern('Opensearch')    ...

Another option might be to configure a Docker-based dummy service that responds with a HTTP status 200 while listening to port 9200. The following is an exampledocker-compose entry for this:

elasticsearch-dummy:command:php -S 0.0.0.0:9200ports:        -9200

When using theDisableModules approach, all tests fail

TheDisableModules class tries to determine which modules are known to Magento by reading from the regularapp/etc/config.php file. If this file is outdated, because modules have been installed but not yet registered to this file, then these modules will be enabled by default through theDisableModules class approach. If you don't want this to happen, first runsetup:upgrade in the regular environment and then run the tests again. Or explicitly disable the new modules as well.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages


[8]ページ先頭

©2009-2025 Movatter.jp