From 5e43c4e044df1d21467402f95604fdaeb2ffd318 Mon Sep 17 00:00:00 2001 From: Claudio Zizza Date: Sun, 7 Dec 2014 23:00:19 +0100 Subject: [PATCH 1/6] Dummy commands created for testing custom command behaviour --- tests/Dummies/Command/MyCommand.php | 13 +++++++++++++ tests/Dummies/Command/MyInconsistentCommand.php | 11 +++++++++++ 2 files changed, 24 insertions(+) create mode 100644 tests/Dummies/Command/MyCommand.php create mode 100644 tests/Dummies/Command/MyInconsistentCommand.php diff --git a/tests/Dummies/Command/MyCommand.php b/tests/Dummies/Command/MyCommand.php new file mode 100644 index 0000000..20469e3 --- /dev/null +++ b/tests/Dummies/Command/MyCommand.php @@ -0,0 +1,13 @@ + Date: Sun, 7 Dec 2014 23:00:55 +0100 Subject: [PATCH 2/6] 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); + } +} From 6c5aaa04da0d573f2c2bddd27ce78183c787efab Mon Sep 17 00:00:00 2001 From: Claudio Zizza Date: Mon, 9 Feb 2015 22:38:43 +0100 Subject: [PATCH 3/6] dummy classes of Command Factory tests removed --- tests/Dummies/Command/MyCommand.php | 13 ------------- tests/Dummies/Command/MyInconsistentCommand.php | 11 ----------- 2 files changed, 24 deletions(-) delete mode 100644 tests/Dummies/Command/MyCommand.php delete mode 100644 tests/Dummies/Command/MyInconsistentCommand.php diff --git a/tests/Dummies/Command/MyCommand.php b/tests/Dummies/Command/MyCommand.php deleted file mode 100644 index 20469e3..0000000 --- a/tests/Dummies/Command/MyCommand.php +++ /dev/null @@ -1,13 +0,0 @@ - Date: Mon, 9 Feb 2015 22:40:08 +0100 Subject: [PATCH 4/6] dummy classes replaced with mocks for Command Factory tests --- tests/MageTest/Command/FactoryTest.php | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/tests/MageTest/Command/FactoryTest.php b/tests/MageTest/Command/FactoryTest.php index 8607622..982ee56 100644 --- a/tests/MageTest/Command/FactoryTest.php +++ b/tests/MageTest/Command/FactoryTest.php @@ -4,16 +4,12 @@ namespace MageTest\Command; use Mage\Command\Factory; use PHPUnit_Framework_TestCase; -use PHPUnit_Framework_Constraint_IsInstanceOf; - -require_once(__DIR__ . '/../../Dummies/Command/MyCommand.php'); -require_once(__DIR__ . '/../../Dummies/Command/MyInconsistentCommand.php'); /** * @group Mage_Command * @group Mage_Command_Factory * - * @todo test case for naming convention + * @group issue-167 */ class FactoryTest extends PHPUnit_Framework_TestCase { @@ -40,6 +36,16 @@ class FactoryTest extends PHPUnit_Framework_TestCase public function testGetCustomCommand() { + $this->getMockBuilder('Mage\\Command\\AbstractCommand') + ->setMockClassName('MyCommand') + ->getMock(); + + /** + * current workaround + * @see 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); } @@ -50,6 +56,8 @@ class FactoryTest extends PHPUnit_Framework_TestCase */ public function testGetInconsistencyException() { + $this->getMock('Command\\MyInconsistentCommand'); + $command = Factory::get('my-inconsistent-command', $this->config); } } From 8bd7f6c56c2ce7035fbbd9cf2f6ba92eb420f590 Mon Sep 17 00:00:00 2001 From: Claudio Zizza Date: Wed, 11 Feb 2015 21:21:19 +0100 Subject: [PATCH 5/6] phpunit annotations added --- tests/MageTest/Command/FactoryTest.php | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/tests/MageTest/Command/FactoryTest.php b/tests/MageTest/Command/FactoryTest.php index 982ee56..1920e21 100644 --- a/tests/MageTest/Command/FactoryTest.php +++ b/tests/MageTest/Command/FactoryTest.php @@ -20,6 +20,9 @@ class FactoryTest extends PHPUnit_Framework_TestCase $this->config = $this->getMock('Mage\Config'); } + /** + * @covers Factory::get + */ public function testGet() { $command = Factory::get('add', $this->config); @@ -28,12 +31,16 @@ class FactoryTest extends PHPUnit_Framework_TestCase /** * @expectedException \Exception + * @covers Factory::get */ public function testGetClassNotFoundException() { $command = Factory::get('commanddoesntexist', $this->config); } + /** + * @covers Factory::get + */ public function testGetCustomCommand() { $this->getMockBuilder('Mage\\Command\\AbstractCommand') @@ -42,7 +49,7 @@ class FactoryTest extends PHPUnit_Framework_TestCase /** * current workaround - * @see https://github.com/sebastianbergmann/phpunit-mock-objects/issues/134 + * @link https://github.com/sebastianbergmann/phpunit-mock-objects/issues/134 */ class_alias('MyCommand', 'Command\\MyCommand'); @@ -53,6 +60,7 @@ class FactoryTest extends PHPUnit_Framework_TestCase /** * @expectedException \Exception * @expectedExceptionMessage The command MyInconsistentCommand must be an instance of Mage\Command\AbstractCommand. + * @covers Factory::get */ public function testGetInconsistencyException() { From 5207b29958604eaed6481e58fe5f8b391c4f918d Mon Sep 17 00:00:00 2001 From: Jakub Turek Date: Tue, 17 Feb 2015 21:22:07 +0100 Subject: [PATCH 6/6] Remove "covers" annotations for static methods --- tests/MageTest/Command/FactoryTest.php | 8 -------- 1 file changed, 8 deletions(-) diff --git a/tests/MageTest/Command/FactoryTest.php b/tests/MageTest/Command/FactoryTest.php index 1920e21..b91f9b6 100644 --- a/tests/MageTest/Command/FactoryTest.php +++ b/tests/MageTest/Command/FactoryTest.php @@ -20,9 +20,6 @@ class FactoryTest extends PHPUnit_Framework_TestCase $this->config = $this->getMock('Mage\Config'); } - /** - * @covers Factory::get - */ public function testGet() { $command = Factory::get('add', $this->config); @@ -31,16 +28,12 @@ class FactoryTest extends PHPUnit_Framework_TestCase /** * @expectedException \Exception - * @covers Factory::get */ public function testGetClassNotFoundException() { $command = Factory::get('commanddoesntexist', $this->config); } - /** - * @covers Factory::get - */ public function testGetCustomCommand() { $this->getMockBuilder('Mage\\Command\\AbstractCommand') @@ -60,7 +53,6 @@ class FactoryTest extends PHPUnit_Framework_TestCase /** * @expectedException \Exception * @expectedExceptionMessage The command MyInconsistentCommand must be an instance of Mage\Command\AbstractCommand. - * @covers Factory::get */ public function testGetInconsistencyException() {