Skip to main content

alef_e2e/codegen/
php.rs

1//! PHP e2e test generator using PHPUnit.
2//!
3//! Generates `e2e/php/composer.json`, `e2e/php/phpunit.xml`, and
4//! `tests/{Category}Test.php` files from JSON fixtures, driven entirely by
5//! `E2eConfig` and `CallConfig`.
6
7use crate::config::E2eConfig;
8use crate::escape::{escape_php, sanitize_filename};
9use crate::field_access::FieldResolver;
10use crate::fixture::{Assertion, CallbackAction, Fixture, FixtureGroup, HttpFixture, ValidationErrorExpectation};
11use alef_backend_php::naming::php_autoload_namespace;
12use alef_core::backend::GeneratedFile;
13use alef_core::config::ResolvedCrateConfig;
14use alef_core::hash::{self, CommentStyle};
15use alef_core::template_versions as tv;
16use anyhow::Result;
17use heck::{ToLowerCamelCase, ToSnakeCase, ToUpperCamelCase};
18use std::collections::HashMap;
19use std::fmt::Write as FmtWrite;
20use std::path::PathBuf;
21
22use super::E2eCodegen;
23use super::client;
24
25/// PHP e2e code generator.
26pub struct PhpCodegen;
27
28impl E2eCodegen for PhpCodegen {
29    fn generate(
30        &self,
31        groups: &[FixtureGroup],
32        e2e_config: &E2eConfig,
33        config: &ResolvedCrateConfig,
34        _type_defs: &[alef_core::ir::TypeDef],
35    ) -> Result<Vec<GeneratedFile>> {
36        let lang = self.language_name();
37        let output_base = PathBuf::from(e2e_config.effective_output()).join(lang);
38
39        let mut files = Vec::new();
40
41        // Resolve top-level call config to derive class/namespace/factory — these are
42        // shared across all categories. Per-fixture call routing (function name, args)
43        // is resolved inside render_test_method via e2e_config.resolve_call().
44        let call = &e2e_config.call;
45        let overrides = call.overrides.get(lang);
46        let extension_name = config.php_extension_name();
47        let class_name = overrides
48            .and_then(|o| o.class.as_ref())
49            .cloned()
50            .map(|cn| cn.split('\\').next_back().unwrap_or(&cn).to_string())
51            .unwrap_or_else(|| extension_name.to_upper_camel_case());
52        let namespace = overrides.and_then(|o| o.module.as_ref()).cloned().unwrap_or_else(|| {
53            if extension_name.contains('_') {
54                extension_name
55                    .split('_')
56                    .map(|p| p.to_upper_camel_case())
57                    .collect::<Vec<_>>()
58                    .join("\\")
59            } else {
60                extension_name.to_upper_camel_case()
61            }
62        });
63        let empty_enum_fields = HashMap::new();
64        let enum_fields = overrides.map(|o| &o.enum_fields).unwrap_or(&empty_enum_fields);
65        let result_is_simple = overrides.is_some_and(|o| o.result_is_simple);
66        let php_client_factory = overrides.and_then(|o| o.php_client_factory.as_deref());
67        let options_via = overrides.and_then(|o| o.options_via.as_deref()).unwrap_or("array");
68
69        // Resolve package config.
70        let php_pkg = e2e_config.resolve_package("php");
71        let pkg_name = php_pkg
72            .as_ref()
73            .and_then(|p| p.name.as_ref())
74            .cloned()
75            .unwrap_or_else(|| {
76                // Derive `<org>/<module>` from the configured repository URL —
77                // alef is vendor-neutral, so we don't fall back to a fixed org.
78                let org = config
79                    .try_github_repo()
80                    .ok()
81                    .as_deref()
82                    .and_then(alef_core::config::derive_repo_org)
83                    .unwrap_or_else(|| config.name.clone());
84                format!("{org}/{}", call.module.replace('_', "-"))
85            });
86        let pkg_path = php_pkg
87            .as_ref()
88            .and_then(|p| p.path.as_ref())
89            .cloned()
90            .unwrap_or_else(|| "../../packages/php".to_string());
91        let pkg_version = php_pkg
92            .as_ref()
93            .and_then(|p| p.version.as_ref())
94            .cloned()
95            .or_else(|| config.resolved_version())
96            .unwrap_or_else(|| "0.1.0".to_string());
97
98        // Derive the e2e composer project metadata from the consumer-binding
99        // pkg_name (`<vendor>/<crate>`) and the configured PHP autoload
100        // namespace — alef is vendor-neutral, so we don't fall back to a
101        // fixed "kreuzberg" string.
102        let e2e_vendor = pkg_name.split('/').next().unwrap_or(&pkg_name).to_string();
103        let e2e_pkg_name = format!("{e2e_vendor}/e2e-php");
104        // PSR-4 autoload keys appear inside a JSON document, so each PHP
105        // namespace separator must be JSON-escaped (`\` → `\\`). The trailing
106        // pair represents the PHP-mandated trailing `\` (which itself escapes
107        // to `\\` in JSON).
108        let php_namespace_escaped = php_autoload_namespace(config).replace('\\', "\\\\");
109        let e2e_autoload_ns = format!("{php_namespace_escaped}\\\\E2e\\\\");
110
111        // Generate composer.json.
112        files.push(GeneratedFile {
113            path: output_base.join("composer.json"),
114            content: render_composer_json(
115                &e2e_pkg_name,
116                &e2e_autoload_ns,
117                &pkg_name,
118                &pkg_path,
119                &pkg_version,
120                e2e_config.dep_mode,
121            ),
122            generated_header: false,
123        });
124
125        // Generate phpunit.xml.
126        files.push(GeneratedFile {
127            path: output_base.join("phpunit.xml"),
128            content: render_phpunit_xml(),
129            generated_header: false,
130        });
131
132        // Check if any fixture needs a mock HTTP server (either http-shape or
133        // liter-llm mock_response-shape) so bootstrap.php spawns it.
134        let has_http_fixtures = groups
135            .iter()
136            .flat_map(|g| g.fixtures.iter())
137            .any(|f| f.needs_mock_server());
138
139        // Check if any fixture uses file_path or bytes args (needs chdir to test_documents).
140        let has_file_fixtures = groups.iter().flat_map(|g| g.fixtures.iter()).any(|f| {
141            let cc = e2e_config.resolve_call_for_fixture(f.call.as_deref(), &f.input);
142            cc.args
143                .iter()
144                .any(|a| a.arg_type == "file_path" || a.arg_type == "bytes")
145        });
146
147        // Generate bootstrap.php that loads both autoloaders and optionally starts the mock server.
148        files.push(GeneratedFile {
149            path: output_base.join("bootstrap.php"),
150            content: render_bootstrap(
151                &pkg_path,
152                has_http_fixtures,
153                has_file_fixtures,
154                &e2e_config.test_documents_relative_from(0),
155            ),
156            generated_header: true,
157        });
158
159        // Generate run_tests.php that loads the extension and invokes phpunit.
160        files.push(GeneratedFile {
161            path: output_base.join("run_tests.php"),
162            content: render_run_tests_php(&extension_name, config.php_cargo_crate_name()),
163            generated_header: true,
164        });
165
166        // Generate test files per category.
167        let tests_base = output_base.join("tests");
168        let field_resolver = FieldResolver::new(
169            &e2e_config.fields,
170            &e2e_config.fields_optional,
171            &e2e_config.result_fields,
172            &e2e_config.fields_array,
173            &std::collections::HashSet::new(),
174        );
175
176        for group in groups {
177            let active: Vec<&Fixture> = group
178                .fixtures
179                .iter()
180                .filter(|f| super::should_include_fixture(f, lang, e2e_config))
181                .collect();
182
183            if active.is_empty() {
184                continue;
185            }
186
187            let test_class = format!("{}Test", sanitize_filename(&group.category).to_upper_camel_case());
188            let filename = format!("{test_class}.php");
189            let content = render_test_file(
190                &group.category,
191                &active,
192                e2e_config,
193                lang,
194                &namespace,
195                &class_name,
196                &test_class,
197                &field_resolver,
198                enum_fields,
199                result_is_simple,
200                php_client_factory,
201                options_via,
202            );
203            files.push(GeneratedFile {
204                path: tests_base.join(filename),
205                content,
206                generated_header: true,
207            });
208        }
209
210        Ok(files)
211    }
212
213    fn language_name(&self) -> &'static str {
214        "php"
215    }
216}
217
218// ---------------------------------------------------------------------------
219// Rendering
220// ---------------------------------------------------------------------------
221
222fn render_composer_json(
223    e2e_pkg_name: &str,
224    e2e_autoload_ns: &str,
225    pkg_name: &str,
226    pkg_path: &str,
227    pkg_version: &str,
228    dep_mode: crate::config::DependencyMode,
229) -> String {
230    let (require_section, autoload_section) = match dep_mode {
231        crate::config::DependencyMode::Registry => {
232            let require = format!(
233                r#"  "require": {{
234    "{pkg_name}": "{pkg_version}"
235  }},
236  "require-dev": {{
237    "phpunit/phpunit": "{phpunit}",
238    "guzzlehttp/guzzle": "{guzzle}"
239  }},"#,
240                phpunit = tv::packagist::PHPUNIT,
241                guzzle = tv::packagist::GUZZLE,
242            );
243            (require, String::new())
244        }
245        crate::config::DependencyMode::Local => {
246            let require = format!(
247                r#"  "require-dev": {{
248    "phpunit/phpunit": "{phpunit}",
249    "guzzlehttp/guzzle": "{guzzle}"
250  }},"#,
251                phpunit = tv::packagist::PHPUNIT,
252                guzzle = tv::packagist::GUZZLE,
253            );
254            // For local mode, add autoload for the local package source.
255            // Extract the namespace from pkg_name (org/module) and map it to src/.
256            let pkg_namespace = pkg_name
257                .split('/')
258                .nth(1)
259                .unwrap_or(pkg_name)
260                .split('-')
261                .map(heck::ToUpperCamelCase::to_upper_camel_case)
262                .collect::<Vec<_>>()
263                .join("\\");
264            let autoload = format!(
265                r#"
266  "autoload": {{
267    "psr-4": {{
268      "{}\\": "{}/src/"
269    }}
270  }},"#,
271                pkg_namespace.replace('\\', "\\\\"),
272                pkg_path
273            );
274            (require, autoload)
275        }
276    };
277
278    crate::template_env::render(
279        "php/composer.json.jinja",
280        minijinja::context! {
281            e2e_pkg_name => e2e_pkg_name,
282            e2e_autoload_ns => e2e_autoload_ns,
283            require_section => require_section,
284            autoload_section => autoload_section,
285        },
286    )
287}
288
289fn render_phpunit_xml() -> String {
290    crate::template_env::render("php/phpunit.xml.jinja", minijinja::context! {})
291}
292
293fn render_bootstrap(
294    pkg_path: &str,
295    has_http_fixtures: bool,
296    has_file_fixtures: bool,
297    test_documents_path: &str,
298) -> String {
299    let header = hash::header(CommentStyle::DoubleSlash);
300    crate::template_env::render(
301        "php/bootstrap.php.jinja",
302        minijinja::context! {
303            header => header,
304            pkg_path => pkg_path,
305            has_http_fixtures => has_http_fixtures,
306            has_file_fixtures => has_file_fixtures,
307            test_documents_path => test_documents_path,
308        },
309    )
310}
311
312fn render_run_tests_php(extension_name: &str, cargo_crate_name: Option<&str>) -> String {
313    let header = hash::header(CommentStyle::DoubleSlash);
314    let ext_lib_name = if let Some(crate_name) = cargo_crate_name {
315        // Cargo replaces hyphens with underscores for lib names, and the crate name
316        // already includes the _php suffix.
317        format!("lib{}", crate_name.replace('-', "_"))
318    } else {
319        format!("lib{extension_name}_php")
320    };
321    format!(
322        r#"#!/usr/bin/env php
323<?php
324{header}
325declare(strict_types=1);
326
327// Determine platform-specific extension suffix.
328$extSuffix = match (PHP_OS_FAMILY) {{
329    'Darwin' => '.dylib',
330    default => '.so',
331}};
332$extPath = __DIR__ . '/../../target/release/{ext_lib_name}' . $extSuffix;
333
334// If the locally-built extension exists and we have not already restarted with it,
335// re-exec PHP with no system ini (-n) to avoid conflicts with any system-installed
336// version of the extension, then load the local build explicitly.
337if (file_exists($extPath) && !getenv('ALEF_PHP_LOCAL_EXT_LOADED')) {{
338    putenv('ALEF_PHP_LOCAL_EXT_LOADED=1');
339    $php = PHP_BINARY;
340    $phpunitPath = __DIR__ . '/vendor/bin/phpunit';
341
342    $cmd = array_merge(
343        [$php, '-n', '-d', 'extension=' . $extPath],
344        [$phpunitPath],
345        array_slice($GLOBALS['argv'], 1)
346    );
347
348    passthru(implode(' ', array_map('escapeshellarg', $cmd)), $exitCode);
349    exit($exitCode);
350}}
351
352// Extension is now loaded (via the restart above with -n flag).
353// Invoke PHPUnit normally.
354$phpunitPath = __DIR__ . '/vendor/bin/phpunit';
355if (!file_exists($phpunitPath)) {{
356    echo "PHPUnit not found at $phpunitPath. Run 'composer install' first.\n";
357    exit(1);
358}}
359
360require $phpunitPath;
361"#
362    )
363}
364
365#[allow(clippy::too_many_arguments)]
366fn render_test_file(
367    category: &str,
368    fixtures: &[&Fixture],
369    e2e_config: &E2eConfig,
370    lang: &str,
371    namespace: &str,
372    class_name: &str,
373    test_class: &str,
374    field_resolver: &FieldResolver,
375    enum_fields: &HashMap<String, String>,
376    result_is_simple: bool,
377    php_client_factory: Option<&str>,
378    options_via: &str,
379) -> String {
380    let header = hash::header(CommentStyle::DoubleSlash);
381
382    // Determine if any handle arg has a non-null config (needs CrawlConfig import).
383    let needs_crawl_config_import = fixtures.iter().any(|f| {
384        let call = e2e_config.resolve_call_for_fixture(f.call.as_deref(), &f.input);
385        call.args.iter().filter(|a| a.arg_type == "handle").any(|a| {
386            let v = f.input.get(&a.field).unwrap_or(&serde_json::Value::Null);
387            !(v.is_null() || v.is_object() && v.as_object().is_some_and(|o| o.is_empty()))
388        })
389    });
390
391    // Determine if any fixture is an HTTP test (needs GuzzleHttp).
392    let has_http_tests = fixtures.iter().any(|f| f.is_http_test());
393
394    // Collect options_type class names that need `use` imports (one import per unique name).
395    let mut options_type_imports: Vec<String> = fixtures
396        .iter()
397        .flat_map(|f| {
398            let call = e2e_config.resolve_call_for_fixture(f.call.as_deref(), &f.input);
399            let php_override = call.overrides.get(lang);
400            let opt_type = php_override.and_then(|o| o.options_type.as_deref()).or_else(|| {
401                e2e_config
402                    .call
403                    .overrides
404                    .get(lang)
405                    .and_then(|o| o.options_type.as_deref())
406            });
407            let element_types: Vec<String> = call
408                .args
409                .iter()
410                .filter_map(|a| a.element_type.as_ref().map(|t| t.to_string()))
411                .filter(|t| !is_php_reserved_type(t))
412                .collect();
413            opt_type.map(|t| t.to_string()).into_iter().chain(element_types)
414        })
415        .collect::<std::collections::HashSet<_>>()
416        .into_iter()
417        .collect();
418    options_type_imports.sort();
419
420    // Build imports_use list
421    let mut imports_use: Vec<String> = Vec::new();
422    if needs_crawl_config_import {
423        imports_use.push(format!("use {namespace}\\CrawlConfig;"));
424    }
425    for type_name in &options_type_imports {
426        if type_name != class_name {
427            imports_use.push(format!("use {namespace}\\{type_name};"));
428        }
429    }
430
431    // Render all test methods
432    let mut fixtures_body = String::new();
433    for (i, fixture) in fixtures.iter().enumerate() {
434        if fixture.is_http_test() {
435            render_http_test_method(&mut fixtures_body, fixture, fixture.http.as_ref().unwrap());
436        } else {
437            render_test_method(
438                &mut fixtures_body,
439                fixture,
440                e2e_config,
441                lang,
442                namespace,
443                class_name,
444                field_resolver,
445                enum_fields,
446                result_is_simple,
447                php_client_factory,
448                options_via,
449            );
450        }
451        if i + 1 < fixtures.len() {
452            fixtures_body.push('\n');
453        }
454    }
455
456    crate::template_env::render(
457        "php/test_file.jinja",
458        minijinja::context! {
459            header => header,
460            namespace => namespace,
461            class_name => class_name,
462            test_class => test_class,
463            category => category,
464            imports_use => imports_use,
465            has_http_tests => has_http_tests,
466            fixtures_body => fixtures_body,
467        },
468    )
469}
470
471// ---------------------------------------------------------------------------
472// HTTP test rendering — shared-driver integration
473// ---------------------------------------------------------------------------
474
475/// Thin renderer that emits PHPUnit test methods targeting a mock server via
476/// Guzzle. Satisfies [`client::TestClientRenderer`] so the shared
477/// [`client::http_call::render_http_test`] driver drives the call sequence.
478struct PhpTestClientRenderer;
479
480impl client::TestClientRenderer for PhpTestClientRenderer {
481    fn language_name(&self) -> &'static str {
482        "php"
483    }
484
485    /// Convert a fixture id to a PHP-valid identifier (snake_case via `sanitize_filename`).
486    fn sanitize_test_name(&self, id: &str) -> String {
487        sanitize_filename(id)
488    }
489
490    /// Emit `/** {description} */ public function test_{fn_name}(): void {`.
491    ///
492    /// When `skip_reason` is `Some`, emits a `markTestSkipped(...)` body and the
493    /// shared driver calls `render_test_close` immediately after, so the closing
494    /// brace is emitted symmetrically.
495    fn render_test_open(&self, out: &mut String, fn_name: &str, description: &str, skip_reason: Option<&str>) {
496        let escaped_reason = skip_reason.map(escape_php);
497        let rendered = crate::template_env::render(
498            "php/http_test_open.jinja",
499            minijinja::context! {
500                fn_name => fn_name,
501                description => description,
502                skip_reason => escaped_reason,
503            },
504        );
505        out.push_str(&rendered);
506    }
507
508    /// Emit the closing `}` for a test method.
509    fn render_test_close(&self, out: &mut String) {
510        let rendered = crate::template_env::render("php/http_test_close.jinja", minijinja::context! {});
511        out.push_str(&rendered);
512    }
513
514    /// Emit a Guzzle request to the mock server's `/fixtures/<fixture_id>` endpoint.
515    ///
516    /// The fixture id is extracted from the path (which the mock server routes as
517    /// `/fixtures/<id>`). `$response` is bound for subsequent assertion methods.
518    fn render_call(&self, out: &mut String, ctx: &client::CallCtx<'_>) {
519        let method = ctx.method.to_uppercase();
520
521        // Build Guzzle options array.
522        let mut opts: Vec<String> = Vec::new();
523
524        if let Some(body) = ctx.body {
525            let php_body = json_to_php(body);
526            opts.push(format!("'json' => {php_body}"));
527        }
528
529        // Merge explicit headers and content_type hint.
530        let mut header_pairs: Vec<String> = Vec::new();
531        if let Some(ct) = ctx.content_type {
532            // Only emit if not already in ctx.headers (avoid duplicate Content-Type).
533            if !ctx.headers.keys().any(|k| k.to_lowercase() == "content-type") {
534                header_pairs.push(format!("\"Content-Type\" => \"{}\"", escape_php(ct)));
535            }
536        }
537        for (k, v) in ctx.headers {
538            header_pairs.push(format!("\"{}\" => \"{}\"", escape_php(k), escape_php(v)));
539        }
540        if !header_pairs.is_empty() {
541            opts.push(format!("'headers' => [{}]", header_pairs.join(", ")));
542        }
543
544        if !ctx.cookies.is_empty() {
545            let cookie_str = ctx
546                .cookies
547                .iter()
548                .map(|(k, v)| format!("{}={}", k, v))
549                .collect::<Vec<_>>()
550                .join("; ");
551            opts.push(format!("'headers' => ['Cookie' => \"{}\"]", escape_php(&cookie_str)));
552        }
553
554        if !ctx.query_params.is_empty() {
555            let pairs: Vec<String> = ctx
556                .query_params
557                .iter()
558                .map(|(k, v)| {
559                    let val_str = match v {
560                        serde_json::Value::String(s) => s.clone(),
561                        other => other.to_string(),
562                    };
563                    format!("\"{}\" => \"{}\"", escape_php(k), escape_php(&val_str))
564                })
565                .collect();
566            opts.push(format!("'query' => [{}]", pairs.join(", ")));
567        }
568
569        let path_lit = format!("\"{}\"", escape_php(ctx.path));
570
571        let rendered = crate::template_env::render(
572            "php/http_request.jinja",
573            minijinja::context! {
574                method => method,
575                path => path_lit,
576                opts => opts,
577                response_var => ctx.response_var,
578            },
579        );
580        out.push_str(&rendered);
581    }
582
583    /// Emit `$this->assertEquals(status, $response->getStatusCode())`.
584    fn render_assert_status(&self, out: &mut String, _response_var: &str, status: u16) {
585        let rendered = crate::template_env::render(
586            "php/http_assertions.jinja",
587            minijinja::context! {
588                response_var => "",
589                status_code => status,
590                headers => Vec::<std::collections::HashMap<&str, String>>::new(),
591                body_assertion => String::new(),
592                partial_body => Vec::<std::collections::HashMap<&str, String>>::new(),
593                validation_errors => Vec::<std::collections::HashMap<&str, String>>::new(),
594            },
595        );
596        out.push_str(&rendered);
597    }
598
599    /// Emit a header assertion using `$response->getHeaderLine(...)` or
600    /// `$response->hasHeader(...)`.
601    ///
602    /// Handles special tokens: `<<present>>`, `<<absent>>`, `<<uuid>>`.
603    fn render_assert_header(&self, out: &mut String, _response_var: &str, name: &str, expected: &str) {
604        let header_key = name.to_lowercase();
605        let header_key_lit = format!("\"{}\"", escape_php(&header_key));
606        let assertion_code = match expected {
607            "<<present>>" => {
608                format!("$this->assertTrue($response->hasHeader({header_key_lit}));")
609            }
610            "<<absent>>" => {
611                format!("$this->assertFalse($response->hasHeader({header_key_lit}));")
612            }
613            "<<uuid>>" => {
614                format!(
615                    "$this->assertMatchesRegularExpression('/^[0-9a-f]{{8}}-[0-9a-f]{{4}}-[0-9a-f]{{4}}-[0-9a-f]{{4}}-[0-9a-f]{{12}}$/i', $response->getHeaderLine({header_key_lit}));"
616                )
617            }
618            literal => {
619                let val_lit = format!("\"{}\"", escape_php(literal));
620                format!("$this->assertEquals({val_lit}, $response->getHeaderLine({header_key_lit}));")
621            }
622        };
623
624        let mut headers = vec![std::collections::HashMap::new()];
625        headers[0].insert("assertion_code", assertion_code);
626
627        let rendered = crate::template_env::render(
628            "php/http_assertions.jinja",
629            minijinja::context! {
630                response_var => "",
631                status_code => 0u16,
632                headers => headers,
633                body_assertion => String::new(),
634                partial_body => Vec::<std::collections::HashMap<&str, String>>::new(),
635                validation_errors => Vec::<std::collections::HashMap<&str, String>>::new(),
636            },
637        );
638        out.push_str(&rendered);
639    }
640
641    /// Emit a JSON body equality assertion.
642    ///
643    /// Plain string bodies are compared against `(string) $response->getBody()` directly;
644    /// structured bodies (objects, arrays, booleans, numbers) are decoded via `json_decode`
645    /// and compared with `assertEquals`.
646    fn render_assert_json_body(&self, out: &mut String, _response_var: &str, expected: &serde_json::Value) {
647        let body_assertion = match expected {
648            serde_json::Value::String(s) if !s.is_empty() => {
649                let php_val = format!("\"{}\"", escape_php(s));
650                format!("$this->assertEquals({php_val}, (string) $response->getBody());")
651            }
652            _ => {
653                let php_val = json_to_php(expected);
654                format!(
655                    "$body = json_decode((string) $response->getBody(), true, 512, JSON_THROW_ON_ERROR);\n        $this->assertEquals({php_val}, $body);"
656                )
657            }
658        };
659
660        let rendered = crate::template_env::render(
661            "php/http_assertions.jinja",
662            minijinja::context! {
663                response_var => "",
664                status_code => 0u16,
665                headers => Vec::<std::collections::HashMap<&str, String>>::new(),
666                body_assertion => body_assertion,
667                partial_body => Vec::<std::collections::HashMap<&str, String>>::new(),
668                validation_errors => Vec::<std::collections::HashMap<&str, String>>::new(),
669            },
670        );
671        out.push_str(&rendered);
672    }
673
674    /// Emit partial body assertions: one `assertEquals` per field in `expected`.
675    fn render_assert_partial_body(&self, out: &mut String, _response_var: &str, expected: &serde_json::Value) {
676        if let Some(obj) = expected.as_object() {
677            let mut partial_body: Vec<std::collections::HashMap<&str, String>> = Vec::new();
678            for (key, val) in obj {
679                let php_key = format!("\"{}\"", escape_php(key));
680                let php_val = json_to_php(val);
681                let assertion_code = format!("$this->assertEquals({php_val}, $body[{php_key}]);");
682                let mut entry = std::collections::HashMap::new();
683                entry.insert("assertion_code", assertion_code);
684                partial_body.push(entry);
685            }
686
687            let rendered = crate::template_env::render(
688                "php/http_assertions.jinja",
689                minijinja::context! {
690                    response_var => "",
691                    status_code => 0u16,
692                    headers => Vec::<std::collections::HashMap<&str, String>>::new(),
693                    body_assertion => String::new(),
694                    partial_body => partial_body,
695                    validation_errors => Vec::<std::collections::HashMap<&str, String>>::new(),
696                },
697            );
698            out.push_str(&rendered);
699        }
700    }
701
702    /// Emit validation-error assertions, checking each expected `msg` against the
703    /// JSON-encoded body string (PHP binding returns ProblemDetails with `errors` array).
704    fn render_assert_validation_errors(
705        &self,
706        out: &mut String,
707        _response_var: &str,
708        errors: &[ValidationErrorExpectation],
709    ) {
710        let mut validation_errors: Vec<std::collections::HashMap<&str, String>> = Vec::new();
711        for err in errors {
712            let msg_lit = format!("\"{}\"", escape_php(&err.msg));
713            let assertion_code =
714                format!("$this->assertStringContainsString({msg_lit}, json_encode($body, JSON_UNESCAPED_SLASHES));");
715            let mut entry = std::collections::HashMap::new();
716            entry.insert("assertion_code", assertion_code);
717            validation_errors.push(entry);
718        }
719
720        let rendered = crate::template_env::render(
721            "php/http_assertions.jinja",
722            minijinja::context! {
723                response_var => "",
724                status_code => 0u16,
725                headers => Vec::<std::collections::HashMap<&str, String>>::new(),
726                body_assertion => String::new(),
727                partial_body => Vec::<std::collections::HashMap<&str, String>>::new(),
728                validation_errors => validation_errors,
729            },
730        );
731        out.push_str(&rendered);
732    }
733}
734
735/// Render a PHPUnit test method for an HTTP server test fixture via the shared driver.
736///
737/// Handles the one PHP-specific pre-condition: HTTP 101 (WebSocket upgrade) causes
738/// cURL/Guzzle to fail; it is emitted as a `markTestSkipped` stub directly.
739fn render_http_test_method(out: &mut String, fixture: &Fixture, http: &HttpFixture) {
740    // HTTP 101 (WebSocket upgrade) causes cURL to treat the connection as an upgrade
741    // and fail with "empty reply from server". Skip these tests in the PHP e2e suite
742    // since Guzzle cannot assert on WebSocket upgrade responses via regular HTTP.
743    if http.expected_response.status_code == 101 {
744        let method_name = sanitize_filename(&fixture.id);
745        let description = &fixture.description;
746        out.push_str(&crate::template_env::render(
747            "php/http_test_skip_101.jinja",
748            minijinja::context! {
749                method_name => method_name,
750                description => description,
751            },
752        ));
753        return;
754    }
755
756    client::http_call::render_http_test(out, &PhpTestClientRenderer, fixture);
757}
758
759// ---------------------------------------------------------------------------
760// Function-call test rendering
761// ---------------------------------------------------------------------------
762
763#[allow(clippy::too_many_arguments)]
764fn render_test_method(
765    out: &mut String,
766    fixture: &Fixture,
767    e2e_config: &E2eConfig,
768    lang: &str,
769    namespace: &str,
770    class_name: &str,
771    field_resolver: &FieldResolver,
772    enum_fields: &HashMap<String, String>,
773    result_is_simple: bool,
774    php_client_factory: Option<&str>,
775    options_via: &str,
776) {
777    // Resolve per-fixture call config: supports named calls via fixture.call field.
778    let call_config = e2e_config.resolve_call_for_fixture(fixture.call.as_deref(), &fixture.input);
779    let call_overrides = call_config.overrides.get(lang);
780    let has_override = call_overrides.is_some_and(|o| o.function.is_some());
781    // Per-call result_is_simple override wins over the language-level default,
782    // so calls like `speech` (returns Vec<u8>) can be marked simple even if
783    // chat/embed are not.
784    let result_is_simple = call_overrides.is_some_and(|o| o.result_is_simple) || result_is_simple;
785    let mut function_name = call_overrides
786        .and_then(|o| o.function.as_ref())
787        .cloned()
788        .unwrap_or_else(|| call_config.function.clone());
789    // ext-php-rs binds async Rust methods with an `_async` suffix (mirroring Magnus).
790    // Append it before camelCasing so e.g. `chat` becomes `chatAsync`.
791    if !has_override && call_config.r#async && !function_name.ends_with("_async") {
792        function_name = format!("{function_name}_async");
793    }
794    if !has_override {
795        function_name = function_name.to_lower_camel_case();
796    }
797    let result_var = &call_config.result_var;
798    let args = &call_config.args;
799
800    let method_name = sanitize_filename(&fixture.id);
801    let description = &fixture.description;
802    let expects_error = fixture.assertions.iter().any(|a| a.assertion_type == "error");
803
804    // Resolve options_type for this call's PHP override, with fallback to the top-level call override.
805    let call_options_type = call_overrides.and_then(|o| o.options_type.as_deref()).or_else(|| {
806        e2e_config
807            .call
808            .overrides
809            .get(lang)
810            .and_then(|o| o.options_type.as_deref())
811    });
812
813    let (mut setup_lines, args_str) = build_args_and_setup(
814        &fixture.input,
815        args,
816        class_name,
817        enum_fields,
818        fixture,
819        options_via,
820        call_options_type,
821    );
822
823    // Check for skip_languages early
824    let skip_test = call_config.skip_languages.iter().any(|l| l == "php");
825    if skip_test {
826        let rendered = crate::template_env::render(
827            "php/test_method.jinja",
828            minijinja::context! {
829                method_name => method_name,
830                description => description,
831                client_factory => String::new(),
832                setup_lines => Vec::<String>::new(),
833                expects_error => false,
834                skip_test => true,
835                has_usable_assertions => false,
836                call_expr => String::new(),
837                result_var => result_var,
838                assertions_body => String::new(),
839            },
840        );
841        out.push_str(&rendered);
842        return;
843    }
844
845    // Build visitor if present and add to setup
846    let mut options_already_created = !args_str.is_empty() && args_str == "$options";
847    if let Some(visitor_spec) = &fixture.visitor {
848        build_php_visitor(&mut setup_lines, visitor_spec);
849        if !options_already_created {
850            setup_lines.push("$builder = \\HtmlToMarkdown\\ConversionOptions::builder();".to_string());
851            setup_lines.push("$options = $builder->visitor($visitor)->build();".to_string());
852            options_already_created = true;
853        }
854    }
855
856    let final_args = if options_already_created {
857        if args_str.is_empty() || args_str == "$options" {
858            "$options".to_string()
859        } else {
860            format!("{args_str}, $options")
861        }
862    } else {
863        args_str
864    };
865
866    let call_expr = if php_client_factory.is_some() {
867        format!("$client->{function_name}({final_args})")
868    } else {
869        format!("{class_name}::{function_name}({final_args})")
870    };
871
872    let has_mock = fixture.mock_response.is_some() || fixture.http.is_some();
873    let api_key_var = fixture.env.as_ref().and_then(|e| e.api_key_var.as_deref());
874    let client_factory = if let Some(factory) = php_client_factory {
875        let fixture_id = &fixture.id;
876        if let Some(var) = api_key_var.filter(|_| has_mock) {
877            format!(
878                "$apiKey = getenv('{var}');\n        $baseUrl = ($apiKey !== false && $apiKey !== '') ? null : getenv('MOCK_SERVER_URL') . '/fixtures/{fixture_id}';\n        fwrite(STDERR, \"{fixture_id}: \" . ($baseUrl === null ? 'using real API ({var} is set)' : 'using mock server ({var} not set)') . \"\\n\");\n        $client = \\{namespace}\\{class_name}::{factory}($baseUrl === null ? $apiKey : 'test-key', $baseUrl);"
879            )
880        } else if has_mock {
881            let base_url_expr = if fixture.has_host_root_route() {
882                let env_key = format!("MOCK_SERVER_{}", fixture_id.to_uppercase());
883                format!("(getenv('{env_key}') ?: getenv('MOCK_SERVER_URL') . '/fixtures/{fixture_id}')")
884            } else {
885                format!("getenv('MOCK_SERVER_URL') . '/fixtures/{fixture_id}'")
886            };
887            format!("$client = \\{namespace}\\{class_name}::{factory}('test-key', {base_url_expr});")
888        } else if let Some(var) = api_key_var {
889            format!(
890                "$apiKey = getenv('{var}');\n        if (!$apiKey) {{ $this->markTestSkipped('{var} not set'); return; }}\n        $client = \\{namespace}\\{class_name}::{factory}($apiKey);"
891            )
892        } else {
893            format!("$client = \\{namespace}\\{class_name}::{factory}('test-key');")
894        }
895    } else {
896        String::new()
897    };
898
899    // Determine if there are usable assertions
900    let has_usable_assertions = fixture.assertions.iter().any(|a| {
901        if a.assertion_type == "error" || a.assertion_type == "not_error" {
902            return false;
903        }
904        match &a.field {
905            Some(f) if !f.is_empty() => field_resolver.is_valid_for_result(f),
906            _ => true,
907        }
908    });
909
910    // Render assertions_body
911    let mut assertions_body = String::new();
912    for assertion in &fixture.assertions {
913        render_assertion(
914            &mut assertions_body,
915            assertion,
916            result_var,
917            field_resolver,
918            result_is_simple,
919            call_config.result_is_array,
920        );
921    }
922
923    let rendered = crate::template_env::render(
924        "php/test_method.jinja",
925        minijinja::context! {
926            method_name => method_name,
927            description => description,
928            client_factory => client_factory,
929            setup_lines => setup_lines,
930            expects_error => expects_error,
931            skip_test => fixture.assertions.is_empty(),
932            has_usable_assertions => has_usable_assertions,
933            call_expr => call_expr,
934            result_var => result_var,
935            assertions_body => assertions_body,
936        },
937    );
938    out.push_str(&rendered);
939}
940
941/// Build setup lines (e.g. handle creation) and the argument list for the function call.
942///
943/// `options_via` controls how `json_object` args are passed:
944/// - `"array"` (default): PHP array literal `["key" => value, ...]`
945/// - `"json"`: JSON string via `json_encode([...])` — use when the Rust method accepts `Option<String>`
946///
947/// `options_type` is the PHP class name (e.g. `"ProcessConfig"`) used when constructing options
948/// via `ClassName::from_json(json_encode([...]))`. Required when `options_via` is not `"json"` and
949/// the binding accepts a typed config object.
950///
951/// Returns `(setup_lines, args_string)`.
952/// Emit PHP batch item array constructors for BatchBytesItem or BatchFileItem arrays.
953fn emit_php_batch_item_array(arr: &serde_json::Value, elem_type: &str) -> String {
954    if let Some(items) = arr.as_array() {
955        let item_strs: Vec<String> = items
956            .iter()
957            .filter_map(|item| {
958                if let Some(obj) = item.as_object() {
959                    match elem_type {
960                        "BatchBytesItem" => {
961                            let content = obj.get("content").and_then(|v| v.as_array());
962                            let mime_type = obj.get("mime_type").and_then(|v| v.as_str()).unwrap_or("text/plain");
963                            let content_code = if let Some(arr) = content {
964                                let bytes: Vec<String> = arr
965                                    .iter()
966                                    .filter_map(|v| v.as_u64())
967                                    .map(|n| format!("\\x{:02x}", n))
968                                    .collect();
969                                format!("\"{}\"", bytes.join(""))
970                            } else {
971                                "\"\"".to_string()
972                            };
973                            Some(format!(
974                                "new {}(content: {}, mimeType: \"{}\")",
975                                elem_type, content_code, mime_type
976                            ))
977                        }
978                        "BatchFileItem" => {
979                            let path = obj.get("path").and_then(|v| v.as_str()).unwrap_or("");
980                            Some(format!("new {}(path: \"{}\")", elem_type, path))
981                        }
982                        _ => None,
983                    }
984                } else {
985                    None
986                }
987            })
988            .collect();
989        format!("[{}]", item_strs.join(", "))
990    } else {
991        "[]".to_string()
992    }
993}
994
995fn build_args_and_setup(
996    input: &serde_json::Value,
997    args: &[crate::config::ArgMapping],
998    class_name: &str,
999    _enum_fields: &HashMap<String, String>,
1000    fixture: &crate::fixture::Fixture,
1001    options_via: &str,
1002    options_type: Option<&str>,
1003) -> (Vec<String>, String) {
1004    let fixture_id = &fixture.id;
1005    if args.is_empty() {
1006        // No args configuration: pass the whole input only if it's non-empty.
1007        // Functions with no parameters (e.g. list_models) have empty input and get no args.
1008        let is_empty_input = match input {
1009            serde_json::Value::Null => true,
1010            serde_json::Value::Object(m) => m.is_empty(),
1011            _ => false,
1012        };
1013        if is_empty_input {
1014            return (Vec::new(), String::new());
1015        }
1016        return (Vec::new(), json_to_php(input));
1017    }
1018
1019    let mut setup_lines: Vec<String> = Vec::new();
1020    let mut parts: Vec<String> = Vec::new();
1021
1022    // True when any arg after `from_idx` has a fixture value (or has no fixture
1023    // value but is required — i.e. would emit *something*). Used to decide
1024    // whether a missing optional middle arg must emit `null` to preserve the
1025    // positional argument layout, or can be safely dropped.
1026    let arg_has_emission = |arg: &crate::config::ArgMapping| -> bool {
1027        let val = if arg.field == "input" {
1028            Some(input)
1029        } else {
1030            let field = arg.field.strip_prefix("input.").unwrap_or(&arg.field);
1031            input.get(field)
1032        };
1033        match val {
1034            None | Some(serde_json::Value::Null) => !arg.optional,
1035            Some(_) => true,
1036        }
1037    };
1038    let any_later_has_emission = |from_idx: usize| -> bool { args[from_idx..].iter().any(arg_has_emission) };
1039
1040    for (idx, arg) in args.iter().enumerate() {
1041        if arg.arg_type == "mock_url" {
1042            if fixture.has_host_root_route() {
1043                let env_key = format!("MOCK_SERVER_{}", fixture_id.to_uppercase());
1044                setup_lines.push(format!(
1045                    "${} = getenv('{env_key}') ?: getenv('MOCK_SERVER_URL') . '/fixtures/{fixture_id}';",
1046                    arg.name,
1047                ));
1048            } else {
1049                setup_lines.push(format!(
1050                    "${} = getenv('MOCK_SERVER_URL') . '/fixtures/{fixture_id}';",
1051                    arg.name,
1052                ));
1053            }
1054            parts.push(format!("${}", arg.name));
1055            continue;
1056        }
1057
1058        if arg.arg_type == "handle" {
1059            // Generate a createEngine (or equivalent) call and pass the variable.
1060            let constructor_name = format!("create{}", arg.name.to_upper_camel_case());
1061            let config_value = if arg.field == "input" {
1062                input
1063            } else {
1064                let field = arg.field.strip_prefix("input.").unwrap_or(&arg.field);
1065                input.get(field).unwrap_or(&serde_json::Value::Null)
1066            };
1067            if config_value.is_null()
1068                || config_value.is_object() && config_value.as_object().is_some_and(|o| o.is_empty())
1069            {
1070                setup_lines.push(format!("${} = {class_name}::{constructor_name}(null);", arg.name,));
1071            } else {
1072                let name = &arg.name;
1073                // Use CrawlConfig::from_json() instead of direct property assignment.
1074                // ext-php-rs doesn't support writable #[php(prop)] fields for complex types,
1075                // so serialize the config to JSON and use from_json() to construct it.
1076                // Filter out empty string enum values before passing to from_json().
1077                let filtered_config = filter_empty_enum_strings(config_value);
1078                setup_lines.push(format!(
1079                    "${name}_config = CrawlConfig::from_json(json_encode({}));",
1080                    json_to_php(&filtered_config)
1081                ));
1082                setup_lines.push(format!(
1083                    "${} = {class_name}::{constructor_name}(${name}_config);",
1084                    arg.name,
1085                ));
1086            }
1087            parts.push(format!("${}", arg.name));
1088            continue;
1089        }
1090
1091        let val = if arg.field == "input" {
1092            Some(input)
1093        } else {
1094            let field = arg.field.strip_prefix("input.").unwrap_or(&arg.field);
1095            input.get(field)
1096        };
1097
1098        // Bytes args: fixture stores either a fixture-relative path string (load
1099        // with file_get_contents at runtime, mirroring the go/python convention)
1100        // or an inline byte array (encode as a "\xNN" escape string).
1101        if arg.arg_type == "bytes" {
1102            match val {
1103                None | Some(serde_json::Value::Null) => {
1104                    if arg.optional {
1105                        parts.push("null".to_string());
1106                    } else {
1107                        parts.push("\"\"".to_string());
1108                    }
1109                }
1110                Some(serde_json::Value::String(s)) => {
1111                    let var_name = format!("{}Bytes", arg.name);
1112                    setup_lines.push(format!(
1113                        "${var_name} = file_get_contents(\"{path}\");\n        if (${var_name} === false) {{ $this->fail(\"failed to read fixture: {path}\"); }}",
1114                        path = s.replace('"', "\\\"")
1115                    ));
1116                    parts.push(format!("${var_name}"));
1117                }
1118                Some(serde_json::Value::Array(arr)) => {
1119                    let bytes: String = arr
1120                        .iter()
1121                        .filter_map(|v| v.as_u64())
1122                        .map(|n| format!("\\x{:02x}", n))
1123                        .collect();
1124                    parts.push(format!("\"{bytes}\""));
1125                }
1126                Some(other) => {
1127                    parts.push(json_to_php(other));
1128                }
1129            }
1130            continue;
1131        }
1132
1133        match val {
1134            None | Some(serde_json::Value::Null) if arg.arg_type == "json_object" && arg.name == "config" => {
1135                // Special case: ExtractionConfig and similar config objects with no fixture value
1136                // should default to an empty instance (e.g., ExtractionConfig::from_json('{}'))
1137                // to satisfy required parameters. This check happens BEFORE the optional check
1138                // so that config args are always provided, even if marked optional in alef.toml.
1139                // Infer the type name from the arg name and capitalize it (e.g., "config" -> "ExtractionConfig").
1140                let type_name = if arg.name == "config" {
1141                    "ExtractionConfig".to_string()
1142                } else {
1143                    format!("{}Config", arg.name.to_upper_camel_case())
1144                };
1145                parts.push(format!("{type_name}::from_json('{{}}')"));
1146                continue;
1147            }
1148            None | Some(serde_json::Value::Null) if arg.optional => {
1149                // Optional arg with no fixture value. If a later arg WILL emit
1150                // something, we must keep this slot in place by passing `null`
1151                // so the positional argument layout matches the PHP signature.
1152                // Otherwise drop the trailing optional argument entirely.
1153                if any_later_has_emission(idx + 1) {
1154                    parts.push("null".to_string());
1155                }
1156                continue;
1157            }
1158            None | Some(serde_json::Value::Null) => {
1159                // Required arg with no fixture value: pass a language-appropriate default.
1160                let default_val = match arg.arg_type.as_str() {
1161                    "string" => "\"\"".to_string(),
1162                    "int" | "integer" => "0".to_string(),
1163                    "float" | "number" => "0.0".to_string(),
1164                    "bool" | "boolean" => "false".to_string(),
1165                    "json_object" if options_via == "json" => "null".to_string(),
1166                    _ => "null".to_string(),
1167                };
1168                parts.push(default_val);
1169            }
1170            Some(v) => {
1171                if arg.arg_type == "json_object" && !v.is_null() {
1172                    // Check for batch item arrays first
1173                    if let Some(elem_type) = &arg.element_type {
1174                        if (elem_type == "BatchBytesItem" || elem_type == "BatchFileItem") && v.is_array() {
1175                            parts.push(emit_php_batch_item_array(v, elem_type));
1176                            continue;
1177                        }
1178                        // When element_type is a scalar/primitive and value is an array,
1179                        // pass it directly as a PHP array (e.g. ["python"]) rather than
1180                        // wrapping in a typed config constructor.
1181                        if v.is_array() && is_php_reserved_type(elem_type) {
1182                            parts.push(json_to_php(v));
1183                            continue;
1184                        }
1185                    }
1186                    match options_via {
1187                        "json" => {
1188                            // Pass as JSON string via json_encode(); the Rust method accepts Option<String>.
1189                            // Filter out empty string enum values.
1190                            let filtered_v = filter_empty_enum_strings(v);
1191
1192                            // If the config is empty after filtering, pass null instead.
1193                            if let serde_json::Value::Object(obj) = &filtered_v {
1194                                if obj.is_empty() {
1195                                    parts.push("null".to_string());
1196                                    continue;
1197                                }
1198                            }
1199
1200                            parts.push(format!("json_encode({})", json_to_php_camel_keys(&filtered_v)));
1201                            continue;
1202                        }
1203                        _ => {
1204                            if let Some(type_name) = options_type {
1205                                // Use TypeName::from_json(json_encode([...])) to construct the
1206                                // typed config object. ext-php-rs structs expose a from_json()
1207                                // static method that accepts a JSON string.
1208                                // Filter out empty string enum values before passing to from_json().
1209                                let filtered_v = filter_empty_enum_strings(v);
1210
1211                                // For empty objects, construct with from_json('{}') to get the
1212                                // type's defaults rather than passing null (which fails for non-optional params).
1213                                if let serde_json::Value::Object(obj) = &filtered_v {
1214                                    if obj.is_empty() {
1215                                        let arg_var = format!("${}", arg.name);
1216                                        setup_lines.push(format!("{arg_var} = {type_name}::from_json('{{}}');"));
1217                                        parts.push(arg_var);
1218                                        continue;
1219                                    }
1220                                }
1221
1222                                let arg_var = format!("${}", arg.name);
1223                                // Use json_to_php (snake_case) instead of json_to_php_camel_keys because
1224                                // Rust's serde deserializes field names as snake_case by default (via #[serde(rename_all = "snake_case")]).
1225                                // PHP should match Rust field naming conventions, not use camelCase.
1226                                setup_lines.push(format!(
1227                                    "{arg_var} = {type_name}::from_json(json_encode({}));",
1228                                    json_to_php(&filtered_v)
1229                                ));
1230                                parts.push(arg_var);
1231                                continue;
1232                            }
1233                            // Fallback: builder pattern when no options_type is configured.
1234                            // This path is kept for backwards compatibility with projects
1235                            // that use a builder-style API without from_json().
1236                            if let Some(obj) = v.as_object() {
1237                                setup_lines.push("$builder = $this->createDefaultOptionsBuilder();".to_string());
1238                                for (k, vv) in obj {
1239                                    let snake_key = k.to_snake_case();
1240                                    if snake_key == "preprocessing" {
1241                                        if let Some(prep_obj) = vv.as_object() {
1242                                            let enabled =
1243                                                prep_obj.get("enabled").and_then(|v| v.as_bool()).unwrap_or(true);
1244                                            let preset =
1245                                                prep_obj.get("preset").and_then(|v| v.as_str()).unwrap_or("Minimal");
1246                                            let remove_navigation = prep_obj
1247                                                .get("remove_navigation")
1248                                                .and_then(|v| v.as_bool())
1249                                                .unwrap_or(true);
1250                                            let remove_forms =
1251                                                prep_obj.get("remove_forms").and_then(|v| v.as_bool()).unwrap_or(true);
1252                                            setup_lines.push(format!(
1253                                                "$preprocessing = $this->createPreprocessingOptions({}, {}, {}, {});",
1254                                                if enabled { "true" } else { "false" },
1255                                                json_to_php(&serde_json::Value::String(preset.to_string())),
1256                                                if remove_navigation { "true" } else { "false" },
1257                                                if remove_forms { "true" } else { "false" }
1258                                            ));
1259                                            setup_lines.push(
1260                                                "$builder = $builder->preprocessing($preprocessing);".to_string(),
1261                                            );
1262                                        }
1263                                    }
1264                                }
1265                                setup_lines.push("$options = $builder->build();".to_string());
1266                                parts.push("$options".to_string());
1267                                continue;
1268                            }
1269                        }
1270                    }
1271                }
1272                parts.push(json_to_php(v));
1273            }
1274        }
1275    }
1276
1277    (setup_lines, parts.join(", "))
1278}
1279
1280fn render_assertion(
1281    out: &mut String,
1282    assertion: &Assertion,
1283    result_var: &str,
1284    field_resolver: &FieldResolver,
1285    result_is_simple: bool,
1286    result_is_array: bool,
1287) {
1288    // Handle synthetic / derived fields before the is_valid_for_result check
1289    // so they are never treated as struct property accesses on the result.
1290    if let Some(f) = &assertion.field {
1291        match f.as_str() {
1292            "chunks_have_content" => {
1293                let pred = format!(
1294                    "array_reduce(${result_var}->chunks ?? [], fn($carry, $c) => $carry && !empty($c->content), true)"
1295                );
1296                out.push_str(&crate::template_env::render(
1297                    "php/synthetic_assertion.jinja",
1298                    minijinja::context! {
1299                        assertion_kind => "chunks_content",
1300                        assertion_type => assertion.assertion_type.as_str(),
1301                        pred => pred,
1302                        field_name => f,
1303                    },
1304                ));
1305                return;
1306            }
1307            "chunks_have_embeddings" => {
1308                let pred = format!(
1309                    "array_reduce(${result_var}->chunks ?? [], fn($carry, $c) => $carry && !empty($c->embedding), true)"
1310                );
1311                out.push_str(&crate::template_env::render(
1312                    "php/synthetic_assertion.jinja",
1313                    minijinja::context! {
1314                        assertion_kind => "chunks_embeddings",
1315                        assertion_type => assertion.assertion_type.as_str(),
1316                        pred => pred,
1317                        field_name => f,
1318                    },
1319                ));
1320                return;
1321            }
1322            // ---- EmbedResponse virtual fields ----
1323            // embed_texts returns array<array<float>> in PHP — no wrapper object.
1324            // $result_var is the embedding matrix; use it directly.
1325            "embeddings" => {
1326                let php_val = assertion.value.as_ref().map(json_to_php).unwrap_or_default();
1327                out.push_str(&crate::template_env::render(
1328                    "php/synthetic_assertion.jinja",
1329                    minijinja::context! {
1330                        assertion_kind => "embeddings",
1331                        assertion_type => assertion.assertion_type.as_str(),
1332                        php_val => php_val,
1333                        result_var => result_var,
1334                    },
1335                ));
1336                return;
1337            }
1338            "embedding_dimensions" => {
1339                let expr = format!("(empty(${result_var}) ? 0 : count(${result_var}[0]))");
1340                let php_val = assertion.value.as_ref().map(json_to_php).unwrap_or_default();
1341                out.push_str(&crate::template_env::render(
1342                    "php/synthetic_assertion.jinja",
1343                    minijinja::context! {
1344                        assertion_kind => "embedding_dimensions",
1345                        assertion_type => assertion.assertion_type.as_str(),
1346                        expr => expr,
1347                        php_val => php_val,
1348                    },
1349                ));
1350                return;
1351            }
1352            "embeddings_valid" | "embeddings_finite" | "embeddings_non_zero" | "embeddings_normalized" => {
1353                let pred = match f.as_str() {
1354                    "embeddings_valid" => {
1355                        format!("array_reduce(${result_var}, fn($carry, $e) => $carry && count($e) > 0, true)")
1356                    }
1357                    "embeddings_finite" => {
1358                        format!(
1359                            "array_reduce(${result_var}, fn($carry, $e) => $carry && array_reduce($e, fn($c, $v) => $c && is_finite($v), true), true)"
1360                        )
1361                    }
1362                    "embeddings_non_zero" => {
1363                        format!(
1364                            "array_reduce(${result_var}, fn($carry, $e) => $carry && count(array_filter($e, fn($v) => $v !== 0.0)) > 0, true)"
1365                        )
1366                    }
1367                    "embeddings_normalized" => {
1368                        format!(
1369                            "array_reduce(${result_var}, fn($carry, $e) => $carry && abs(array_sum(array_map(fn($v) => $v * $v, $e)) - 1.0) < 1e-3, true)"
1370                        )
1371                    }
1372                    _ => unreachable!(),
1373                };
1374                let assertion_kind = format!("embeddings_{}", f.strip_prefix("embeddings_").unwrap_or(f));
1375                out.push_str(&crate::template_env::render(
1376                    "php/synthetic_assertion.jinja",
1377                    minijinja::context! {
1378                        assertion_kind => assertion_kind,
1379                        assertion_type => assertion.assertion_type.as_str(),
1380                        pred => pred,
1381                        field_name => f,
1382                    },
1383                ));
1384                return;
1385            }
1386            // ---- keywords / keywords_count ----
1387            // PHP ExtractionResult does not expose extracted_keywords; skip.
1388            "keywords" | "keywords_count" => {
1389                out.push_str(&crate::template_env::render(
1390                    "php/synthetic_assertion.jinja",
1391                    minijinja::context! {
1392                        assertion_kind => "keywords",
1393                        field_name => f,
1394                    },
1395                ));
1396                return;
1397            }
1398            _ => {}
1399        }
1400    }
1401
1402    // Skip assertions on fields that don't exist on the result type.
1403    if let Some(f) = &assertion.field {
1404        if !f.is_empty() && !field_resolver.is_valid_for_result(f) {
1405            out.push_str(&crate::template_env::render(
1406                "php/synthetic_assertion.jinja",
1407                minijinja::context! {
1408                    assertion_kind => "skipped",
1409                    field_name => f,
1410                },
1411            ));
1412            return;
1413        }
1414    }
1415
1416    // When result_is_simple, skip assertions that reference non-content fields
1417    // (e.g., metadata, document, structure) since the binding returns a plain value.
1418    if result_is_simple {
1419        if let Some(f) = &assertion.field {
1420            let f_lower = f.to_lowercase();
1421            if !f.is_empty()
1422                && f_lower != "content"
1423                && (f_lower.starts_with("metadata")
1424                    || f_lower.starts_with("document")
1425                    || f_lower.starts_with("structure"))
1426            {
1427                out.push_str(&crate::template_env::render(
1428                    "php/synthetic_assertion.jinja",
1429                    minijinja::context! {
1430                        assertion_kind => "result_is_simple",
1431                        field_name => f,
1432                    },
1433                ));
1434                return;
1435            }
1436        }
1437    }
1438
1439    let field_expr = match &assertion.field {
1440        // When result_is_simple, the result is a scalar (bytes/string/etc.) — any
1441        // field access on it would fail. Treat all assertions as referring to the
1442        // result itself.
1443        _ if result_is_simple => format!("${result_var}"),
1444        Some(f) if !f.is_empty() => field_resolver.accessor(f, "php", &format!("${result_var}")),
1445        _ => format!("${result_var}"),
1446    };
1447
1448    // Detect if this field is an array type
1449    // When there's no field, default to result_is_array (the result itself is the array)
1450    let field_is_array = assertion.field.as_ref().map_or(result_is_array, |f| {
1451        if f.is_empty() {
1452            result_is_array
1453        } else {
1454            field_resolver.is_array(f)
1455        }
1456    });
1457
1458    // For string equality, trim trailing whitespace to handle trailing newlines.
1459    // Only apply trim() when the expected value is a string — calling trim() on int/bool
1460    // throws TypeError in PHP 8.4+.
1461    let trimmed_field_expr_for = |expected: &serde_json::Value| -> String {
1462        if expected.is_string() {
1463            format!("trim({})", field_expr)
1464        } else {
1465            field_expr.clone()
1466        }
1467    };
1468
1469    // Prepare template context.
1470    let assertion_type = assertion.assertion_type.as_str();
1471    let has_php_val = assertion.value.is_some();
1472    // serde collapses `"value": null` to `None`, but `equals` against null is a real
1473    // assertion (e.g. `result.message.content == null`). Default to PHP `null` in that
1474    // case so the rendered code compiles instead of producing `assertEquals(, ...)`.
1475    let php_val = match assertion.value.as_ref() {
1476        Some(v) => json_to_php(v),
1477        None if assertion_type == "equals" => "null".to_string(),
1478        None => String::new(),
1479    };
1480    let trimmed_field_expr = trimmed_field_expr_for(assertion.value.as_ref().unwrap_or(&serde_json::Value::Null));
1481    let is_string_val = assertion.value.as_ref().is_some_and(|v| v.is_string());
1482    let values_php: Vec<String> = assertion
1483        .values
1484        .as_ref()
1485        .map_or(Vec::new(), |vals| vals.iter().map(json_to_php).collect());
1486    let contains_any_checks: Vec<String> = assertion
1487        .values
1488        .as_ref()
1489        .map_or(Vec::new(), |vals| vals.iter().map(json_to_php).collect());
1490    let n = assertion.value.as_ref().and_then(|v| v.as_u64()).unwrap_or(0);
1491
1492    // For method_result assertions.
1493    let call_expr = if let Some(method_name) = &assertion.method {
1494        build_php_method_call(result_var, method_name, assertion.args.as_ref())
1495    } else {
1496        String::new()
1497    };
1498    let check = assertion.check.as_deref().unwrap_or("is_true");
1499    let has_php_check_val = matches!(assertion.assertion_type.as_str(), "method_result") && assertion.value.is_some();
1500    let php_check_val = if matches!(assertion.assertion_type.as_str(), "method_result") {
1501        assertion.value.as_ref().map(json_to_php).unwrap_or_default()
1502    } else {
1503        String::new()
1504    };
1505    let check_n = assertion.value.as_ref().and_then(|v| v.as_u64()).unwrap_or(0);
1506    let is_bool_val = assertion.value.as_ref().is_some_and(|v| v.is_boolean());
1507    let bool_is_true = assertion.value.as_ref().and_then(|v| v.as_bool()).unwrap_or(false);
1508
1509    // Early returns for non-template-renderable assertions.
1510    if matches!(assertion_type, "not_error" | "error") {
1511        if assertion_type == "not_error" {
1512            // Already handled by the call succeeding without exception.
1513        }
1514        // "error" is handled at the test method level.
1515        return;
1516    }
1517
1518    let rendered = crate::template_env::render(
1519        "php/assertion.jinja",
1520        minijinja::context! {
1521            assertion_type => assertion_type,
1522            field_expr => field_expr,
1523            php_val => php_val,
1524            has_php_val => has_php_val,
1525            trimmed_field_expr => trimmed_field_expr,
1526            is_string_val => is_string_val,
1527            field_is_array => field_is_array,
1528            values_php => values_php,
1529            contains_any_checks => contains_any_checks,
1530            n => n,
1531            call_expr => call_expr,
1532            check => check,
1533            php_check_val => php_check_val,
1534            has_php_check_val => has_php_check_val,
1535            check_n => check_n,
1536            is_bool_val => is_bool_val,
1537            bool_is_true => bool_is_true,
1538        },
1539    );
1540    let _ = write!(out, "        {}", rendered);
1541}
1542
1543/// Build a PHP call expression for a `method_result` assertion.
1544///
1545/// Uses generic instance method dispatch: `$result_var->method_name(args...)`.
1546/// Args from the fixture JSON object are emitted as positional PHP arguments in
1547/// insertion order, using best-effort type conversion (strings → PHP string literals,
1548/// numbers and booleans → verbatim literals).
1549fn build_php_method_call(result_var: &str, method_name: &str, args: Option<&serde_json::Value>) -> String {
1550    let extra_args = if let Some(args_val) = args {
1551        args_val
1552            .as_object()
1553            .map(|obj| {
1554                obj.values()
1555                    .map(|v| match v {
1556                        serde_json::Value::String(s) => format!("\"{}\"", s.replace('\\', "\\\\").replace('"', "\\\"")),
1557                        serde_json::Value::Bool(true) => "true".to_string(),
1558                        serde_json::Value::Bool(false) => "false".to_string(),
1559                        serde_json::Value::Number(n) => n.to_string(),
1560                        serde_json::Value::Null => "null".to_string(),
1561                        other => format!("\"{}\"", other.to_string().replace('\\', "\\\\").replace('"', "\\\"")),
1562                    })
1563                    .collect::<Vec<_>>()
1564                    .join(", ")
1565            })
1566            .unwrap_or_default()
1567    } else {
1568        String::new()
1569    };
1570
1571    if extra_args.is_empty() {
1572        format!("${result_var}->{method_name}()")
1573    } else {
1574        format!("${result_var}->{method_name}({extra_args})")
1575    }
1576}
1577
1578/// Filters out empty string enum values from JSON objects before rendering.
1579/// When a field has an empty string value, it's treated as a missing/null enum field
1580/// and should not be included in the PHP array.
1581fn filter_empty_enum_strings(value: &serde_json::Value) -> serde_json::Value {
1582    match value {
1583        serde_json::Value::Object(map) => {
1584            let filtered: serde_json::Map<String, serde_json::Value> = map
1585                .iter()
1586                .filter_map(|(k, v)| {
1587                    // Skip empty string values (typically represent missing enum variants)
1588                    if let serde_json::Value::String(s) = v {
1589                        if s.is_empty() {
1590                            return None;
1591                        }
1592                    }
1593                    // Recursively filter nested objects and arrays
1594                    Some((k.clone(), filter_empty_enum_strings(v)))
1595                })
1596                .collect();
1597            serde_json::Value::Object(filtered)
1598        }
1599        serde_json::Value::Array(arr) => {
1600            let filtered: Vec<serde_json::Value> = arr.iter().map(filter_empty_enum_strings).collect();
1601            serde_json::Value::Array(filtered)
1602        }
1603        other => other.clone(),
1604    }
1605}
1606
1607/// Convert a `serde_json::Value` to a PHP literal string.
1608fn json_to_php(value: &serde_json::Value) -> String {
1609    match value {
1610        serde_json::Value::String(s) => format!("\"{}\"", escape_php(s)),
1611        serde_json::Value::Bool(true) => "true".to_string(),
1612        serde_json::Value::Bool(false) => "false".to_string(),
1613        serde_json::Value::Number(n) => n.to_string(),
1614        serde_json::Value::Null => "null".to_string(),
1615        serde_json::Value::Array(arr) => {
1616            let items: Vec<String> = arr.iter().map(json_to_php).collect();
1617            format!("[{}]", items.join(", "))
1618        }
1619        serde_json::Value::Object(map) => {
1620            let items: Vec<String> = map
1621                .iter()
1622                .map(|(k, v)| format!("\"{}\" => {}", escape_php(k), json_to_php(v)))
1623                .collect();
1624            format!("[{}]", items.join(", "))
1625        }
1626    }
1627}
1628
1629/// Like `json_to_php` but recursively converts all object keys to lowerCamelCase.
1630/// Used when generating PHP option arrays passed to `from_json()` — the PHP binding
1631/// structs use `#[serde(rename_all = "camelCase")]` so snake_case fixture keys
1632/// (e.g. `remove_forms`) must become `removeForms` in the generated test code.
1633fn json_to_php_camel_keys(value: &serde_json::Value) -> String {
1634    match value {
1635        serde_json::Value::Object(map) => {
1636            let items: Vec<String> = map
1637                .iter()
1638                .map(|(k, v)| {
1639                    let camel_key = k.to_lower_camel_case();
1640                    format!("\"{}\" => {}", escape_php(&camel_key), json_to_php_camel_keys(v))
1641                })
1642                .collect();
1643            format!("[{}]", items.join(", "))
1644        }
1645        serde_json::Value::Array(arr) => {
1646            let items: Vec<String> = arr.iter().map(json_to_php_camel_keys).collect();
1647            format!("[{}]", items.join(", "))
1648        }
1649        _ => json_to_php(value),
1650    }
1651}
1652
1653// ---------------------------------------------------------------------------
1654// Visitor generation
1655// ---------------------------------------------------------------------------
1656
1657/// Build a PHP visitor object and add setup lines. The visitor is assigned to $visitor variable.
1658fn build_php_visitor(setup_lines: &mut Vec<String>, visitor_spec: &crate::fixture::VisitorSpec) {
1659    setup_lines.push("$visitor = new class {".to_string());
1660    for (method_name, action) in &visitor_spec.callbacks {
1661        emit_php_visitor_method(setup_lines, method_name, action);
1662    }
1663    setup_lines.push("};".to_string());
1664}
1665
1666/// Emit a PHP visitor method for a callback action.
1667fn emit_php_visitor_method(setup_lines: &mut Vec<String>, method_name: &str, action: &CallbackAction) {
1668    let params = match method_name {
1669        "visit_link" => "$ctx, $href, $text, $title",
1670        "visit_image" => "$ctx, $src, $alt, $title",
1671        "visit_heading" => "$ctx, $level, $text, $id",
1672        "visit_code_block" => "$ctx, $lang, $code",
1673        "visit_code_inline"
1674        | "visit_strong"
1675        | "visit_emphasis"
1676        | "visit_strikethrough"
1677        | "visit_underline"
1678        | "visit_subscript"
1679        | "visit_superscript"
1680        | "visit_mark"
1681        | "visit_button"
1682        | "visit_summary"
1683        | "visit_figcaption"
1684        | "visit_definition_term"
1685        | "visit_definition_description" => "$ctx, $text",
1686        "visit_text" => "$ctx, $text",
1687        "visit_list_item" => "$ctx, $ordered, $marker, $text",
1688        "visit_blockquote" => "$ctx, $content, $depth",
1689        "visit_table_row" => "$ctx, $cells, $isHeader",
1690        "visit_custom_element" => "$ctx, $tagName, $html",
1691        "visit_form" => "$ctx, $actionUrl, $method",
1692        "visit_input" => "$ctx, $input_type, $name, $value",
1693        "visit_audio" | "visit_video" | "visit_iframe" => "$ctx, $src",
1694        "visit_details" => "$ctx, $isOpen",
1695        "visit_element_end" | "visit_table_end" | "visit_definition_list_end" | "visit_figure_end" => "$ctx, $output",
1696        "visit_list_start" => "$ctx, $ordered",
1697        "visit_list_end" => "$ctx, $ordered, $output",
1698        _ => "$ctx",
1699    };
1700
1701    let (action_type, action_value) = match action {
1702        CallbackAction::Skip => ("skip", String::new()),
1703        CallbackAction::Continue => ("continue", String::new()),
1704        CallbackAction::PreserveHtml => ("preserve_html", String::new()),
1705        CallbackAction::Custom { output } => ("custom", escape_php(output)),
1706        CallbackAction::CustomTemplate { template } => ("custom_template", escape_php(template)),
1707    };
1708
1709    let rendered = crate::template_env::render(
1710        "php/visitor_method.jinja",
1711        minijinja::context! {
1712            method_name => method_name,
1713            params => params,
1714            action_type => action_type,
1715            action_value => action_value,
1716        },
1717    );
1718    for line in rendered.lines() {
1719        setup_lines.push(line.to_string());
1720    }
1721}
1722
1723/// Returns true if the type name is a PHP reserved/primitive type that cannot be imported.
1724fn is_php_reserved_type(name: &str) -> bool {
1725    matches!(
1726        name.to_ascii_lowercase().as_str(),
1727        "string"
1728            | "int"
1729            | "integer"
1730            | "float"
1731            | "double"
1732            | "bool"
1733            | "boolean"
1734            | "array"
1735            | "object"
1736            | "null"
1737            | "void"
1738            | "callable"
1739            | "iterable"
1740            | "never"
1741            | "self"
1742            | "parent"
1743            | "static"
1744            | "true"
1745            | "false"
1746            | "mixed"
1747    )
1748}