Documentation: Unit Tests
test.php is a script whose only purpose is to use Unit Testing API in running as well as generating unit tests (unlike industry standard PHPUnit). All classes subject of unit testing must be located in src folder and their unit test equivalents will be located in tests folder and use Tests\Lucinda\Project\... namespaces in order to be autoloaded!
In order to run unit tests already included in project skeleton you will have to obey following requirements:
- have an SQL vendor installed (eg: MySQL) + PHP extension
- have an NoSQL vendor installed (eg: Redis) + PHP extension
- have SQL mock user created: test identified by me
- have SQL mock schema created: test, for whom above user has full rights
- have SQL tables created in above schema: user_logins, migrations and sessions.
In case you're using different vendors than MySQL and Redis, be aware that unit tests are configured in one or more XML files (depending on what it's tested) found in tests/mocks folder! Otherwise, please use SQL provided in Tutorial.
Running/Generating Unit Tests
To run unit tests on console go to your project root, open console and run:
- loop through all classes in src folder
- loop through all classes in tests folder
- if a class in src folder has no equivalent in tests folder, it will be created according to following algorithm:
- if class @ src has namespace Lucinda\Project\Foo\Bar, then class @ tests will have namespace Test\Lucinda\Project\Foo\BarTest
- for each public method in src class, it creates a same named no-argument public method (*) in tests class
- if a class in src folder has equivalent in tests folder, but one of its public methods is not mirrored:
- for each public method in src class not found in its tests equivalent, it creates in latter a same named no-argument public method
- loops through all classes in tests folder and:
- instances them
- executes each public method in the order they are set
- collects results of above
displays results collected in console
Writing Unit Tests
If you've developed a class in src folder (or developed a new method to an existing class) and ran test.php, you will need to feed code to methods generated. Rules:
- each public method in tests class MUST call its equivalent public method in src class
each public method in tests class can only return:
By default framework uses mock versions of stdout.xmlstdout.xml & stderr.xml as well as mock DAOs for database operations, but you can also use real mock DB on tests schema. In order to keep order for your project, it is advised to develop mocks in tests/mocks folder!
To learn more how to write unit tests, check Tutorial!