From 1ee32f8e1aeec8b075be952ec51bb7f8999da6ed Mon Sep 17 00:00:00 2001 From: Gerard Roche Date: Thu, 3 Aug 2023 11:55:16 +0100 Subject: [PATCH 01/13] Refactor: events --- lib/events.py | 36 ++++++++++++++++++++++++++++++++++++ plugin.py | 16 +++------------- 2 files changed, 39 insertions(+), 13 deletions(-) create mode 100644 lib/events.py diff --git a/lib/events.py b/lib/events.py new file mode 100644 index 0000000..f68642b --- /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 + + +class Listener(): + + 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() diff --git a/plugin.py b/plugin.py index a7dca4e..82135b0 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,7 @@ 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) From 0b2e7ce2c1b22c1cf31020c5a2b08b89b3949cd5 Mon Sep 17 00:00:00 2001 From: Gerard Roche Date: Thu, 3 Aug 2023 11:55:26 +0100 Subject: [PATCH 02/13] Update README --- README.md | 150 +++++++++++++++++++++++++++++++++--------------------- 1 file changed, 93 insertions(+), 57 deletions(-) diff --git a/README.md b/README.md index 19d60e5..05d5a25 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ -

PHPUnit Kit

+

PHPUnit Kit

-

+

GitHub CI Status AppVeyor CI Status CodeCov Coverage Status @@ -19,8 +19,8 @@ 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: * Auto run test on save * Colour output * Fast jump to next and previous failure @@ -39,43 +39,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 +116,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 +137,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 +165,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 +177,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 +208,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 +220,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 +234,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 +253,7 @@ Default: Auto discovery. **Examples** -Menu → Preferences → Settings +Command Palette → Preferences: PHPUnit Settings ```json { @@ -236,7 +272,7 @@ Default: Auto discovery. **Examples** -Menu → Preferences → Settings +Command Palette → Preferences: PHPUnit Settings ```json { @@ -285,7 +321,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 { From cd7838bc52f74ef1856e579093d4fcebfa3f09a2 Mon Sep 17 00:00:00 2001 From: Gerard Roche Date: Thu, 3 Aug 2023 11:55:37 +0100 Subject: [PATCH 03/13] Doc: Preferences --- Preferences.sublime-settings | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) 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 From 731d3ede5808842361aa4422b6061ef66c7dc2ae Mon Sep 17 00:00:00 2001 From: Gerard Roche Date: Thu, 3 Aug 2023 11:55:48 +0100 Subject: [PATCH 04/13] Add tests: events --- tests/test_events.py | 54 ++++++++++++++++++++++++++++++++++++++++++++ tests/unittest.py | 11 ++++++++- 2 files changed, 64 insertions(+), 1 deletion(-) create mode 100644 tests/test_events.py 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..a3a1400 100644 --- a/tests/unittest.py +++ b/tests/unittest.py @@ -15,10 +15,11 @@ # You should have received a copy of the GNU General Public License # along with PHPUnitKit. If not, see . -import os from unittest import TestCase from unittest import mock # noqa: F401 from unittest import skipIf # noqa: F401 +import os +import sys from sublime import find_resources from sublime import active_window @@ -50,3 +51,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) From 5ebaf2019570bb8268419edaacfee7c7d1dcf9e7 Mon Sep 17 00:00:00 2001 From: Gerard Roche Date: Thu, 3 Aug 2023 11:55:58 +0100 Subject: [PATCH 05/13] Refactor: events --- lib/events.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/events.py b/lib/events.py index f68642b..8d6e711 100644 --- a/lib/events.py +++ b/lib/events.py @@ -29,8 +29,8 @@ def on_post_save(self, view): 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'): + post_save_commands = view.settings().get('phpunit.on_post_save') if command in post_save_commands: PHPUnit(view.window()).run_file() From 307a0489abba86962835e25c8657a7d64903691c Mon Sep 17 00:00:00 2001 From: Gerard Roche Date: Thu, 3 Aug 2023 11:56:08 +0100 Subject: [PATCH 06/13] Check on_post_save is not none --- lib/events.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/events.py b/lib/events.py index 8d6e711..7067e96 100644 --- a/lib/events.py +++ b/lib/events.py @@ -32,5 +32,5 @@ def on_post_save(self, view): # 'run_test_file' is deprecated since 3.12.4; use 'phpunit_test_file' instead for command in ('phpunit_test_file', 'run_test_file'): post_save_commands = view.settings().get('phpunit.on_post_save') - if command in post_save_commands: + if post_save_commands and command in post_save_commands: PHPUnit(view.window()).run_file() From 707e7ee385edd3a87dee31c609d3fbbafeadaf7a Mon Sep 17 00:00:00 2001 From: Gerard Roche Date: Thu, 3 Aug 2023 11:56:16 +0100 Subject: [PATCH 07/13] Refactor: on post save event --- lib/events.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/events.py b/lib/events.py index 7067e96..600342e 100644 --- a/lib/events.py +++ b/lib/events.py @@ -31,6 +31,6 @@ def on_post_save(self, view): # 'run_test_file' is deprecated since 3.12.4; use 'phpunit_test_file' instead for command in ('phpunit_test_file', 'run_test_file'): - post_save_commands = view.settings().get('phpunit.on_post_save') - if post_save_commands and command in post_save_commands: + on_post_save_events = view.settings().get('phpunit.on_post_save') + if on_post_save_events and command in on_post_save_events: PHPUnit(view.window()).run_file() From f65b1d84fd0b49e339bd2e4bf527e78329bfec6a Mon Sep 17 00:00:00 2001 From: Gerard Roche Date: Thu, 3 Aug 2023 11:56:30 +0100 Subject: [PATCH 08/13] Refactor: settings --- lib/events.py | 3 ++- lib/utils.py | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/events.py b/lib/events.py index 600342e..309899e 100644 --- a/lib/events.py +++ b/lib/events.py @@ -17,6 +17,7 @@ from PHPUnitKit.lib.runner import PHPUnit +from PHPUnitKit.lib.utils import get_setting class Listener(): @@ -31,6 +32,6 @@ def on_post_save(self, view): # 'run_test_file' is deprecated since 3.12.4; use 'phpunit_test_file' instead for command in ('phpunit_test_file', 'run_test_file'): - on_post_save_events = view.settings().get('phpunit.on_post_save') + on_post_save_events = get_setting(view, 'on_post_save') if on_post_save_events and command in on_post_save_events: PHPUnit(view.window()).run_file() 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 From f612222ff26f02fa458337b85b0cc0333e972863 Mon Sep 17 00:00:00 2001 From: Gerard Roche Date: Thu, 3 Aug 2023 11:57:15 +0100 Subject: [PATCH 09/13] Remove on post save run_test_file event --- CHANGELOG.md | 6 ++++++ lib/events.py | 7 +++---- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c8b24ae..4bd379f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,12 @@ All notable changes are documented in this file using the [Keep a CHANGELOG](http://keepachangelog.com/) principles. +## 3.17.0 - Unreleased + +### 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/lib/events.py b/lib/events.py index 309899e..9ca49ea 100644 --- a/lib/events.py +++ b/lib/events.py @@ -30,8 +30,7 @@ def on_post_save(self, view): if not file_name.endswith('.php'): return - # 'run_test_file' is deprecated since 3.12.4; use 'phpunit_test_file' instead - for command in ('phpunit_test_file', 'run_test_file'): - on_post_save_events = get_setting(view, 'on_post_save') - if on_post_save_events and command in on_post_save_events: + 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() From 429fbc862df27657c26c753ce3e16afe6efc3ca1 Mon Sep 17 00:00:00 2001 From: Gerard Roche Date: Thu, 3 Aug 2023 11:57:24 +0100 Subject: [PATCH 10/13] Add side bar and context commands Close #121 Close #122 --- CHANGELOG.md | 5 +++++ Context.sublime-menu | 22 ++++++++++++++++++++++ Side Bar.sublime-menu | 9 +++++++++ lib/events.py | 2 +- lib/runner.py | 9 ++++++--- plugin.py | 19 +++++++++++++++++++ 6 files changed, 62 insertions(+), 4 deletions(-) create mode 100644 Context.sublime-menu create mode 100644 Side Bar.sublime-menu diff --git a/CHANGELOG.md b/CHANGELOG.md index 4bd379f..91c9a2c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,11 @@ All notable changes are documented in this file using the [Keep a CHANGELOG](htt ## 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. 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/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 index 9ca49ea..7f17c89 100644 --- a/lib/events.py +++ b/lib/events.py @@ -22,7 +22,7 @@ class Listener(): - def on_post_save(self, view): + def on_post_save(self, view) -> None: file_name = view.file_name() if not file_name: return 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/plugin.py b/plugin.py index 82135b0..6ebea6a 100644 --- a/plugin.py +++ b/plugin.py @@ -85,3 +85,22 @@ class PhpunitListener(sublime_plugin.EventListener): def on_post_save(self, view): 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 From 555566233cfc41b37e54ce352bcb88673137b722 Mon Sep 17 00:00:00 2001 From: Gerard Roche Date: Thu, 3 Aug 2023 12:01:24 +0100 Subject: [PATCH 11/13] Update README [skip ci] --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 05d5a25..c6bf9f4 100644 --- a/README.md +++ b/README.md @@ -21,6 +21,8 @@ PHPUnit support for [Sublime Text](https://sublimetext.com). * Run multiple test methods using a multiple cursor * 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 From 6f6d9817380b6de4b803c2601120172801314bf1 Mon Sep 17 00:00:00 2001 From: Gerard Roche Date: Fri, 4 Aug 2023 05:32:27 +0100 Subject: [PATCH 12/13] Fix README --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index c6bf9f4..4bfe005 100644 --- a/README.md +++ b/README.md @@ -27,7 +27,7 @@ PHPUnit support for [Sublime Text](https://sublimetext.com). * 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. From d42051a791603bf812a9c602cf4cd7d387cec8a2 Mon Sep 17 00:00:00 2001 From: Gerard Roche Date: Fri, 4 Aug 2023 05:32:34 +0100 Subject: [PATCH 13/13] Refactor: tests --- tests/commands.py | 1 + tests/lib/__init__.py | 0 tests/lib/strategy/__init__.py | 0 tests/{ => lib/strategy}/test_create_exec_output_panel.py | 3 ++- tests/{ => lib/strategy}/test_exec_file_regex.py | 3 ++- tests/lib/utils/__init__.py | 0 tests/{ => lib/utils}/test_build_cmd_options.py | 0 tests/{ => lib/utils}/test_build_filter_option.py | 0 tests/{ => lib/utils}/test_find_nearest_tests.py | 1 + tests/{ => lib/utils}/test_find_nearest_tests_for_pest.py | 1 + tests/{ => lib/utils}/test_find_php_classes.py | 1 + tests/{ => lib/utils}/test_find_switchable.py | 0 tests/{ => lib/utils}/test_finder.py | 1 + tests/{ => lib/utils}/test_get_php_executable.py | 3 ++- tests/{ => lib/utils}/test_get_phpunit_executable.py | 3 ++- tests/{ => lib/utils}/test_get_phpunit_options.py | 3 ++- tests/{ => lib/utils}/test_has_test.py | 1 + tests/{ => lib/utils}/test_is_debug.py | 0 .../utils}/test_is_valid_php_version_file_version.py | 0 tests/{ => lib/utils}/test_phpunit.py | 3 ++- tests/{ => lib/utils}/test_resolve_working_dir.py | 3 ++- tests/unittest.py | 5 +++-- 22 files changed, 23 insertions(+), 9 deletions(-) create mode 100644 tests/lib/__init__.py create mode 100644 tests/lib/strategy/__init__.py rename tests/{ => lib/strategy}/test_create_exec_output_panel.py (99%) rename tests/{ => lib/strategy}/test_exec_file_regex.py (99%) create mode 100644 tests/lib/utils/__init__.py rename tests/{ => lib/utils}/test_build_cmd_options.py (100%) rename tests/{ => lib/utils}/test_build_filter_option.py (100%) rename tests/{ => lib/utils}/test_find_nearest_tests.py (99%) rename tests/{ => lib/utils}/test_find_nearest_tests_for_pest.py (99%) rename tests/{ => lib/utils}/test_find_php_classes.py (99%) rename tests/{ => lib/utils}/test_find_switchable.py (100%) rename tests/{ => lib/utils}/test_finder.py (99%) rename tests/{ => lib/utils}/test_get_php_executable.py (99%) rename tests/{ => lib/utils}/test_get_phpunit_executable.py (99%) rename tests/{ => lib/utils}/test_get_phpunit_options.py (99%) rename tests/{ => lib/utils}/test_has_test.py (99%) rename tests/{ => lib/utils}/test_is_debug.py (100%) rename tests/{ => lib/utils}/test_is_valid_php_version_file_version.py (100%) rename tests/{ => lib/utils}/test_phpunit.py (99%) rename tests/{ => lib/utils}/test_resolve_working_dir.py (99%) 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/unittest.py b/tests/unittest.py index a3a1400..bdfd820 100644 --- a/tests/unittest.py +++ b/tests/unittest.py @@ -15,11 +15,12 @@ # You should have received a copy of the GNU General Public License # 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 -import os -import sys from sublime import find_resources from sublime import active_window