An open API service indexing awesome lists of open source software.

https://github.com/ghostwriter/psalm-sandbox

Provides a framework for testing Psalm plugins with PHPUnit
https://github.com/ghostwriter/psalm-sandbox

ghostwriter psalm-plugin psalm-plugin-tester

Last synced: 28 days ago
JSON representation

Provides a framework for testing Psalm plugins with PHPUnit

Awesome Lists containing this project

README

        

# Psalm Sandbox

[![GitHub Sponsors](https://img.shields.io/github/sponsors/ghostwriter?label=Sponsor+@ghostwriter/psalm-sandbox&logo=GitHub+Sponsors)](https://github.com/sponsors/ghostwriter)
[![Automation](https://github.com/ghostwriter/psalm-sandbox/actions/workflows/automation.yml/badge.svg)](https://github.com/ghostwriter/psalm-sandbox/actions/workflows/automation.yml)
[![Supported PHP Version](https://badgen.net/packagist/php/ghostwriter/psalm-sandbox?color=8892bf)](https://www.php.net/supported-versions)
[![Downloads](https://badgen.net/packagist/dt/ghostwriter/psalm-sandbox?color=blue)](https://packagist.org/packages/ghostwriter/psalm-sandbox)

work in progress

> [!WARNING]
>
> This project is not finished yet, work in progress.

## Installation

You can install the package via composer:

``` bash
composer require ghostwriter/psalm-sandbox --dev
```

## Usage

You can create a test for your plugin by extending the `AbstractPsalmSandboxTestCase` class.

eg. `tests/Unit/ExamplePluginTest.php`

```php

*/
public const PLUGINS = [ExamplePlugin::class];

/**
* @var array>
*/
public const ISSUES = [MissingReturnType::class];
}
```

You can then run your tests using PHPUnit.

``` bash
vendor/bin/phpunit
```

## Plugin Development

### Example Directory Structure

Your plugin should have the following directory structure:

``` text
composer.json
ExamplePlugin.php
src/
│ {PsalmIssueType}/
│ │ {FixIssue}.php
│ │ {ReportIssue}.php
│ │ {SuppressIssue}.php
│ MissingReturnType/
│ │ FixMissingReturnType.php
│ │ ReportMissingReturnType.php
│ │ SuppressMissingReturnType.php
tests/
│ Fixture/
│ │ {PsalmIssueType}/
│ │ │ fix-{PsalmIssueType}.php.inc
│ │ │ report-{PsalmIssueType}.php.inc
│ │ │ suppress-{PsalmIssueType}.php.inc
│ │ MissingReturnType/
│ │ │ fix-0001.php.inc
│ │ │ fix-missing-returntype.php.inc
│ │ │ report-0001.php.inc
│ │ │ report-missing-returntype.php.inc
│ │ │ suppress-0001.php.inc
│ │ │ suppress-missing-returntype.php.inc
│ Unit/
│ │ ExamplePluginTest.php
```

### Example `Fix` Test

When you run `vendor/bin/psalm --alter`, it will automatically fix the code for you.

To create a `Fix` test, you need to create a file in the `tests/Fixture/{PsalmIssueType}` directory.

The file name should be prefixed with `fix-` and any unique identifier, e.g. `fix-0001.php.inc`.

The file MUST use `--FIX--` to separate the `before` and `after` code.

The file should be structured as follows:

```php

--FIX--

```

### Example `Report` Test

When you run `vendor/bin/psalm`, it will report the issue in your code.

To create a `Report` test, you need to create a file in the `tests/Fixture/{PsalmIssueType}` directory.

The file name should be prefixed with `report-` and any unique identifier, e.g. `report-0001.php.inc`.

The file should be structured as follows:

```php