A helper file is required to bootstrap the application for running the tests. We have prepared a sample file. Put the file in your tests/ directory as TestHelper.php.

<?phpusePhalcon\Di;usePhalcon\Di\FactoryDefault;usePhalcon\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/incubatorinclude__DIR__."/../vendor/autoload.php";// Use the application autoloader to autoload the classes// Autoload the dependencies found in composer$loader=newLoader();$loader->registerDirs([ROOT_PATH,]);$loader->register();$di=newFactoryDefault();Di::reset();// Add any needed services to the DI hereDi::setDefault($di);

Should you need to test any components from your own library, add them to the autoloader or use the autoloader from your main application.

To help you build the unit tests, we made a few abstract classes you can use to bootstrap the unit tests themselves.
These files exist in the Phalcon incubator @ https://github.com/phalcon/incubator.

To run any unit tests you need to define them. The autoloader will make sure the proper files are loaded so all you need to do is create the files and phpunit will run the tests for you.

This example does not contain a config file, most test cases however, do need one. You can add it to the DI to get the UnitTestCase file.

First create a base unit test called UnitTestCase.php in your /tests directory:

<?phpusePhalcon\Di;usePhalcon\Test\UnitTestCaseasPhalconTestCase;abstractclassUnitTestCaseextendsPhalconTestCase{/** * @var bool */private$_loaded=false;publicfunctionsetUp(){parent::setUp();// 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$this->setDi($di);$this->_loaded=true;}/** * Check if the test case is setup properly * * @throws \PHPUnit_Framework_IncompleteTestError; */publicfunction__destruct(){if(!$this->_loaded){thrownew\PHPUnit_Framework_IncompleteTestError("Please run parent::setUp().");}}}

It’s always a good idea to separate your Unit tests in namespaces. For this test we will create the namespace ‘Test’. So create a file called testsTestUnitTest.php: