Symfony Testing Cookbook: Difference between revisions

From Littledamien Wiki
Jump to navigation Jump to search
(Created page with "Category:Symfony Category:PHP Category:Web Development == Running unit/functional tests involving database connections on remote machines == === Symptom === Serv...")
 
No edit summary
Line 1: Line 1:
[[Category:Symfony]] [[Category:PHP]] [[Category:Web Development]]
[[Category:Symfony]] [[Category:PHP]] [[Category:Web Development]]
== Running unit/functional tests involving database connections on remote machines ==
== Unit/functional tests with database connections on remote machines ==


=== Symptom ===
=== Symptom ===


Server returns a 500 error stating that access is denied for `user@localhost` or whatever.
Server returns a 500 error stating that access is denied for `user@localhost` or whatever.
=== Fix ===
Remember that since `phpunit` is running from the command-line, the remote host is NOT `localhost` like it would be when invoking a controller through a browser request.
when running `phpunit`, it's necessary to switch the database host from `localhost` to the ip address of the MySQL server.
Set up two separate Symfony "parameters" configuration files, `parameters.yml` and `parameters_remote.yml`. Set the database host to the IP of the server in `_remote`. Then, in the main config file, uncomment `<nowiki>- { resource: parameters_remote.yml }</nowiki>`:
<syntaxhighlight lang="yaml">
# /app/config/config.yml
imports:
    - { resource: parameters.yml }
    # - { resource: parameters_remote.yml }
    - { resource: security.yml }
    - { resource: services.yml }
#...
</syntaxhighlight>
Don't forget to set it back before trying to load a page in a browser.
== HTTP authentication ==
Authentication credentials can be passed to the client object or request method.<ref>[http://symfony.com/doc/current/cookbook/testing/http_authentication.html How to Simulate HTTP Authentication in a Functional Test] (Symfony Cookbook)</ref>
<syntaxhighlight lang="php">
$client = static::createClient(array(), array(
    'PHP_AUTH_USER' => 'username',
    'PHP_AUTH_PW'  => 'pa$$word',
));
</syntaxhighlight>
== Notes ==
<references />

Revision as of 15:50, 7 March 2015

Unit/functional tests with database connections on remote machines

Symptom

Server returns a 500 error stating that access is denied for user@localhost or whatever.

Fix

Remember that since phpunit is running from the command-line, the remote host is NOT localhost like it would be when invoking a controller through a browser request.

when running phpunit, it's necessary to switch the database host from localhost to the ip address of the MySQL server.

Set up two separate Symfony "parameters" configuration files, parameters.yml and parameters_remote.yml. Set the database host to the IP of the server in _remote. Then, in the main config file, uncomment `- { resource: parameters_remote.yml }`:

# /app/config/config.yml

imports:
    - { resource: parameters.yml }
    # - { resource: parameters_remote.yml }
    - { resource: security.yml }
    - { resource: services.yml }

#...

Don't forget to set it back before trying to load a page in a browser.

HTTP authentication

Authentication credentials can be passed to the client object or request method.[1]

$client = static::createClient(array(), array(
    'PHP_AUTH_USER' => 'username',
    'PHP_AUTH_PW'   => 'pa$$word',
));

Notes