Add Deactivate Module Command Line

This commit is contained in:
root
2014-03-06 00:31:27 +01:00
parent a27e317245
commit 05073d09cb
2 changed files with 197 additions and 0 deletions

View File

@@ -0,0 +1,84 @@
<?php
/*************************************************************************************/
/* */
/* Thelia */
/* */
/* Copyright (c) OpenStudio */
/* email : info@thelia.net */
/* web : http://www.thelia.net */
/* */
/* This program is free software; you can redistribute it and/or modify */
/* it under the terms of the GNU General Public License as published by */
/* the Free Software Foundation; either version 3 of the License */
/* */
/* This program is distributed in the hope that it will be useful, */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
/* GNU General Public License for more details. */
/* */
/* You should have received a copy of the GNU General Public License */
/* along with this program. If not, see <http://www.gnu.org/licenses/>. */
/* */
/*************************************************************************************/
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 <nicolas@libre-shop.com>
*
*/
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");
}
}
}

View File

@@ -0,0 +1,113 @@
<?php
/*************************************************************************************/
/* */
/* Thelia */
/* */
/* Copyright (c) OpenStudio */
/* email : info@thelia.net */
/* web : http://www.thelia.net */
/* */
/* This program is free software; you can redistribute it and/or modify */
/* it under the terms of the GNU General Public License as published by */
/* the Free Software Foundation; either version 3 of the License */
/* */
/* This program is distributed in the hope that it will be useful, */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
/* GNU General Public License for more details. */
/* */
/* You should have received a copy of the GNU General Public License */
/* along with this program. If not, see <http://www.gnu.org/licenses/>. */
/* */
/*************************************************************************************/
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 <nicolas@libre-shop.com>
*/
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;
}
}