Skip to content

Commit f871ab7

Browse files
committed
Add missing stubs
1 parent 37b3131 commit f871ab7

4 files changed

Lines changed: 76 additions & 7 deletions

File tree

.github/workflows/release.yml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,20 @@ jobs:
5353
with:
5454
key: ${{ matrix.target }}
5555

56+
- uses: shivammathur/setup-php@v2
57+
with:
58+
php-version: '8.4'
59+
tools: composer
60+
61+
- name: Cache Composer stubs
62+
uses: actions/cache@v4
63+
with:
64+
path: stubs
65+
key: composer-stubs-${{ hashFiles('composer.lock') }}
66+
restore-keys: composer-stubs-
67+
68+
- run: composer install --no-interaction --prefer-dist
69+
5670
- name: Build
5771
run: cargo build --release --target ${{ matrix.target }}
5872

src/lib.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,29 @@ impl Backend {
149149
}
150150
}
151151

152+
/// Create a `Backend` for tests with custom stub class index.
153+
///
154+
/// This allows tests to inject minimal stub content (e.g. `UnitEnum`,
155+
/// `BackedEnum`) without depending on `composer install` having been run.
156+
pub fn new_test_with_stubs(stub_index: HashMap<&'static str, &'static str>) -> Self {
157+
Self {
158+
name: "PHPantomLSP".to_string(),
159+
version: "0.1.0".to_string(),
160+
open_files: Arc::new(Mutex::new(HashMap::new())),
161+
ast_map: Arc::new(Mutex::new(HashMap::new())),
162+
client: None,
163+
workspace_root: Arc::new(Mutex::new(None)),
164+
psr4_mappings: Arc::new(Mutex::new(Vec::new())),
165+
use_map: Arc::new(Mutex::new(HashMap::new())),
166+
namespace_map: Arc::new(Mutex::new(HashMap::new())),
167+
global_functions: Arc::new(Mutex::new(HashMap::new())),
168+
class_index: Arc::new(Mutex::new(HashMap::new())),
169+
stub_index,
170+
stub_function_index: stubs::build_stub_function_index(),
171+
stub_constant_index: stubs::build_stub_constant_index(),
172+
}
173+
}
174+
152175
/// Create a `Backend` for tests with a specific workspace root and PSR-4
153176
/// mappings pre-configured.
154177
pub fn new_test_with_workspace(

tests/common/mod.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,44 @@
11
#![allow(dead_code)]
22

33
use phpantom_lsp::Backend;
4+
use std::collections::HashMap;
45
use std::fs;
56

67
pub fn create_test_backend() -> Backend {
78
Backend::new_test()
89
}
910

11+
// Minimal PHP stubs for UnitEnum and BackedEnum so that tests exercising
12+
// the "embedded stub" code-path work without `composer install`.
13+
static UNIT_ENUM_STUB: &str = "\
14+
<?php
15+
interface UnitEnum
16+
{
17+
public static function cases(): array;
18+
public readonly string $name;
19+
}
20+
";
21+
22+
static BACKED_ENUM_STUB: &str = "\
23+
<?php
24+
interface BackedEnum extends UnitEnum
25+
{
26+
public static function from(int|string $value): static;
27+
public static function tryFrom(int|string $value): ?static;
28+
public readonly int|string $value;
29+
}
30+
";
31+
32+
/// Create a test backend whose `stub_index` contains minimal `UnitEnum`
33+
/// and `BackedEnum` stubs. This makes "embedded stub" tests fully
34+
/// self-contained — they no longer require a prior `composer install`.
35+
pub fn create_test_backend_with_stubs() -> Backend {
36+
let mut stubs: HashMap<&'static str, &'static str> = HashMap::new();
37+
stubs.insert("UnitEnum", UNIT_ENUM_STUB);
38+
stubs.insert("BackedEnum", BACKED_ENUM_STUB);
39+
Backend::new_test_with_stubs(stubs)
40+
}
41+
1042
/// Helper: create a temp workspace with a composer.json and PHP files,
1143
/// then return a Backend configured with that workspace root + PSR-4 mappings.
1244
pub fn create_psr4_workspace(

tests/completion_enums.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
mod common;
22

3-
use common::{create_psr4_workspace, create_test_backend};
3+
use common::{create_psr4_workspace, create_test_backend, create_test_backend_with_stubs};
44

55
use tower_lsp::LanguageServer;
66
use tower_lsp::lsp_types::*;
@@ -11,7 +11,7 @@ use tower_lsp::lsp_types::*;
1111
/// just like `Priority::tryFrom($int)->` does.
1212
#[tokio::test]
1313
async fn test_completion_nullsafe_arrow_on_tryfrom() {
14-
let backend = create_test_backend();
14+
let backend = create_test_backend_with_stubs();
1515

1616
let uri = Url::parse("file:///nullsafe_enum.php").unwrap();
1717
let text = concat!(
@@ -80,7 +80,7 @@ async fn test_completion_nullsafe_arrow_on_tryfrom() {
8080
/// Test: Verify that `->` (without `?`) on tryFrom still works (regression guard).
8181
#[tokio::test]
8282
async fn test_completion_regular_arrow_on_tryfrom() {
83-
let backend = create_test_backend();
83+
let backend = create_test_backend_with_stubs();
8484

8585
let uri = Url::parse("file:///regular_arrow_enum.php").unwrap();
8686
let text = concat!(
@@ -1851,7 +1851,7 @@ async fn test_parser_enum_with_trait_also_has_implicit_interface() {
18511851
/// `$name` without any manually opened stub files.
18521852
#[tokio::test]
18531853
async fn test_completion_unit_enum_gets_cases_from_embedded_stub() {
1854-
let backend = create_test_backend();
1854+
let backend = create_test_backend_with_stubs();
18551855

18561856
let uri = Url::parse("file:///embedded_unit.php").unwrap();
18571857
let text = concat!(
@@ -1938,7 +1938,7 @@ async fn test_completion_unit_enum_gets_cases_from_embedded_stub() {
19381938
/// BackedEnum extends UnitEnum.
19391939
#[tokio::test]
19401940
async fn test_completion_backed_enum_gets_all_spl_members_from_embedded_stubs() {
1941-
let backend = create_test_backend();
1941+
let backend = create_test_backend_with_stubs();
19421942

19431943
let uri = Url::parse("file:///embedded_backed.php").unwrap();
19441944
let text = concat!(
@@ -2037,7 +2037,7 @@ async fn test_completion_backed_enum_gets_all_spl_members_from_embedded_stubs()
20372037
/// (from UnitEnum) and `$value` (from BackedEnum) plus instance methods.
20382038
#[tokio::test]
20392039
async fn test_completion_backed_enum_arrow_gets_properties_from_embedded_stubs() {
2040-
let backend = create_test_backend();
2040+
let backend = create_test_backend_with_stubs();
20412041

20422042
let uri = Url::parse("file:///embedded_arrow.php").unwrap();
20432043
let text = concat!(
@@ -2124,7 +2124,7 @@ async fn test_completion_backed_enum_arrow_gets_properties_from_embedded_stubs()
21242124
/// enum in the same session also gets the methods without re-parsing.
21252125
#[tokio::test]
21262126
async fn test_completion_embedded_stub_caching_across_files() {
2127-
let backend = create_test_backend();
2127+
let backend = create_test_backend_with_stubs();
21282128

21292129
// First file: a unit enum
21302130
let uri1 = Url::parse("file:///cache_test_1.php").unwrap();

0 commit comments

Comments
 (0)