-
Notifications
You must be signed in to change notification settings - Fork 224
Writing Tests (Own Modules)
We provide a test framework to test 3rd party modules. It's highly recommended to secure/stabalize code by unit tests.
The test framework is available on github (https://github.com/netz98/n98-magerun2-test-framework) and contains base test cases for PHPUnit.
We offer an example module: https://github.com/netz98/n98-magerun2-example-module
The test runner requires a vanilla Magento 2 installation. To run test suite it's necessary to set the environment variable N98_MAGERUN2_TEST_MAGENTO_ROOT with the root path to the installation.
All unit tests run against this installation. Please don't use any production environment!
Example Test Case:
<?php
namespace Acme\Example\Command;
use N98\Magento\Command\PHPUnit\TestCase;
use Symfony\Component\Console\Tester\CommandTester;
class FooCommandTest extends TestCase
{
/**
* @test
*/
public function testOutput()
{
/**
* Load module config for unit test. In this case the relative
* path from current test case.
*/
$this->loadConfigFile(__DIR__ . '/../../n98-magerun2.yaml');
/**
* Test if command could be found
*/
$command = $this->getApplication()->find('foo');
/**
* Call command
*/
$commandTester = new CommandTester($command);
$commandTester->execute(
[
]
);
}
}
Create a reference file which contains the content of the code generator. In the following test case the file is place in "DIR /_files/ExampleSomething.php".
The method mockWriterFileWriteFileAssertion
of the TestCase class mocks the module writer and compares the output against the reference file.
<?php
namespace Acme\Example\Command\CodeGenerator;
use N98\Magento\Command\Developer\Console\PHPUnit\TestCase;
class MakeSomethingCommandTest extends TestCase
{
/**
* @test
*/
public function testGenerator()
{
$command = new MakeSomethingCommand();
$commandTester = $this->createCommandTester($command);
$command->setCurrentModuleName('Acme_Example');
$writerMock = $this->mockWriterFileWriteFileAssertion(
__DIR__ . '/_files/ExampleSomething.php'
);
$command->setCurrentModuleDirectoryWriter($writerMock);
$commandTester->execute([
/* pass your parameters */
]);
}
}
N98_MAGERUN2_TEST_MAGENTO_ROOT=/path/to/magento/root ./vendor/bin/phpunit
Example module structure:
.
├── README.md
├── composer.json
├── n98-magerun2.yaml
├── phpunit.xml.dist
├── src
│ └── Command
│ └── ExampleCommand.php
└── tests
└── Command
└── ExampleCommandTest.php
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
bootstrap="vendor/autoload.php"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
<coverage includeUncoveredFiles="true">
<include>
<directory>src</directory>
</include>
</coverage>
<testsuites>
<testsuite name="My Custom n98-magerun2 Commands">
<directory>tests</directory>
</testsuite>
</testsuites>
</phpunit>
Currently there are three basedirs available to place custom modules.
- A global folder for the system:
/usr/local/share/n98-magerun2/modules
- A folder inside the user’s home dir.
~/.n98-magerun2/modules
- A folder inside the Magento installation
MAGENTO_ROOT/lib/n98-magerun2/modules
Every config in a module will then be merged together. The order of config merging process of the complete n98-magerun tool is now the following:
- [DIST] config.yaml inside the phar file
- [Module-Level] found module config as described above
- [System-Level] config in /etc/n98-magerun2.yaml
- [User-Level] ~/n98-magerun2.yaml
- [Project-Level] MAGENTO_ROOT/app/etc/n98-magerun2.yaml
{
"name": "acme/example",
"description": "Some commands",
"require": {
"n98/magerun2": "1.2.*"
},
"require-dev": {
"n98/magerun2-test-framework": "dev-master",
"phpunit/phpunit": "~4.1.0"
},
"autoload-dev": {
"psr-4": {
"Acme\\Example\\": "tests"
}
},
"autoload": {
"psr-4": {
"Acme\\Example\\": "src"
}
}
}
To add a n98-magerun2 module via composer in a single project we need to add a post-install-command in the composer.json of the main project.
Add this to your project composer.json:
"post-install-cmd": [
"mkdir -p lib/n98-magerun2/modules && ln -rsfT vendor/acme/example lib/n98-magerun2/modules/example"
]
Modify the path so that it matches your modules name.