Elentra Platform Technical Documentation
  • Introduction
  • Reporting Issues
    • Security Notices
  • Administrators
    • Server Requirements
    • Application Server
      • Shibboleth Single Sign-on
      • Installing Supervisor
      • Branded API Setup
      • Installing Mailhog
    • Database Server
  • Developers
    • Getting Started
    • Overview
    • Contributions
      • Request for Change
      • Jira and GitHub Details
      • Coding Standards
      • Quickstart Guide
      • Code Review
    • Database
    • Dataviews for Analytics
    • Global Namespace
    • Elentra ACL
    • Authentication Methods
    • Compatibility Matrix
    • Elentra ME Versions
    • Elentra Settings
    • Elentra Deployment
    • Elentra API
    • Elentra JS
    • Feature Configuration
      • Event Resource Integration
      • Microsoft Teams Integration
    • Troubleshooting & Guides
      • New developer features in Elentra ME 1.22
      • Testing With Codeception
        • Unit Testing
          • Unit Testing Best Practises
          • Writing Unit Testable Code
          • PHPUnit Testing for API
          • PHPUnit Json String Compare Function
        • Functional Testing
          • Functional Testing Best Practices
        • Integration Testing
          • Integration Testing Best Practices
      • Upgrading elentra-developer Docker
      • VS Code Setup
      • Using XDebug in VSCode
      • Upgrading PHP
      • Switching Databases
      • Creating a new virtual host
      • Logging In
      • Composer
      • Curriculum Tag Selector
      • Performance Issues
      • Docker
      • Seed Data Generation
      • Fail Fast Programming
      • Advanced Custom Exception Classes
    • Support
  • Upgrade Guides
    • Upgrading to ME v28.0
    • Upgrading to ME 1.27
    • Upgrading to ME 1.26
    • Upgrading to ME 1.25
    • Upgrading to ME 1.24
    • Upgrading to ME 1.23
    • Upgrading to ME 1.22
    • Upgrading to ME 1.21
    • Upgrading to ME 1.20
    • Upgrading to ME 1.19
    • Upgrading to ME 1.18
    • Upgrading to ME 1.17
    • Upgrading to ME 1.16
    • Upgrading to ME 1.15
Powered by GitBook
On this page
  • Step 1 - Starting Docker Container
  • Step 2 - Seeding the database
  • Step 3 - Creating a PHP Unit Test
  • Step 4 - Running php unit
  • Troubleshooting
  1. Developers
  2. Troubleshooting & Guides
  3. Testing With Codeception
  4. Unit Testing

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>
PreviousWriting Unit Testable CodeNextPHPUnit Json String Compare Function

Last updated 2 years ago