As you know ZF2 is a general purpose framework with a wide set of features. One of those is adding your personal CLI commands that you can require via your preferred shell application:

php public/index.php my-custom-command

The command creation is pretty simple, just create a simple controller that implements your business logic

<?php
namespace ModuleNameController;

use ZendMvcControllerAbstractConsoleController;

class MyFeatureController extends AbstractConsoleController
{
    public function helloAction()
    {
        return "This is my incredible CLI command!";
    }
}

In your configuration you have to create a new custom route in your router that map a new CLI execution

<?php
return array(
   'controllers' => array(
        'invokables' => array(
            "ModuleName\\Controller\\MyFeature" => "ModuleName\\Controller\\MyFeatureController"
        )
   ),    
   'console' => array(
        'router' => array(
            'routes' => array(
                'version-bump' => array(
                    'options' => array(
                        'route'    => "my-custom-command",
                        'defaults' => array(
                            'controller' => "ModuleName\\Controller\\MyFeature",
                            'action'     => "hello"
                        )
                    )
                ),
            )
        )
    ),
);

Now that you have all rules in place you can add a simple test in order to check your functionalities. In ZF2, testing your features is quite simple thanks to a layer built on top of PHPUnit and we have many custom assertions that we can use in order to test our expectations. Just see the test case

<?php
namespace CorleyVersionController;

use ZendTestPHPUnitControllerAbstractConsoleControllerTestCase;

class MyFeatureControllerTest extends AbstractConsoleControllerTestCase
{
    public function setUp()
    {
        $this->setApplicationConfig(include __DIR__ . '/application.conf.php');
    }

    public function testMyCommandReplyAsAgreed()
    {
        $this->dispatch('my-custom-command');

        $this->assertConsoleOutputContains("This is my incredible CLI command!");
    }
}

As you can see the test case does not extends the typical “PHPUnit_Framework_TestCase” but “AbstractConsoleControllerTestCase” instead, provided by ZF2. Thanks to this inherit we can set up the testcase in order to bootstrap our module in isolation, thank to the “setApplicationConfing” method that can uses an ad-hoc test configuration like this:

<?php
return [
    "modules" => [
        "ModuleName",
    ],
    'module_listener_options' => [
        "module_paths" => [
            "ModuleName" => dirname(__DIR__),
        ],
        'config_glob_paths' => [],
    ],
];

The configuration prepares our module without including our application stack. This is important because typically we want to develop a Module that can runs in different applications and not in just one project. The test method is so simple, it just requires our command and assert that there is the sentence as an output.

Comments

comments powered by Disqus

cloudparty

Follow Us