if any one can to help me with identical error, please do it)
TestHelper.php
define('ROOT_PATH', DIR);
define('PATH_LIBRARY', DIR . '/../app/library/');
define('PATH_SERVICES', DIR . '/../app/services/');
define('PATH_RESOURCES', DIR . '/../app/resources/');
define('APP_PATH', realpath(DIR . '/..') . '/');
define('LIB_PATH', APP_PATH . 'common/lib/');
define('MODELS_PATH', APP_PATH . 'common/models/');
define('COLLECTIONS_PATH', APP_PATH . 'common/collections/');
define('CONFIG_PATH', APP_PATH . 'common/config/');
define('TEMP_PATH', APP_PATH . 'temp/');
set_include_path(
ROOT_PATH . PATH_SEPARATOR . get_include_path()
);
// required for phalcon/incubator
include DIR . "/../vendor/autoload.php";
require_once(CONFIG_PATH . 'config.php');
// use the application autoloader to autoload the classes
// autoload the dependencies found in composer
$loader = new \Phalcon\Loader();
$loader->registerDirs(array(
ROOT_PATH
));
Register common namespaces
$loader->registerNamespaces(array(
'App\Models' => MODELS_PATH,
'App\Collections' => COLLECTIONS_PATH,
'App\Lib' => LIB_PATH,
));
$loader->register();
$di = new FactoryDefault();
DI::reset();
Register config
require_once(CONFIG_PATH . 'services.php');
// add any needed services to the DI here
DI::setDefault($di);
service.php
<?php
Register DB
$di->setShared('mongo', function () use($config) {
$mongo = new MongoClient('mongodb://mongo:27017');
return $mongo->selectDB($config->mongo->name);
});
$di->setShared('collectionManager', function(){
return new Phalcon\Mvc\Collection\Manager();
});
Mailer (requires composer component)
$di->setShared('mailer', function () use($config) {
$mailer = new \App\Lib\Mailer([
'templates' => APP_PATH . 'common/emails/',
'host' => $config->smtp->host,
'port' => $config->smtp->port,
'username' => $config->smtp->username,
'password' => $config->smtp->password,
'security' => $config->smtp->security
]);
if (!empty($config->project->admin_email) && !empty($config->project->admin_name)) {
$mailer->setFrom($config->project->admin_email, $config->project->admin_name);
}
return $mailer;
});
Logger
$di->setShared('logger', function() use($config, $di) {
$logger = new \Phalcon\Logger\Multiple;
$logger->push(new \Phalcon\Logger\Adapter\File($config->project->log_path));
$logger->push(new \Phalcon\Logger\Adapter\Stream('php://stdout'));
return $logger;
});
$di->setShared('crypt', function() use($config) {
$crypt = new \Phalcon\Crypt();
$crypt->setMode(MCRYPT_MODE_CFB);
$crypt->setKey($config->project->crypt_key);
return $crypt;
});
Session
$di->setShared('session', function() use($config) {
$params = [];
if (!empty($config->project->sess_prefix)) {
$params['uniqueId'] = $config->project->sess_prefix;
}
$session = new \Phalcon\Session\Adapter\Files($params);
$session->start();
return $session;
});
Flash messaging
$di->setShared('flash', function() {
return new \Phalcon\Flash\Session([
'error' => 'alert alert-danger',
'warning' => 'alert alert-warning',
'success' => 'alert alert-success',
'notice' => 'alert alert-info'
]);
});
Config
$di->setShared('config', $config);<?php
Register DB
$di->setShared('mongo', function () use($config) {
$mongo = new MongoClient('mongodb://mongo:27017');
return $mongo->selectDB($config->mongo->name);
});
$di->setShared('collectionManager', function(){
return new Phalcon\Mvc\Collection\Manager();
});
Mailer (requires composer component)
$di->setShared('mailer', function () use($config) {
$mailer = new \App\Lib\Mailer([
'templates' => APP_PATH . 'common/emails/',
'host' => $config->smtp->host,
'port' => $config->smtp->port,
'username' => $config->smtp->username,
'password' => $config->smtp->password,
'security' => $config->smtp->security
]);
if (!empty($config->project->admin_email) && !empty($config->project->admin_name)) {
$mailer->setFrom($config->project->admin_email, $config->project->admin_name);
}
return $mailer;
});
Logger
$di->setShared('logger', function() use($config, $di) {
$logger = new \Phalcon\Logger\Multiple;
$logger->push(new \Phalcon\Logger\Adapter\File($config->project->log_path));
$logger->push(new \Phalcon\Logger\Adapter\Stream('php://stdout'));
return $logger;
});
$di->setShared('crypt', function() use($config) {
$crypt = new \Phalcon\Crypt();
$crypt->setMode(MCRYPT_MODE_CFB);
$crypt->setKey($config->project->crypt_key);
return $crypt;
});
Session
$di->setShared('session', function() use($config) {
$params = [];
if (!empty($config->project->sess_prefix)) {
$params['uniqueId'] = $config->project->sess_prefix;
}
$session = new \Phalcon\Session\Adapter\Files($params);
$session->start();
return $session;
});
Flash messaging
$di->setShared('flash', function() {
return new \Phalcon\Flash\Session([
'error' => 'alert alert-danger',
'warning' => 'alert alert-warning',
'success' => 'alert alert-success',
'notice' => 'alert alert-info'
]);
});
Config
$di->setShared('config', $config);
UnitTestCase.php
<?php
use Phalcon\DI,
\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) {
// Load any additional services that might be required during testing
$di = DI::getDefault();
// get any DI components here. If you have a config, be sure to pass it to the parent
parent::setUp($di);
$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().');
}
}
}
and my test file
<?php
namespace Test;
/**
class InvoiceTest extends \UnitTestCase
{
//test mongo handler
/**
* @dataProvider providerMongoAccount
*/
public function testMongoAccount($a)
{
$testObject = new \App\Collections\Accounts();
$testObject->createAccount($a);
var_dump($testObject->_id); die;
}
public static function providerMongoAccount ()
{
return array (
array ('test1'),
array ('test2'),
array ('test3'),
array ('test4'),
);
}
}
and i get error
Phalcon\Di\Exception: Service 'collectionManager' wasn't found in the dependency injection container