diff --git a/Mage/Command/Factory.php b/Mage/Command/Factory.php index 4088239..d5668ee 100644 --- a/Mage/Command/Factory.php +++ b/Mage/Command/Factory.php @@ -43,14 +43,17 @@ class Factory // 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.'); } diff --git a/tests/MageTest/Command/FactoryTest.php b/tests/MageTest/Command/FactoryTest.php new file mode 100644 index 0000000..b91f9b6 --- /dev/null +++ b/tests/MageTest/Command/FactoryTest.php @@ -0,0 +1,63 @@ +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); + } +}