public function testTidy() { CM_Bootloader::getInstance()->setDebug(true); $page = $this->_createPage('CM_Page_Example'); $html = $this->_renderPage($page); $this->assertTidy($html, false); }
public function iconRefresh() { /** @var CM_File[] $svgFileList */ $svgFileList = array(); foreach (CM_Bootloader::getInstance()->getModules() as $moduleName) { $iconPath = CM_Util::getModulePath($moduleName) . 'layout/default/resource/img/icon/'; foreach (glob($iconPath . '*.svg') as $svgPath) { $svgFile = new CM_File($svgPath); $svgFileList[strtolower($svgFile->getFileName())] = $svgFile; } } if (0 === count($svgFileList)) { throw new CM_Exception_Invalid('Cannot process `0` icons'); } $this->_getStreamOutput()->writeln('Processing ' . count($svgFileList) . ' unique icons...'); $dirWork = CM_File::createTmpDir(); $dirBuild = $dirWork->joinPath('/build'); foreach ($svgFileList as $fontFile) { $fontFile->copyToFile($dirWork->joinPath($fontFile->getFileName())); } CM_Util::exec('fontcustom', array('compile', $dirWork->getPathOnLocalFilesystem(), '--no-hash', '--autowidth', '--font-name=icon-webfont', '--output=' . $dirBuild->getPathOnLocalFilesystem())); $cssFile = $dirBuild->joinPath('/icon-webfont.css'); $less = preg_replace('/url\\("(?:.*?\\/)(.+?)(\\??#.+?)?"\\)/', 'url(urlFont("\\1") + "\\2")', $cssFile->read()); CM_File::create(DIR_PUBLIC . 'static/css/library/icon.less', $less); foreach (glob($dirBuild->joinPath('/icon-webfont.*')->getPathOnLocalFilesystem()) as $fontPath) { $fontFile = new CM_File($fontPath); $fontFile->rename(DIR_PUBLIC . 'static/font/' . $fontFile->getFileName()); } $dirWork->delete(true); $this->_getStreamOutput()->writeln('Created web-font and stylesheet.'); }
public function __construct() { $this->_events = array(); $this->_storage = new CM_Clockwork_Storage_Memory(); $this->_timeZone = CM_Bootloader::getInstance()->getTimeZone(); $this->_startTime = $this->_getCurrentDateTimeUTC(); }
public function testWrite() { $streamPath = CM_Bootloader::getInstance()->getDirTmp() . 'bar'; $outputStream = $this->getMockBuilder('CM_OutputStream_Stream_Abstract')->setConstructorArgs(array($streamPath))->getMockForAbstractClass(); /** @var CM_OutputStream_Stream_Abstract $outputStream */ $outputStream->write('foo'); $this->assertSame('foo', file_get_contents($streamPath)); }
public function testRead() { $streamPath = CM_Bootloader::getInstance()->getDirTmp() . 'bar'; $stream = fopen($streamPath, 'w'); fwrite($stream, 'foo'); /** @var CM_InputStream_Stream_Abstract $stream */ $stream = $this->getMockForAbstractClass('CM_InputStream_Stream_Abstract', array($streamPath)); $this->assertSame('foo', $stream->read('hint')); }
public function testConstructDefaultPrefix() { $pathFile = CM_Bootloader::getInstance()->getDirTmp() . 'foo'; $adapter = new CM_File_Filesystem_Adapter_Local(); $filesystem = new CM_File_Filesystem($adapter); $file = new CM_File($pathFile, $filesystem); $file->write('hello'); $this->assertSame('/', $adapter->getPathPrefix()); $this->assertSame('hello', $adapter->read($pathFile)); }
/** * @param array[] $servers */ public function __construct(array $servers) { $this->_memcache = new Memcache(); foreach ($servers as $server) { $this->_memcache->addServer($server['host'], $server['port'], true, 1, 1, 15, true, function ($host, $port) { $warning = new CM_Exception('Cannot connect to memcached host `' . $host . '` on port `' . $port . '`', CM_Exception::WARN); CM_Bootloader::getInstance()->getExceptionHandler()->handleException($warning); }); } }
/** * @param CM_Site_Abstract $site * @return string * @throws CM_Exception_Invalid */ private function _getAppClassName(CM_Site_Abstract $site) { foreach ($site->getModules() as $moduleName) { $file = new CM_File(DIR_ROOT . CM_Bootloader::getInstance()->getModulePath($moduleName) . 'library/' . $moduleName . '/App.js'); if ($file->exists()) { return $moduleName . '_App'; } } throw new CM_Exception_Invalid('No App class found'); }
/** * @param string $pathRoot * @throws CM_Exception_Invalid */ public function __construct($pathRoot) { if (self::$_instance) { throw new CM_Exception_Invalid('Bootloader already instantiated'); } self::$_instance = $this; mb_internal_encoding('UTF-8'); umask(0); define('DIR_ROOT', $pathRoot); $this->_debug = (bool) getenv('CM_DEBUG'); }
/** * @param string $type * @param string $file * @param CM_Frontend_Render $render * @return string * @throws CM_Exception_Invalid */ function smarty_helper_resourceJs($type, $file, $render) { if (!in_array($type, array('vendor', 'library'))) { throw new CM_Exception_Invalid('Invalid type `' . $type . '` provided'); } if (CM_Bootloader::getInstance()->isDebug() && $type === 'library' && $file === 'library.js') { return smarty_helper_resourceJs_libraryDebug($render); } $url = $render->getUrlResource($type . '-js', $file); return '<script type="text/javascript" src="' . $url . '" crossorigin="anonymous"></script>' . PHP_EOL; }
public function testSetNull() { $environment = new CM_Frontend_Environment(); $this->assertEquals(CM_Site_Abstract::factory(), $environment->getSite()); $this->assertNull($environment->getViewer()); $this->assertNull($environment->getLanguage()); $this->assertSame('en', $environment->getLocale()); $this->assertEquals(CM_Bootloader::getInstance()->getTimeZone(), $environment->getTimeZone()); $this->assertSame(CM_Bootloader::getInstance()->isDebug(), $environment->isDebug()); $this->assertNull($environment->getLocation()); }
public function __construct(CM_Site_Abstract $site) { $content = ''; foreach (array_reverse($site->getModules()) as $moduleName) { $libraryPath = DIR_ROOT . CM_Bootloader::getInstance()->getModulePath($moduleName) . 'client-vendor/after-body/'; foreach (CM_Util::rglob('*.js', $libraryPath) as $path) { $content .= (new CM_File($path))->read() . ';' . PHP_EOL; } } $this->_content = $content; }
/** * @param string $verbName * @param CM_Model_User|int $actor * @param int $typeEmail */ public function __construct($verbName, $actor, $typeEmail) { parent::__construct($verbName, $actor); $typeEmail = (int) $typeEmail; try { $className = CM_Mail::_getClassName($typeEmail); $this->_nameEmail = ucwords(CM_Util::uncamelize(str_replace('_', '', preg_replace('#\\A[^_]++_[^_]++_#', '', $className)), ' ')); } catch (CM_Class_Exception_TypeNotConfiguredException $exception) { $exception->setSeverity(CM_Exception::WARN); CM_Bootloader::getInstance()->getExceptionHandler()->handleException($exception); $this->_nameEmail = (string) $typeEmail; } }
/** * @throws Exception */ public function runAndSendWorkload() { $workload = $this->_workload; $result = new CM_Process_WorkloadResult(); try { $return = $workload($result); $result->setResult($return); } catch (Exception $e) { CM_Bootloader::getInstance()->getExceptionHandler()->handleException($e); $result->setException($e); } fwrite($this->_ipcStream, serialize($result)); }
public function testSetNull() { $defaultCurrency = CM_Model_Currency::create('840', 'USD'); $environment = new CM_Frontend_Environment(); $this->assertEquals(CM_Site_Abstract::factory(), $environment->getSite()); $this->assertNull($environment->getViewer()); $this->assertNull($environment->getLanguage()); $this->assertSame('en', $environment->getLocale()); $this->assertEquals(CM_Bootloader::getInstance()->getTimeZone(), $environment->getTimeZone()); $this->assertSame(CM_Bootloader::getInstance()->isDebug(), $environment->isDebug()); $this->assertNull($environment->getLocation()); $this->assertEquals($defaultCurrency, $environment->getCurrency()); $this->assertNull($environment->getClientDevice()); }
protected function _printException(Exception $exception) { $output = new CM_OutputStream_Stream_Output(); $formatter = new CM_ExceptionHandling_Formatter_Html(); if (!headers_sent()) { header('HTTP/1.1 500 Internal Server Error'); header('Content-Type: text/html'); } if (!CM_Bootloader::getInstance()->isDebug()) { $output->writeln('Internal server error'); } else { $output->writeln($formatter->formatException($exception)); } }
public function __construct(CM_Frontend_Render $render) { parent::__construct($render); $extensions = array('css', 'less'); foreach (array_reverse($render->getSite()->getModules()) as $moduleName) { $libraryPath = DIR_ROOT . CM_Bootloader::getInstance()->getModulePath($moduleName) . 'client-vendor/'; foreach ($extensions as $extension) { foreach (CM_Util::rglob('*.' . $extension, $libraryPath) as $path) { $file = new CM_File($path); $this->add($file->read()); } } } }
/** * @runInSeparateProcess * @preserveGlobalState disabled */ public function testForkAndWaitForChildrenWithResults() { $bootloader = CM_Bootloader::getInstance(); $exceptionHandlerBackup = $bootloader->getExceptionHandler(); /** * Increase print-severity to make sure "child 3"'s exception output doesn't disturb phpunit */ $exceptionHandler = new CM_ExceptionHandling_Handler_Cli(); $exceptionHandler->setPrintSeverityMin(CM_Exception::FATAL); $bootloader->setExceptionHandler($exceptionHandler); $process = CM_Process::getInstance(); $process->fork(function () { usleep(100 * 1000); return 'Child 1 finished'; }); $process->fork(function () { usleep(50 * 1000); return array('msg' => 'Child 2 finished'); }); $process->fork(function (CM_Process_WorkloadResult $result) { usleep(150 * 1000); throw new CM_Exception('Child 3 finished'); }); $process->fork(function (CM_Process_WorkloadResult $result) { usleep(200 * 1000); $result->setException(new CM_Exception('Child 4 finished')); }); $workloadResultList = $process->waitForChildren(); $this->assertCount(4, $workloadResultList); $this->assertSame('Child 1 finished', $workloadResultList[1]->getResult()); $this->assertSame(null, $workloadResultList[1]->getException()); $this->assertTrue($workloadResultList[1]->isSuccess()); $this->assertSame(array('msg' => 'Child 2 finished'), $workloadResultList[2]->getResult()); $this->assertSame(null, $workloadResultList[2]->getException()); $this->assertTrue($workloadResultList[2]->isSuccess()); $this->assertSame(null, $workloadResultList[3]->getResult()); $this->assertSame('Child 3 finished', $workloadResultList[3]->getException()->getMessage()); $this->assertFalse($workloadResultList[3]->isSuccess()); $errorLog = new CM_Paging_Log_Error(); $this->assertSame(1, $errorLog->getCount()); $this->assertContains('Child 3 finished', $errorLog->getItem(0)['msg']); $this->assertSame(null, $workloadResultList[4]->getResult()); $this->assertSame('Child 4 finished', $workloadResultList[4]->getException()->getMessage()); $this->assertFalse($workloadResultList[4]->isSuccess()); $this->assertSame(1, $errorLog->getCount()); $bootloader->setExceptionHandler($exceptionHandlerBackup); }
/** * @param \Elastica\Client|null $client * @param int|null $version * @throws CM_Exception_Invalid */ public function __construct(Elastica\Client $client = null, $version = null) { if (null === static::INDEX_NAME) { throw new CM_Exception_Invalid('Index name has to be set'); } $indexName = CM_Bootloader::getInstance()->getDataPrefix() . static::INDEX_NAME; if ($version) { $indexName .= '.' . $version; } $typeName = static::INDEX_NAME; if (!$client) { $client = CM_Service_Manager::getInstance()->getElasticsearch()->getRandomClient(); } $this->_client = $client; $this->_index = new Elastica\Index($this->_client, $indexName); $this->_type = new Elastica\Type($this->_index, $typeName); }
/** * @param boolean|null $quiet * @param boolean|null $quietWarnings * @param boolean|null $nonInteractive * @param int|null $forks */ public function configure($quiet = null, $quietWarnings = null, $nonInteractive = null, $forks = null) { $forks = (int) $forks; if ($quiet) { $this->_setStreamOutput(new CM_OutputStream_Null()); $this->_setStreamError(new CM_OutputStream_Null()); } if ($quietWarnings) { CM_Bootloader::getInstance()->getExceptionHandler()->setPrintSeverityMin(CM_Exception::ERROR); } if ($nonInteractive) { $this->_setStreamInput(new CM_InputStream_Null()); } if ($forks > 1) { $this->_forks = $forks; } }
/** * @return string */ public function fetch() { $page = $this->_getPage(); $layout = $this->_getLayout(); $page->checkAccessible($this->getRender()->getEnvironment()); $frontend = $this->getRender()->getGlobalResponse(); $viewResponse = new CM_Frontend_ViewResponse($layout); $viewResponse->setTemplateName('default'); $layout->prepare($this->getRender()->getEnvironment(), $viewResponse); $viewResponse->set('viewResponse', $viewResponse); $viewResponse->set('page', $page); $viewResponse->set('pageTitle', $this->fetchTitle()); $viewResponse->set('pageDescription', $this->fetchDescription()); $viewResponse->set('pageKeywords', $this->fetchKeywords()); $viewResponse->set('renderAdapter', $this); $environmentDefault = new CM_Frontend_Environment($this->getRender()->getEnvironment()->getSite()); $renderDefault = new CM_Frontend_Render($environmentDefault); $viewResponse->set('renderDefault', $renderDefault); $viewResponse->set('languageList', new CM_Paging_Language_Enabled()); $serviceManager = CM_Service_Manager::getInstance(); $options = array(); $options['deployVersion'] = CM_App::getInstance()->getDeployVersion(); $options['renderStamp'] = floor(microtime(true) * 1000); $options['site'] = CM_Params::encode($this->getRender()->getSite()); $options['url'] = $this->getRender()->getSite()->getUrl(); $options['urlCdn'] = $this->getRender()->getSite()->getUrlCdn(); $options['urlUserContentList'] = $serviceManager->getUserContent()->getUrlList(); $options['language'] = $this->getRender()->getLanguage(); $options['debug'] = CM_Bootloader::getInstance()->isDebug(); $options['stream'] = $serviceManager->getStreamMessage()->getClientOptions(); if ($viewer = $this->getRender()->getViewer()) { $options['stream']['channel']['key'] = CM_Model_StreamChannel_Message_User::getKeyByUser($viewer); $options['stream']['channel']['type'] = CM_Model_StreamChannel_Message_User::getTypeStatic(); } $frontend->getOnloadHeaderJs()->append('cm.options = ' . CM_Params::encode($options, true)); if ($viewer = $this->getRender()->getViewer()) { $frontend->getOnloadHeaderJs()->append('cm.viewer = ' . CM_Params::encode($viewer, true)); } $frontend->treeExpand($viewResponse); $frontend->getOnloadReadyJs()->append('cm.getLayout()._ready();'); $frontend->getOnloadHeaderJs()->append('cm.ready();'); $html = $this->getRender()->fetchViewResponse($viewResponse); $frontend->treeCollapse(); return $html; }
public function testLogExceptionFileLog() { $errorLog = CM_Bootloader::getInstance()->getDirTmp() . uniqid(); $log = $this->getMockBuilder('CM_Paging_Log_Error')->setMethods(array('add'))->disableOriginalConstructor()->getMock(); $log->expects($this->any())->method('add')->will($this->throwException(new Exception('foo'))); $exception = $this->getMockBuilder('CM_Exception')->setMethods(array('getLog', 'getMetaInfo'))->getMock(); $exception->expects($this->any())->method('getLog')->will($this->returnValue($log)); $exception->expects($this->any())->method('getMetaInfo')->will($this->returnValue(array())); $method = CMTest_TH::getProtectedMethod('CM_ExceptionHandling_Handler_Abstract', 'logException'); $exceptionHandler = $this->getMockBuilder('CM_ExceptionHandling_Handler_Abstract')->setMethods(array('_getLogFile'))->getMockForAbstractClass(); $exceptionHandler->expects($this->any())->method('_getLogFile')->will($this->returnValue(new CM_File($errorLog))); $this->assertFileNotExists($errorLog); $method->invoke($exceptionHandler, $exception); $logContents = file_get_contents($errorLog); $this->assertNotEmpty($logContents); $this->assertContains('### Cannot log error: ', $logContents); $this->assertContains('### Original Exception: ', $logContents); }
public function testFetchDescriptionKeywordsConsiderNamespaceWideLocation() { $dirTmp = CM_Bootloader::getInstance()->getDirTmp(); $render = $this->getMockBuilder('CM_Frontend_Render')->setMethods(array('getTemplatePath', 'getLayoutPath'))->getMock(); $render->expects($this->any())->method('getTemplatePath')->will($this->returnValue(null)); $render->expects($this->exactly(2))->method('getLayoutPath')->will($this->returnCallback(function ($templateName) use($dirTmp) { $templateFile = new CM_File($dirTmp . $templateName); $templateFile->ensureParentDirectory(); $templateFile->write('test-' . $templateName); return $templateFile->getPath(); })); /** @var CM_Frontend_Render $render */ $page = $this->getMockBuilder('CM_Page_Abstract')->getMockForAbstractClass(); /** @var CM_Page_Abstract $page */ $renderAdapter = new CM_RenderAdapter_Page($render, $page); $this->assertSame('test-Page/Abstract/meta-description.tpl', $renderAdapter->fetchDescription()); $this->assertSame('test-Page/Abstract/meta-keywords.tpl', $renderAdapter->fetchKeywords()); }
function smarty_function_resourceJs(array $params, Smarty_Internal_Template $template) { /** @var $render CM_Frontend_Render */ $render = $template->smarty->getTemplateVars('render'); $type = (string) $params['type']; $file = (string) $params['file']; $debug = CM_Bootloader::getInstance()->isDebug(); if (!in_array($type, array('vendor', 'library'))) { throw new CM_Exception_Invalid('Invalid type provided', null, ['type' => $type]); } $scripts = []; if ($debug && 'vendor' == $type) { $scripts[] = $render->getUrlResource($type . '-js', 'dist-' . $file); $scripts[] = $render->getUrlResource($type . '-js', 'source-' . $file); } else { $scripts[] = $render->getUrlResource($type . '-js', $file); } return \Functional\reduce_left($scripts, function ($url, $index, $collection, $reduction) { return $reduction . PHP_EOL . '<script type="text/javascript" src="' . $url . '" crossorigin="anonymous"></script>'; }, ''); }
/** * @param array $localConfig * @return \Composer\Composer */ public function createComposer(array $localConfig) { $io = new \Composer\IO\NullIO(); $composer = new \Composer\Composer(); $composerConfig = new \Composer\Config(); $composerConfig->merge(array('config' => array('home' => CM_Bootloader::getInstance()->getDirTmp() . 'composer/'))); $composerConfig->merge($localConfig); $composer->setConfig($composerConfig); $im = $this->createInstallationManager(); $composer->setInstallationManager($im); $this->createDefaultInstallers($im, $composer, $io); $dispatcher = new \Composer\EventDispatcher\EventDispatcher($composer, $io); $composer->setEventDispatcher($dispatcher); $generator = new \Composer\Autoload\AutoloadGenerator($dispatcher); $composer->setAutoloadGenerator($generator); $rm = $this->createRepositoryManager($composer, $io); $composer->setRepositoryManager($rm); $loader = new \Composer\Package\Loader\RootPackageLoader($rm, $composerConfig); $package = $loader->load($localConfig); $composer->setPackage($package); return $composer; }
public function testEquals() { $dirTmp = CM_Bootloader::getInstance()->getDirTmp(); $filesystem1 = new CM_File_Filesystem(new CM_File_Filesystem_Adapter_Local($dirTmp)); $filesystem2 = new CM_File_Filesystem(new CM_File_Filesystem_Adapter_Local($dirTmp)); $filesystem3 = new CM_File_Filesystem(new CM_File_Filesystem_Adapter_Local($dirTmp)); $filesystemSecondary1 = new CM_File_Filesystem(new CM_File_Filesystem_Adapter_Local($dirTmp . 'sec1')); $filesystemSecondary2 = new CM_File_Filesystem(new CM_File_Filesystem_Adapter_Local($dirTmp . 'sec2')); $filesystemSecondary3 = new CM_File_Filesystem(new CM_File_Filesystem_Adapter_Local($dirTmp . 'sec1')); $this->assertTrue($filesystem1->equals($filesystem2)); $filesystem1->addSecondary($filesystemSecondary1); $this->assertFalse($filesystem1->equals($filesystem2)); $filesystem2->addSecondary($filesystemSecondary1); $this->assertTrue($filesystem1->equals($filesystem2)); $filesystem3->addSecondary($filesystemSecondary2); $this->assertFalse($filesystem1->equals($filesystem3)); $filesystem1->addSecondary($filesystemSecondary1); $filesystem2->addSecondary($filesystemSecondary3); $this->assertTrue($filesystem1->equals($filesystem2)); $filesystem1->addSecondary($filesystemSecondary1); $filesystem2->addSecondary($filesystemSecondary2); $this->assertFalse($filesystem1->equals($filesystem2)); }
public function testGetFile() { $file = new CM_File(CM_Bootloader::getInstance()->getDirTmp() . 'foo'); $params = new CM_Params(array('file' => $file, 'filename' => $file->getPath())); $this->assertEquals($file, $params->getFile('file')); $this->assertEquals($file, $params->getFile('filename')); }
/** * @return bool */ public function isDebug() { $debug = $this->_debug; if (null === $debug) { $debug = CM_Bootloader::getInstance()->isDebug(); } return $debug; }
/** * @return Smarty */ private function _getSmarty() { if (!isset(self::$_smarty)) { self::$_smarty = new Smarty(); self::$_smarty->setTemplateDir(DIR_ROOT); self::$_smarty->setCompileDir(CM_Bootloader::getInstance()->getDirTmp() . 'smarty/'); self::$_smarty->_file_perms = 0666; self::$_smarty->_dir_perms = 0777; self::$_smarty->compile_check = CM_Bootloader::getInstance()->isDebug(); self::$_smarty->caching = false; self::$_smarty->error_reporting = error_reporting(); } $pluginDirs = array(SMARTY_PLUGINS_DIR); foreach ($this->getSite()->getModules() as $moduleName) { $pluginDirs[] = CM_Util::getModulePath($moduleName) . 'library/' . $moduleName . '/SmartyPlugins'; } self::$_smarty->setPluginsDir($pluginDirs); self::$_smarty->loadFilter('pre', 'translate'); return self::$_smarty; }
protected function setUp() { $this->_dir = CM_Bootloader::getInstance()->getDirTmp(); }