ripht-php-sapi 0.1.0-rc.9

Ripht PHP SAPI - A PHP SAPI written in Rust to expose safe and convenient APIs to encourage additional Rust tooling development for PHP
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
use super::*;
use std::error::Error;
use std::path::PathBuf;

fn php_script_path(name: &str) -> PathBuf {
    PathBuf::from(env!("CARGO_MANIFEST_DIR"))
        .join("tests/php_scripts")
        .join(name)
}

fn nonexistent_script_path() -> PathBuf {
    PathBuf::from("/definitely/does/not/exist/script.php")
}

#[derive(Debug, Clone)]
struct TestAdapter {
    required_field: Option<String>,
    optional_port: u16,
    allow_empty_required: bool,
}

impl TestAdapter {
    fn new() -> Self {
        Self {
            required_field: None,
            optional_port: 8080,
            allow_empty_required: false,
        }
    }

    fn with_required_field(mut self, value: impl Into<String>) -> Self {
        self.required_field = Some(value.into());
        self
    }

    fn with_port(mut self, port: u16) -> Self {
        self.optional_port = port;
        self
    }

    fn allow_empty_required(mut self) -> Self {
        self.allow_empty_required = true;
        self
    }
}
impl PhpSapiAdapter for TestAdapter {
    fn build(
        self,
        script_path: impl AsRef<Path>,
    ) -> Result<ExecutionContext, AdapterError> {
        let validated_path = Self::validate_script_path(script_path)?;

        let required = self
            .required_field
            .ok_or_else(|| {
                AdapterError::MissingConfiguration("required_field".to_string())
            })?;

        if !self.allow_empty_required {
            Self::validate_non_empty("required_field", &required)?;
        }

        Self::validate_field(
            "port",
            &self.optional_port,
            |&port| port > 0,
            "must be greater than 0",
        )?;

        Ok(ExecutionContext::script(validated_path)
            .var("TEST_REQUIRED", required)
            .var("TEST_PORT", self.optional_port.to_string()))
    }
}

#[test]
fn test_adapter_error_display() {
    let path = PathBuf::from("/test/path.php");

    let errors = vec![
        (
            AdapterError::ScriptNotFound(path.clone()),
            "Script not found: /test/path.php",
        ),
        (
            AdapterError::MissingConfiguration("field_name".to_string()),
            "Missing required configuration: field_name",
        ),
        (
            AdapterError::InvalidConfiguration {
                field: "port".to_string(),
                value: "99999".to_string(),
                reason: "too large".to_string(),
            },
            "Invalid configuration for 'port' = '99999': too large",
        ),
    ];

    for (error, expected) in errors {
        assert_eq!(error.to_string(), expected);
    }
}

#[test]
fn test_adapter_error_from_web_error() {
    let web_error = WebRequestError::MissingMethod;
    let adapter_error: AdapterError = web_error.clone().into();

    match adapter_error {
        AdapterError::Web(err) => {
            assert_eq!(format!("{}", err), format!("{}", web_error));
        }
        _ => panic!("Expected Web variant"),
    }
}

#[test]
fn test_adapter_error_from_cli_error() {
    let path = PathBuf::from("/test.php");
    let cli_error = CliRequestError::ScriptNotFound(path.clone());
    let adapter_error: AdapterError = cli_error.clone().into();

    match adapter_error {
        AdapterError::Cli(err) => {
            assert_eq!(format!("{}", err), format!("{}", cli_error));
        }
        _ => panic!("Expected Cli variant"),
    }
}

#[test]
fn test_adapter_error_source() {
    let web_error = WebRequestError::InvalidMethod("INVALID".to_string());
    let adapter_error = AdapterError::Web(web_error.clone());

    assert!(adapter_error
        .source()
        .is_some());

    let missing_config = AdapterError::MissingConfiguration("test".to_string());
    assert!(missing_config
        .source()
        .is_none());
}

#[test]
fn test_validate_script_path_existing() {
    let script_path = php_script_path("hello.php");

    let result = TestAdapter::validate_script_path(&script_path);
    assert!(result.is_ok());

    let validated_path = result.unwrap();
    assert!(validated_path.exists());
    assert!(validated_path.is_absolute() || validated_path == script_path);
}

