Skip to content

Commit

Permalink
Merge pull request #167 from SenseException/command-factory
Browse files Browse the repository at this point in the history
Command factory tests
  • Loading branch information
eps90 committed Feb 21, 2015
2 parents fe2c9fb + a315064 commit 9becda4
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
3 changes: 3 additions & 0 deletions Mage/Command/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,17 @@ public static function get($commandName, Config $config)
// try a custom command
$className = 'Command\\' . $commandName;

// TODO use a custom exception
if (!class_exists($className)) {
throw new Exception('Command "' . $commandName . '" not found.');
}
}

/** @var AbstractCommand $instance */
// TODO dependencies like $config should be injected into constructor
$instance = new $className;
if (! $instance instanceOf AbstractCommand) {
// TODO use a custom exception
throw new Exception('The command ' . $commandName . ' must be an instance of Mage\Command\AbstractCommand.');
}

Expand Down
63 changes: 63 additions & 0 deletions tests/MageTest/Command/FactoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

namespace MageTest\Command;

use Mage\Command\Factory;
use PHPUnit_Framework_TestCase;

/**
* @group Mage_Command
* @group Mage_Command_Factory
*
* @group issue-167
*/
class FactoryTest extends PHPUnit_Framework_TestCase
{
private $config;

protected function setUp()
{
$this->config = $this->getMock('Mage\Config');
}

public function testGet()
{
$command = Factory::get('add', $this->config);
$this->assertInstanceOf('Mage\\Command\\BuiltIn\\AddCommand', $command);
}

/**
* @expectedException \Exception
*/
public function testGetClassNotFoundException()
{
$command = Factory::get('commanddoesntexist', $this->config);
}

public function testGetCustomCommand()
{
$this->getMockBuilder('Mage\\Command\\AbstractCommand')
->setMockClassName('MyCommand')
->getMock();

/**
* current workaround
* @link https://github.com/sebastianbergmann/phpunit-mock-objects/issues/134
*/
class_alias('MyCommand', 'Command\\MyCommand');

$command = Factory::get('my-command', $this->config);
$this->assertInstanceOf('Command\\MyCommand', $command);
}

/**
* @expectedException \Exception
* @expectedExceptionMessage The command MyInconsistentCommand must be an instance of Mage\Command\AbstractCommand.
*/
public function testGetInconsistencyException()
{
$this->getMock('Command\\MyInconsistentCommand');

$command = Factory::get('my-inconsistent-command', $this->config);
}
}

0 comments on commit 9becda4

Please sign in to comment.