diff --git a/CHANGELOG.md b/CHANGELOG.md
index c8b24ae..91c9a2c 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -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
diff --git a/Context.sublime-menu b/Context.sublime-menu
new file mode 100644
index 0000000..afc154f
--- /dev/null
+++ b/Context.sublime-menu
@@ -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"
+ }
+]
diff --git a/Preferences.sublime-settings b/Preferences.sublime-settings
index 3af146c..a22a2b0 100644
--- a/Preferences.sublime-settings
+++ b/Preferences.sublime-settings
@@ -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
diff --git a/README.md b/README.md
index 19d60e5..4bfe005 100644
--- a/README.md
+++ b/README.md
@@ -1,6 +1,6 @@
-
PHPUnit Kit
+PHPUnit Kit
-
+
@@ -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.
@@ -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.
+
+ Table of Contents (click to expand)
+
+- [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)
+
+
+
## 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: Test 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: Test File** | Run tests for the current file.
-**PHPUnit: Test Suite** | Run test suite of the current file.
-**PHPUnit: Test Last** | Runs the last test.
-**PHPUnit: Test Switch** | In a test file opens the file under test, otherwise opens the test file.
-**PHPUnit: Test Visit** | Open the last run test.
-**PHPUnit: Test Results** | Opens the test output panel (only applies to "basic" strategy).
-**PHPUnit: Test Cancel** | Cancel any currently running tests.
-**PHPUnit: Test Coverage** | Opens the code coverage in a browser.
-**PHPUnit: Toggle...** | Toggle options e.g. PHPUnit CLI options.
+| Command | Description
+| :-------------------------------------------- | :----------
+| **PHPUnit: Test 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: Test File** | Run tests for the current file.
+| **PHPUnit: Test Suite** | Run test suite of the current file.
+| **PHPUnit: Test Last** | Runs the last test.
+| **PHPUnit: Test Switch** | In a test file opens the file under test, otherwise opens the test file.
+| **PHPUnit: Test Visit** | Open the last run test.
+| **PHPUnit: Test Results** | Opens the test output panel (only applies to "basic" strategy).
+| **PHPUnit: Test Cancel** | Cancel any currently running tests.
+| **PHPUnit: Test Coverage** | Opens the code coverage in a browser.
+| **PHPUnit: Toggle...** | Toggle options e.g. PHPUnit CLI options.
+| **Preferences: PHPUnit Settings** | Edit settings.
## Key Bindings
@@ -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
@@ -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`
`list` | Auto 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.
Example: `vendor/bin/phpunit`
+| `phpunit.options` | `dict` | `{}` | Command-line options to pass to PHPUnit.
Example: `{"no-coverage": true}`
+| `phpunit.php_executable` | `string` | Auto discovery. | The path to the PHP executable to use when running tests. Environment variables and user home directory ~ placeholder are expanded.
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.
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
| :-------------------- | :------------ | :-------- | :----------
@@ -129,7 +167,7 @@ Menu → Preferences → Settings
| `phpunit.ssh_host` | `string` | `null` | The host to use when running tests via SSH.
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
| :-------------------- | :------------ | :-------- | :----------
@@ -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
{
@@ -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
{
@@ -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
{
@@ -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
{
@@ -217,7 +255,7 @@ Default: Auto discovery.
**Examples**
-Menu → Preferences → Settings
+Command Palette → Preferences: PHPUnit Settings
```json
{
@@ -236,7 +274,7 @@ Default: Auto discovery.
**Examples**
-Menu → Preferences → Settings
+Command Palette → Preferences: PHPUnit Settings
```json
{
@@ -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
{
diff --git a/Side Bar.sublime-menu b/Side Bar.sublime-menu
new file mode 100644
index 0000000..44e1cca
--- /dev/null
+++ b/Side Bar.sublime-menu
@@ -0,0 +1,9 @@
+[
+ {
+ "caption": "PHPUnit: Test File",
+ "command": "phpunit_side_bar_test_file",
+ "args": {
+ "files": []
+ }
+ }
+]
diff --git a/lib/events.py b/lib/events.py
new file mode 100644
index 0000000..7f17c89
--- /dev/null
+++ b/lib/events.py
@@ -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 .
+
+
+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()
diff --git a/lib/runner.py b/lib/runner.py
index bcbde25..2b23b38 100644
--- a/lib/runner.py
+++ b/lib/runner.py
@@ -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)
diff --git a/lib/utils.py b/lib/utils.py
index 9d683b5..5ebdcef 100644
--- a/lib/utils.py
+++ b/lib/utils.py
@@ -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
diff --git a/plugin.py b/plugin.py
index a7dca4e..6ebea6a 100644
--- a/plugin.py
+++ b/plugin.py
@@ -17,6 +17,7 @@
import sublime_plugin
+from PHPUnitKit.lib.events import Listener
from PHPUnitKit.lib.runner import PHPUnit
@@ -80,18 +81,26 @@ def run(self):
PHPUnit(self.window).coverage()
-class PhpunitEvents(sublime_plugin.EventListener):
+class PhpunitListener(sublime_plugin.EventListener):
def on_post_save(self, view):
- file_name = view.file_name()
- if not file_name:
- return
-
- if not file_name.endswith('.php'):
- return
-
- post_save_commands = view.settings().get('phpunit.on_post_save')
- # 'run_test_file' is deprecated since 3.12.4; use 'phpunit_test_file' instead
- for command in ('phpunit_test_file', 'run_test_file'):
- if command in post_save_commands:
- PHPUnit(view.window()).run_file()
+ Listener().on_post_save(view)
+
+
+class PhpunitSideBarTestFileCommand(sublime_plugin.WindowCommand):
+
+ def run(self, files):
+ file = self._getTestableFile(files)
+ if file:
+ PHPUnit(self.window).run_file(file)
+ else:
+ PHPUnit(self.window).run_file()
+
+ def is_enabled(self, files):
+ return len(files) == 0 or bool(self._getTestableFile(files))
+
+ def _getTestableFile(self, files):
+ if files and len(files) == 1 and files[0].endswith('.php'):
+ return files[0]
+
+ return None
diff --git a/tests/commands.py b/tests/commands.py
index f2b5288..78e2575 100644
--- a/tests/commands.py
+++ b/tests/commands.py
@@ -19,6 +19,7 @@
class PhpunitTestSetupFixtureCommand(TextCommand):
+
def run(self, edit, text):
# This fixes an issue where an exception is thrown when reloading the
diff --git a/tests/lib/__init__.py b/tests/lib/__init__.py
new file mode 100644
index 0000000..e69de29
diff --git a/tests/lib/strategy/__init__.py b/tests/lib/strategy/__init__.py
new file mode 100644
index 0000000..e69de29
diff --git a/tests/test_create_exec_output_panel.py b/tests/lib/strategy/test_create_exec_output_panel.py
similarity index 99%
rename from tests/test_create_exec_output_panel.py
rename to tests/lib/strategy/test_create_exec_output_panel.py
index 981a257..26779a2 100644
--- a/tests/test_create_exec_output_panel.py
+++ b/tests/lib/strategy/test_create_exec_output_panel.py
@@ -17,9 +17,10 @@
import sublime
-from PHPUnitKit.lib.strategy import _create_exec_output_panel
from PHPUnitKit.tests import unittest
+from PHPUnitKit.lib.strategy import _create_exec_output_panel
+
class TestCreateExecOutputPanel(unittest.ViewTestCase):
diff --git a/tests/test_exec_file_regex.py b/tests/lib/strategy/test_exec_file_regex.py
similarity index 99%
rename from tests/test_exec_file_regex.py
rename to tests/lib/strategy/test_exec_file_regex.py
index 7fac650..9ad8a68 100644
--- a/tests/test_exec_file_regex.py
+++ b/tests/lib/strategy/test_exec_file_regex.py
@@ -16,10 +16,11 @@
# along with PHPUnitKit. If not, see .
import re
-from PHPUnitKit.tests import unittest
from sublime import platform
+from PHPUnitKit.tests import unittest
+
from PHPUnitKit.lib.strategy import _exec_file_regex
diff --git a/tests/lib/utils/__init__.py b/tests/lib/utils/__init__.py
new file mode 100644
index 0000000..e69de29
diff --git a/tests/test_build_cmd_options.py b/tests/lib/utils/test_build_cmd_options.py
similarity index 100%
rename from tests/test_build_cmd_options.py
rename to tests/lib/utils/test_build_cmd_options.py
diff --git a/tests/test_build_filter_option.py b/tests/lib/utils/test_build_filter_option.py
similarity index 100%
rename from tests/test_build_filter_option.py
rename to tests/lib/utils/test_build_filter_option.py
diff --git a/tests/test_find_nearest_tests.py b/tests/lib/utils/test_find_nearest_tests.py
similarity index 99%
rename from tests/test_find_nearest_tests.py
rename to tests/lib/utils/test_find_nearest_tests.py
index 099cde6..df78937 100644
--- a/tests/test_find_nearest_tests.py
+++ b/tests/lib/utils/test_find_nearest_tests.py
@@ -18,6 +18,7 @@
from sublime import find_resources
from PHPUnitKit.tests import unittest
+
from PHPUnitKit.lib.utils import find_nearest_tests
diff --git a/tests/test_find_nearest_tests_for_pest.py b/tests/lib/utils/test_find_nearest_tests_for_pest.py
similarity index 99%
rename from tests/test_find_nearest_tests_for_pest.py
rename to tests/lib/utils/test_find_nearest_tests_for_pest.py
index 1444cb6..b8746e0 100644
--- a/tests/test_find_nearest_tests_for_pest.py
+++ b/tests/lib/utils/test_find_nearest_tests_for_pest.py
@@ -16,6 +16,7 @@
# along with PHPUnitKit. If not, see .
from PHPUnitKit.tests import unittest
+
from PHPUnitKit.lib.utils import find_nearest_tests
diff --git a/tests/test_find_php_classes.py b/tests/lib/utils/test_find_php_classes.py
similarity index 99%
rename from tests/test_find_php_classes.py
rename to tests/lib/utils/test_find_php_classes.py
index 95bc39a..2019fa9 100644
--- a/tests/test_find_php_classes.py
+++ b/tests/lib/utils/test_find_php_classes.py
@@ -16,6 +16,7 @@
# along with PHPUnitKit. If not, see .
from PHPUnitKit.tests import unittest
+
from PHPUnitKit.lib.utils import find_php_classes
diff --git a/tests/test_find_switchable.py b/tests/lib/utils/test_find_switchable.py
similarity index 100%
rename from tests/test_find_switchable.py
rename to tests/lib/utils/test_find_switchable.py
diff --git a/tests/test_finder.py b/tests/lib/utils/test_finder.py
similarity index 99%
rename from tests/test_finder.py
rename to tests/lib/utils/test_finder.py
index 82ed10e..16ab1c6 100644
--- a/tests/test_finder.py
+++ b/tests/lib/utils/test_finder.py
@@ -16,6 +16,7 @@
# along with PHPUnitKit. If not, see .
import os
+
from PHPUnitKit.tests import unittest
from PHPUnitKit.lib.utils import find_phpunit_configuration_file
diff --git a/tests/test_get_php_executable.py b/tests/lib/utils/test_get_php_executable.py
similarity index 99%
rename from tests/test_get_php_executable.py
rename to tests/lib/utils/test_get_php_executable.py
index 526cd29..1ef5f6a 100644
--- a/tests/test_get_php_executable.py
+++ b/tests/lib/utils/test_get_php_executable.py
@@ -19,9 +19,10 @@
import sublime
-from PHPUnitKit.lib.utils import get_php_executable
from PHPUnitKit.tests import unittest
+from PHPUnitKit.lib.utils import get_php_executable
+
class TestGetPHPExecutable(unittest.ViewTestCase):
diff --git a/tests/test_get_phpunit_executable.py b/tests/lib/utils/test_get_phpunit_executable.py
similarity index 99%
rename from tests/test_get_phpunit_executable.py
rename to tests/lib/utils/test_get_phpunit_executable.py
index 48e07ea..52079ed 100644
--- a/tests/test_get_phpunit_executable.py
+++ b/tests/lib/utils/test_get_phpunit_executable.py
@@ -19,9 +19,10 @@
import sublime
-from PHPUnitKit.lib.utils import get_phpunit_executable
from PHPUnitKit.tests import unittest
+from PHPUnitKit.lib.utils import get_phpunit_executable
+
class TestGetPHPUnitExecutable(unittest.ViewTestCase):
diff --git a/tests/test_get_phpunit_options.py b/tests/lib/utils/test_get_phpunit_options.py
similarity index 99%
rename from tests/test_get_phpunit_options.py
rename to tests/lib/utils/test_get_phpunit_options.py
index 1c09673..dea9f67 100644
--- a/tests/test_get_phpunit_options.py
+++ b/tests/lib/utils/test_get_phpunit_options.py
@@ -15,9 +15,10 @@
# You should have received a copy of the GNU General Public License
# along with PHPUnitKit. If not, see .
-from PHPUnitKit.lib.utils import get_phpunit_options
from PHPUnitKit.tests import unittest
+from PHPUnitKit.lib.utils import get_phpunit_options
+
@unittest.mock.patch.dict('PHPUnitKit.lib.utils._session', {}, clear=True)
class TestGetPHPUnitOptions(unittest.ViewTestCase):
diff --git a/tests/test_has_test.py b/tests/lib/utils/test_has_test.py
similarity index 99%
rename from tests/test_has_test.py
rename to tests/lib/utils/test_has_test.py
index ac8b915..c2a6439 100644
--- a/tests/test_has_test.py
+++ b/tests/lib/utils/test_has_test.py
@@ -16,6 +16,7 @@
# along with PHPUnitKit. If not, see .
from PHPUnitKit.tests import unittest
+
from PHPUnitKit.lib.utils import has_test
diff --git a/tests/test_is_debug.py b/tests/lib/utils/test_is_debug.py
similarity index 100%
rename from tests/test_is_debug.py
rename to tests/lib/utils/test_is_debug.py
diff --git a/tests/test_is_valid_php_version_file_version.py b/tests/lib/utils/test_is_valid_php_version_file_version.py
similarity index 100%
rename from tests/test_is_valid_php_version_file_version.py
rename to tests/lib/utils/test_is_valid_php_version_file_version.py
diff --git a/tests/test_phpunit.py b/tests/lib/utils/test_phpunit.py
similarity index 99%
rename from tests/test_phpunit.py
rename to tests/lib/utils/test_phpunit.py
index 60d95b5..c8f4888 100644
--- a/tests/test_phpunit.py
+++ b/tests/lib/utils/test_phpunit.py
@@ -15,9 +15,10 @@
# You should have received a copy of the GNU General Public License
# along with PHPUnitKit. If not, see .
+from PHPUnitKit.tests import unittest
+
from PHPUnitKit.lib.utils import set_last_run
from PHPUnitKit.lib.utils import get_phpunit_options
-from PHPUnitKit.tests import unittest
@unittest.mock.patch.dict('PHPUnitKit.lib.utils._session', {'options': {}}, clear=True)
diff --git a/tests/test_resolve_working_dir.py b/tests/lib/utils/test_resolve_working_dir.py
similarity index 99%
rename from tests/test_resolve_working_dir.py
rename to tests/lib/utils/test_resolve_working_dir.py
index 2c86c72..0ebc76c 100644
--- a/tests/test_resolve_working_dir.py
+++ b/tests/lib/utils/test_resolve_working_dir.py
@@ -15,9 +15,10 @@
# You should have received a copy of the GNU General Public License
# along with PHPUnitKit. If not, see .
-from PHPUnitKit.lib.utils import resolve_working_dir
from PHPUnitKit.tests import unittest
+from PHPUnitKit.lib.utils import resolve_working_dir
+
class ViewStub():
def __init__(self, file_name: str, folders: list):
diff --git a/tests/test_events.py b/tests/test_events.py
new file mode 100644
index 0000000..8ac5994
--- /dev/null
+++ b/tests/test_events.py
@@ -0,0 +1,54 @@
+# 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 .
+
+from PHPUnitKit.plugin import PhpunitListener
+from PHPUnitKit.tests import unittest
+
+
+class TestEvents(unittest.ViewTestCase):
+
+ @unittest.mock.patch('sublime.View.file_name')
+ @unittest.mock.patch('PHPUnitKit.plugin.PHPUnit.run_file')
+ def test_can_run_test_file_on_post_save(self, phpunit, file_name):
+ file_name.return_value = '/tmp/fizz.php'
+ self.view.settings().set('phpunit.on_post_save', ['phpunit_test_file'])
+ PhpunitListener().on_post_save(self.view)
+ phpunit.assert_called()
+
+ @unittest.mock.patch('sublime.View.file_name')
+ @unittest.mock.patch('PHPUnitKit.plugin.PHPUnit')
+ def test_on_post_save_event_does_not_run_if_file_is_not_a_real_file_on_disk(self, phpunit, file_name):
+ file_name.return_value = None
+ self.view.settings().set('phpunit.on_post_save', ['phpunit_test_file'])
+ PhpunitListener().on_post_save(self.view)
+ self.assertMockNotCalled(phpunit)
+
+ @unittest.mock.patch('sublime.View.file_name')
+ @unittest.mock.patch('PHPUnitKit.plugin.PHPUnit')
+ def test_on_post_save_event_does_not_run_for_non_php_files(self, phpunit, file_name):
+ file_name.return_value = '/tmp/fizz.txt'
+ self.view.settings().set('phpunit.on_post_save', ['phpunit_test_file'])
+ PhpunitListener().on_post_save(self.view)
+ self.assertMockNotCalled(phpunit)
+
+ @unittest.mock.patch('sublime.View.file_name')
+ @unittest.mock.patch('PHPUnitKit.plugin.PHPUnit')
+ def test_on_post_save_event_does_not_run_if_no_event_set(self, phpunit, file_name):
+ file_name.return_value = '/tmp/fizz.php'
+ self.view.settings().set('phpunit.on_post_save', [])
+ PhpunitListener().on_post_save(self.view)
+ self.assertMockNotCalled(phpunit)
diff --git a/tests/unittest.py b/tests/unittest.py
index 04c5df8..bdfd820 100644
--- a/tests/unittest.py
+++ b/tests/unittest.py
@@ -16,6 +16,8 @@
# along with PHPUnitKit. If not, see .
import os
+import sys
+
from unittest import TestCase
from unittest import mock # noqa: F401
from unittest import skipIf # noqa: F401
@@ -50,3 +52,11 @@ def fixturePath(self, *path) -> str:
def fixture(self, text) -> None:
self.view.run_command('phpunit_test_setup_fixture', {'text': text})
+
+ def assertMockNotCalled(self, mock_obj) -> None:
+ # https://docs.python.org/3/library/unittest.mock.html
+ # Polyfill for a new mock method added in version 3.5.
+ if sys.version_info >= (3, 5): # pragma: no cover
+ mock_obj.assert_not_called()
+ else:
+ self.assertEqual(mock_obj.call_count, 0)