> For the complete documentation index, see [llms.txt](https://docs.elentra.org/technical/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.elentra.org/technical/developers/troubleshooting/testing-with-codeception/unit-testing/phpunit-testing-for-api.md).

# 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 <a href="#user-content-step-2-seeding-the-database" id="user-content-step-2-seeding-the-database"></a>

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 <a href="#user-content-step-3-creating-a-php-unit-test" id="user-content-step-3-creating-a-php-unit-test"></a>

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 <a href="#user-content-step-4-running-php-unit" id="user-content-step-4-running-php-unit"></a>

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 <a href="#user-content-troubleshooting" id="user-content-troubleshooting"></a>

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>
```
