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

phpunit create model in test - fatal error

Hi,

I try to create my first unit test and I have problem in create model in test. I did everythink step by step from documentation https://docs.phalcon.io/pl/latest/reference/unit-testing.html and while running tests seems to works poperly.

But if I put following code:

  public function testTestCase()
  {
      $object = new ExampleModel;
  }

I'm getting an error

Fatal error: Class 'ExampleModel' not found in (...)\Project\tests\testsTestUnitTest.php on line 13

ExampleModel is in models dir and working good in application.

Did you loaded it with composer/phalcon autoloader?

I think yes. This is my bootstrap file (TestHelper.php)

use Phalcon\Di;
use Phalcon\Di\FactoryDefault;
use Phalcon\Loader;

ini_set("display_errors", 1);
error_reporting(E_ALL);

define("ROOT_PATH", __DIR__);

set_include_path(
                ROOT_PATH.PATH_SEPARATOR.get_include_path()
);

// Required for phalcon/incubator
include __DIR__."/../vendor/autoload.php";

// Use the application autoloader to autoload the classes
// Autoload the dependencies found in composer
$loader = new Loader();

$loader->registerDirs(
                [
                    ROOT_PATH,
                ]
);

$loader->register();

$di = new FactoryDefault();

Di::reset();

// Add any needed services to the DI here

Di::setDefault($di);

And in which folder this TestHelper.php is?

edited Apr '17

Folder structure:

  app
      cache
      config
      controllers
      forms
      library
      logs
      models
      views
  other
  public
  tests
       PHPunit.xml
       TestHelper.php  <-- bootstrap file, defined in PHPunit.xml
       testsTestUnitTest.php
       UnitTestCase.php
   vendor


145.0k
Accepted
answer

Then it registers tests folder only, you need to register app/models too i guess and other classes from there you need in tests.