PHPUnit throws "Warning: date(): It is not safe..." - phpunit

When running phpunit --coverage-html I get the well-known warning about timezones.
PHP Warning: date(): It is not safe to rely on the system's timezone
settings. You are required to use the date.timezone setting or the
date_default_timezone_set() function. In case you used any of those
methods and you are still getting this warning, you most likely
misspelled the timezone identifier. We selected the timezone 'UTC' for
now, but please set date.timezone to select your timezone.
Everything works as expected, but it becomes really annoying.
Of course, I could solve this by changing my php.ini, but I'd prefer to avoid it, if possible, in order to keep some server-agnosticism. In addition, I don't want to prevent this warning to appear if triggered by my testable code.
Is there a way to define the default timezone only for internal PHPUnit operations?

I set the TimeZone in the bootstrap.php file.
<?php
// PHP and Web server settings
error_reporting(E_ALL | E_STRICT);
date_default_timezone_set("America/Toronto"); // Set the default timezone
$_SERVER['SERVER_NAME'] = 'http://myserver'; // Set Web Server name
// Process the Include Path to allow the additional application to be set.
$IncludePaths = explode( PATH_SEPARATOR, get_include_path() );
$NewIncludePaths = array_merge( $IncludePaths, array(dirname(__FILE__) ));
set_include_path( implode( PATH_SEPARATOR, array_unique($NewIncludePaths))); // Update Include Path
//define('PHPUNIT_RUNNING', 1); // Indicate to the code that Automated Testing is running.
?>

Related

MariaDB default charset

Documentation for MariaDB says clearly how to set up server defaults like charset and collation. So I opened my Mariadb console and run:
SET character_set_server = 'utf8mb4';
SET collation_server = 'utf8mb4_slovak_ci';
Consoles wrote OK.
Then I restart the server, but as I tried to create new database there are still the same latin2 charset and swedish collation.
I do it automatically via Symfony cosole command
php bin/console doctrine:database:create
What is wrong with that? I did it like documentation says.
SET character_set_server changes the character set for the current connection.
SET global character_set_server changes the character set globally for each new connection.
However if you restart your server the default character sets for server will be read from the configuration file. If the configuration file doesn't specify character set, then defaults will be used. So for making your settings permanent, you have to change the character sets in your configuration file (https://mariadb.com/kb/en/configuring-mariadb-with-option-files/)
First, run the SHOW VARIABLES like "%collation%"; to show you the current configs.
To change collation_server setting, you have to use the keyword GLOBAL and therefore your statement will be SET GLOBAL collation_server = 'utf8mb4_slovak_ci';

Symfony 2 When running Generate:Bundle it does not work

I am trying to add a new bundle using the console with the follow code...
php app/console generate:bundle
How ever it is giving me the following error...
[Symfony\Component\Debug\Exception\ContextErrorException]
Warning: date_default_timezone_get(): It is not safe to rely on the system'
s timezone settings. You are *required* to use the date.timezone setting or
the date_default_timezone_set() function. In case you used any of those me
thods and you are still getting this warning, you most likely misspelled th
e timezone identifier. We selected the timezone 'UTC' for now, but please s
et date.timezone to select your timezone. in /Users/Phil/Sites/SoccerTips24
/vendor/monolog/monolog/src/Monolog/Logger.php line 233
generate:bundle [--namespace="..."] [--dir="..."] [--bundle-name="..."] [--format="..."] [--structure]
I understand the first one is something to do with the time settings but then bundles wont add to my src folder...
Any ideas how to fix?
Set timezone in your php.ini
php -i |grep php.ini
uncomment timezone record, and set you timezone:
date.timezone = "Europe/Paris"
Once you go to your php.ini and set your timezone it should work.

working dev environment symfony2.3

I'm working with a project symfony version 2.3.6 specifically. The problem is that I make changes in the code but the change is not reflected on the page until the page charge many times. I'm in dev environment. is as if not compile the code every time I load the page. Are not you supposed to be in dev environment should always be compiled? a greeting
Thanks four your answer. I use MAMP and php 5.5.3 and no select cache.
the app_dev is:
<?php
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Debug\Debug;
// If you don't want to setup permissions the proper way, just uncomment the following PHP line
// read http://symfony.com/doc/current/book/installation.html#configuration-and-setup for more information
//umask(0000);
// This check prevents access to debug front controllers that are deployed by accident to production servers.
// Feel free to remove this, extend it, or make something more sophisticated.
if (isset($_SERVER['HTTP_CLIENT_IP'])
|| isset($_SERVER['HTTP_X_FORWARDED_FOR'])
|| !in_array(#$_SERVER['REMOTE_ADDR'], array('127.0.0.1', 'fe80::1', '::1'))
) {
header('HTTP/1.0 403 Forbidden');
exit('You are not allowed to access this file. Check '.basename(__FILE__).' for more information.');
}
$loader = require_once __DIR__.'/../app/bootstrap.php.cache';
Debug::enable();
require_once __DIR__.'/../app/AppKernel.php';
$kernel = new AppKernel('dev', true);
//$kernel->loadClassCache();
$request = Request::createFromGlobals();
$response = $kernel->handle($request);
$response->send();
$kernel->terminate($request, $response);
i comment the line //$kernel->loadClassCache(); but the problem no solve.
Symfony caches everything by default, especially Twig. And it's tedious, I know.
Looking at the manual (http://symfony.com/doc/current/book/templating.html) it says:
If you add a template in a new location, you may need to clear your
cache (php app/console cache:clear), even if you are in debug mode.
And so, you need to clear the cache. But attention deleting all, because the user sessions are stored in /cache/dev/sessions. That's another strong limitation: Symfony2 requires a writable filesystem.
There is a radical solution, but your app could get really slow, also in dev mode:
twig:
cache: false
In your config.yml

Why does Symfony2 set the error_reporting?

I've been trying to remove the E_NOTICE and E_STRICT error levels to avoid this error:
Runtime Notice: Only variables should be passed by reference
I tried modifying php.ini but didn't work, error_reporting always is -1. Then I tried setting it dynamically in the current action, and worked fine. Then I also tried the same in the first line in app_dev.php and didn't work. Which means Symfony2 is setting it dynamically somewhere.
What should I do?
EDIT
For those who are not familiar with the error:
$user = $this->('security.context')->getToken()->getUser();
$first = reset($user->getRoles()); // error
$roles = $user->getRoles();
$first = reset($roles); // works fine
Whilst the notice is not 'retarded', this is a reasonable question in other contexts so: this is set in the Kernel instance, instantiated in app_dev.php (or app.php).
The second parameter to the construct is a boolean debug flag, and if true then error_reporting is set to -1 and display_errors to 1, otherwise default and 0 respectively.
$kernel = new AppKernel('dev', false);
symfony documentation
Hope this helps.
I got the same error in the following scenario i.e.
Scenario:
I lost the development environment for my existing LIVE project. But I got the whole code from GIT repo and then installed the symfony 2.4.2 (same version as on LIVE site) in my new development environment. Then I found that the web-application pages working on the LIVE site are broken in my new DEV environment.
Solution:
I spent quit a lot time to understand why the problem is then I found that i.e.
When I installed symfony 2.4.2 in my development environment using composer.phar then it created a new web/app_dev.php file in my development environment and it has the following entry to turn it off i.e.
Debug::enable();
Just comment the above line then the php notices will be off and then all the pages that were giving me notices are fixed.
I hope this will be helpful for someone having the same problem like me. Good Luck!
Cheers.
PS: But I will recommend to enable the above line in your new development projects so that you can see the PHP notices and then remove them during development.

How to set the environment in a Symfony2 console command

Hopefully a simple question - how does one specify which environment to use when running a console command in Symfony2. I've created a few commands, however I would like to run them in the context of my 'staging' environment when on my staging server and my 'prod' environment when on my production server (the different environments define different database connections). How do I configure and pass this information to my console command?
You have two options that will help you out. You can specify the environment that the console will run in with the --env flag, and use --no-debug to disable debug mode.
php app/console --env=staging your:console:command or php app/console --env=prod your:console:command should do what you're looking for (the console runs in the dev environment with debug on by default).
You can look at the code of the app/console file for more info.
You can also use the SYMFONY_ENV environment variable to set a default environment distinct to 'dev' (e.g. export SYMFONY_ENV=prod in ~/.bash_profile)
To answer the question #croca had, to expand on what #Francesc Rosàs posted, and as #Problematic suggested.
If you look in app/console you should see $env = $input->getParameterOption(array('--env', '-e'), getenv('SYMFONY_ENV') ?: 'dev');
All this does is checks the input arguments passed to the console for --env or -e, checks the default value from getenv('SYMFONY_ENV'), or sets it to dev if neither are supplied.
It is then passed to $kernel = new AppKernel($env, $debug);
You could essentially either make changes directly to app/console to achieve your application's specific functionality or copy app/console to a separate file such as app/exec, then process the $env variable how you prefer to determine the desired environment.
Simple Example: app/exec
#!/usr/bin/env php
<?php
/**
* disabled debug mode,
* set environment based on requesting address being local
* otherwise the server's IP address
* be sure to perform at least chmod(750) on the file to ensure it is executable
* otherwise it must be prefixed with your php executable
*/
set_time_limit(0);
require_once __DIR__.'/bootstrap.php.cache';
require_once __DIR__.'/AppKernel.php';
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Component\Console\Input\ArgvInput;
$input = new ArgvInput();
$env = ($_SERVER['REMOTE_ADDR'] === '127.0.0.1' || $_SERVER['REMOTE_ADDR'] === '::1' ? 'dev' :
($_SERVER['SERVER_ADDR'] === '192.168.2.1' ? 'test' : 'prod')
);
$kernel = new AppKernel($env, false);
$application = new Application($kernel);
$application->run($input);
Then call php app/exec namespace:command arguments --flags
Additionally you could process your own application instead of using the app/console AppKernel - instructions from Symfony can be found here: http://symfony.com/doc/current/components/console/introduction.html
The official documentation says:
By default, console commands run in the dev environment and you may want to change this for some commands.
So, as #Problematic suggest, you can specify the environment for your command using the flag "--env=your_env" or its shortcut "-e your_env".
I wonder whether it is possible to force the default environment for a command. I mean, without having to use the flags. Any idea?
There is another variant, not so convenient though, but works also with composer
SYMFONY_ENV=prod app/console cache:clear
Maybe you could look at the implementation of the cache:clear command. This may give you some clues on how to choose the environment from a command.
app/console help cache:clear

Resources