From 5b63f4b5d08605c5d04df1d7e0d28176ff74e7f6 Mon Sep 17 00:00:00 2001 From: Claudio Zizza Date: Sun, 7 Dec 2014 23:00:55 +0100 Subject: [PATCH] tests for command factory created --- Mage/Command/Factory.php | 3 ++ tests/MageTest/Command/FactoryTest.php | 55 ++++++++++++++++++++++++++ 2 files changed, 58 insertions(+) create mode 100644 tests/MageTest/Command/FactoryTest.php 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..8607622 --- /dev/null +++ b/tests/MageTest/Command/FactoryTest.php @@ -0,0 +1,55 @@ +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() + { + $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() + { + $command = Factory::get('my-inconsistent-command', $this->config); + } +}