Skip to content

Commit

Permalink
add php.env for loading $_ENV config
Browse files Browse the repository at this point in the history
  • Loading branch information
shadowhand committed Nov 8, 2014
1 parent b31d14d commit d3e20f2
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 6 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Composer directory
vendor/

# $_ENV config overloading
.env

# This is a library
composer.lock
22 changes: 20 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,27 @@ composer install --dev
phpunit --bootstrap=vendor/autoload.php tests
```

to set the database host, user, or password:
The default database connection details are:

- hostname: localhost
- database: ohanzee
- username: ohanzee
- password: (empty)

These values can be modified by creating a `.env` file in the root of the project:

```ini
PHPUNIT_TEST_HOSTNAME=server
PHPUNIT_TEST_DATABASE=testing
PHPUNIT_TEST_USERNAME=jane
PHPUNIT_TEST_PASSWORD=likesblue
```

The `.env` file is a standard ini file and will be loaded using [dotenv](https://github.com/vlucas/phpdotenv).

Any of the parameters can also be passed directly on the commandline:

```
PHPUNIT_TEST_HOSTNAME=myhostname PHPUNIT_TEST_USERNAME=bob PHPUNIT_TEST_PASSWORD=secret \
PHPUNIT_TEST_USERNAME=joe PHPUNIT_TEST_PASSWORD=likesgreen \
phpunit --bootstrap=vendor/autoload.php tests
```
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,6 @@
},
"require-dev": {
"phpunit/phpunit": "4.2.*"
"vlucas/phpdotenv": "~1.0"
}
}
13 changes: 9 additions & 4 deletions tests/Database/MysqliTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,18 @@

class MysqliTest extends PHPUnit_Framework_TestCase
{
public function setUp()
{
Dotenv::load(realpath(__DIR__ . '/../../'));
}

private function getEnvConfig()
{
$config = [
'hostname' => @$_ENV['PHPUNIT_TEST_HOSTNAME'],
'database' => @$_ENV['PHPUNIT_TEST_DATABASE'],
'username' => @$_ENV['PHPUNIT_TEST_USERNAME'],
'password' => @$_ENV['PHPUNIT_TEST_PASSWORD'],
'hostname' => getenv('PHPUNIT_TEST_HOSTNAME'),
'database' => getenv('PHPUNIT_TEST_DATABASE'),
'username' => getenv('PHPUNIT_TEST_USERNAME'),
'password' => getenv('PHPUNIT_TEST_PASSWORD'),
];

return array_filter($config);
Expand Down

0 comments on commit d3e20f2

Please sign in to comment.