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


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.elentra.org/technical/developers/troubleshooting/testing-with-codeception/unit-testing/phpunit-testing-for-api.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
