Tutorials: Making Your Project Unit Tested & PSR-4 Compliant

By default, when framework is installed, your project files in application folder will use global namespace and load by require_once. To make your project fully PSR-4 autoload compliant and unit tested follow these steps:

Setting Namespaces

By default, all resources inside application folder are using global namespace. However, you can easily convert it into a namespaced project by following steps:

  1. choose a project specific root namespace
    Example: Foo\Bar
  2. rename all directories and subdirectories in application folder to be pascal-cased
    Example: resolvers becomes Resolvers
  3. update paths @ stdout.xml or stderr.xml to refer at these new locations
    Example: resolvers="application/resolvers" becomes resolvers="application/Resolvers"
  4. namespace existing files in application folder according to PSR-4 autoload standards
    Example: HtmlResolver becomes Foo\Bar\Resolver\Html
  5. update classes @ stdout.xml or stderr.xml to refer at these new names
    Example: class="HtmlResolver" becomes class="Foo\Bar\Resolver\Html"
  6. start developing controllers, models and other classes using this PSR-4 paradigm


Now that you've built a PSR-4 autoload compliant structure, the only step left is to use composer autoload itself by these steps:

  1. remove any existing require_once or require inside project (except composer autoload @ index.php)
  2. open composer.json file in your project root and append this line:
    "autoload": { "psr-4": { "Foo\\Bar\\": "application/" } }
  3. every time you're building a new class, you must remember to write in console:
    composer dump-autoload

Please remember that classes referenced in stdout.xml or stderr.xml will still be automatically require-ed instead of autoloaded! Everything else will use autoloading.

Building Unit Tests

Now that you've made a PSR-4 autoloaded project, the only step left is building unit tests. Of all resources inside a web project only models make any sense to be unit tested. Here are the steps to do so:

  1. inside project root, create folder tests to mirror application/Models
  2. inside project root, create a tests.xml file to configure Unit Testing API with following content:
    <?xml version="1.0" encoding="utf-8"?> <!DOCTYPE xml> <xml> <unit_tests> <unit_test> <sources path="application/Models" namespace="Foo\Bar\Models\"/> <tests path="tests" namespace="Test\Foo\Bar\Models\"/> </unit_test> </unit_tests> </xml>
  3. inside project root, create a test.php file with following content:
    <?php require __DIR__ . '/vendor/autoload.php'; try { new Lucinda\UnitTest\ConsoleController("unit-tests.xml", "local"); } catch (Exception $e) { echo $e->getMessage(); }
  4. open console and execute this file:
    php test.php
  5. above will inspect models folder recursively and create blank unit tests for each class and public method found using exactly the same folder structure, class & method names, only with Tests\ appended to namespace
  6. start developing unit tests using this as model mirroring this. All resources in project under testing will be autoloaded, so no require is needed!
  7. after you've finished building unit tests, execute test.php again to see results.