views:

25

answers:

1

I am new into PHPUnit, and just digging through the manual. I cannot find a decent example of how to build a complete test from end to end though, and so, am left with questions.

One of these is how can I prep my environment to properly test my code?

I am trying to figure out how to properly pass various configuration values needed for both the test setup/teardown methods, and the configs for the class itself.

// How can I set these variables on testing start?
protected $_db = null;
protected $_config = null;

// So that this function runs properly?
public function setUp(){
    $this->_acl = new acl(
        $this->_db,    // The database connection for the class passed 
                       // from whatever test construct

        $this->_config // Config values passed in from construct
    );
}

// Can I just drop in a construct like this, and have it work properly?
// And if so, how can I set the construct call properly?
public function __construct(
    Zend_Db_Adapter_Abstract $db, $config = array(),
    $baselinedatabase = NULL, $databaseteardown = NULL
){
    $this->_db = $db;
    $this->_config = $config;
    $this->_baselinedatabase = $baselinedatabase;
    $this->_databaseteardown = $databaseteardown;
}

// Or is the wrong idea to be pursuing?
+1  A: 

Since it seems like you're working with Zend Framework, I can tell how we did it, but I can't vouch it's THE right solution. But it works :)

All tests are in a separate tests folder which has Test Suite defined as an XML(so you run it with phpunit --configuration TestSuite.xml command). At the root level there is the TestHelper file however that every test is invoking and that does bootstraping by calling into application's bootstrap class. In app. bootstrap there is a method and does much of bootstrapping but without actual request dispatching. So what you have after running such method will be a ready to use environment(where you have all your Zend_Db, log, modules etc. assembled and ready to go) that unit tests can use. Call into TestHelper happens at the very beginning of each unit test class. Here is a quick example:

/**
 * Unit test for GeoAddressTable model
 * (skipped)
 */

// Call GeoAddressTableTest::main() if this source file is executed directly.
if (!defined('PHPUnit_MAIN_METHOD')) {
        define('PHPUnit_MAIN_METHOD', 'GeoAddressTableTest::main');
}

require_once 'PHPUnit/Framework.php';

require_once dirname(dirname(__FILE__)).'/GeoTestHelper.php';

Hope this helps

Alex N.