From 05073d09cb0126f6859a2d10325c2744ed90d489 Mon Sep 17 00:00:00 2001 From: root Date: Thu, 6 Mar 2014 00:31:27 +0100 Subject: [PATCH] Add Deactivate Module Command Line --- .../Command/ModuleDeactivateCommand.php | 84 +++++++++++++ .../Command/ModuleDeactivateCommandTest.php | 113 ++++++++++++++++++ 2 files changed, 197 insertions(+) create mode 100755 core/lib/Thelia/Command/ModuleDeactivateCommand.php create mode 100755 core/lib/Thelia/Tests/Command/ModuleDeactivateCommandTest.php diff --git a/core/lib/Thelia/Command/ModuleDeactivateCommand.php b/core/lib/Thelia/Command/ModuleDeactivateCommand.php new file mode 100755 index 000000000..18d38778c --- /dev/null +++ b/core/lib/Thelia/Command/ModuleDeactivateCommand.php @@ -0,0 +1,84 @@ +. */ +/* */ +/*************************************************************************************/ + +namespace Thelia\Command; + +use Symfony\Component\Console\Input\InputArgument; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Output\OutputInterface; + +use Thelia\Model\ModuleQuery; + +/** + * DeActivates a module + * + * Class ModuleDeactivateCommand + * @package Thelia\Command + * @author Nicolas Villa + * + */ +class ModuleDeactivateCommand extends BaseModuleGenerate +{ + protected function configure() + { + $this + ->setName("module:deactivate") + ->setDescription("Deactivate a module") + ->addArgument( + "module" , + InputArgument::REQUIRED, + "module to deactivate" + ) + ; + } + + protected function execute(InputInterface $input, OutputInterface $output) + { + $moduleCode = $this->formatModuleName($input->getArgument("module")); + + $module = ModuleQuery::create()->findOneByCode($moduleCode); + + if (null === $module) { + throw new \RuntimeException(sprintf("module %s not found", $moduleCode)); + } + + try { + $moduleReflection = new \ReflectionClass($module->getFullNamespace()); + + $moduleInstance = $moduleReflection->newInstance(); + + $moduleInstance->deActivate(); + } catch (\Exception $e) { + throw new \RuntimeException(sprintf("Deactivation fail with Exception : [%d] %s", $e->getCode(), $e->getMessage())); + } + + //impossible to change output class in CommandTester... + if (method_exists($output, "renderBlock")) { + $output->renderBlock(array( + '', + sprintf("Deactivation succeed for module %s", $moduleCode), + '' + ), "bg=green;fg=black"); + } + } +} diff --git a/core/lib/Thelia/Tests/Command/ModuleDeactivateCommandTest.php b/core/lib/Thelia/Tests/Command/ModuleDeactivateCommandTest.php new file mode 100755 index 000000000..309a29ff8 --- /dev/null +++ b/core/lib/Thelia/Tests/Command/ModuleDeactivateCommandTest.php @@ -0,0 +1,113 @@ +. */ +/* */ +/*************************************************************************************/ +namespace Thelia\Tests\Command; + +use Symfony\Component\Console\Tester\CommandTester; +use Thelia\Command\ModuleDeactivateCommand; +use Thelia\Core\Application; +use Thelia\Model\ModuleQuery; +use Thelia\Module\BaseModule; + +/** + * Class ModuleDeactivateCommandTest + * + * @package Thelia\Tests\Command + * @author Nicolas Villa + */ +class ModuleDeactivateCommandTest extends \PHPUnit_Framework_TestCase +{ + public function testModuleDeactivateCommand() + { + $module = ModuleQuery::create()->findOne(); + + if (null !== $module) { + + $prev_activation_status = $module->getDeactivate(); + + $application = new Application($this->getKernel()); + + $module->setDeactivate(BaseModule::IS_NOT_ACTIVATED); + $module->save(); + + $moduleDeactivate = new ModuleDeactivateCommand(); + $moduleDeactivate->setContainer($this->getContainer()); + + $application->add($moduleDeactivate); + + $command = $application->find("module:deactivate"); + $commandTester = new CommandTester($command); + $commandTester->execute(array( + "command" => $command->getName(), + "module" => $module->getCode(), + )); + + $deactivated = ModuleQuery::create()->findPk($module->getId())->getDeactivate(); + + // Restore activation status + $module->setDeactivate($prev_activation_status)->save(); + + $this->assertEquals(BaseModule::IS_ACTIVATED, $deactivated); + } + } + + /** + * @expectedException \RuntimeException + * @expectedExceptionMessage module Letshopethismoduledoesnotexists not found + */ + public function testModuleDeactivateCommandUnknownModule() + { + $testedModule = ModuleQuery::create()->findOneByCode('Letshopethismoduledoesnotexists'); + + if (null == $testedModule) { + $application = new Application($this->getKernel()); + + $moduleDeactivate = new ModuleDeactivateCommand(); + $moduleDeactivate->setContainer($this->getContainer()); + + $application->add($moduleDeactivate); + + $command = $application->find("module:deactivate"); + $commandTester = new CommandTester($command); + $commandTester->execute(array( + "command" => $command->getName(), + "module" => "letshopethismoduledoesnotexists", + )); + + $out = true; + } + } + + public function getKernel() + { + $kernel = $this->getMock("Symfony\Component\HttpKernel\KernelInterface"); + + return $kernel; + } + + public function getContainer() + { + $container = new \Symfony\Component\DependencyInjection\ContainerBuilder(); + + return $container; + } +}