From 891ebcd491e560f6937efc96f29e4a1c95c0db5e Mon Sep 17 00:00:00 2001 From: Etienne Roudeix Date: Wed, 18 Sep 2013 19:51:01 +0200 Subject: [PATCH] fix tests, edit reset_install.sh, manage payment modules, change debugbar namespace to theliadebugbar --- core/lib/Thelia/Command/CacheClear.php | 20 +++- .../Thelia/Command/ModuleActivateCommand.php | 27 ++--- .../lib/Thelia/Core/Template/Loop/Product.php | 11 +- .../Template/Loop/ProductSaleElements.php | 20 +++- core/lib/Thelia/Module/BaseModule.php | 14 ++- .../Command/ModuleActivateCommandTest.php | 109 ++++++++++++++++++ install/insert.sql | 2 +- local/modules/DebugBar/Config/schema.xml | 7 -- .../Config/config.xml | 4 +- .../Config/plugin.xml | 0 .../modules/TheliaDebugBar/Config/schema.xml | 6 + .../DataCollector/PropelCollector.php | 7 +- .../Listeners/DebugBarListeners.php | 7 +- .../Smarty/Plugin/DebugBar.php | 2 +- .../TheliaDebugBar.php} | 6 +- reset_install.sh | 26 +++-- 16 files changed, 203 insertions(+), 65 deletions(-) create mode 100755 core/lib/Thelia/Tests/Command/ModuleActivateCommandTest.php delete mode 100755 local/modules/DebugBar/Config/schema.xml rename local/modules/{DebugBar => TheliaDebugBar}/Config/config.xml (87%) rename local/modules/{DebugBar => TheliaDebugBar}/Config/plugin.xml (100%) create mode 100755 local/modules/TheliaDebugBar/Config/schema.xml rename local/modules/{DebugBar => TheliaDebugBar}/DataCollector/PropelCollector.php (98%) rename local/modules/{DebugBar => TheliaDebugBar}/Listeners/DebugBarListeners.php (96%) rename local/modules/{DebugBar => TheliaDebugBar}/Smarty/Plugin/DebugBar.php (99%) rename local/modules/{DebugBar/DebugBar.php => TheliaDebugBar/TheliaDebugBar.php} (95%) diff --git a/core/lib/Thelia/Command/CacheClear.php b/core/lib/Thelia/Command/CacheClear.php index 72b7571d2..1126f99a6 100755 --- a/core/lib/Thelia/Command/CacheClear.php +++ b/core/lib/Thelia/Command/CacheClear.php @@ -62,26 +62,34 @@ class CacheClear extends ContainerAwareCommand $this->clearCache($cacheDir, $output); if (!$input->getOption("without-assets")) { - $this->clearCache(THELIA_WEB_DIR . "/assets", $output); + $this->clearCache(THELIA_WEB_DIR . "assets", $output); } } protected function clearCache($dir, OutputInterface $output) { - if (!is_writable($dir)) { - throw new \RuntimeException(sprintf('Unable to write in the "%s" directory', $dir)); - } - $output->writeln(sprintf("Clearing cache in %s directory", $dir)); + try { + $directoryBrowser = new \DirectoryIterator($dir); + } catch(\UnexpectedValueException $e) { + // throws same exception code for does not exist and permission denied ... + if(!file_exists($dir)) { + $output->writeln(sprintf("%s cache dir already clear", $dir)); + return; + } + + throw $e; + } + $fs = new Filesystem(); try { $fs->remove($dir); $output->writeln(sprintf("%s cache dir cleared successfully", $dir)); } catch (IOException $e) { - $output->writeln(sprintf("error during clearing cache : %s", $e->getMessage())); + $output->writeln(sprintf("Error during clearing cache : %s", $e->getMessage())); } } } diff --git a/core/lib/Thelia/Command/ModuleActivateCommand.php b/core/lib/Thelia/Command/ModuleActivateCommand.php index 71177c02d..cddfd5290 100755 --- a/core/lib/Thelia/Command/ModuleActivateCommand.php +++ b/core/lib/Thelia/Command/ModuleActivateCommand.php @@ -63,31 +63,28 @@ class ModuleActivateCommand extends BaseModuleGenerate $module = ModuleQuery::create()->findOneByCode($moduleCode); if(null === $module) { - $output->renderBlock(array( - '', - sprintf("module %s not found", $moduleCode), - '' - ), "bg=red;fg=white"); + throw new \RuntimeException(sprintf("module %s not found", $moduleCode)); } try { + new \TheliaDebugBar\TheliaDebugBar(); + $moduleReflection = new \ReflectionClass($module->getFullNamespace()); $moduleInstance = $moduleReflection->newInstance(); $moduleInstance->activate(); } catch(\Exception $e) { - $output->renderBlock(array( - '', - sprintf("Activation fail with Exception : [%d] %s", $e->getCode(), $e->getMessage()), - '' - ), "bg=red;fg=white"); + throw new \RuntimeException(sprintf("Activation fail with Exception : [%d] %s", $e->getCode(), $e->getMessage())); } - $output->renderBlock(array( - '', - sprintf("Activation succeed for module %s", $moduleCode), - '' - ), "bg=green;fg=black"); + //impossible to change output class in CommandTester... + if (method_exists($output, "renderBlock")) { + $output->renderBlock(array( + '', + sprintf("Activation succeed for module %s", $moduleCode), + '' + ), "bg=green;fg=black"); + } } } diff --git a/core/lib/Thelia/Core/Template/Loop/Product.php b/core/lib/Thelia/Core/Template/Loop/Product.php index 4a401d464..7cbcaec5b 100755 --- a/core/lib/Thelia/Core/Template/Loop/Product.php +++ b/core/lib/Thelia/Core/Template/Loop/Product.php @@ -32,6 +32,7 @@ use Thelia\Core\Template\Element\LoopResultRow; use Thelia\Core\Template\Loop\Argument\ArgumentCollection; use Thelia\Core\Template\Loop\Argument\Argument; +use Thelia\Exception\TaxEngineException; use Thelia\Model\CategoryQuery; use Thelia\Model\CountryQuery; use Thelia\Model\CurrencyQuery; @@ -603,9 +604,13 @@ class Product extends BaseI18nLoop $price = $product->getRealLowestPrice(); - $taxedPrice = $product->getTaxedPrice( - CountryQuery::create()->findOneById(64) // @TODO : make it magic - ); + try { + $taxedPrice = $product->getTaxedPrice( + CountryQuery::create()->findOneById(64) // @TODO : make it magic + ); + } catch(TaxEngineException $e) { + $taxedPrice = null; + } // Find previous and next product, in the default category. $default_category_id = $product->getDefaultCategoryId(); diff --git a/core/lib/Thelia/Core/Template/Loop/ProductSaleElements.php b/core/lib/Thelia/Core/Template/Loop/ProductSaleElements.php index e27626129..d38d49efe 100755 --- a/core/lib/Thelia/Core/Template/Loop/ProductSaleElements.php +++ b/core/lib/Thelia/Core/Template/Loop/ProductSaleElements.php @@ -151,13 +151,21 @@ class ProductSaleElements extends BaseLoop $loopResultRow = new LoopResultRow($loopResult, $PSEValue, $this->versionable, $this->timestampable, $this->countable); $price = $PSEValue->getPrice(); - $taxedPrice = $PSEValue->getTaxedPrice( - CountryQuery::create()->findOneById(64) // @TODO : make it magic - ); + try { + $taxedPrice = $PSEValue->getTaxedPrice( + CountryQuery::create()->findOneById(64) // @TODO : make it magic + ); + } catch(TaxEngineException $e) { + $taxedPrice = null; + } $promoPrice = $PSEValue->getPromoPrice(); - $taxedPromoPrice = $PSEValue->getTaxedPromoPrice( - CountryQuery::create()->findOneById(64) // @TODO : make it magic - ); + try { + $taxedPromoPrice = $PSEValue->getTaxedPromoPrice( + CountryQuery::create()->findOneById(64) // @TODO : make it magic + ); + } catch(TaxEngineException $e) { + $taxedPromoPrice = null; + } $loopResultRow->set("ID", $PSEValue->getId()) ->set("QUANTITY", $PSEValue->getQuantity()) diff --git a/core/lib/Thelia/Module/BaseModule.php b/core/lib/Thelia/Module/BaseModule.php index dd57ea531..56b2c23af 100755 --- a/core/lib/Thelia/Module/BaseModule.php +++ b/core/lib/Thelia/Module/BaseModule.php @@ -52,7 +52,13 @@ abstract class BaseModule extends ContainerAware if($moduleModel->getActivate() == self::IS_NOT_ACTIVATED) { $moduleModel->setActivate(self::IS_ACTIVATED); $moduleModel->save(); - $this->afterActivation(); + try { + $this->afterActivation(); + } catch(\Exception $e) { + $moduleModel->setActivate(self::IS_NOT_ACTIVATED); + $moduleModel->save(); + throw $e; + } } } @@ -106,20 +112,20 @@ abstract class BaseModule extends ContainerAware $increment = 0; while(file_exists($imageDirectory . '/' . $imageFileName)) { - $imageFileName = sprintf("module/%s-%d-%d-%s", $module->getCode(), $image->getId(), $increment, $fileName); + $imageFileName = sprintf("%s-%d-%d-%s", $module->getCode(), $image->getId(), $increment, $fileName); $increment++; } $imagePath = sprintf('%s/%s', $imageDirectory, $imageFileName); if (! is_dir($imageDirectory)) { - if(! mkdir($imageDirectory, 0777, true)) { + if(! @mkdir($imageDirectory, 0777, true)) { $con->rollBack(); throw new ModuleException(sprintf("Cannot create directory : %s", $imageDirectory), ModuleException::CODE_NOT_FOUND); } } - if(! copy($filePath, $imagePath)) { + if(! @copy($filePath, $imagePath)) { $con->rollBack(); throw new ModuleException(sprintf("Cannot copy file : %s to : %s", $filePath, $imagePath), ModuleException::CODE_NOT_FOUND); } diff --git a/core/lib/Thelia/Tests/Command/ModuleActivateCommandTest.php b/core/lib/Thelia/Tests/Command/ModuleActivateCommandTest.php new file mode 100755 index 000000000..e488e60ff --- /dev/null +++ b/core/lib/Thelia/Tests/Command/ModuleActivateCommandTest.php @@ -0,0 +1,109 @@ +. */ +/* */ +/*************************************************************************************/ +namespace Thelia\Tests\Command; + +use Symfony\Component\Console\Tester\CommandTester; +use Thelia\Command\ModuleActivateCommand; +use Thelia\Core\Application; +use Thelia\Model\ModuleQuery; +use Thelia\Module\BaseModule; + +/** + * Class ModuleActivateCommandTest + * + * @package Thelia\Tests\Command + * @author Etienne Roudeix + */ +class ModuleActivateCommandTest extends \PHPUnit_Framework_TestCase +{ + public function testModuleActivateCommand() + { + $module = ModuleQuery::create()->findOne(); + + if(null !== $module) { + $application = new Application($this->getKernel()); + + $module->setActivate(BaseModule::IS_NOT_ACTIVATED); + $module->save(); + + $moduleActivate = new ModuleActivateCommand(); + $moduleActivate->setContainer($this->getContainer()); + + $application->add($moduleActivate); + + $command = $application->find("module:activate"); + $commandTester = new CommandTester($command); + $commandTester->execute(array( + "command" => $command->getName(), + "module" => $module->getCode(), + )); + + $this->assertEquals(BaseModule::IS_ACTIVATED, ModuleQuery::create()->findPk($module->getId())->getActivate()); + } + } + + /** + * @expectedException \RuntimeException + * @expectedExceptionMessage module Letshopethismoduledoesnotexists not found + */ + public function testModuleActivateCommandUnknownModule() + { + $module = ModuleQuery::create()->findOne(); + $testedModule = ModuleQuery::create()->findOneByCode('Letshopethismoduledoesnotexists'); + + if(null !== $module && null == $testedModule) { + $application = new Application($this->getKernel()); + + $module->setActivate(BaseModule::IS_NOT_ACTIVATED); + $module->save(); + + $moduleActivate = new ModuleActivateCommand(); + $moduleActivate->setContainer($this->getContainer()); + + $application->add($moduleActivate); + + $command = $application->find("module:activate"); + $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; + } +} diff --git a/install/insert.sql b/install/insert.sql index 2689c8f70..b937b347e 100755 --- a/install/insert.sql +++ b/install/insert.sql @@ -31,7 +31,7 @@ INSERT INTO `config` (`name`, `value`, `secured`, `hidden`, `created_at`, `updat INSERT INTO `module` (`id`, `code`, `type`, `activate`, `position`, `full_namespace`, `created_at`, `updated_at`) VALUES -(1, 'DebugBar', 1, 1, 1, 'DebugBar\\DebugBar', NOW(), NOW()), +(1, 'TheliaDebugBar', 1, 1, 1, 'TheliaDebugBar\\TheliaDebugBar', NOW(), NOW()), (2, 'Colissimo', 2, 0, 1, 'Colissimo\\Colissimo', NOW(), NOW()), (3, 'Cheque', 3, 0, 1, 'Cheque\\Cheque', NOW(), NOW()), (4, 'FakeCB', 3, 0, 2, 'FakeCB\\FakeCB', NOW(), NOW()); diff --git a/local/modules/DebugBar/Config/schema.xml b/local/modules/DebugBar/Config/schema.xml deleted file mode 100755 index 86ccca913..000000000 --- a/local/modules/DebugBar/Config/schema.xml +++ /dev/null @@ -1,7 +0,0 @@ - - - - - diff --git a/local/modules/DebugBar/Config/config.xml b/local/modules/TheliaDebugBar/Config/config.xml similarity index 87% rename from local/modules/DebugBar/Config/config.xml rename to local/modules/TheliaDebugBar/Config/config.xml index 12c5ccd24..564c17e66 100755 --- a/local/modules/DebugBar/Config/config.xml +++ b/local/modules/TheliaDebugBar/Config/config.xml @@ -32,13 +32,13 @@ - + %kernel.debug% - + diff --git a/local/modules/DebugBar/Config/plugin.xml b/local/modules/TheliaDebugBar/Config/plugin.xml similarity index 100% rename from local/modules/DebugBar/Config/plugin.xml rename to local/modules/TheliaDebugBar/Config/plugin.xml diff --git a/local/modules/TheliaDebugBar/Config/schema.xml b/local/modules/TheliaDebugBar/Config/schema.xml new file mode 100755 index 000000000..9056a554b --- /dev/null +++ b/local/modules/TheliaDebugBar/Config/schema.xml @@ -0,0 +1,6 @@ + + + + diff --git a/local/modules/DebugBar/DataCollector/PropelCollector.php b/local/modules/TheliaDebugBar/DataCollector/PropelCollector.php similarity index 98% rename from local/modules/DebugBar/DataCollector/PropelCollector.php rename to local/modules/TheliaDebugBar/DataCollector/PropelCollector.php index c0ce87746..2605e07dd 100755 --- a/local/modules/DebugBar/DataCollector/PropelCollector.php +++ b/local/modules/TheliaDebugBar/DataCollector/PropelCollector.php @@ -21,14 +21,17 @@ /* */ /*************************************************************************************/ -namespace DebugBar\DataCollector; +namespace TheliaDebugBar\DataCollector; + +use DebugBar\DataCollector\DataCollector; +use DebugBar\DataCollector\Renderable; use Propel\Runtime\Propel; use Psr\Log\LoggerInterface; /** * Class PropelCollector - * @package DebugBar\DataCollector + * @package TheliaDebugBar\DataCollector * @author Manuel Raynaud */ class PropelCollector extends DataCollector implements Renderable, LoggerInterface diff --git a/local/modules/DebugBar/Listeners/DebugBarListeners.php b/local/modules/TheliaDebugBar/Listeners/DebugBarListeners.php similarity index 96% rename from local/modules/DebugBar/Listeners/DebugBarListeners.php rename to local/modules/TheliaDebugBar/Listeners/DebugBarListeners.php index edcd5cb21..c7b9d015c 100755 --- a/local/modules/DebugBar/Listeners/DebugBarListeners.php +++ b/local/modules/TheliaDebugBar/Listeners/DebugBarListeners.php @@ -21,11 +21,12 @@ /* */ /*************************************************************************************/ -namespace DebugBar\Listeners; +namespace TheliaDebugBar\Listeners; + use DebugBar\DataCollector\MemoryCollector; use DebugBar\DataCollector\MessagesCollector; use DebugBar\DataCollector\PhpInfoCollector; -use DebugBar\DataCollector\PropelCollector; +use TheliaDebugBar\DataCollector\PropelCollector; use DebugBar\DataCollector\TimeDataCollector; use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Symfony\Component\HttpKernel\KernelEvents; @@ -35,7 +36,7 @@ use Thelia\Core\Event\TheliaEvents; /** * Class DebugBarListeners - * @package DebugBar\Listeners + * @package TheliaDebugBar\Listeners * @author Manuel Raynaud */ class DebugBarListeners extends BaseAction implements EventSubscriberInterface { diff --git a/local/modules/DebugBar/Smarty/Plugin/DebugBar.php b/local/modules/TheliaDebugBar/Smarty/Plugin/DebugBar.php similarity index 99% rename from local/modules/DebugBar/Smarty/Plugin/DebugBar.php rename to local/modules/TheliaDebugBar/Smarty/Plugin/DebugBar.php index 0cd1abee9..b2aefa790 100755 --- a/local/modules/DebugBar/Smarty/Plugin/DebugBar.php +++ b/local/modules/TheliaDebugBar/Smarty/Plugin/DebugBar.php @@ -21,7 +21,7 @@ /* */ /*************************************************************************************/ -namespace DebugBar\Smarty\Plugin; +namespace TheliaDebugBar\Smarty\Plugin; use Thelia\Core\Template\Smarty\AbstractSmartyPlugin; use Thelia\Core\Template\Smarty\an; use Thelia\Core\Template\Smarty\SmartyPluginDescriptor; diff --git a/local/modules/DebugBar/DebugBar.php b/local/modules/TheliaDebugBar/TheliaDebugBar.php similarity index 95% rename from local/modules/DebugBar/DebugBar.php rename to local/modules/TheliaDebugBar/TheliaDebugBar.php index c6339bb9b..da9fddf12 100755 --- a/local/modules/DebugBar/DebugBar.php +++ b/local/modules/TheliaDebugBar/TheliaDebugBar.php @@ -21,11 +21,11 @@ /* */ /*************************************************************************************/ -namespace DebugBar; +namespace TheliaDebugBar; use Thelia\Module\BaseModule; -class DebugBar extends BaseModule +class TheliaDebugBar extends BaseModule { /** * YOU HAVE TO IMPLEMENT HERE ABSTRACT METHODD FROM BaseModule Class @@ -49,6 +49,6 @@ class DebugBar extends BaseModule public function getCode() { - return 'DebugBar'; + return 'TheliaDebugBar'; } } diff --git a/reset_install.sh b/reset_install.sh index f5244b0c9..8280173d9 100755 --- a/reset_install.sh +++ b/reset_install.sh @@ -2,41 +2,43 @@ # @author Guillaume MOREL # v0.2 -echo -e "\033[47m\033[1;31m\n[WARN] This script will reset this Thelia2 install\n\033[0m" +echo -e "\033[47m\033[1;31m\n[WARNING] This script will reset this Thelia2 install\nPress ENTER to continue or ^C to cancel\033[0m" -echo -e "\n\e[01;34m[INFO] Clearing caches\e[00m\n" +read test + +echo -e "\n\033[01;34m[INFO] Clearing caches\033[00m\n" php Thelia cache:clear -echo -e "\n\e[01;34m[INFO] Downloading vendors\e[00m\n" +echo -e "\n\033[01;34m[INFO] Downloading vendors\033[00m\n" composer install --prefer-dist --optimize-autoloader cd local/config/ -echo -e "\n\e[01;34m[INFO] Building Models file\e[00m\n" +echo -e "\n\033[01;34m[INFO] Building Models file\033[00m\n" ../../bin/propel build -v --output-dir=../../core/lib/ -echo -e "\n\e[01;34m[INFO] Building SQL CREATE file\e[00m\n" +echo -e "\n\033[01;34m[INFO] Building SQL CREATE file\033[00m\n" ../../bin/propel sql:build -v --output-dir=../../install/ -echo -e "\n\e[01;34m[INFO] Reloading Thelia2 database\e[00m\n" +echo -e "\n\033[01;34m[INFO] Reloading Thelia2 database\033[00m\n" cd ../.. rm install/sqldb.map php Thelia thelia:dev:reloadDB -echo -e "\n\e[01;34m[INFO] Installing fixtures\e[00m\n" +echo -e "\n\033[01;34m[INFO] Installing fixtures\033[00m\n" php install/faker.php -echo -e "\n\e[01;34m[INFO] Adding admin\e[00m\n" +echo -e "\n\033[01;34m[INFO] Adding admin\033[00m\n" php Thelia thelia:create-admin --login_name thelia2 --password thelia2 --last_name thelia2 --first_name thelia2 -echo -e "\n\e[01;34m[INFO] Clearing caches\e[00m\n" +echo -e "\n\033[01;34m[INFO] Clearing caches\033[00m\n" php Thelia cache:clear -echo -e "\n\e[01;34m[INFO] Activating Delivery Module(s)\e[00m\n" +echo -e "\n\033[01;34m[INFO] Activating Delivery Module(s)\033[00m\n" php Thelia module:activate Colissimo -echo -e "\n\e[01;34m[INFO] Activating Payment Module(s)\e[00m\n" +echo -e "\n\033[01;34m[INFO] Activating Payment Module(s)\033[00m\n" php Thelia module:activate Cheque php Thelia module:activate FakeCB -echo -e "\n\e[00;32m[SUCCESS] Reset done\e[00m\n" \ No newline at end of file +echo -e "\n\033[00;32m[SUCCESS] Reset done\033[00m\n" \ No newline at end of file