#[test]
fn test_validate_script_path_nonexistent() {
    let script_path = nonexistent_script_path();

    let result = TestAdapter::validate_script_path(&script_path);
    assert!(result.is_err());

    match result.unwrap_err() {
        AdapterError::ScriptNotFound(path) => {
            assert_eq!(path, script_path);
        }
        _ => panic!("Expected ScriptNotFound error"),
    }
}

#[test]
fn test_validate_non_empty_valid() {
    let result = TestAdapter::validate_non_empty("test_field", "valid_value");
    assert!(result.is_ok());
}

#[test]
fn test_validate_non_empty_invalid() {
    let result = TestAdapter::validate_non_empty("test_field", "");
    assert!(result.is_err());

    match result.unwrap_err() {
        AdapterError::MissingConfiguration(field) => {
            assert_eq!(field, "test_field");
        }
        _ => panic!("Expected MissingConfiguration error"),
    }
}

#[test]
fn test_validate_field_success() {
    let result = TestAdapter::validate_field(
        "port",
        &8080u16,
        |&port| port > 1000 && port < 9000,
        "must be between 1000 and 9000",
    );
    assert!(result.is_ok());
}

#[test]
fn test_validate_field_failure() {
    let result = TestAdapter::validate_field(
        "port",
        &65535u16,
        |&port| port < 65535,
        "must be less than 65535",
    );
    assert!(result.is_err());

    match result.unwrap_err() {
        AdapterError::InvalidConfiguration {
            field,
            value,
            reason,
        } => {
            assert_eq!(field, "port");
            assert_eq!(value, "65535");
            assert_eq!(reason, "must be less than 65535");
        }
        _ => panic!("Expected InvalidConfiguration error"),
    }
}

#[test]
fn test_custom_adapter_success() {
    let script_path = php_script_path("hello.php");

    let context = TestAdapter::new()
        .with_required_field("test_value")
        .with_port(3000)
        .build(&script_path)
        .expect("should build successfully");

    assert_eq!(context.script_path, script_path);

    let has_required = context
        .server_vars
        .iter()
        .any(|(k, _)| k == "TEST_REQUIRED");
    let has_port = context
        .server_vars
        .iter()
        .any(|(k, _)| k == "TEST_PORT");

    assert!(has_required, "TEST_REQUIRED should be set");
    assert!(has_port, "TEST_PORT should be set");
}

#[test]
fn test_custom_adapter_missing_required() {
    let script_path = php_script_path("hello.php");

    let result = TestAdapter::new()
        .with_port(3000)
        .build(&script_path);

    assert!(result.is_err());
    match result.unwrap_err() {
        AdapterError::MissingConfiguration(field) => {
            assert_eq!(field, "required_field");
        }
        _ => panic!("Expected MissingConfiguration error"),
    }
}

#[test]
fn test_custom_adapter_empty_required() {
    let script_path = php_script_path("hello.php");

    let result = TestAdapter::new()
        .with_required_field("")
        .build(&script_path);

    assert!(result.is_err());
    match result.unwrap_err() {
        AdapterError::MissingConfiguration(field) => {
            assert_eq!(field, "required_field");
        }
        _ => panic!("Expected MissingConfiguration error"),
    }

    let result = TestAdapter::new()
        .with_required_field("")
        .allow_empty_required()
        .build(&script_path);

    assert!(result.is_ok());
}

#[test]
fn test_custom_adapter_invalid_port() {
    let script_path = php_script_path("hello.php");
    let invalid_port = 0;

    let result = TestAdapter::new()
        .with_required_field("test")
        .with_port(invalid_port)
        .build(&script_path);

    assert!(result.is_err());
    match result.unwrap_err() {
        AdapterError::InvalidConfiguration {
            field,
            value,
            reason,
        } => {
            assert_eq!(field, "port");
            assert_eq!(value, "0");
            assert_eq!(reason, "must be greater than 0");
        }
        _ => panic!("Expected InvalidConfiguration error"),
    }
}

