Skip to content
cmuench edited this page Apr 28, 2013 · 6 revisions

Test Framework

We deliver a test framework for n98-magerun commands.

Configuration

Set the environment variable N98_MAGERUN_TEST_MAGENTO_ROOT with a path to a magento installation which can be used to run tests.

i.e.

export N98_MAGERUN_TEST_MAGENTO_ROOT=/home/myinstallation

Don't use a production environment!

Run Tests

You need PHPUnit to run the tests. If you don't have PHPUnit installed on your system you can use the following command to install all test tools at once.

composer.phar --dev install

Run PHPUnit in n98-magerun root folder. If you have installed with composer you can run:

vendor/bin/phpunit

Example Test

Test should be placed in "tests" folder according to it's structure in src folder. To prevent naming conflicts you should place test class in same namespace as the class to test.

Example:

<?php

namespace N98\Magento\Command\Cache;

use Symfony\Component\Console\Tester\CommandTester;
use N98\Magento\Command\PHPUnit\TestCase;

class ListCommandTest extends TestCase
{
    public function testExecute()
    {
       $application = $this->getApplication();
       $application->add(new ListCommand());
       $command = $this->getApplication()->find('cache:list');

       $commandTester = new CommandTester($command);
       $commandTester->execute(
           array(
               'command' => $command->getName()
               /* add options and arguments here    */
               /* 'my-argument' => 'argument-value' */
               /* '--my-option' => 'option-value'   */
           )
       );

       $this->assertRegExp('/config/', $commandTester->getDisplay());
       $this->assertRegExp('/collections/', $commandTester->getDisplay());
    }
}