jeudi 29 janvier 2015

Passing in values to scripts running under PHP built-in server

I have a PHP application which implements an API as a RESTful web service and uses SQLite for data storage. The relevant software versions are:



  • PHP: PHP 5.4.30 (cli) (built: Jul 29 2014 23:43:29)

  • SQLite: 3.7.13

  • PHPUnit: 4.3.4


I'm using PHPUnit to test the application in two broad areas:



  1. Testing the methods of each class.

  2. Testing the web service URLs (e.g. that GET /items/1 returns a JSON representation of item 1).


Testing the methods of each class is easy as everything is handled within PHPUnit and I can set constants to given values using a phpunit.xml configuration file (including the path to the test database which is created at the start of the test run and the data is reset for each test). For testing the web service I have a bootstrap script which starts up the PHP built-in server like so:


php -S localhost:8000 -t ./htdocs


The bootstrap script also stores the ID of the server process so it can kill it when PHPUnit finishes. This all works as I'd expect.


The problem I'm having is that when I'm testing the web service, I want it to use the test database, in production I want it to use the live database, and the two databases have different paths. At present the production database path is defined by a constant in a config.php file, but I want to override that when running PHPUnit. Ideally I'd like to pass in a value via the built-in server using something like the following:


php -S localhost:8000 -t ./htdocs -D DATABASE_PATH=/path/to/sqlite.db


Where DATABASE_PATH would appear as a constant in the PHP code which runs the web service. I can then use an if (!defined('DATABASE_PATH') check to provide the live database path if no path has already been specified. Is there a way to do this using the built-in server? I've checked the man page for php but I can find any arguments which provide this functionality - the closest I've found is -d for defining INI entries.


Alternatively, am I going about this the wrong way and is there another (better?) way to achieve the same thing?


Aucun commentaire:

Enregistrer un commentaire