Symfony Testing Cookbook: Difference between revisions
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...") |
|||
| (2 intermediate revisions by the same user not shown) | |||
| Line 1: | Line 1: | ||
[[Category:Symfony]] [[Category:PHP]] [[Category:Web Development]] | [[Category:Symfony]] [[Category:PHP]] [[Category:Web Development]] | ||
== | == Unit/functional tests with database connections on remote machines == | ||
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. Otherwise, the server returns a 500 error stating that access is denied for `user@localhost` or whatever. | |||
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`. Load the `_remote` parameters into `/app/config/config_test.yml`: | |||
<syntaxhighlight lang="yaml"> | |||
# /app/config/config_test.yml | |||
imports: | |||
# ... | |||
- { resource: parameters_remote.yml } | |||
#... | |||
doctrine: | |||
dbal: | |||
driver: "%database_driver%" | |||
host: "%database_host%" | |||
port: "%database_port%" | |||
dbname: "%database_name%" | |||
user: "%database_user%" | |||
password: "%database_password%" | |||
charset: UTF8 | |||
</syntaxhighlight> | |||
== 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 /> | |||
Latest revision as of 16:11, 7 March 2015
Unit/functional tests with database connections on remote machines[edit]
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. Otherwise, the server returns a 500 error stating that access is denied for user@localhost or whatever.
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. Load the _remote parameters into /app/config/config_test.yml:
# /app/config/config_test.yml
imports:
# ...
- { resource: parameters_remote.yml }
#...
doctrine:
dbal:
driver: "%database_driver%"
host: "%database_host%"
port: "%database_port%"
dbname: "%database_name%"
user: "%database_user%"
password: "%database_password%"
charset: UTF8
HTTP authentication[edit]
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[edit]
- ↑ How to Simulate HTTP Authentication in a Functional Test (Symfony Cookbook)