Apologies. I am following the phalcon phpunit testing docs here:
https://docs.phalcon.io/en/latest/reference/unit-testing.html
So assuming you have installed phalcon incubator (mine is in my project)....
Here is my TestHelper.php as in the example:
I have loaded the config included it and used it to set the db service in the dependency injector.
<?php
use Phalcon\DI,
Phalcon\DI\FactoryDefault;
ini_set('display_errors',1);
error_reporting(E_ALL);
define('ROOT_PATH', __DIR__);
define('PATH_INCUBATOR', __DIR__ . '/../vendor/incubator/');
define('PATH_CONFIG', __DIR__ . '/../app/config/config.php');
define('PATH_MODELS', __DIR__ . '/../app/models/');
set_include_path(
ROOT_PATH . PATH_SEPARATOR . get_include_path()
);
// use the application autoloader to autoload the classes
// autoload the dependencies found in composer
$loader = new \Phalcon\Loader();
$loader->registerDirs(array(
ROOT_PATH,
PATH_CONFIG,
PATH_MODELS
));
$loader->registerNamespaces(array(
'Phalcon' => PATH_INCUBATOR.'Library/Phalcon/'
));
$loader->register();
$config = include PATH_CONFIG;
$di = new FactoryDefault();
DI::reset();
// add any needed services to the DI here
/**
* Database connection is created based in the parameters defined in the configuration file
*/
$di->set('db', function() use ($config) {
return new \Phalcon\Db\Adapter\Pdo\Mysql(array(
"host" => $config->database->host,
"username" => $config->database->username,
"password" => $config->database->password,
"dbname" => $config->database->dbname
));
},true);
DI::setDefault($di);
Then in the UnitTestCase which extends the PhalconTestCase from incubator I have accessed the global config which I use to set the dependency injector with the db service (this is so we can have the db service available in each and every test).
<?php
use Phalcon\DI,
Phalcon\DI\FactoryDefault,
\Phalcon\Test\UnitTestCase as PhalconTestCase;
abstract class UnitTestCase extends PhalconTestCase {
/**
* @var \Voice\Cache
*/
protected $_cache;
/**
* @var \Phalcon\Config
*/
protected $_config;
/**
* @var bool
*/
private $_loaded = false;
public function setUp(Phalcon\DiInterface $di = NULL, Phalcon\Config $config = NULL) {
global $config;
// Load any additional services that might be required during testing
$di = DI::getDefault();
$di = new FactoryDefault();
$di->set('db', function() use ($config) {
return new \Phalcon\Db\Adapter\Pdo\Mysql(array(
"host" => $config->database->host,
"username" => $config->database->username,
"password" => $config->database->password,
"dbname" => $config->database->dbname
));
});
// get any DI components here. If you have a config, be sure to pass it to the parent
parent::setUp($di,$config);
$this->_loaded = true;
}
/**
* Check if the test case is setup properly
* @throws \PHPUnit_Framework_IncompleteTestError;
*/
public function __destruct() {
if(!$this->_loaded) {
throw new \PHPUnit_Framework_IncompleteTestError('Please run parent::setUp().');
}
}
}
My unit tests extend \UnitTestCase as defined above.
I can't recommend doing this. It might be better to setup a database adapter in the phpunit test itself.