#[test]
fn test_custom_adapter_nonexistent_script() {
    let script_path = nonexistent_script_path();

    let result = TestAdapter::new()
        .with_required_field("test")
        .build(&script_path);

    assert!(result.is_err());
    match result.unwrap_err() {
        AdapterError::ScriptNotFound(path) => {
            assert_eq!(path, script_path);
        }
        _ => panic!("Expected ScriptNotFound error"),
    }
}

#[test]
fn test_web_adapter_trait_implementation() {
    let script_path = php_script_path("hello.php");

    let context = WebRequest::get()
        .with_uri("/test")
        .build(&script_path)
        .expect("should build successfully");

    assert_eq!(context.script_path, script_path);
    assert!(!context.log_to_stderr);
}

#[test]
fn test_web_adapter_trait_error_conversion() {
    let script_path = nonexistent_script_path();

    let result: Result<ExecutionContext, AdapterError> =
        PhpSapiAdapter::build(WebRequest::get(), &script_path);

    assert!(result.is_err());
    match result.unwrap_err() {
        AdapterError::Web(WebRequestError::ScriptNotFound(path)) => {
            assert_eq!(path, script_path);
        }
        _ => panic!("Expected Web(ScriptNotFound) error"),
    }
}

#[test]
fn test_web_adapter_trait_missing_method() {
    let script_path = php_script_path("hello.php");

    let web_request = WebRequest::default();
    let result: Result<ExecutionContext, AdapterError> =
        PhpSapiAdapter::build(web_request, &script_path);

    assert!(result.is_err());
    match result.unwrap_err() {
        AdapterError::Web(WebRequestError::MissingMethod) => {
            // Expected
        }
        _ => panic!("Expected Web(MissingMethod) error"),
    }
}

#[test]
fn test_cli_adapter_trait_implementation() {
    let script_path = php_script_path("hello.php");

    let context = CliRequest::new()
        .with_arg("--test")
        .build(&script_path)
        .expect("should build successfully");

    assert_eq!(context.script_path, script_path);
    assert!(context.log_to_stderr);
}

#[test]
fn test_cli_adapter_trait_error_conversion() {
    let script_path = nonexistent_script_path();

    let result: Result<ExecutionContext, AdapterError> =
        PhpSapiAdapter::build(CliRequest::new(), &script_path);

    assert!(result.is_err());
    match result.unwrap_err() {
        AdapterError::Cli(CliRequestError::ScriptNotFound(path)) => {
            assert_eq!(path, script_path);
        }
        _ => panic!("Expected Cli(ScriptNotFound) error"),
    }
}

// Note: trait objects not supported due to Self: Sized constraints on utility methods
fn execute_adapter<A: PhpSapiAdapter>(
    adapter: A,
    script: impl AsRef<Path>,
) -> Result<ExecutionContext, AdapterError> {
    adapter.build(script)
}

#[test]
fn test_generic_adapter_function() {
    let script_path = php_script_path("hello.php");

    let web_result = execute_adapter(WebRequest::get(), &script_path);
    assert!(web_result.is_ok());

    let cli_result = execute_adapter(CliRequest::new(), &script_path);
    assert!(cli_result.is_ok());

    let test_result = execute_adapter(
        TestAdapter::new().with_required_field("test"),
        &script_path,
    );
    assert!(test_result.is_ok());
}

#[test]
fn test_validation_error_performance() {
    let start = std::time::Instant::now();

    for i in 0..1000 {
        let _ = TestAdapter::validate_field(
            "test_field",
            &i,
            |&val| val < 500,
            "must be less than 500",
        );
    }

    let elapsed = start.elapsed();
    assert!(elapsed.as_millis() < 100, "Validation should be fast");
}

#[test]
fn test_large_configuration_validation() {
    let script_path = php_script_path("hello.php");

    let long_value = "a".repeat(10000);

    let result = TestAdapter::new()
        .with_required_field(&long_value)
        .build(&script_path);

    assert!(result.is_ok());

    let context = result.unwrap();
    let required_value = context
        .server_vars
        .iter()
        .find(|(k, _)| k == "TEST_REQUIRED")
        .map(|(_, v)| v)
        .expect("TEST_REQUIRED should be set");

    assert_eq!(required_value, &long_value);
}