I am writing some tests for a CakePHP 3 plugin, and some of my actions use Router::url calls. When I run phpunit I get the following error: include([project dir]\\config\routes.php): failed to open stream: No such file or directory.
What I'd like to know is if this file is really required just for unit testing to work. If I create the file on that folder the testing works right. I have tried adding
DispatcherFactory::add('Asset');
DispatcherFactory::add('Routing');
DispatcherFactory::add('ControllerFactory');
to my tests/bootstrap.php file, however it did no change at all.
Since this is a standalone plugin I'd find it a little odd to have a config folder with a routes.php file in it for the sole purpose of testing. Is there any workaround to this?
The router requires a routes.php file to be present on application level, so what you should do is configure a test application environment where you can place such files.
In your tests/bootstrap.php file, define the required constants and configuration that your test environment needs. If it where just for the router, it would probably be enough if you'd define the CONFIG constant accordingly, which is being used in \Cake\Routing\Router::_loadRoutes(), like
define('CONFIG', dirname(__DIR__) . DS . 'tests' . DS . 'TestApp' . DS . 'config' . DS);
This would set the config dir to tests/TestApp/config/, where you could place the routes.php file.
Generally I'd recommend to setup all constants, and at least basic application configuration, here's an example from one of my plugins:
use Cake\Core\Configure;
use Cake\Core\Plugin;
if (!defined('DS')) {
define('DS', DIRECTORY_SEPARATOR);
}
define('ROOT', dirname(__DIR__));
define('APP_DIR', 'src');
define('APP_ROOT', ROOT . DS . 'tests' . DS . 'TestApp' . DS);
define('APP', APP_ROOT . APP_DIR . DS);
define('CONFIG', APP_ROOT . DS . 'config' . DS);
define('WWW_ROOT', APP . DS . 'webroot' . DS);
define('TESTS', ROOT . DS . 'tests' . DS);
define('TMP', APP_ROOT . DS . 'tmp' . DS);
define('LOGS', APP_ROOT . DS . 'logs' . DS);
define('CACHE', TMP . 'cache' . DS);
define('CAKE_CORE_INCLUDE_PATH', ROOT . DS . 'vendor' . DS . 'cakephp' . DS . 'cakephp');
define('CORE_PATH', CAKE_CORE_INCLUDE_PATH . DS);
define('CAKE', CORE_PATH . 'src' . DS);
require_once ROOT . DS . 'vendor' . DS . 'autoload.php';
require_once CORE_PATH . 'config' . DS . 'bootstrap.php';
$config = [
'debug' => true,
'App' => [
'namespace' => 'App',
'encoding' => 'UTF-8',
'defaultLocale' => 'en_US',
'base' => false,
'baseUrl' => false,
'dir' => 'src',
'webroot' => 'webroot',
'wwwRoot' => WWW_ROOT,
'fullBaseUrl' => 'http://localhost',
'imageBaseUrl' => 'img/',
'cssBaseUrl' => 'css/',
'jsBaseUrl' => 'js/',
'paths' => [
'plugins' => [APP_ROOT . 'plugins' . DS],
'templates' => [APP . 'Template' . DS],
'locales' => [APP . 'Locale' . DS],
],
]
];
Configure::write($config);
date_default_timezone_set('UTC');
mb_internal_encoding(Configure::read('App.encoding'));
ini_set('intl.default_locale', Configure::read('App.defaultLocale'));
Plugin::load('MyPlugin', ['path' => ROOT]);
I can't run using php artisan serve cause of :
PHP Fatal error: Cannot redeclare class Composer\Autoload\ComposerStaticInit4783eae8fdb0bbd7059e05caa6aed997 in /var/www/html/project/config/vendor/composer/autoload_static.php
Script php artisan optimize handling the post-update-cmd event returned with error code 255
Heres is my autoload_static.php:
class ComposerStaticInit57efd0b78daf784a291fa20fc5e6edcd
{
public static $files = array (
'0e6d7bf4a5811bfa5cf40c5ccd6fae6a' => __DIR__ . '/..' . '/symfony/polyfill-mbstring/bootstrap.php',
'667aeda72477189d0494fecd327c3641' => __DIR__ . '/..' . '/symfony/var-dumper/Resources/functions/dump.php',
'1d1b89d124cc9cb8219922c9d5569199' => __DIR__ . '/..' . '/hamcrest/hamcrest-php/hamcrest/Hamcrest.php',
'3919eeb97e98d4648304477f8ef734ba' => __DIR__ . '/..' . '/phpseclib/phpseclib/phpseclib/Crypt/Random.php',
'5255c38a0faeba867671b61dfda6d864' => __DIR__ . '/..' . '/paragonie/random_compat/lib/random.php',
'2c102faa651ef8ea5874edb585946bce' => __DIR__ . '/..' . '/swiftmailer/swiftmailer/lib/swift_required.php',
'bd9634f2d41831496de0d3dfe4c94881' => __DIR__ . '/..' . '/symfony/polyfill-php56/bootstrap.php',
'a0edc8309cc5e1d60e3047b5df6b7052' => __DIR__ . '/..' . '/guzzlehttp/psr7/src/functions_include.php',
'c964ee0ededf28c96ebd9db5099ef910' => __DIR__ . '/..' . '/guzzlehttp/promises/src/functions_include.php',
'e7223560d890eab89cda23685e711e2c' => __DIR__ . '/..' . '/psy/psysh/src/Psy/functions.php',
'f18cc91337d49233e5754e93f3ed9ec3' => __DIR__ . '/..' . '/laravelcollective/html/src/helpers.php',
'37a3dc5111fe8f707ab4c132ef1dbc62' => __DIR__ . '/..' . '/guzzlehttp/guzzle/src/functions_include.php',
'f0906e6318348a765ffb6eb24e0d0938' => __DIR__ . '/..' . '/laravel/framework/src/Illuminate/Foundation/helpers.php',
'58571171fd5812e6e447dce228f52f4d' => __DIR__ . '/..' . '/laravel/framework/src/Illuminate/Support/helpers.php',
);
}
already try composer install and update but still didnt work at all. anyone know ?
So the problem is wrong step while compose require internvention/image. i didnt check the app/config.php first. should comment the provider and the aliases. i think the case is solve. thank you everyone.
In my company we developing a ZF2 application, no programm;-), right now. We are at a point where we want to test some part of the application by bringing it into the public www.
We prepared a STAGE-environment and i do some performance tuning now. I read that using the ClassMapAutoloader is much faster than the StandardAutoLoader like described e.g. http://samminds.com/2012/11/zf2-performance-quicktipp-2-classmap-autoloading/ . I do understand why it should be faster but in my case i profiled the site with and without ClassMapAutoloader using xdebug profiling, WinCacheGring/QCacheGrind and it is slower about 0,2%.
Does anyone has an idea why this could be slower?
I am using CentOS and PHP Version => 5.6.12
EDIT ADDED INFORMATION:
Example of one autoload_classmap.php:
<?php
// Generated by ZF2's ./bin/classmap_generator.php
return array(
'Search\Elasticsearch\Document\AbstractDocument' => __DIR__ . '/src/Search/Elasticsearch/Document/AbstractDocument.php',
'Search\Elasticsearch\Document\ArticleDocument' => __DIR__ . '/src/Search/Elasticsearch/Document/ArticleDocument.php',
'Search\Elasticsearch\Document\BookingDocument' => __DIR__ . '/src/Search/Elasticsearch/Document/BookingDocument.php',
'Search\Elasticsearch\Document\DocumentType' => __DIR__ . '/src/Search/Elasticsearch/Document/DocumentType.php',
'Search\Elasticsearch\Document\InvoiceDocument' => __DIR__ . '/src/Search/Elasticsearch/Document/InvoiceDocument.php',
'Search\Elasticsearch\Document\OfficeDocument' => __DIR__ . '/src/Search/Elasticsearch/Document/OfficeDocument.php',
'Search\Elasticsearch\Document\OfficeMemberDocument' => __DIR__ . '/src/Search/Elasticsearch/Document/OfficeMemberDocument.php',
'Search\Elasticsearch\Document\ProductDocument' => __DIR__ . '/src/Search/Elasticsearch/Document/ProductDocument.php',
'Search\Elasticsearch\Document\ProfileDocument' => __DIR__ . '/src/Search/Elasticsearch/Document/ProfileDocument.php',
'Search\Elasticsearch\Document\RatingDocument' => __DIR__ . '/src/Search/Elasticsearch/Document/RatingDocument.php',
'Search\Elasticsearch\Document\AbstractWebSearchDocument' => __DIR__ . '/src/Search/Elasticsearch/Document/AbstractWebSearchDocument.php',
'Search\Elasticsearch\AutoSuggestionQueryHandler' => __DIR__ . '/src/Search/Elasticsearch/AutoSuggestionQueryHandler.php',
'Search\Elasticsearch\SearchStatisticIndexHandler' => __DIR__ . '/src/Search/Elasticsearch/SearchStatisticIndexHandler.php',
'Search\Elasticsearch\TermRecognizerQueryHandler' => __DIR__ . '/src/Search/Elasticsearch/TermRecognizerQueryHandler.php',
'Search\Elasticsearch\SearchIndexHandler' => __DIR__ . '/src/Search/Elasticsearch/SearchIndexHandler.php',
'Search\Elasticsearch\SearchQueryHandler' => __DIR__ . '/src/Search/Elasticsearch/SearchQueryHandler.php',
'Search\Elasticsearch\TermRecognizerIndexHandler' => __DIR__ . '/src/Search/Elasticsearch/TermRecognizerIndexHandler.php',
'Search\Exception\Exception' => __DIR__ . '/src/Search/Exception/Exception.php',
'Search\Factory\AutoSuggestQueryHandlerFactory' => __DIR__ . '/src/Search/Factory/AutoSuggestQueryHandlerFactory.php',
'Search\Factory\AutoSuggestServiceFactory' => __DIR__ . '/src/Search/Factory/AutoSuggestServiceFactory.php',
'Search\Factory\DocumentStorerServiceFactory' => __DIR__ . '/src/Search/Factory/DocumentStorerServiceFactory.php',
'Search\Factory\QueueWorkerServiceFactory' => __DIR__ . '/src/Search/Factory/QueueWorkerServiceFactory.php',
'Search\Factory\SearchIndexHandlerFactory' => __DIR__ . '/src/Search/Factory/SearchIndexHandlerFactory.php',
'Search\Factory\SearchQueryHandlerFactory' => __DIR__ . '/src/Search/Factory/SearchQueryHandlerFactory.php',
'Search\Factory\SearchServiceFactory' => __DIR__ . '/src/Search/Factory/SearchServiceFactory.php',
'Search\Factory\SearchSimpleServiceFactory' => __DIR__ . '/src/Search/Factory/SearchSimpleServiceFactory.php',
'Search\Factory\SearchStatistikIndexHandlerFactory' => __DIR__ . '/src/Search/Factory/SearchStatistikIndexHandlerFactory.php',
'Search\Factory\TermRecognizerServiceFactory' => __DIR__ . '/src/Search/Factory/TermRecognizerServiceFactory.php',
'Search\Factory\TermrecognizerIndexHandlerFactory' => __DIR__ . '/src/Search/Factory/TermrecognizerIndexHandlerFactory.php',
'Search\Factory\TermrecognizerQueryHandlerFactory' => __DIR__ . '/src/Search/Factory/TermrecognizerQueryHandlerFactory.php',
'Search\Factory\RequestHandlerFactory' => __DIR__ . '/src/Search/Factory/RequestHandlerFactory.php',
'Search\Logger\LoggerInterface' => __DIR__ . '/src/Search/Logger/LoggerInterface.php',
'Search\Logger\StatisticLogger' => __DIR__ . '/src/Search/Logger/StatisticLogger.php',
'DatabaseQueue' => __DIR__ . '/src/Search/Queue/DatabaseQueue.php',
'Search\Search\QueryWordReducer' => __DIR__ . '/src/Search/Search/QueryWordReducer.php',
'Search\Search\RecognizedTermConsumer' => __DIR__ . '/src/Search/Search/RecognizedTermConsumer.php',
'Search\Search\SearchService' => __DIR__ . '/src/Search/Search/SearchService.php',
'Search\Search\SuggestionListBuilder' => __DIR__ . '/src/Search/Search/SuggestionListBuilder.php',
'Search\Search\Util' => __DIR__ . '/src/Search/Search/Util.php',
'Search\Search\ViewState' => __DIR__ . '/src/Search/Search/ViewState.php',
'Search\Search\ViewStateToSearchRequestTransformer' => __DIR__ . '/src/Search/Search/ViewStateToSearchRequestTransformer.php',
'Search\Search\SearchSimpleService' => __DIR__ . '/src/Search/Search/SearchSimpleService.php',
'Search\AutoSuggester' => __DIR__ . '/src/Search/AutoSuggester.php',
'Search\QueryCleaner' => __DIR__ . '/src/Search/QueryCleaner.php',
'Search\Request' => __DIR__ . '/src/Search/Request.php',
'Search\RequestHandler' => __DIR__ . '/src/Search/RequestHandler.php',
'Search\SearchSource' => __DIR__ . '/src/Search/SearchSource.php',
'Search\Util' => __DIR__ . '/src/Search/Util.php',
'Search\QueueWorker' => __DIR__ . '/src/Search/QueueWorker.php',
'Search\AbstractDocumentStorer' => __DIR__ . '/src/Search/AbstractDocumentStorer.php',
'Search\DocumentStorer' => __DIR__ . '/src/Search/DocumentStorer.php',
'Search\TermRecognizer' => __DIR__ . '/src/Search/TermRecognizer.php',
'Search\Module' => __DIR__ . '/Module.php',
);
Extract from the correlating Module.php:
public function getAutoloaderConfig() {
return [
'Zend\Loader\ClassMapAutoloader' => [
__DIR__ . '/autoload_classmap.php'
],
'Zend\Loader\StandardAutoloader' => [
'namespaces' => [
__NAMESPACE__ => __DIR__ . '/src/' . str_replace('\\', '/', __NAMESPACE__)
]
]
];
}
I checked already that the Autoloader uses the classmap.
EDIT ADDED INFORMATION:
Sorry for the late answer. Right now your app loads for half a second - 0.6s to be precise. So I the autoloader is doing his works. Your queries are also executed fast. There are two more ways I can think of to speed up your applications.
First way - using template map
Locate your templatemap_generator.php file. It should be in your vendor/zendframework/zendframework/bin folder. Navigate to your module folder e.g. Application directory where the src, view, config folders are. Open a terminal and type php ../../vendor/zendframework/zendframework/bin/templatemap_generator.php ZF will create a template map in your module directory. Now to use this template, simply modify your module.config.php file. The file structure is similar to the one from clasmap_autoloader.php
return array(
// Telling where the views are
'view_manager' => array(
'display_not_found_reason' => true,
'display_exceptions' => true,
'doctype' => 'HTML5',
'not_found_template' => 'error/404',
'exception_template' => 'error/index',
'template_map' => include __DIR__ . '/../template_map.php', // <-- add this line. You can remove `template_path_stack`
),
In your controllersfor each action add a view template.
public indexAction()
{
$view = new ViewModel();
$view->setTemplate("aplication/index/index");
return $view;
}
Second way - Using module cache in production environment.
Let's say you have this line in your .htaccess - SetEnv APPLICATION_ENV "development"
In your public/index.php file if you haven't done something similar, add this:
/**
* Set global ENV. Used for debugging
*/
if (isset($_SERVER['APPLICATION_ENV']) && $_SERVER["APPLICATION_ENV"] === 'development') {
define("APP_ENV", 'development');
} else {
define("APP_ENV", "production");
}
This will ensure that you have a global env across your application which says if debugging is on or off and it helps you avoid DRY code.
Now from your root folder open config/application.config.php
<?php
$modules = [];
if (APP_ENV === 'development') {
$modules[] = 'ZendDeveloperTools';
$modules[] = 'BjyProfiler';
$modules[] = 'SanSessionToolbar';
}
$modules[] = 'Application';
$modules[] = 'Admin';
return [
// This should be an array of module namespaces used in the application.
'modules' => $modules,
// These are various options for the listeners attached to the ModuleManager
'module_listener_options' => [
// This should be an array of paths in which modules reside.
// If a string key is provided, the listener will consider that a module
// namespace, the value of that key the specific path to that module's
// Module class.
'module_paths' => [
'./module',
'./vendor',
],
// An array of paths from which to glob configuration files after
// modules are loaded. These effectively override configuration
// provided by modules themselves. Paths may use GLOB_BRACE notation.
'config_glob_paths' => [
'config/autoload/{{,*.}global,{,*.}local}.php',
],
// Whether or not to enable a configuration cache.
// If enabled, the merged configuration will be cached and used in
// subsequent requests.
'config_cache_enabled' => (APP_ENV === 'production'),
// The key used to create the configuration cache file name.
'config_cache_key' => md5('app_config'),
// Whether or not to enable a module class map cache.
// If enabled, creates a module class map cache which will be used
// by in future requests, to reduce the autoloading process.
'module_map_cache_enabled' => (APP_ENV === 'production'),
// The key used to create the class map cache file name.
'module_map_cache_key' => md5('module_map'),
// The path in which to cache merged configuration.
'cache_dir' => dirname(__DIR__)."/data/cache",
// Whether or not to enable modules dependency checking.
// Enabled by default, prevents usage of modules that depend on other modules
// that weren't loaded.
'check_dependencies' => (APP_ENV !== 'production'),
],
// Used to create an own service manager. May contain one or more child arrays.
//'service_listener_options' => [
// [
// 'service_manager' => $stringServiceManagerName,
// 'config_key' => $stringConfigKey,
// 'interface' => $stringOptionalInterface,
// 'method' => $stringRequiredMethodName,
// ],
// )
// Initial configuration with which to seed the ServiceManager.
// Should be compatible with Zend\ServiceManager\Config.
// 'service_manager' => [],
];
config_cache_* will activate and cache your route config when your app is in production. All your module routes will be cached. Also If the website is in production the debugging modules will not be loaded.
You can replace md5('app_config') with whatever you want.
I'm having I confusion in understanding the namespace from the Yii documentation as their are not enough example to understand, as I'm new to Yii framework please give some easy and detailed examples so that I can understand it's purpose.
You may try read this documentation http://yiiframework.ru/doc/guide/en/basics.namespace
In general Yii namespaces - it is aliases for folders.
I.E.
'aliases' => array(
'frontend' => dirname(__FILE__) . '/../..' . '/frontend',
'common' => dirname(__FILE__) . '/../..' . '/common',
'backend' => dirname(__FILE__) . '/../..' . '/backend',
'vendor' => dirname(__FILE__) . '/../..' . '/common/lib/vendor'
),
...
'import' => array(
'common.extensions.components.*',
'common.components.*',
'common.helpers.*',
'common.models.*',
'application.controllers.*',
// In result "common" = dirname(__FILE__) . '/../..' . '/common'
Code:
$app->register(new Silex\Provider\TranslationServiceProvider(), array(
'locale' => 'sr_Latn',
'translation.class_path' => __DIR__ . '/../vendor/symfony/src',
'translator.messages' => array('sr_Latn' => __DIR__ .'/../vendor/symfony/src/Symfony/Bundle/FrameworkBundle/Resources/translations/validators.sr_Latn.xlf')
));
$app['translator.loader'] = new Symfony\Component\Translation\Loader\XliffFileLoader();
and I still get validation messages in english. any idea?
You need to add a call to Translator::addResource:
$file = __DIR__ .'/../vendor/symfony/src/Symfony/Bundle/FrameworkBundle/Resources/translations/validators.sr_Latn.xlf';
$app['translator']->addResource('xliff', $file, 'sr_Latn', 'validators');
See also Symfony\Bundle\FrameworkBundle\DependencyInjection::registerTranslatorConfiguration.