Skip to content

Commit 84d806c

Browse files
committed
Fix tests when no stubs are present
1 parent 7343775 commit 84d806c

2 files changed

Lines changed: 70 additions & 3 deletions

File tree

tests/common/mod.rs

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,61 @@ class SimpleXMLElement
212212
}
213213
";
214214

215+
// ─── Exception class stubs ──────────────────────────────────────────────────
216+
217+
static EXCEPTION_CLASS_STUB: &str = "\
218+
<?php
219+
class Exception implements Throwable
220+
{
221+
public function __construct(string $message = \"\", int $code = 0, ?Throwable $previous = null) {}
222+
223+
/**
224+
* @return string
225+
*/
226+
final public function getMessage(): string {}
227+
228+
/**
229+
* @return int
230+
*/
231+
final public function getCode(): int {}
232+
233+
/**
234+
* @return string
235+
*/
236+
final public function getFile(): string {}
237+
238+
/**
239+
* @return int
240+
*/
241+
final public function getLine(): int {}
242+
243+
/**
244+
* @return array
245+
*/
246+
final public function getTrace(): array {}
247+
248+
/**
249+
* @return string
250+
*/
251+
final public function getTraceAsString(): string {}
252+
253+
/**
254+
* @return ?Throwable
255+
*/
256+
final public function getPrevious(): ?Throwable {}
257+
258+
/**
259+
* @return string
260+
*/
261+
public function __toString(): string {}
262+
}
263+
";
264+
265+
static RUNTIME_EXCEPTION_CLASS_STUB: &str = "\
266+
<?php
267+
class RuntimeException extends Exception {}
268+
";
269+
215270
// ─── Constant stubs ─────────────────────────────────────────────────────────
216271

217272
static CONSTANTS_STUB: &str = "\
@@ -224,6 +279,16 @@ define('SORT_ASC', 4);
224279
define('SORT_DESC', 3);
225280
";
226281

282+
/// Create a test backend whose `stub_index` contains minimal `Exception`
283+
/// and `RuntimeException` stubs. This makes catch-variable tests fully
284+
/// self-contained — they work without phpstorm-stubs installed.
285+
pub fn create_test_backend_with_exception_stubs() -> Backend {
286+
let mut stubs: HashMap<&'static str, &'static str> = HashMap::new();
287+
stubs.insert("Exception", EXCEPTION_CLASS_STUB);
288+
stubs.insert("RuntimeException", RUNTIME_EXCEPTION_CLASS_STUB);
289+
Backend::new_test_with_stubs(stubs)
290+
}
291+
227292
/// Create a test backend whose `stub_index` contains minimal `UnitEnum`
228293
/// and `BackedEnum` stubs. This makes "embedded stub" tests fully
229294
/// self-contained — they no longer require a prior `composer install`.

tests/completion_variables.rs

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

3-
use common::{create_psr4_workspace, create_test_backend};
3+
use common::{
4+
create_psr4_workspace, create_test_backend, create_test_backend_with_exception_stubs,
5+
};
46
use tower_lsp::LanguageServer;
57
use tower_lsp::lsp_types::*;
68

@@ -9442,7 +9444,7 @@ async fn test_completion_catch_variable_top_level() {
94429444
async fn test_completion_catch_variable_no_own_methods_no_namespace() {
94439445
// Same as the namespace test but WITHOUT a namespace — verifies
94449446
// whether the bug is namespace-specific or inheritance-specific.
9445-
let backend = create_test_backend();
9447+
let backend = create_test_backend_with_exception_stubs();
94469448

94479449
let uri = Url::parse("file:///catch_bare_no_ns.php").unwrap();
94489450
let text = concat!(
@@ -9513,7 +9515,7 @@ async fn test_completion_catch_variable_namespace_no_own_methods() {
95139515
// Reproduces: namespace + exception class with NO own methods (only
95149516
// inherits from \RuntimeException) → catch variable should still
95159517
// resolve via inheritance.
9516-
let backend = create_test_backend();
9518+
let backend = create_test_backend_with_exception_stubs();
95179519

95189520
let uri = Url::parse("file:///catch_ns_bare.php").unwrap();
95199521
let text = concat!(

0 commit comments

Comments
 (0)