Skip to content

Integration Test: Configuration

Fabian Schmengler / edited this page Oct 5, 2017 · 2 revisions

These exercises cover tests for module configuration

You will learn

  • how to create a Magento 2 integration test
  • how to use the object manager in integration tests
  • how to find relevant interfaces to retrieve framework information

Exercise 1: Module Activation

Create a new module in app/code/TddWizard/ExerciseModule.

The essential files in any Magento module are registration.php and module.xml to define and activate the module.

Even for those, you can start with a test.

Use Magento\Framework\Module\ModuleList to test if the module is enabled. Create registration.php and module.xml to make the test pass.

You can go a step further and test, if the module is enabled in the real environment, not only in the test environment, where all installed modules are enabled by default. This is explained in The Skeleton Module Kata by Vinai Kopp (Code).

Exercise 2: Plugin Registration

When configuring a plugin we can test if it is registered correctly. You will create a before plugin for \Magento\Customer\Api\CustomerRepositoryInterface::save().

Use Magento\Framework\Interception\PluginListInterface to get the registered plugins in the test. Create di.xml with the plugin definition to make the test pass. Your plugin method can stay empty at this point.

Exercise 3: Preference

Now do the same for a preference, for Magento\Customer\Api\Data\Customer. Use \Magento\Framework\ObjectManager\ConfigInterface to get all preferences. Update di.xml with a custom preference to make the test pass. Your class can be simply a child of the core class without additional code at this point.