So I have started implementing unit testing as part of my phalcon application and have come across an issue in relation to using the Phalcon DI Container. It might just be my lack of understanding but I can't seem to get my service to be used as a shared instance across all test cases. Is this something that's possible to begin with ? I have scoured the web for as many resources as I could find and even followed the tutorial on the Phaclon website, but to no avail. So here are my files so far
init.php
<?php
use Phalcon\Loader;
use Phalcon\Di;
use Phalcon\Di\FactoryDefault;
ini_set("display_errors", 1);
error_reporting(E_ALL);
define("ROOT_PATH", __DIR__);
define("SRC_PATH", __DIR__ . '/../src');
set_include_path(
ROOT_PATH . PATH_SEPARATOR . get_include_path()
);
// Required for phalcon/incubator
include __DIR__ . "/../vendor/autoload.php";
// Setup Phalcon autoloader.
$loader = new Loader();
$loader->registerDirs(
[
ROOT_PATH,
SRC_PATH . '/util'
]
);
$loader->registerNamespaces(
[
'App\Test\Libs' => ROOT_PATH . '/libs'
]
);
$loader->register();
// Setup DI Container.
$di = new FactoryDefault();
$di->setShared('someClass', function () {
return new \App\Test\Libs\SomeClass();
});
Di::setDefault($di);
Now in my UnitTestCase if i do
public function setUp() {
parent::setUp();
$di = Di::getDefault();
var_dump($di->getServices()); // None of my services from the bootstrap show up ???
$this->setDi($di);
$this->_loaded = TRUE;
}
And as a result of the above none of my actual tests which extend UnitTestCase can access the custom services.
I tried moving the Phalcon DI initialisation inside the UnitTestCase setUp() however this meant that a new instance of my service was being returned each time, when I would like to just retrieve the same instance.
So is there something I am missing ? Is this even the correct way to do it ?
I Would really appreciate any form of guidance on this issue