PHPUnit Testing for API

Step 1 - Starting Docker Container

From the ~/Documents/elentra-developer directory enter:

docker-compose up

This will start all relevant containers.

Step 2 - Seeding the database

From the ~/Documents/elentra-developer directory, shell into the docker container:

./developer shell

This will open a Command Line Interface (CLI) inside the virtual server To seed the database, run the Elentra seed script (WARNING: you will lose data in existing databases and the contents of your config.inc.php file):

install-elentra

this will drop all the databases, recreate them, and seed the database tables with data from the Seeder classes.

Step 3 - Creating a PHP Unit Test

Inside the elentra-1x-api/tests directory, go into either the Functional or Unit directory (please ensure what type of test you are creating or it will be rejected during code review if it's in the wrong directory), create a new file. eg:

<?php

namespace Tests\Integration\Locations\Services;

use Entrada\Modules\Locations\Database\Seeds\ClinicalLocationsSeedData;
use Tests\TestCase;

class LocationsServiceTest extends TestCase
{

    private $locationsService;

    public function setUp(): void
    {
        parent::setUp();
        $this->locationsService = $this->app->make('Entrada\Modules\Locations\Services\LocationsService');
    }

    /**
     * @test
     * @return void
     */
    public function baseTest() : void {
        $locations = $this->locationsService->listall();

        $this->assertEquals($locations, ClinicalLocationsSeedData::getClinicalLocations());
    }
}

The setUp() method will instantiate any objects required for running tests. These are created by specifying the complete namespace path to the file itself.

the test methods must have the annotation for a test in the comment block:

/**
 * Set parameters for submission
 
 * @test
 */

Without this, the method will be ignored when the tests are run.

Step 4 - Running php unit

from the /var/www/vhosts/elentra-1x-api directory, type in:

composer test

This will run all Functional and Integration tests.

To test a single file, type:

composer test-single <path-to-file>

Troubleshooting

Make sure your elentra-1x-api/phpunit.xml has test suites for both Functional AND Integration specified:

<testsuites>   
    <testsuite name="Functional">
        <directory suffix="Test.php">./tests/Functional</directory>
        <directory suffix="Test.php">./tests/Integration</directory>
    </testsuite>
  </testsuites>

Last updated