Skip to content

Testing

The CRM module includes comprehensive test coverage using PHPUnit for both unit and kernel tests.

Test Coverage

The test suite includes:

  • Unit Tests: Testing individual classes and methods in isolation
  • Kernel Tests: Testing entity operations, services, and form functionality with a minimal Drupal kernel
  • Functional Tests: Testing complete workflows and user interactions

Tests are located in the tests/src/ directory:

  • tests/src/Unit/: Unit tests
  • tests/src/Kernel/: Kernel tests
  • tests/src/Functional/: Functional tests

Running Tests

Standard PHPUnit Execution

Run all CRM tests using PHPUnit:

# From the Drupal root directory
vendor/bin/phpunit web/modules/contrib/crm

# Using DDEV
ddev exec vendor/bin/phpunit web/modules/contrib/crm

Running Specific Test Types

# Run only unit tests
vendor/bin/phpunit --testsuite=unit web/modules/contrib/crm

# Run only kernel tests
vendor/bin/phpunit --testsuite=kernel web/modules/contrib/crm

# Run only functional tests
vendor/bin/phpunit --testsuite=functional web/modules/contrib/crm

# Run a specific test file
vendor/bin/phpunit web/modules/contrib/crm/tests/src/Kernel/Entity/ContactTest.php

Running Tests in Parallel

CRM's composer.json includes ParaTest in require-dev, which can significantly reduce test execution time by running tests in parallel.

Basic ParaTest Usage

# Run all tests in parallel (default 2 processes)
ddev exec ./vendor/bin/paratest

# Specify number of parallel processes
ddev exec ./vendor/bin/paratest -p 4

# Run with specific configuration
ddev exec ./vendor/bin/paratest --phpunit=phpunit.xml.dist

PhpStorm Integration

ParaTest provides support for PhpStorm IDE:

  1. Open Run/Debug Configurations
  2. Create a new PHPUnit configuration
  3. Check the "Use ParaTest" option
  4. Specify the ParaTest path: /path/to/crm/vendor/bin/paratest_for_phpstorm
  5. Configure other options as needed

See ParaTest's GitHub page and PhpStorm's documentation for more information.

Writing Tests

When contributing to the CRM module, ensure your code includes appropriate test coverage:

Test Guidelines

  • Unit Tests: For standalone classes with minimal dependencies
  • Kernel Tests: For entity operations, service methods, and form functionality
  • Functional Tests: For complete user workflows and UI interactions

Example Test Structure

<?php

declare(strict_types=1);

namespace Drupal\Tests\crm\Kernel;

use Drupal\KernelTests\KernelTestBase;
use Drupal\crm\Entity\Contact;

/**
 * Tests contact entity operations.
 *
 * @group crm
 */
class ContactTest extends KernelTestBase {

  /**
   * {@inheritdoc}
   */
  protected static $modules = ['crm', 'user', 'system'];

  /**
   * {@inheritdoc}
   */
  protected function setUp(): void {
    parent::setUp();
    $this->installEntitySchema('crm_contact');
    $this->installEntitySchema('user');
  }

  /**
   * Tests creating a contact entity.
   */
  public function testContactCreation(): void {
    $contact = Contact::create([
      'type' => 'person',
      'name' => 'Test Person',
    ]);

    $contact->save();

    $this->assertNotNull($contact->id());
    $this->assertEquals('Test Person', $contact->label());
  }

}

Continuous Integration

The CRM module uses GitLab CI for automated testing. The configuration is defined in .gitlab-ci.yml in the module root.

All merge requests must pass the full test suite before being merged.