Unit Testing Shell - CConsoleCommands

So the scenario is this:

We have the shell command in protected/commands/shell/MyTestCommand.php




class MyTestCommand extends CConsoleCommand

{

	public function run($args)

	{

		//actual command code here

		return TRUE;	

	}

}



We want to unit test it.

So lets get to it.

Step one, edit the default test configuration file in protected/config/test.php

and import the Shell Classes that we need to test (which extend CConsoleCommand).




	'import'=>array(

		'application.commands.shell.*',

	),



This way our unit test application, will be able to auto load/import our MyTestCommand class.

Step two, we create the unit test file protected/tests/unit/ShellTest.php




class ShellTest extends CTestCase

{

	function testShellCommand()

	{

		$commandName='MyTest';

		$CCRunner=new CConsoleCommandRunner();

		

		$shell=new MyTestCommand($commandName,$CCRunner);

		$this->assertTrue($shell->run(array()));

	}

}



It should run 1 test and 1 assertion successfully.

Please note that this is a basic unit testing of a shell command just for starters.

Thank you for reading, feel free to correct any mistakes in my code if they exist.

If this small tutorial/example helped you in any way, show your gratitude by Voting +1 on this post.