We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Set up tests

I am having trouble when I try to load a class in a phpunit test, when I run a test where I load a class: There was 1 error:

1) Tests\Unit\UnitTest::testTestCase Error: Class 'app\models\Users' not found

I have installed a fresh instance of phalcon, connected to a database with a table users, ran phalcon scaffold users, followed the test setup as described in the documentation and made a test:

<?php

declare(strict_types=1);

namespace Tests\Unit;

class UnitTest extends AbstractUnitTest { public function testTestCase(): void { $this->assertEquals( "roman", "roman", "This will pass" );

$user = new \app\models\Users;

}

}

I am probably calling the class in a wrong way or not using the autoloader properly, I have been struggling with this for about 3 days now though and I cant figure out how this should be done, any help will be greatly appreciated.



8.4k
edited Jul '20

i never used unit test but you could get Phalcon\Loader to do that for you

$loader = new \Phalcon\Loader();

$loader->registerDirs(
    [
        'path/to/app/models'
    ]
)->register();

$user = new Users;