tags:

views:

159

answers:

3

I'm starting to write PHPUnit tests and I'd like the tests to be run from developers machines as well as from our servers. Developers machines are set up differently than the servers and even differently from each other.

To run in these different places it seems to be the person that runs the test is going to have to indicate where it's being run. The test can then look up the proper config of the machine it's running on.

I'm imagining something like:

phpunit.bat -X johns_laptop unittest.php

or on the alpha server:

phpunit -X alpha unittest.php

In the test I would be able to get the value if the 'X' (or whatever it is) parameter and know, for example, what the path to the app root is for this machine.

It doesn't look like the command line allows for that - or have I missed something?

A: 

If you would like to run tests on remote machine, use ssh then run it. On locale machine you only have to cd to your root dir, then run phpunit.

user@local:/path/to/your/project$ phpunit
user@remote:/var/www/project$ phpunit

Edit: You are talking about a machine dependent configuration. (What kind of conf btw?) My solution is to put these config under the same, not versioncontrolled place, then read/parse it runtime, in the needed set up methds for example.

erenon
I know how to run phpunit. My question is how do I pass config info into it so it can be aware of the configuration of that machine?
dl__
@dl: see my edit.
erenon
I think I see what your suggesting. The problem is that I'd like the configurations to be version controlled. They may change over time and I'd like to track them.
dl__
@dl: That doesn't make sense for me. If you want to version control the configs, then why are they different on different machines?
erenon
They are different because they are on different machines owned by different people who set them up their own way. So I'll have a config file (a YANL file) divided into different sections, one section per machine. When the test is run the user must specify the machine name so the correct section can be read from the configuration.I want it version controlled so that when I delete the files from the alpha server, for example, I can just restore from version control and the configuration is back.
dl__
+2  A: 

One way would be for you to inspect $argv and $argc. Something like:

<?php

require_once 'PHPUnit/Framework/TestCase.php';

class EnvironmentTest extends PHPUnit_Framework_TestCase {
    protected function setUp() {
            parent::setUp ();
    }

    protected function tearDown() {
            parent::tearDown ();
    }

    public function __construct() {
    }

    public function testHasParam() {
            global $argv, $argc;
            $this->assertGreaterThan(2, $argc, 'No environment name passed');
            $environment = $argv[2];
    }
}

Then you can call your phpunittest like this:

phpunit EnvironmentTest.php my-computer
Paolo
This sounds promising, I'll try it
dl__
Worked for me. Thanks.
dl__
+1  A: 

You can use PHPUnit's --bootstrap switch for this.

--bootstrap <file>       A "bootstrap" PHP file that is run before the tests.

Then, make a bootstrap.php file that contains variables:

$port = 4445;

In your tests, you can grab those values:

global $port;
$this->setPort($port);

Then run:

phpunit --bootstrap boot.php MyTest.php
Mark Theunissen