I'm having trouble migrating an old project from Phalcon 2 (2.0.9) to Phalcon 3 (3.1.2). The code itself seems to run ok (first look), but the unit-tests just won't work. I've been through the documentation (https://docs.phalcon.io/en/3.0.1/reference/unit-testing.html) and even rebuild my TestHelper and UnitTestCase from scratch just to make sure that everything's in place, but especially the DI-container seems to work a lot differently in Phalcon 3 compared to Phalcon 2 in the tests.
For example:
- "parent::setUp()" needs to be run at the beginnig of "setUp()" in UnitTestCase.
- "parent::setUp()" at the end of UnitTestCase needs to be changed to "$this->setDi($di)"
- The config is unknown in all tests unless you explicitely use "$this->setConfig($config)" in UnitTestCase (took me hours just to figure that out).
- It seems I need to register "request" and "response" (as shared) in my DI to run any unit-test that needs the request- or response-object. This was not necessary before. Still the tests won't work, but at least the warning that the services "request/response" are unknown went away.
- The "modelsManager" is now unknown to the DI, also I need to register "modelsMetadata" in the DI to get the unit-tests to work. This also was not necessary with Phalcon 2.
And it still won't work (although I fixed hundreds of warnings with the above fixes), right now I'm getting the message "Phalcon\Di\Exception: Service 'filter' wasn't found in the dependency injection container" when running a unit-test which tries to validate a form using "$form->isValid($post)" which was working fine this way with Phalcon 2.
I've been struggling with this for a couple of days now and starting to plan not to upgrade to Phalcon 3 at all but to keep an old server with Phalcon 2 running for legacy projects, even though this feels like failure ... is anyone experiencing the same problems and is this really the - hard to believe - way to go to upgrade to Phalcon 3?
Here's some of my code, TestHelper:
include __DIR__ . '/../../../vendor/autoload.php';
$loader = new \Phalcon\Loader();
$loader->registerDirs([ROOT_PATH]));
$loader->registerNamespaces( ... );
$loader->register();
$di = new FactoryDefault();
Di::reset();
Di::setDefault($di);
UnitTestCase (for each module):
abstract class UnitTestCase extends PhalconTestCase
{
public function setUp()
{
parent::setUp();
$di = Di::getDefault();
(... setting my services as well as Request/Response here...)
$this->setDi($di);
}
}