# Testing

> This package provides a consistent set of Composer scripts for local validation.

This package provides a consistent set of [Composer](https://getcomposer.org/) scripts for local validation.

Tool references:

- [Actionlint](https://github.com/rhysd/actionlint) for GitHub Actions workflow linting.
- [Codespell](https://github.com/codespell-project/codespell) for spelling checks.
- [Composer Require Checker](https://github.com/maglnet/ComposerRequireChecker) for dependency definition checks.
- [Easy Coding Standard (ECS)](https://github.com/easy-coding-standard/easy-coding-standard) for coding standards.
- [EditorConfig Checker](https://github.com/editorconfig-checker/editorconfig-checker) for EditorConfig compliance.
- [Gitleaks](https://github.com/gitleaks/gitleaks) for secret scanning.
- [Infection](https://infection.github.io/) for mutation testing.
- [Markdownlint-cli2](https://github.com/DavidAnson/markdownlint-cli2) for Markdown linting.
- [PHPStan](https://phpstan.org/) for static analysis.
- [PHPUnit](https://phpunit.de/) for unit tests.
- [Prettier](https://prettier.io/) for Markdown and YAML formatting checks.
- [Rector](https://github.com/rectorphp/rector) for automated refactoring.
- [Yamllint](https://github.com/adrienverge/yamllint) for YAML linting.
- [Zizmor](https://github.com/zizmorcore/zizmor) for GitHub Actions security audits.

## Automated refactoring (Rector)

Run Rector to apply automated code refactoring. This command modifies files.

```bash
composer rector
```

Preview the refactoring without modifying files.

```bash
composer rector-check
```

## Coding standards (ECS)

Check coding standards without modifying files.

```bash
composer ecs-check
```

Apply coding standard fixes. Review the changes before keeping them.

```bash
composer ecs
```

## Dependency definition check

Verify that runtime dependencies are correctly declared in `composer.json`.

```bash
composer check-dependencies
```

## Mutation testing (Infection)

Run mutation testing through the launcher described under [OpenLDAP directories](#openldap-directories). Without the
directories the `openldap` group is skipped, some mutants survive and the thresholds are not met.

```bash
bash tests/integration/run-openldap.sh composer mutation
```

Run mutation testing with PHPStan static analysis enabled. CI uses this variant and enforces the thresholds in
`infection.json5`; some mutants are caught only by static analysis, so the plain command reports a lower score.

```bash
bash tests/integration/run-openldap.sh composer mutation-static
```

## Repository quality checks

CI runs actionlint, codespell, EditorConfig Checker, markdownlint-cli2, Prettier and yamllint through the shared
[yii2-framework/actions](https://github.com/yii2-framework/actions) quality workflow. The repository's formatting rules
stay in `.editorconfig`.

Make actionlint and ShellCheck available on `PATH` to lint workflows locally.

```bash
composer actionlint
```

## Static analysis (PHPStan)

Run static analysis.

```bash
composer static
```

## Unit tests (PHPUnit)

The test suite needs the PHP LDAP extension. Without a directory server it covers configuration, registry ownership,
setup ordering and failure handling, and skips the `openldap` group. Install dependencies for one Yii release at a time
and repeat with the other version to check compatibility.

```bash
composer update --with yiisoft/yii2:2.0.55
composer tests
```

Use `22.0.x-dev` in place of `2.0.55` for the Yii 22 check. To generate a Clover report with PCOV or Xdebug, run:

```bash
composer tests -- --coverage-clover runtime/coverage.xml
```

## OpenLDAP directories

The `openldap` group runs against real directories: login, distinct groups for the same username, multi-page
synchronization, attribute mapping, single-value membership changes, read-only LDAP ACLs, service-credential handling
and rejection of an untrusted certificate even when the process-wide TLS policy is permissive.

Install the OpenLDAP server tools (`slapd` and `slapadd`) and OpenSSL. The launcher starts three temporary servers bound
to loopback, generates a disposable CA and certificates, runs a command with the connection settings in its environment
and cleans up on exit. It uses no existing directory or credentials. Run the whole suite with the directories available:

```bash
bash tests/integration/run-openldap.sh composer tests
```

Without a command it runs only the `openldap` group. With `--detach` it leaves the servers running and writes the
settings to `GITHUB_ENV`, which is how the CI jobs share them with later steps.

On systems that install OpenLDAP schemas outside `/etc/ldap/schema`, set `LDAP_SCHEMA_DIR`; Arch Linux uses
`/etc/openldap/schema`. If MDB is a shared module, set `LDAP_MODULE_DIR` to the OpenLDAP module directory. To use
alternate binaries or ports, set `SLAPD_BIN`, `SLAPADD_BIN`, `LDAP_TEST_PORT_A`, `LDAP_TEST_PORT_B` or
`LDAP_TEST_PORT_C`.

On Ubuntu runners `tests/integration/install-openldap.sh` installs `slapd` and allows the fixture directory in the
packaged AppArmor profile when its local include is present. It does not disable the profile.

## Deployment acceptance

Before deployment, repeat these checks with non-privileged test accounts in your directory system. The tests above do
not establish Active Directory interoperability. Verify your AD bind format, CA and hostname, plain LDAP refusal, and
membership ACLs independently. Confirm that the application's failed logins cannot create a session and that monitoring
does not capture credentials. Do not use production secrets for acceptance testing.

## Workflow security checks

CI runs zizmor and Gitleaks through the shared security workflow. Gitleaks uses its default rules. The Gitleaks scan
requires `GITLEAKS_LICENSE` in the workflow.

Make zizmor available on `PATH` to audit the workflows locally with the same pedantic persona.

```bash
composer zizmor
```

Zizmor needs GitHub API access for its online audits. Use the offline flag to run without API access.

```bash
composer zizmor -- --offline
```

Report hosted CI execution and its PHP/OS matrix separately from these local commands. Keep generated reports in ignored
`internal/`.

## Passing extra arguments

Composer scripts support forwarding additional arguments using `--`.

Run PHPUnit with code coverage report generation.

```bash
composer tests -- --coverage-html internal/coverage
```

Run PHPStan with a different memory limit.

```bash
composer static -- --memory-limit=512M
```

[Back to documentation](/docs/extensions/ldap/index.md#documentation)