Testing

Adhering to best practices

The first step in order to ensure the best outcome of testing is making sure xdebug is installed and activated. Neoan3 installs with PHPunit and has a phpunit.xml set up accordingly.

Creating tests

Test-classes are expected to be within the respective component or model. You can use the cli-tool to create these files.

neoan3 new test <component|model> <name>

The tool is smart enough to guess the differences between types of components (e.g. api or route) and suggests test-methods accordingly. In some cases the tests can be completely generated for you.

What this means is best understood by simply trying it out: generate a new route component with:
neoan3 new component example -t:route -v:yes -f:demo and create a test for the component with
neoan3 new test component example

You should have the following in file /component/example/ExampleTest.php

        
            <?php

            namespace Neoan3\Components;

            use PHPUnit\Framework\TestCase;

            class ExampleTest extends TestCase
            {
                private Example $instance;
                function setUp(): void
                {
                    $this->instance = new Example();
                }

                public function testInit()
                {
                    $this->expectOutputRegex('/^<!doctype html>/');
                    $this->instance->init();
                }

            }
        
    

In this simple example the tool identifies that the component is a route component and tests that the output starts with the doctype declaration, ensuring that there are no errors thrown or headers printed.

Running tests

The easiest way is to run neoan3 test, which will execute all tests and (if xdebug is available), create a coverage report in /tests.

Mocking

Since the introduction of providers, mocking has become easier and a MockDatabaseWrapper is available at your disposal. This class implements the Database interface and can therefore be used to write tests without actually executing database transactions. In order to explore its capabilities, simply create a test for an existing model and inspect how the database is tested. Please be advised that your frame has to support dependency injection of a database provider in order to mock components that use models.

Continuous Integration

Neoan3 ships with a .travis.yml which resolves build-testing with php 7.4 and reporting to code-climate for quality, velocity and test-coverage evaluation.

Using Travis CI & Code Climate

The recommended workflow for setting up these tools is to

  • 1. Share your project on GitHub
  • 2. Add your repository to codeclimate
  • 3. Retrieve your Test Reporter ID for the repository from codeclimate
  • 4. Add your repository to Travis-CI
  • 5. Add your Test Reporter ID as an environment variable to your repo on Travis-CI (CC_TEST_REPORTER_ID)

Once these steps are completed, every pull request on your repository on GitHub should trigger a build-process. (Don't forget your badges ;-) )