Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
* develop:
  Refactor: tests
  Fix README
  Update README
  Add side bar and context commands
  Remove on post save run_test_file event
  Refactor: settings
  Refactor: on post save event
  Check on_post_save is not none
  Refactor: events
  Add tests: events
  Doc: Preferences
  Update README
  Refactor: events
  • Loading branch information
gerardroche committed Aug 4, 2023
2 parents bc7186e + d42051a commit aed16b9
Show file tree
Hide file tree
Showing 32 changed files with 289 additions and 85 deletions.
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,17 @@

All notable changes are documented in this file using the [Keep a CHANGELOG](http://keepachangelog.com/) principles.

## 3.17.0 - Unreleased

### Added

[#121](https://github.com/NeoVintageous/NeoVintageous/issues/121): Run tests from the side bar menu
[#122](https://github.com/NeoVintageous/NeoVintageous/issues/122): Run tests from the context menu

### Removed

* Removed on post save "run_test_file" event, use on post save "phpunit_test_file" event instead.

## 3.16.0 - 2023-08-02

### Added
Expand Down
22 changes: 22 additions & 0 deletions Context.sublime-menu
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
[
{
"caption": "PHPUnit: Test File",
"command": "phpunit_test_file"
},
{
"caption": "PHPUnit: Test Suite",
"command": "phpunit_test_suite"
},
{
"caption": "PHPUnit: Test Nearest",
"command": "phpunit_test_nearest"
},
{
"caption": "PHPUnit: Test Last",
"command": "phpunit_test_last"
},
{
"caption": "PHPUnit: Test Switch",
"command": "phpunit_test_switch"
}
]
5 changes: 2 additions & 3 deletions Preferences.sublime-settings
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@
"phpunit.font_size": null,

// Commands to run when a file is saved.
"phpunit.on_post_save": [
// "phpunit_test_file",
],
// Example: ["phpunit_test_file"]
"phpunit.on_post_save": [],

// Command-line options to pass to PHPUnit.
// https://phpunit.de/manual/current/en/textui.html#textui.clioptions
Expand Down
154 changes: 96 additions & 58 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<h1 align="center">PHPUnit Kit</h1>
<h1>PHPUnit Kit</h1>

<p align="center">
<p>
<a href="https://github.com/gerardroche/sublime-phpunit/actions/workflows/ci.yml"><img alt="GitHub CI Status" src="https://github.com/gerardroche/sublime-phpunit/actions/workflows/ci.yml/badge.svg?branch=master"></a>
<a href="https://ci.appveyor.com/project/gerardroche/sublime-phpunit/branch/master"><img alt="AppVeyor CI Status" src="https://ci.appveyor.com/api/projects/status/wknvpma8qgjlqh1q/branch/master?svg=true"></a>
<a href="https://codecov.io/gh/gerardroche/sublime-phpunit"><img src="https://codecov.io/gh/gerardroche/sublime-phpunit/branch/master/graph/badge.svg?token=rnB0MiBXlK" alt="CodeCov Coverage Status" /></a>
Expand All @@ -19,13 +19,15 @@ PHPUnit support for [Sublime Text](https://sublimetext.com).
* Run the nearest test
* Run the last test
* Run multiple test methods using a multiple cursor
* Run tests on remote server via SSH
* Run tests via Docker
* Run tests on remote server via SSH :new:
* Run tests via Docker :new:
* Run tests via side bar menu :new:
* Run tests via context menu :new:
* Auto run test on save
* Colour output
* Fast jump to next and previous failure
* Fast switch between test and file-under-test
* Toggle CLI options from the command palette
* Toggle options from the command palette
* Fully customized CLI options configuration
* Support for
- [Artisan] - Artisan is the command line interface included with Laravel.
Expand All @@ -39,43 +41,73 @@ PHPUnit support for [Sublime Text](https://sublimetext.com).

Read [Running PHPUnit Tests from Sublime Text](https://blog.gerardroche.com/2023/05/05/running-phpunit-tests-within-sublime-text/) for a quick introduction.

<details>
<summary><strong>Table of Contents</strong> (click to expand)</summary>

- [Installation](#installation)
- [Setup](#setup)
- [Commands](#commands)
- [Key Bindings](#key-bindings)
- [Strategies](#strategies)
- [Configuring](#configuring)
- [CLI Options](#cli-options)
- [PHPUnit Executable](#phpunit-executable)
- [PHP Executable](#php-executable)
- [SSH](#ssh)
- [Docker](#docker)
- [Auto Commands](#auto-commands)
- [Toggle Commands](#toggle-commands)
- [Custom Toggle Commands](#custom-toggle-commands)
- [NeoVintageous mappings](#neovintageous-mappings)
- [Contributing](#contributing)
- [Changelog](#changelog)
- [Credits](#credits)
- [License](#license)

</details>

## Installation

Install [PHPUnitKit](https://packagecontrol.io/packages/PHPUnitKit) via Package Control.

## Setup

Zero configuration required.
Optional. Zero configuration required.

Add your preferred key bindings.

Optional. Add your preferred key bindings.
**Example**

Menu → Preferences → Key Bindings

```js
{ "keys": ["ctrl+shift+a"], "command": "phpunit_test_suite" },
{ "keys": ["ctrl+shift+c"], "command": "phpunit_test_cancel" },
{ "keys": ["ctrl+shift+f"], "command": "phpunit_test_file" },
{ "keys": ["ctrl+shift+l"], "command": "phpunit_test_last" },
{ "keys": ["ctrl+shift+n"], "command": "phpunit_test_nearest" },
{ "keys": ["ctrl+shift+r"], "command": "phpunit_test_results" },
{ "keys": ["ctrl+shift+s"], "command": "phpunit_test_switch" },
{ "keys": ["ctrl+shift+v"], "command": "phpunit_test_visit" },
```json
[
{ "keys": ["ctrl+shift+a"], "command": "phpunit_test_suite" },
{ "keys": ["ctrl+shift+c"], "command": "phpunit_test_cancel" },
{ "keys": ["ctrl+shift+f"], "command": "phpunit_test_file" },
{ "keys": ["ctrl+shift+l"], "command": "phpunit_test_last" },
{ "keys": ["ctrl+shift+n"], "command": "phpunit_test_nearest" },
{ "keys": ["ctrl+shift+r"], "command": "phpunit_test_results" },
{ "keys": ["ctrl+shift+s"], "command": "phpunit_test_switch" },
{ "keys": ["ctrl+shift+v"], "command": "phpunit_test_visit" }
]
```

## Commands

Command | Description
:------ | :----------
**PHPUnit:&nbsp;Test&nbsp;Nearest** | Run a test nearest to the cursor. If the current file is not a test file, it runs the tests for current file.
**PHPUnit:&nbsp;Test&nbsp;File** | Run tests for the current file.
**PHPUnit:&nbsp;Test&nbsp;Suite** | Run test suite of the current file.
**PHPUnit:&nbsp;Test&nbsp;Last** | Runs the last test.
**PHPUnit:&nbsp;Test&nbsp;Switch** | In a test file opens the file under test, otherwise opens the test file.
**PHPUnit:&nbsp;Test&nbsp;Visit** | Open the last run test.
**PHPUnit:&nbsp;Test&nbsp;Results** | Opens the test output panel (only applies to "basic" strategy).
**PHPUnit:&nbsp;Test&nbsp;Cancel** | Cancel any currently running tests.
**PHPUnit:&nbsp;Test&nbsp;Coverage** | Opens the code coverage in a browser.
**PHPUnit:&nbsp;Toggle...** | Toggle options e.g. PHPUnit CLI options.
| Command | Description
| :-------------------------------------------- | :----------
| **PHPUnit:&nbsp;Test&nbsp;Nearest** | Run a test nearest to the cursor. If the current file is not a test file, it runs the tests for current file.
| **PHPUnit:&nbsp;Test&nbsp;File** | Run tests for the current file.
| **PHPUnit:&nbsp;Test&nbsp;Suite** | Run test suite of the current file.
| **PHPUnit:&nbsp;Test&nbsp;Last** | Runs the last test.
| **PHPUnit:&nbsp;Test&nbsp;Switch** | In a test file opens the file under test, otherwise opens the test file.
| **PHPUnit:&nbsp;Test&nbsp;Visit** | Open the last run test.
| **PHPUnit:&nbsp;Test&nbsp;Results** | Opens the test output panel (only applies to "basic" strategy).
| **PHPUnit:&nbsp;Test&nbsp;Cancel** | Cancel any currently running tests.
| **PHPUnit:&nbsp;Test&nbsp;Coverage** | Opens the code coverage in a browser.
| **PHPUnit:&nbsp;Toggle...** | Toggle options e.g. PHPUnit CLI options.
| **Preferences:&nbsp;PHPUnit&nbsp;Settings** | Edit settings.

## Key Bindings

Expand All @@ -86,10 +118,16 @@ Key | Description

## Strategies

PHPUnitKit can run tests using different execution environments called "strategies". To use a specific strategy, assign it to a setting:
PHPUnitKit can run tests using different execution environments called "strategies".

```js
"phpunit.strategy": "kitty"
**Example:** Use the Kitty terminal strategy

Command Palette → Preferences: PHPUnit Settings

```json
{
"phpunit.strategy": "kitty"
}
```

| Strategy | Identifier | Description
Expand All @@ -101,25 +139,25 @@ PHPUnitKit can run tests using different execution environments called "strategi

## Configuring

Menu → Preferences Settings

| Setting | Type | Default | Description
| :------------------------ | :----------------- | :--------------- | :----------
| `phpunit.executable` | `string` or `list` | Auto discovered. | Path to PHPUnit executable.
| `phpunit.php_executable` | `string` | Auto discovered. | Path to PHP executable.
| `phpunit.options` | `dict` | `{}` | CLI Options to pass to PHPUnit.
| `phpunit.save_all_on_run` | `boolean` | `true` | Save all dirty buffers before running tests.
| `phpunit.on_post_save` | `list` | `[]` | Auto commands when views are saved.
| `phpunit.prepend_cmd` | `list` | `[]` | Prepends test runner command.
| `phpunit.strategy` | `string` | `basic` | Execution environment to run tests.
| `phpunit.composer` | `boolean` | `true` | Use Composer installed executables, if they exist.
| `phpunit.artisan` | `boolean` | `false` | Use Artisan to run tests, if it exists.
| `phpunit.paratest` | `boolean` | `false` | Use ParaTest to run tests, if it exists.
| `phpunit.pest` | `boolean` | `false` | Use Pest to run tests, if it exists.
| `phpunit.font_size` | `integer` | Editor default. | Font size of PHPUnit output.
| `phpunit.debug` | `boolean` | `false` | Prints test runner debug information.

**SSH**
Command Palette → Preferences: PHPUnit Settings

| Setting | Type | Default | Description
| :------------------------ | :----------------- | :------------------- | :----------
| `phpunit.executable` | `string`<br>`list` | Auto&nbsp;discovery. | The path to the PHPUnit executable to use when running tests. Environment variables and user home directory ~ placeholder are expanded. The executable can be a string or a list of parameters. <br>Example: `vendor/bin/phpunit`
| `phpunit.options` | `dict` | `{}` | Command-line options to pass to PHPUnit. <br>Example: `{"no-coverage": true}`
| `phpunit.php_executable` | `string` | Auto&nbsp;discovery. | The path to the PHP executable to use when running tests. Environment variables and user home directory ~ placeholder are expanded. <br>Example: `~/.phpenv/versions/8.2/bin/php`
| `phpunit.save_all_on_run` | `boolean` | `true` | Save all dirty buffers before running tests.
| `phpunit.on_post_save` | `list` | `[]` | Auto commands when views are saved. <br>Example: `["phpunit_test_file"]`
| `phpunit.debug` | `boolean` | `false` | Prints test runner debug information.
| `phpunit.prepend_cmd` | `list` | `[]` | Prepends test runner command.
| `phpunit.strategy` | `string` | `basic` | Execution environment to run tests.
| `phpunit.font_size` | `integer` | Editor default. | Font size of PHPUnit output.
| `phpunit.composer` | `boolean` | `true` | Use Composer installed executables.
| `phpunit.artisan` | `boolean` | `false` | Use Artisan to run tests.
| `phpunit.paratest` | `boolean` | `false` | Use ParaTest to run tests.
| `phpunit.pest` | `boolean` | `false` | Use Pest to run tests.

**SSH settings** :rocket:

| Setting | Type | Default | Description
| :-------------------- | :------------ | :-------- | :----------
Expand All @@ -129,7 +167,7 @@ Menu → Preferences → Settings
| `phpunit.ssh_host` | `string` | `null` | The host to use when running tests via SSH. <br>Example: homestead.test
| `phpunit.ssh_paths` | `dict` | `{}` | The path map to use when running tests via SSH. The keys are local paths and the values are the replacement remote paths. Environment variables and user home directory ~ placeholder are expanded. Example: `{"~/code/project1": "~/project1"}`

**Docker**
**Docker settings** :rocket:

| Setting | Type | Default | Description
| :-------------------- | :------------ | :-------- | :----------
Expand All @@ -141,7 +179,7 @@ Menu → Preferences → Settings

If you want some CLI options to stick around, you can configure them in your settings.

Menu → Preferences Settings
Command Palette → Preferences: PHPUnit Settings

```json
{
Expand Down Expand Up @@ -172,7 +210,7 @@ The above options will be passed to PHPUnit as CLI options:

This can help keep your tests fast. You can toggle no-coverage from the command palette when you need it.

Menu → Preferences Settings
Command Palette → Preferences: PHPUnit Settings

```json
{
Expand All @@ -184,7 +222,7 @@ Menu → Preferences → Settings

**Example:** Stop after first error, failure, warning, or risky test

Menu → Preferences Settings
Command Palette → Preferences: PHPUnit Settings

```json
{
Expand All @@ -198,7 +236,7 @@ Menu → Preferences → Settings

This is useful if you are migrating from PHPUnit to Pest and want to hide superfluous output.

Menu → Preferences Settings
Command Palette → Preferences: PHPUnit Settings

```json
{
Expand All @@ -217,7 +255,7 @@ Default: Auto discovery.

**Examples**

Menu → Preferences Settings
Command Palette → Preferences: PHPUnit Settings

```json
{
Expand All @@ -236,7 +274,7 @@ Default: Auto discovery.
**Examples**


Menu → Preferences Settings
Command Palette → Preferences: PHPUnit Settings

```json
{
Expand Down Expand Up @@ -285,7 +323,7 @@ You can configure the `on_post_save` event to run the "Test File" command when v

**Example:** Run Test File on Save

Menu → Preferences Settings
Command Palette → Preferences: PHPUnit Settings

```json
{
Expand Down
9 changes: 9 additions & 0 deletions Side Bar.sublime-menu
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[
{
"caption": "PHPUnit: Test File",
"command": "phpunit_side_bar_test_file",
"args": {
"files": []
}
}
]
36 changes: 36 additions & 0 deletions lib/events.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Copyright (C) 2023 Gerard Roche
#
# This file is part of PHPUnitKit.
#
# PHPUnitKit is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# PHPUnitKit is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with PHPUnitKit. If not, see <https://www.gnu.org/licenses/>.


from PHPUnitKit.lib.runner import PHPUnit
from PHPUnitKit.lib.utils import get_setting


class Listener():

def on_post_save(self, view) -> None:
file_name = view.file_name()
if not file_name:
return

if not file_name.endswith('.php'):
return

on_post_save_events = get_setting(view, 'on_post_save')
if on_post_save_events:
if 'phpunit_test_file' in on_post_save_events:
PHPUnit(view.window()).run_file()
9 changes: 6 additions & 3 deletions lib/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,13 +149,16 @@ def run_last(self) -> None:

self.run(**last_test_args)

def run_file(self, options=None) -> None:
def run_file(self, file=None, options=None) -> None:
if file is None:
file = self.view.file_name()

if options is None:
options = {}

file = self.view.file_name()
if not file:
return status_message('PHPUnit: not a test file')
status_message('PHPUnit: not a test file')
return

if has_test(self.view):
self.run(file=file, options=options)
Expand Down
2 changes: 1 addition & 1 deletion lib/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def debug_message(msg, *args) -> None:

def is_debug(view) -> bool:
if view:
return view.settings().get('phpunit.debug')
return get_setting(view, 'debug')

return False

Expand Down
Loading

0 comments on commit aed16b9

Please sign in to comment.