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