From bfdd9d89a7d8a9e907db14ce52ff20a05a35a790 Mon Sep 17 00:00:00 2001 From: Remco Smits Date: Wed, 22 May 2024 13:49:20 +0200 Subject: [PATCH] php: Add runnable tests (#11514) ### This pull request adds the following: - Missing mapping for the `yield` keyword. - Outline scheme for `describe`, `it` and `test` function_call_expressions (to support Pest runnable) - Pest runnable support - PHPUnit runnable support - Task for running selected PHP code. ## Queries explanations #### Query 1 (PHPUnit: Run specific method test): 1. Class is not abstract (because you cannot run tests from an abstract class) 2. Class has `Test` suffix 3. Method has public modifier(or no modifiers, default is public) 4. Method has `test` prefix #### Query 2 (PHPUnit: Run specific method test with `@test` annotation): 1. Class is not abstract (because you cannot run tests from an abstract class) 2. Class has `Test` suffix 3. Method has public modifier(or no modifiers, default is public) 4. Method has `@test` annotation #### Query 3 (PHPUnit: Run specific method test with `#[Test]` attribute): 1. Class is not abstract (because you cannot run tests from an abstract class) 2. Class has `Test` suffix 3. Method has public modifier(or no modifiers, default is public) 4. Method has `#[Test]` attribute #### Query 4 (PHPUnit: Run all tests inside the class): 1. Class is not abstract (because you cannot run tests from an abstract class) 2. Class has `Test` suffix #### Query 5 (Pest: Run function test) 1. Function expression has one of the following names: `describe`, `it` or `test` 2. Function expression first argument is a string ### **PHPUnit: Example for valid test class** Screenshot 2024-05-08 at 10 41 34 ### **PHPUnit: Example for invalid test class** All the methods should be ignored because you cannot run tests on an abstract class. Screenshot 2024-05-07 at 22 28 57 ### **Pest: Example** https://github.com/zed-industries/zed/assets/62463826/bce133eb-0a6f-4ca2-9739-12d9169bb9d6 You should now see all your **Pest** tests inside the buffer symbols modal. ![Screenshot 2024-05-08 at 22 51 25](https://github.com/zed-industries/zed/assets/62463826/9c818b74-383c-45e5-9b41-8dec92759a14) Release Notes: - Added test runnable detection for PHP (PHPUnit & Pest). - Added task for running selected PHP code. - Added `describe`, `test` and `it` functions to buffer symbols, to support Pest runnable. - Added `yield` keyword to PHP keyword mapping. --- extensions/php/languages/php/highlights.scm | 1 + extensions/php/languages/php/outline.scm | 12 +++ extensions/php/languages/php/runnables.scm | 88 +++++++++++++++++++++ extensions/php/languages/php/tasks.json | 19 +++++ 4 files changed, 120 insertions(+) create mode 100644 extensions/php/languages/php/runnables.scm create mode 100644 extensions/php/languages/php/tasks.json diff --git a/extensions/php/languages/php/highlights.scm b/extensions/php/languages/php/highlights.scm index 5f366b53c3..4f1e35ebf2 100644 --- a/extensions/php/languages/php/highlights.scm +++ b/extensions/php/languages/php/highlights.scm @@ -122,3 +122,4 @@ "try" @keyword "use" @keyword "while" @keyword +"yield" @keyword diff --git a/extensions/php/languages/php/outline.scm b/extensions/php/languages/php/outline.scm index 87986f1032..aff05692fb 100644 --- a/extensions/php/languages/php/outline.scm +++ b/extensions/php/languages/php/outline.scm @@ -27,3 +27,15 @@ "trait" @context name: (_) @name ) @item + +; Add support for Pest runnable +(function_call_expression + function: (_) @context + (#any-of? @context "it" "test" "describe") + arguments: (arguments + . + (argument + (encapsed_string (string_value) @name) + ) + ) +) @item diff --git a/extensions/php/languages/php/runnables.scm b/extensions/php/languages/php/runnables.scm new file mode 100644 index 0000000000..8e36d901ea --- /dev/null +++ b/extensions/php/languages/php/runnables.scm @@ -0,0 +1,88 @@ +; Class that follow the naming convention of PHPUnit test classes +; and that doesn't have the abstract modifier +; and have a method that follow the naming convention of PHPUnit test methods +; and the method is public +(class_declaration + modifier: (_)? @_modifier + (#not-eq? @_modifier "abstract") + name: (_) @_name + (#match? @_name ".*Test$") + body: (declaration_list + (method_declaration + (visibility_modifier)? @_visibility + (#eq? @_visibility "public") + name: (_) @run + (#match? @run "^test.*") + ) + ) +) @phpunit-test + +; Class that follow the naming convention of PHPUnit test classes +; and that doesn't have the abstract modifier +; and have a method that has the @test annotation +; and the method is public +(class_declaration + modifier: (_)? @_modifier + (#not-eq? @_modifier "abstract") + name: (_) @_name + (#match? @_name ".*Test$") + body: (declaration_list + ((comment) @_comment + (#match? @_comment ".*@test\\b.*") + . + (method_declaration + (visibility_modifier)? @_visibility + (#eq? @_visibility "public") + name: (_) @run + (#not-match? @run "^test.*") + )) + ) +) @phpunit-test + + +; Class that follow the naming convention of PHPUnit test classes +; and that doesn't have the abstract modifier +; and have a method that has the #[Test] attribute +; and the method is public +(class_declaration + modifier: (_)? @_modifier + (#not-eq? @_modifier "abstract") + name: (_) @_name + (#match? @_name ".*Test$") + body: (declaration_list + (method_declaration + (attribute_list + (attribute_group + (attribute (name) @_attribute) + ) + ) + (#eq? @_attribute "Test") + (visibility_modifier)? @_visibility + (#eq? @_visibility "public") + name: (_) @run + (#not-match? @run "^test.*") + ) + ) +) @phpunit-test + +; Class that follow the naming convention of PHPUnit test classes +; and that doesn't have the abstract modifier +(class_declaration + modifier: (_)? @_modifier + (#not-eq? @_modifier "abstract") + name: (_) @run + (#match? @run ".*Test$") +) @phpunit-test + +; Add support for Pest runnable +; Function expression that has `it`, `test` or `describe` as the function name +(function_call_expression + function: (_) @_name + (#any-of? @_name "it" "test" "describe") + arguments: (arguments + . + (argument + (encapsed_string (string_value) @run) + ) + ) +) @pest-test diff --git a/extensions/php/languages/php/tasks.json b/extensions/php/languages/php/tasks.json new file mode 100644 index 0000000000..65800eebe2 --- /dev/null +++ b/extensions/php/languages/php/tasks.json @@ -0,0 +1,19 @@ +[ + { + "label": "phpunit test $ZED_SYMBOL", + "command": "./vendor/bin/phpunit", + "args": ["--filter $ZED_SYMBOL $ZED_FILE"], + "tags": ["phpunit-test"] + }, + { + "label": "pest test $ZED_SYMBOL", + "command": "./vendor/bin/pest", + "args": ["--filter \"$ZED_SYMBOL\" $ZED_FILE"], + "tags": ["pest-test"] + }, + { + "label": "execute selection $ZED_SELECTED_TEXT", + "command": "php", + "args": ["-r", "$ZED_SELECTED_TEXT"] + } +]