1use crate::config::E2eConfig;
8use crate::escape::{escape_php, sanitize_filename};
9use crate::field_access::{FieldResolver, PhpGetterMap};
10use crate::fixture::{
11 Assertion, CallbackAction, Fixture, FixtureGroup, HttpFixture, TemplateReturnForm, ValidationErrorExpectation,
12};
13use alef_backend_php::naming::php_autoload_namespace;
14use alef_core::backend::GeneratedFile;
15use alef_core::config::ResolvedCrateConfig;
16use alef_core::hash::{self, CommentStyle};
17use alef_core::ir::TypeRef;
18use alef_core::template_versions as tv;
19use anyhow::Result;
20use heck::{ToLowerCamelCase, ToSnakeCase, ToUpperCamelCase};
21use std::collections::{HashMap, HashSet};
22use std::fmt::Write as FmtWrite;
23use std::path::PathBuf;
24
25use super::E2eCodegen;
26use super::client;
27
28pub struct PhpCodegen;
30
31impl E2eCodegen for PhpCodegen {
32 fn generate(
33 &self,
34 groups: &[FixtureGroup],
35 e2e_config: &E2eConfig,
36 config: &ResolvedCrateConfig,
37 type_defs: &[alef_core::ir::TypeDef],
38 enums: &[alef_core::ir::EnumDef],
39 ) -> Result<Vec<GeneratedFile>> {
40 let lang = self.language_name();
41 let output_base = PathBuf::from(e2e_config.effective_output()).join(lang);
42
43 let mut files = Vec::new();
44
45 let call = &e2e_config.call;
49 let overrides = call.overrides.get(lang);
50 let extension_name = config.php_extension_name();
51 let class_name = overrides
52 .and_then(|o| o.class.as_ref())
53 .cloned()
54 .map(|cn| cn.split('\\').next_back().unwrap_or(&cn).to_string())
55 .unwrap_or_else(|| extension_name.to_upper_camel_case());
56 let namespace = overrides.and_then(|o| o.module.as_ref()).cloned().unwrap_or_else(|| {
57 if extension_name.contains('_') {
58 extension_name
59 .split('_')
60 .map(|p| p.to_upper_camel_case())
61 .collect::<Vec<_>>()
62 .join("\\")
63 } else {
64 extension_name.to_upper_camel_case()
65 }
66 });
67 let empty_enum_fields = HashMap::new();
68 let enum_fields = overrides.map(|o| &o.enum_fields).unwrap_or(&empty_enum_fields);
69 let result_is_simple = overrides.is_some_and(|o| o.result_is_simple);
70 let php_client_factory = overrides.and_then(|o| o.php_client_factory.as_deref());
71 let options_via = overrides.and_then(|o| o.options_via.as_deref()).unwrap_or("array");
72
73 let php_pkg = e2e_config.resolve_package("php");
75 let pkg_name = php_pkg
76 .as_ref()
77 .and_then(|p| p.name.as_ref())
78 .cloned()
79 .unwrap_or_else(|| {
80 let org = config
83 .try_github_repo()
84 .ok()
85 .as_deref()
86 .and_then(alef_core::config::derive_repo_org)
87 .unwrap_or_else(|| config.name.clone());
88 format!("{org}/{}", call.module.replace('_', "-"))
89 });
90 let pkg_path = php_pkg
91 .as_ref()
92 .and_then(|p| p.path.as_ref())
93 .cloned()
94 .unwrap_or_else(|| "../../packages/php".to_string());
95 let pkg_version = php_pkg
96 .as_ref()
97 .and_then(|p| p.version.as_ref())
98 .cloned()
99 .or_else(|| config.resolved_version())
100 .unwrap_or_else(|| "0.1.0".to_string());
101
102 let e2e_vendor = pkg_name.split('/').next().unwrap_or(&pkg_name).to_string();
107 let e2e_pkg_name = format!("{e2e_vendor}/e2e-php");
108 let php_namespace_escaped = php_autoload_namespace(config).replace('\\', "\\\\");
113 let e2e_autoload_ns = format!("{php_namespace_escaped}\\\\E2e\\\\");
114
115 files.push(GeneratedFile {
117 path: output_base.join("composer.json"),
118 content: render_composer_json(
119 &e2e_pkg_name,
120 &e2e_autoload_ns,
121 &pkg_name,
122 &pkg_path,
123 &pkg_version,
124 e2e_config.dep_mode,
125 ),
126 generated_header: false,
127 });
128
129 files.push(GeneratedFile {
131 path: output_base.join("phpunit.xml"),
132 content: render_phpunit_xml(),
133 generated_header: false,
134 });
135
136 let has_http_fixtures = groups
139 .iter()
140 .flat_map(|g| g.fixtures.iter())
141 .any(|f| f.needs_mock_server());
142
143 let has_file_fixtures = groups.iter().flat_map(|g| g.fixtures.iter()).any(|f| {
145 let cc = e2e_config.resolve_call_for_fixture(f.call.as_deref(), &f.input);
146 cc.args
147 .iter()
148 .any(|a| a.arg_type == "file_path" || a.arg_type == "bytes")
149 });
150
151 files.push(GeneratedFile {
153 path: output_base.join("bootstrap.php"),
154 content: render_bootstrap(
155 &pkg_path,
156 has_http_fixtures,
157 has_file_fixtures,
158 &e2e_config.test_documents_relative_from(0),
159 ),
160 generated_header: true,
161 });
162
163 files.push(GeneratedFile {
165 path: output_base.join("run_tests.php"),
166 content: render_run_tests_php(&extension_name, config.php_cargo_crate_name()),
167 generated_header: true,
168 });
169
170 let tests_base = output_base.join("tests");
172
173 let php_enum_names: HashSet<String> = enums.iter().map(|e| e.name.clone()).collect();
187 let php_getter_map = build_php_getter_map(type_defs, &php_enum_names, call, &e2e_config.result_fields);
188
189 let field_resolver = FieldResolver::new_with_php_getters(
190 &e2e_config.fields,
191 &e2e_config.fields_optional,
192 &e2e_config.result_fields,
193 &e2e_config.fields_array,
194 &HashSet::new(),
195 &HashMap::new(),
196 php_getter_map,
197 );
198
199 for group in groups {
200 let active: Vec<&Fixture> = group
201 .fixtures
202 .iter()
203 .filter(|f| super::should_include_fixture(f, lang, e2e_config))
204 .collect();
205
206 if active.is_empty() {
207 continue;
208 }
209
210 let test_class = format!("{}Test", sanitize_filename(&group.category).to_upper_camel_case());
211 let filename = format!("{test_class}.php");
212 let content = render_test_file(
213 &group.category,
214 &active,
215 e2e_config,
216 lang,
217 &namespace,
218 &class_name,
219 &test_class,
220 &field_resolver,
221 enum_fields,
222 result_is_simple,
223 php_client_factory,
224 options_via,
225 );
226 files.push(GeneratedFile {
227 path: tests_base.join(filename),
228 content,
229 generated_header: true,
230 });
231 }
232
233 Ok(files)
234 }
235
236 fn language_name(&self) -> &'static str {
237 "php"
238 }
239}
240
241fn build_php_getter_map(
267 type_defs: &[alef_core::ir::TypeDef],
268 enum_names: &HashSet<String>,
269 call: &alef_core::config::e2e::CallConfig,
270 result_fields: &HashSet<String>,
271) -> PhpGetterMap {
272 let mut getters: HashMap<String, HashSet<String>> = HashMap::new();
273 let mut field_types: HashMap<String, HashMap<String, String>> = HashMap::new();
274 let mut all_fields: HashMap<String, HashSet<String>> = HashMap::new();
275 for td in type_defs {
276 let mut getter_fields: HashSet<String> = HashSet::new();
277 let mut field_type_map: HashMap<String, String> = HashMap::new();
278 let mut td_all_fields: HashSet<String> = HashSet::new();
279 for f in &td.fields {
280 td_all_fields.insert(f.name.clone());
281 if !is_php_scalar(&f.ty, enum_names) {
282 getter_fields.insert(f.name.clone());
283 }
284 if let Some(named) = inner_named(&f.ty) {
285 field_type_map.insert(f.name.clone(), named);
286 }
287 }
288 getters.insert(td.name.clone(), getter_fields);
289 all_fields.insert(td.name.clone(), td_all_fields);
290 if !field_type_map.is_empty() {
291 field_types.insert(td.name.clone(), field_type_map);
292 }
293 }
294 let root_type = derive_root_type(call, type_defs, result_fields);
295 PhpGetterMap {
296 getters,
297 field_types,
298 root_type,
299 all_fields,
300 }
301}
302
303fn inner_named(ty: &TypeRef) -> Option<String> {
306 match ty {
307 TypeRef::Named(n) => Some(n.clone()),
308 TypeRef::Optional(inner) | TypeRef::Vec(inner) => inner_named(inner),
309 _ => None,
310 }
311}
312
313fn derive_root_type(
324 call: &alef_core::config::e2e::CallConfig,
325 type_defs: &[alef_core::ir::TypeDef],
326 result_fields: &HashSet<String>,
327) -> Option<String> {
328 const LOOKUP_LANGS: &[&str] = &["php", "c", "csharp", "java", "kotlin", "go"];
329 for lang in LOOKUP_LANGS {
330 if let Some(o) = call.overrides.get(*lang)
331 && let Some(rt) = o.result_type.as_deref()
332 && !rt.is_empty()
333 && type_defs.iter().any(|td| td.name == rt)
334 {
335 return Some(rt.to_string());
336 }
337 }
338 if result_fields.is_empty() {
339 return None;
340 }
341 let matches: Vec<&alef_core::ir::TypeDef> = type_defs
342 .iter()
343 .filter(|td| {
344 let names: HashSet<&str> = td.fields.iter().map(|f| f.name.as_str()).collect();
345 result_fields.iter().all(|rf| names.contains(rf.as_str()))
346 })
347 .collect();
348 if matches.len() == 1 {
349 return Some(matches[0].name.clone());
350 }
351 None
352}
353
354fn is_php_scalar(ty: &TypeRef, enum_names: &HashSet<String>) -> bool {
355 match ty {
356 TypeRef::Primitive(_) | TypeRef::String | TypeRef::Char | TypeRef::Duration | TypeRef::Path => true,
357 TypeRef::Optional(inner) => is_php_scalar(inner, enum_names),
358 TypeRef::Vec(inner) => {
359 matches!(inner.as_ref(), TypeRef::Primitive(_) | TypeRef::String | TypeRef::Char)
360 || matches!(inner.as_ref(), TypeRef::Named(n) if enum_names.contains(n))
361 }
362 TypeRef::Named(n) if enum_names.contains(n) => true,
363 TypeRef::Named(_) | TypeRef::Map(_, _) | TypeRef::Json | TypeRef::Bytes | TypeRef::Unit => false,
364 }
365}
366
367fn render_composer_json(
372 e2e_pkg_name: &str,
373 e2e_autoload_ns: &str,
374 pkg_name: &str,
375 pkg_path: &str,
376 pkg_version: &str,
377 dep_mode: crate::config::DependencyMode,
378) -> String {
379 let (require_section, autoload_section) = match dep_mode {
380 crate::config::DependencyMode::Registry => {
381 let require = format!(
382 r#" "require": {{
383 "{pkg_name}": "{pkg_version}"
384 }},
385 "require-dev": {{
386 "phpunit/phpunit": "{phpunit}",
387 "guzzlehttp/guzzle": "{guzzle}"
388 }},"#,
389 phpunit = tv::packagist::PHPUNIT,
390 guzzle = tv::packagist::GUZZLE,
391 );
392 (require, String::new())
393 }
394 crate::config::DependencyMode::Local => {
395 let require = format!(
396 r#" "require-dev": {{
397 "phpunit/phpunit": "{phpunit}",
398 "guzzlehttp/guzzle": "{guzzle}"
399 }},"#,
400 phpunit = tv::packagist::PHPUNIT,
401 guzzle = tv::packagist::GUZZLE,
402 );
403 let pkg_namespace = pkg_name
406 .split('/')
407 .nth(1)
408 .unwrap_or(pkg_name)
409 .split('-')
410 .map(heck::ToUpperCamelCase::to_upper_camel_case)
411 .collect::<Vec<_>>()
412 .join("\\");
413 let autoload = format!(
414 r#"
415 "autoload": {{
416 "psr-4": {{
417 "{}\\": "{}/src/"
418 }}
419 }},"#,
420 pkg_namespace.replace('\\', "\\\\"),
421 pkg_path
422 );
423 (require, autoload)
424 }
425 };
426
427 crate::template_env::render(
428 "php/composer.json.jinja",
429 minijinja::context! {
430 e2e_pkg_name => e2e_pkg_name,
431 e2e_autoload_ns => e2e_autoload_ns,
432 require_section => require_section,
433 autoload_section => autoload_section,
434 },
435 )
436}
437
438fn render_phpunit_xml() -> String {
439 crate::template_env::render("php/phpunit.xml.jinja", minijinja::context! {})
440}
441
442fn render_bootstrap(
443 pkg_path: &str,
444 has_http_fixtures: bool,
445 has_file_fixtures: bool,
446 test_documents_path: &str,
447) -> String {
448 let header = hash::header(CommentStyle::DoubleSlash);
449 crate::template_env::render(
450 "php/bootstrap.php.jinja",
451 minijinja::context! {
452 header => header,
453 pkg_path => pkg_path,
454 has_http_fixtures => has_http_fixtures,
455 has_file_fixtures => has_file_fixtures,
456 test_documents_path => test_documents_path,
457 },
458 )
459}
460
461fn render_run_tests_php(extension_name: &str, cargo_crate_name: Option<&str>) -> String {
462 let header = hash::header(CommentStyle::DoubleSlash);
463 let ext_lib_name = if let Some(crate_name) = cargo_crate_name {
464 format!("lib{}", crate_name.replace('-', "_"))
467 } else {
468 format!("lib{extension_name}_php")
469 };
470 format!(
471 r#"#!/usr/bin/env php
472<?php
473{header}
474declare(strict_types=1);
475
476// Determine platform-specific extension suffix.
477$extSuffix = match (PHP_OS_FAMILY) {{
478 'Darwin' => '.dylib',
479 default => '.so',
480}};
481$extPath = __DIR__ . '/../../target/release/{ext_lib_name}' . $extSuffix;
482
483// If the locally-built extension exists and we have not already restarted with it,
484// re-exec PHP with no system ini (-n) to avoid conflicts with any system-installed
485// version of the extension, then load the local build explicitly.
486if (file_exists($extPath) && !getenv('ALEF_PHP_LOCAL_EXT_LOADED')) {{
487 putenv('ALEF_PHP_LOCAL_EXT_LOADED=1');
488 $php = PHP_BINARY;
489 $phpunitPath = __DIR__ . '/vendor/bin/phpunit';
490
491 $cmd = array_merge(
492 [$php, '-n', '-d', 'extension=' . $extPath],
493 [$phpunitPath],
494 array_slice($GLOBALS['argv'], 1)
495 );
496
497 passthru(implode(' ', array_map('escapeshellarg', $cmd)), $exitCode);
498 exit($exitCode);
499}}
500
501// Extension is now loaded (via the restart above with -n flag).
502// Invoke PHPUnit normally.
503$phpunitPath = __DIR__ . '/vendor/bin/phpunit';
504if (!file_exists($phpunitPath)) {{
505 echo "PHPUnit not found at $phpunitPath. Run 'composer install' first.\n";
506 exit(1);
507}}
508
509require $phpunitPath;
510"#
511 )
512}
513
514#[allow(clippy::too_many_arguments)]
515fn render_test_file(
516 category: &str,
517 fixtures: &[&Fixture],
518 e2e_config: &E2eConfig,
519 lang: &str,
520 namespace: &str,
521 class_name: &str,
522 test_class: &str,
523 field_resolver: &FieldResolver,
524 enum_fields: &HashMap<String, String>,
525 result_is_simple: bool,
526 php_client_factory: Option<&str>,
527 options_via: &str,
528) -> String {
529 let header = hash::header(CommentStyle::DoubleSlash);
530
531 let needs_crawl_config_import = fixtures.iter().any(|f| {
533 let call = e2e_config.resolve_call_for_fixture(f.call.as_deref(), &f.input);
534 call.args.iter().filter(|a| a.arg_type == "handle").any(|a| {
535 let v = f.input.get(&a.field).unwrap_or(&serde_json::Value::Null);
536 !(v.is_null() || v.is_object() && v.as_object().is_some_and(|o| o.is_empty()))
537 })
538 });
539
540 let has_http_tests = fixtures.iter().any(|f| f.is_http_test());
542
543 let mut options_type_imports: Vec<String> = fixtures
545 .iter()
546 .flat_map(|f| {
547 let call = e2e_config.resolve_call_for_fixture(f.call.as_deref(), &f.input);
548 let php_override = call.overrides.get(lang);
549 let opt_type = php_override.and_then(|o| o.options_type.as_deref()).or_else(|| {
550 e2e_config
551 .call
552 .overrides
553 .get(lang)
554 .and_then(|o| o.options_type.as_deref())
555 });
556 let element_types: Vec<String> = call
557 .args
558 .iter()
559 .filter_map(|a| a.element_type.as_ref().map(|t| t.to_string()))
560 .filter(|t| !is_php_reserved_type(t))
561 .collect();
562 opt_type.map(|t| t.to_string()).into_iter().chain(element_types)
563 })
564 .collect::<std::collections::HashSet<_>>()
565 .into_iter()
566 .collect();
567 options_type_imports.sort();
568
569 let mut imports_use: Vec<String> = Vec::new();
571 if needs_crawl_config_import {
572 imports_use.push(format!("use {namespace}\\CrawlConfig;"));
573 }
574 for type_name in &options_type_imports {
575 if type_name != class_name {
576 imports_use.push(format!("use {namespace}\\{type_name};"));
577 }
578 }
579
580 let mut fixtures_body = String::new();
582 for (i, fixture) in fixtures.iter().enumerate() {
583 if fixture.is_http_test() {
584 render_http_test_method(&mut fixtures_body, fixture, fixture.http.as_ref().unwrap());
585 } else {
586 render_test_method(
587 &mut fixtures_body,
588 fixture,
589 e2e_config,
590 lang,
591 namespace,
592 class_name,
593 field_resolver,
594 enum_fields,
595 result_is_simple,
596 php_client_factory,
597 options_via,
598 );
599 }
600 if i + 1 < fixtures.len() {
601 fixtures_body.push('\n');
602 }
603 }
604
605 crate::template_env::render(
606 "php/test_file.jinja",
607 minijinja::context! {
608 header => header,
609 namespace => namespace,
610 class_name => class_name,
611 test_class => test_class,
612 category => category,
613 imports_use => imports_use,
614 has_http_tests => has_http_tests,
615 fixtures_body => fixtures_body,
616 },
617 )
618}
619
620struct PhpTestClientRenderer;
628
629impl client::TestClientRenderer for PhpTestClientRenderer {
630 fn language_name(&self) -> &'static str {
631 "php"
632 }
633
634 fn sanitize_test_name(&self, id: &str) -> String {
636 sanitize_filename(id)
637 }
638
639 fn render_test_open(&self, out: &mut String, fn_name: &str, description: &str, skip_reason: Option<&str>) {
645 let escaped_reason = skip_reason.map(escape_php);
646 let rendered = crate::template_env::render(
647 "php/http_test_open.jinja",
648 minijinja::context! {
649 fn_name => fn_name,
650 description => description,
651 skip_reason => escaped_reason,
652 },
653 );
654 out.push_str(&rendered);
655 }
656
657 fn render_test_close(&self, out: &mut String) {
659 let rendered = crate::template_env::render("php/http_test_close.jinja", minijinja::context! {});
660 out.push_str(&rendered);
661 }
662
663 fn render_call(&self, out: &mut String, ctx: &client::CallCtx<'_>) {
668 let method = ctx.method.to_uppercase();
669
670 let mut opts: Vec<String> = Vec::new();
672
673 if let Some(body) = ctx.body {
674 let php_body = json_to_php(body);
675 opts.push(format!("'json' => {php_body}"));
676 }
677
678 let mut header_pairs: Vec<String> = Vec::new();
680 if let Some(ct) = ctx.content_type {
681 if !ctx.headers.keys().any(|k| k.to_lowercase() == "content-type") {
683 header_pairs.push(format!("\"Content-Type\" => \"{}\"", escape_php(ct)));
684 }
685 }
686 for (k, v) in ctx.headers {
687 header_pairs.push(format!("\"{}\" => \"{}\"", escape_php(k), escape_php(v)));
688 }
689 if !header_pairs.is_empty() {
690 opts.push(format!("'headers' => [{}]", header_pairs.join(", ")));
691 }
692
693 if !ctx.cookies.is_empty() {
694 let cookie_str = ctx
695 .cookies
696 .iter()
697 .map(|(k, v)| format!("{}={}", k, v))
698 .collect::<Vec<_>>()
699 .join("; ");
700 opts.push(format!("'headers' => ['Cookie' => \"{}\"]", escape_php(&cookie_str)));
701 }
702
703 if !ctx.query_params.is_empty() {
704 let pairs: Vec<String> = ctx
705 .query_params
706 .iter()
707 .map(|(k, v)| {
708 let val_str = match v {
709 serde_json::Value::String(s) => s.clone(),
710 other => other.to_string(),
711 };
712 format!("\"{}\" => \"{}\"", escape_php(k), escape_php(&val_str))
713 })
714 .collect();
715 opts.push(format!("'query' => [{}]", pairs.join(", ")));
716 }
717
718 let path_lit = format!("\"{}\"", escape_php(ctx.path));
719
720 let rendered = crate::template_env::render(
721 "php/http_request.jinja",
722 minijinja::context! {
723 method => method,
724 path => path_lit,
725 opts => opts,
726 response_var => ctx.response_var,
727 },
728 );
729 out.push_str(&rendered);
730 }
731
732 fn render_assert_status(&self, out: &mut String, _response_var: &str, status: u16) {
734 let rendered = crate::template_env::render(
735 "php/http_assertions.jinja",
736 minijinja::context! {
737 response_var => "",
738 status_code => status,
739 headers => Vec::<std::collections::HashMap<&str, String>>::new(),
740 body_assertion => String::new(),
741 partial_body => Vec::<std::collections::HashMap<&str, String>>::new(),
742 validation_errors => Vec::<std::collections::HashMap<&str, String>>::new(),
743 },
744 );
745 out.push_str(&rendered);
746 }
747
748 fn render_assert_header(&self, out: &mut String, _response_var: &str, name: &str, expected: &str) {
753 let header_key = name.to_lowercase();
754 let header_key_lit = format!("\"{}\"", escape_php(&header_key));
755 let assertion_code = match expected {
756 "<<present>>" => {
757 format!("$this->assertTrue($response->hasHeader({header_key_lit}));")
758 }
759 "<<absent>>" => {
760 format!("$this->assertFalse($response->hasHeader({header_key_lit}));")
761 }
762 "<<uuid>>" => {
763 format!(
764 "$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}));"
765 )
766 }
767 literal => {
768 let val_lit = format!("\"{}\"", escape_php(literal));
769 format!("$this->assertEquals({val_lit}, $response->getHeaderLine({header_key_lit}));")
770 }
771 };
772
773 let mut headers = vec![std::collections::HashMap::new()];
774 headers[0].insert("assertion_code", assertion_code);
775
776 let rendered = crate::template_env::render(
777 "php/http_assertions.jinja",
778 minijinja::context! {
779 response_var => "",
780 status_code => 0u16,
781 headers => headers,
782 body_assertion => String::new(),
783 partial_body => Vec::<std::collections::HashMap<&str, String>>::new(),
784 validation_errors => Vec::<std::collections::HashMap<&str, String>>::new(),
785 },
786 );
787 out.push_str(&rendered);
788 }
789
790 fn render_assert_json_body(&self, out: &mut String, _response_var: &str, expected: &serde_json::Value) {
796 let body_assertion = match expected {
797 serde_json::Value::String(s) if !s.is_empty() => {
798 let php_val = format!("\"{}\"", escape_php(s));
799 format!("$this->assertEquals({php_val}, (string) $response->getBody());")
800 }
801 _ => {
802 let php_val = json_to_php(expected);
803 format!(
804 "$body = json_decode((string) $response->getBody(), true, 512, JSON_THROW_ON_ERROR);\n $this->assertEquals({php_val}, $body);"
805 )
806 }
807 };
808
809 let rendered = crate::template_env::render(
810 "php/http_assertions.jinja",
811 minijinja::context! {
812 response_var => "",
813 status_code => 0u16,
814 headers => Vec::<std::collections::HashMap<&str, String>>::new(),
815 body_assertion => body_assertion,
816 partial_body => Vec::<std::collections::HashMap<&str, String>>::new(),
817 validation_errors => Vec::<std::collections::HashMap<&str, String>>::new(),
818 },
819 );
820 out.push_str(&rendered);
821 }
822
823 fn render_assert_partial_body(&self, out: &mut String, _response_var: &str, expected: &serde_json::Value) {
825 if let Some(obj) = expected.as_object() {
826 let mut partial_body: Vec<std::collections::HashMap<&str, String>> = Vec::new();
827 for (key, val) in obj {
828 let php_key = format!("\"{}\"", escape_php(key));
829 let php_val = json_to_php(val);
830 let assertion_code = format!("$this->assertEquals({php_val}, $body[{php_key}]);");
831 let mut entry = std::collections::HashMap::new();
832 entry.insert("assertion_code", assertion_code);
833 partial_body.push(entry);
834 }
835
836 let rendered = crate::template_env::render(
837 "php/http_assertions.jinja",
838 minijinja::context! {
839 response_var => "",
840 status_code => 0u16,
841 headers => Vec::<std::collections::HashMap<&str, String>>::new(),
842 body_assertion => String::new(),
843 partial_body => partial_body,
844 validation_errors => Vec::<std::collections::HashMap<&str, String>>::new(),
845 },
846 );
847 out.push_str(&rendered);
848 }
849 }
850
851 fn render_assert_validation_errors(
854 &self,
855 out: &mut String,
856 _response_var: &str,
857 errors: &[ValidationErrorExpectation],
858 ) {
859 let mut validation_errors: Vec<std::collections::HashMap<&str, String>> = Vec::new();
860 for err in errors {
861 let msg_lit = format!("\"{}\"", escape_php(&err.msg));
862 let assertion_code =
863 format!("$this->assertStringContainsString({msg_lit}, json_encode($body, JSON_UNESCAPED_SLASHES));");
864 let mut entry = std::collections::HashMap::new();
865 entry.insert("assertion_code", assertion_code);
866 validation_errors.push(entry);
867 }
868
869 let rendered = crate::template_env::render(
870 "php/http_assertions.jinja",
871 minijinja::context! {
872 response_var => "",
873 status_code => 0u16,
874 headers => Vec::<std::collections::HashMap<&str, String>>::new(),
875 body_assertion => String::new(),
876 partial_body => Vec::<std::collections::HashMap<&str, String>>::new(),
877 validation_errors => validation_errors,
878 },
879 );
880 out.push_str(&rendered);
881 }
882}
883
884fn render_http_test_method(out: &mut String, fixture: &Fixture, http: &HttpFixture) {
889 if http.expected_response.status_code == 101 {
893 let method_name = sanitize_filename(&fixture.id);
894 let description = &fixture.description;
895 out.push_str(&crate::template_env::render(
896 "php/http_test_skip_101.jinja",
897 minijinja::context! {
898 method_name => method_name,
899 description => description,
900 },
901 ));
902 return;
903 }
904
905 client::http_call::render_http_test(out, &PhpTestClientRenderer, fixture);
906}
907
908#[allow(clippy::too_many_arguments)]
913fn render_test_method(
914 out: &mut String,
915 fixture: &Fixture,
916 e2e_config: &E2eConfig,
917 lang: &str,
918 namespace: &str,
919 class_name: &str,
920 field_resolver: &FieldResolver,
921 enum_fields: &HashMap<String, String>,
922 result_is_simple: bool,
923 php_client_factory: Option<&str>,
924 options_via: &str,
925) {
926 let call_config = e2e_config.resolve_call_for_fixture(fixture.call.as_deref(), &fixture.input);
928 let call_overrides = call_config.overrides.get(lang);
929 let has_override = call_overrides.is_some_and(|o| o.function.is_some());
930 let result_is_simple = call_overrides.is_some_and(|o| o.result_is_simple) || result_is_simple;
934 let mut function_name = call_overrides
935 .and_then(|o| o.function.as_ref())
936 .cloned()
937 .unwrap_or_else(|| call_config.function.clone());
938 if !has_override {
943 function_name = function_name.to_lower_camel_case();
944 }
945 let result_var = &call_config.result_var;
946 let args = &call_config.args;
947
948 let method_name = sanitize_filename(&fixture.id);
949 let description = &fixture.description;
950 let expects_error = fixture.assertions.iter().any(|a| a.assertion_type == "error");
951
952 let call_options_type = call_overrides.and_then(|o| o.options_type.as_deref()).or_else(|| {
954 e2e_config
955 .call
956 .overrides
957 .get(lang)
958 .and_then(|o| o.options_type.as_deref())
959 });
960
961 let (mut setup_lines, args_str) = build_args_and_setup(
962 &fixture.input,
963 args,
964 class_name,
965 enum_fields,
966 fixture,
967 options_via,
968 call_options_type,
969 );
970
971 let skip_test = call_config.skip_languages.iter().any(|l| l == "php");
973 if skip_test {
974 let rendered = crate::template_env::render(
975 "php/test_method.jinja",
976 minijinja::context! {
977 method_name => method_name,
978 description => description,
979 client_factory => String::new(),
980 setup_lines => Vec::<String>::new(),
981 expects_error => false,
982 skip_test => true,
983 has_usable_assertions => false,
984 call_expr => String::new(),
985 result_var => result_var,
986 assertions_body => String::new(),
987 },
988 );
989 out.push_str(&rendered);
990 return;
991 }
992
993 let mut options_already_created = !args_str.is_empty() && args_str == "$options";
995 if let Some(visitor_spec) = &fixture.visitor {
996 build_php_visitor(&mut setup_lines, visitor_spec);
997 if !options_already_created {
998 let options_type = call_options_type.unwrap_or("ConversionOptions");
999 setup_lines.push(format!("$builder = \\{namespace}\\{options_type}::builder();"));
1000 setup_lines.push("$options = $builder->visitor($visitor)->build();".to_string());
1001 options_already_created = true;
1002 }
1003 }
1004
1005 let final_args = if options_already_created {
1006 if args_str.is_empty() || args_str == "$options" {
1007 "$options".to_string()
1008 } else {
1009 format!("{args_str}, $options")
1010 }
1011 } else {
1012 args_str
1013 };
1014
1015 let call_expr = if php_client_factory.is_some() {
1016 format!("$client->{function_name}({final_args})")
1017 } else {
1018 format!("{class_name}::{function_name}({final_args})")
1019 };
1020
1021 let has_mock = fixture.mock_response.is_some() || fixture.http.is_some();
1022 let api_key_var = fixture.env.as_ref().and_then(|e| e.api_key_var.as_deref());
1023 let client_factory = if let Some(factory) = php_client_factory {
1024 let fixture_id = &fixture.id;
1025 if let Some(var) = api_key_var.filter(|_| has_mock) {
1026 format!(
1027 "$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);"
1028 )
1029 } else if has_mock {
1030 let base_url_expr = if fixture.has_host_root_route() {
1031 let env_key = format!("MOCK_SERVER_{}", fixture_id.to_uppercase());
1032 format!("(getenv('{env_key}') ?: getenv('MOCK_SERVER_URL') . '/fixtures/{fixture_id}')")
1033 } else {
1034 format!("getenv('MOCK_SERVER_URL') . '/fixtures/{fixture_id}'")
1035 };
1036 format!("$client = \\{namespace}\\{class_name}::{factory}('test-key', {base_url_expr});")
1037 } else if let Some(var) = api_key_var {
1038 format!(
1039 "$apiKey = getenv('{var}');\n if (!$apiKey) {{ $this->markTestSkipped('{var} not set'); return; }}\n $client = \\{namespace}\\{class_name}::{factory}($apiKey);"
1040 )
1041 } else {
1042 format!("$client = \\{namespace}\\{class_name}::{factory}('test-key');")
1043 }
1044 } else {
1045 String::new()
1046 };
1047
1048 let is_streaming = crate::codegen::streaming_assertions::resolve_is_streaming(fixture, call_config.streaming);
1050
1051 let has_usable_assertions = fixture.assertions.iter().any(|a| {
1054 if a.assertion_type == "error" || a.assertion_type == "not_error" {
1055 return false;
1056 }
1057 match &a.field {
1058 Some(f) if !f.is_empty() => {
1059 if is_streaming && crate::codegen::streaming_assertions::is_streaming_virtual_field(f) {
1060 return true;
1061 }
1062 field_resolver.is_valid_for_result(f)
1063 }
1064 _ => true,
1065 }
1066 });
1067
1068 let collect_snippet = if is_streaming {
1070 crate::codegen::streaming_assertions::StreamingFieldResolver::collect_snippet("php", result_var, "chunks")
1071 .unwrap_or_default()
1072 } else {
1073 String::new()
1074 };
1075
1076 let mut assertions_body = String::new();
1078 for assertion in &fixture.assertions {
1079 render_assertion(
1080 &mut assertions_body,
1081 assertion,
1082 result_var,
1083 field_resolver,
1084 result_is_simple,
1085 call_config.result_is_array,
1086 );
1087 }
1088
1089 if is_streaming && !expects_error && assertions_body.trim().is_empty() {
1095 assertions_body.push_str(" $this->assertTrue(is_array($chunks), 'expected drained chunks list');\n");
1096 }
1097
1098 let rendered = crate::template_env::render(
1099 "php/test_method.jinja",
1100 minijinja::context! {
1101 method_name => method_name,
1102 description => description,
1103 client_factory => client_factory,
1104 setup_lines => setup_lines,
1105 expects_error => expects_error,
1106 skip_test => fixture.assertions.is_empty(),
1107 has_usable_assertions => has_usable_assertions || is_streaming,
1108 call_expr => call_expr,
1109 result_var => result_var,
1110 collect_snippet => collect_snippet,
1111 assertions_body => assertions_body,
1112 },
1113 );
1114 out.push_str(&rendered);
1115}
1116
1117fn emit_php_batch_item_array(arr: &serde_json::Value, elem_type: &str) -> String {
1130 if let Some(items) = arr.as_array() {
1131 let item_strs: Vec<String> = items
1132 .iter()
1133 .filter_map(|item| {
1134 if let Some(obj) = item.as_object() {
1135 match elem_type {
1136 "BatchBytesItem" => {
1137 let content = obj.get("content").and_then(|v| v.as_array());
1138 let mime_type = obj.get("mime_type").and_then(|v| v.as_str()).unwrap_or("text/plain");
1139 let content_code = if let Some(arr) = content {
1140 let bytes: Vec<String> = arr
1141 .iter()
1142 .filter_map(|v| v.as_u64())
1143 .map(|n| format!("\\x{:02x}", n))
1144 .collect();
1145 format!("\"{}\"", bytes.join(""))
1146 } else {
1147 "\"\"".to_string()
1148 };
1149 Some(format!(
1150 "new {}(content: {}, mimeType: \"{}\")",
1151 elem_type, content_code, mime_type
1152 ))
1153 }
1154 "BatchFileItem" => {
1155 let path = obj.get("path").and_then(|v| v.as_str()).unwrap_or("");
1156 Some(format!("new {}(path: \"{}\")", elem_type, path))
1157 }
1158 _ => None,
1159 }
1160 } else {
1161 None
1162 }
1163 })
1164 .collect();
1165 format!("[{}]", item_strs.join(", "))
1166 } else {
1167 "[]".to_string()
1168 }
1169}
1170
1171fn build_args_and_setup(
1172 input: &serde_json::Value,
1173 args: &[crate::config::ArgMapping],
1174 class_name: &str,
1175 _enum_fields: &HashMap<String, String>,
1176 fixture: &crate::fixture::Fixture,
1177 options_via: &str,
1178 options_type: Option<&str>,
1179) -> (Vec<String>, String) {
1180 let fixture_id = &fixture.id;
1181 if args.is_empty() {
1182 let is_empty_input = match input {
1185 serde_json::Value::Null => true,
1186 serde_json::Value::Object(m) => m.is_empty(),
1187 _ => false,
1188 };
1189 if is_empty_input {
1190 return (Vec::new(), String::new());
1191 }
1192 return (Vec::new(), json_to_php(input));
1193 }
1194
1195 let mut setup_lines: Vec<String> = Vec::new();
1196 let mut parts: Vec<String> = Vec::new();
1197
1198 let arg_has_emission = |arg: &crate::config::ArgMapping| -> bool {
1203 let val = if arg.field == "input" {
1204 Some(input)
1205 } else {
1206 let field = arg.field.strip_prefix("input.").unwrap_or(&arg.field);
1207 input.get(field)
1208 };
1209 match val {
1210 None | Some(serde_json::Value::Null) => !arg.optional,
1211 Some(_) => true,
1212 }
1213 };
1214 let any_later_has_emission = |from_idx: usize| -> bool { args[from_idx..].iter().any(arg_has_emission) };
1215
1216 for (idx, arg) in args.iter().enumerate() {
1217 if arg.arg_type == "mock_url" {
1218 if fixture.has_host_root_route() {
1219 let env_key = format!("MOCK_SERVER_{}", fixture_id.to_uppercase());
1220 setup_lines.push(format!(
1221 "${} = getenv('{env_key}') ?: getenv('MOCK_SERVER_URL') . '/fixtures/{fixture_id}';",
1222 arg.name,
1223 ));
1224 } else {
1225 setup_lines.push(format!(
1226 "${} = getenv('MOCK_SERVER_URL') . '/fixtures/{fixture_id}';",
1227 arg.name,
1228 ));
1229 }
1230 parts.push(format!("${}", arg.name));
1231 continue;
1232 }
1233
1234 if arg.arg_type == "handle" {
1235 let constructor_name = format!("create{}", arg.name.to_upper_camel_case());
1237 let config_value = if arg.field == "input" {
1238 input
1239 } else {
1240 let field = arg.field.strip_prefix("input.").unwrap_or(&arg.field);
1241 input.get(field).unwrap_or(&serde_json::Value::Null)
1242 };
1243 if config_value.is_null()
1244 || config_value.is_object() && config_value.as_object().is_some_and(|o| o.is_empty())
1245 {
1246 setup_lines.push(format!("${} = {class_name}::{constructor_name}(null);", arg.name,));
1247 } else {
1248 let name = &arg.name;
1249 let filtered_config = filter_empty_enum_strings(config_value);
1254 setup_lines.push(format!(
1255 "${name}_config = CrawlConfig::from_json(json_encode({}));",
1256 json_to_php_camel_keys(&filtered_config)
1257 ));
1258 setup_lines.push(format!(
1259 "${} = {class_name}::{constructor_name}(${name}_config);",
1260 arg.name,
1261 ));
1262 }
1263 parts.push(format!("${}", arg.name));
1264 continue;
1265 }
1266
1267 let val = if arg.field == "input" {
1268 Some(input)
1269 } else {
1270 let field = arg.field.strip_prefix("input.").unwrap_or(&arg.field);
1271 input.get(field)
1272 };
1273
1274 if arg.arg_type == "bytes" {
1278 match val {
1279 None | Some(serde_json::Value::Null) => {
1280 if arg.optional {
1281 parts.push("null".to_string());
1282 } else {
1283 parts.push("\"\"".to_string());
1284 }
1285 }
1286 Some(serde_json::Value::String(s)) => {
1287 let var_name = format!("{}Bytes", arg.name);
1288 setup_lines.push(format!(
1289 "${var_name} = file_get_contents(\"{path}\");\n if (${var_name} === false) {{ $this->fail(\"failed to read fixture: {path}\"); }}",
1290 path = s.replace('"', "\\\"")
1291 ));
1292 parts.push(format!("${var_name}"));
1293 }
1294 Some(serde_json::Value::Array(arr)) => {
1295 let bytes: String = arr
1296 .iter()
1297 .filter_map(|v| v.as_u64())
1298 .map(|n| format!("\\x{:02x}", n))
1299 .collect();
1300 parts.push(format!("\"{bytes}\""));
1301 }
1302 Some(other) => {
1303 parts.push(json_to_php(other));
1304 }
1305 }
1306 continue;
1307 }
1308
1309 match val {
1310 None | Some(serde_json::Value::Null) if arg.arg_type == "json_object" && arg.name == "config" => {
1311 let type_name = if arg.name == "config" {
1317 "ExtractionConfig".to_string()
1318 } else {
1319 format!("{}Config", arg.name.to_upper_camel_case())
1320 };
1321 parts.push(format!("{type_name}::from_json('{{}}')"));
1322 continue;
1323 }
1324 None | Some(serde_json::Value::Null) if arg.optional => {
1325 if any_later_has_emission(idx + 1) {
1330 parts.push("null".to_string());
1331 }
1332 continue;
1333 }
1334 None | Some(serde_json::Value::Null) => {
1335 let default_val = match arg.arg_type.as_str() {
1337 "string" => "\"\"".to_string(),
1338 "int" | "integer" => "0".to_string(),
1339 "float" | "number" => "0.0".to_string(),
1340 "bool" | "boolean" => "false".to_string(),
1341 "json_object" if options_via == "json" => "null".to_string(),
1342 _ => "null".to_string(),
1343 };
1344 parts.push(default_val);
1345 }
1346 Some(v) => {
1347 if arg.arg_type == "json_object" && !v.is_null() {
1348 if let Some(elem_type) = &arg.element_type {
1350 if (elem_type == "BatchBytesItem" || elem_type == "BatchFileItem") && v.is_array() {
1351 parts.push(emit_php_batch_item_array(v, elem_type));
1352 continue;
1353 }
1354 if v.is_array() && is_php_reserved_type(elem_type) {
1358 parts.push(json_to_php(v));
1359 continue;
1360 }
1361 }
1362 match options_via {
1363 "json" => {
1364 let filtered_v = filter_empty_enum_strings(v);
1367
1368 if let serde_json::Value::Object(obj) = &filtered_v {
1370 if obj.is_empty() {
1371 parts.push("null".to_string());
1372 continue;
1373 }
1374 }
1375
1376 parts.push(format!("json_encode({})", json_to_php_camel_keys(&filtered_v)));
1377 continue;
1378 }
1379 _ => {
1380 if let Some(type_name) = options_type {
1381 let filtered_v = filter_empty_enum_strings(v);
1386
1387 if let serde_json::Value::Object(obj) = &filtered_v {
1390 if obj.is_empty() {
1391 let arg_var = format!("${}", arg.name);
1392 setup_lines.push(format!("{arg_var} = {type_name}::from_json('{{}}');"));
1393 parts.push(arg_var);
1394 continue;
1395 }
1396 }
1397
1398 let arg_var = format!("${}", arg.name);
1399 setup_lines.push(format!(
1403 "{arg_var} = {type_name}::from_json(json_encode({}));",
1404 json_to_php_camel_keys(&filtered_v)
1405 ));
1406 parts.push(arg_var);
1407 continue;
1408 }
1409 if let Some(obj) = v.as_object() {
1413 setup_lines.push("$builder = $this->createDefaultOptionsBuilder();".to_string());
1414 for (k, vv) in obj {
1415 let snake_key = k.to_snake_case();
1416 if snake_key == "preprocessing" {
1417 if let Some(prep_obj) = vv.as_object() {
1418 let enabled =
1419 prep_obj.get("enabled").and_then(|v| v.as_bool()).unwrap_or(true);
1420 let preset =
1421 prep_obj.get("preset").and_then(|v| v.as_str()).unwrap_or("Minimal");
1422 let remove_navigation = prep_obj
1423 .get("remove_navigation")
1424 .and_then(|v| v.as_bool())
1425 .unwrap_or(true);
1426 let remove_forms =
1427 prep_obj.get("remove_forms").and_then(|v| v.as_bool()).unwrap_or(true);
1428 setup_lines.push(format!(
1429 "$preprocessing = $this->createPreprocessingOptions({}, {}, {}, {});",
1430 if enabled { "true" } else { "false" },
1431 json_to_php(&serde_json::Value::String(preset.to_string())),
1432 if remove_navigation { "true" } else { "false" },
1433 if remove_forms { "true" } else { "false" }
1434 ));
1435 setup_lines.push(
1436 "$builder = $builder->preprocessing($preprocessing);".to_string(),
1437 );
1438 }
1439 }
1440 }
1441 setup_lines.push("$options = $builder->build();".to_string());
1442 parts.push("$options".to_string());
1443 continue;
1444 }
1445 }
1446 }
1447 }
1448 parts.push(json_to_php(v));
1449 }
1450 }
1451 }
1452
1453 (setup_lines, parts.join(", "))
1454}
1455
1456fn render_assertion(
1457 out: &mut String,
1458 assertion: &Assertion,
1459 result_var: &str,
1460 field_resolver: &FieldResolver,
1461 result_is_simple: bool,
1462 result_is_array: bool,
1463) {
1464 if let Some(f) = &assertion.field {
1467 match f.as_str() {
1468 "chunks_have_content" => {
1469 let pred = format!(
1470 "array_reduce(${result_var}->chunks ?? [], fn($carry, $c) => $carry && !empty($c->content), true)"
1471 );
1472 out.push_str(&crate::template_env::render(
1473 "php/synthetic_assertion.jinja",
1474 minijinja::context! {
1475 assertion_kind => "chunks_content",
1476 assertion_type => assertion.assertion_type.as_str(),
1477 pred => pred,
1478 field_name => f,
1479 },
1480 ));
1481 return;
1482 }
1483 "chunks_have_embeddings" => {
1484 let pred = format!(
1485 "array_reduce(${result_var}->chunks ?? [], fn($carry, $c) => $carry && !empty($c->embedding), true)"
1486 );
1487 out.push_str(&crate::template_env::render(
1488 "php/synthetic_assertion.jinja",
1489 minijinja::context! {
1490 assertion_kind => "chunks_embeddings",
1491 assertion_type => assertion.assertion_type.as_str(),
1492 pred => pred,
1493 field_name => f,
1494 },
1495 ));
1496 return;
1497 }
1498 "embeddings" => {
1502 let php_val = assertion.value.as_ref().map(json_to_php).unwrap_or_default();
1503 out.push_str(&crate::template_env::render(
1504 "php/synthetic_assertion.jinja",
1505 minijinja::context! {
1506 assertion_kind => "embeddings",
1507 assertion_type => assertion.assertion_type.as_str(),
1508 php_val => php_val,
1509 result_var => result_var,
1510 },
1511 ));
1512 return;
1513 }
1514 "embedding_dimensions" => {
1515 let expr = format!("(empty(${result_var}) ? 0 : count(${result_var}[0]))");
1516 let php_val = assertion.value.as_ref().map(json_to_php).unwrap_or_default();
1517 out.push_str(&crate::template_env::render(
1518 "php/synthetic_assertion.jinja",
1519 minijinja::context! {
1520 assertion_kind => "embedding_dimensions",
1521 assertion_type => assertion.assertion_type.as_str(),
1522 expr => expr,
1523 php_val => php_val,
1524 },
1525 ));
1526 return;
1527 }
1528 "embeddings_valid" | "embeddings_finite" | "embeddings_non_zero" | "embeddings_normalized" => {
1529 let pred = match f.as_str() {
1530 "embeddings_valid" => {
1531 format!("array_reduce(${result_var}, fn($carry, $e) => $carry && count($e) > 0, true)")
1532 }
1533 "embeddings_finite" => {
1534 format!(
1535 "array_reduce(${result_var}, fn($carry, $e) => $carry && array_reduce($e, fn($c, $v) => $c && is_finite($v), true), true)"
1536 )
1537 }
1538 "embeddings_non_zero" => {
1539 format!(
1540 "array_reduce(${result_var}, fn($carry, $e) => $carry && count(array_filter($e, fn($v) => $v !== 0.0)) > 0, true)"
1541 )
1542 }
1543 "embeddings_normalized" => {
1544 format!(
1545 "array_reduce(${result_var}, fn($carry, $e) => $carry && abs(array_sum(array_map(fn($v) => $v * $v, $e)) - 1.0) < 1e-3, true)"
1546 )
1547 }
1548 _ => unreachable!(),
1549 };
1550 let assertion_kind = format!("embeddings_{}", f.strip_prefix("embeddings_").unwrap_or(f));
1551 out.push_str(&crate::template_env::render(
1552 "php/synthetic_assertion.jinja",
1553 minijinja::context! {
1554 assertion_kind => assertion_kind,
1555 assertion_type => assertion.assertion_type.as_str(),
1556 pred => pred,
1557 field_name => f,
1558 },
1559 ));
1560 return;
1561 }
1562 "keywords" | "keywords_count" => {
1565 out.push_str(&crate::template_env::render(
1566 "php/synthetic_assertion.jinja",
1567 minijinja::context! {
1568 assertion_kind => "keywords",
1569 field_name => f,
1570 },
1571 ));
1572 return;
1573 }
1574 _ => {}
1575 }
1576 }
1577
1578 if let Some(f) = &assertion.field {
1581 if !f.is_empty() && crate::codegen::streaming_assertions::is_streaming_virtual_field(f) {
1582 if let Some(expr) =
1583 crate::codegen::streaming_assertions::StreamingFieldResolver::accessor(f, "php", "chunks")
1584 {
1585 let line = match assertion.assertion_type.as_str() {
1586 "count_min" => {
1587 if let Some(n) = assertion.value.as_ref().and_then(|v| v.as_u64()) {
1588 format!(
1589 " $this->assertGreaterThanOrEqual({n}, count({expr}), 'expected >= {n} chunks');\n"
1590 )
1591 } else {
1592 String::new()
1593 }
1594 }
1595 "count_equals" => {
1596 if let Some(n) = assertion.value.as_ref().and_then(|v| v.as_u64()) {
1597 format!(" $this->assertCount({n}, {expr});\n")
1598 } else {
1599 String::new()
1600 }
1601 }
1602 "equals" => {
1603 if let Some(serde_json::Value::String(s)) = &assertion.value {
1604 let escaped = s.replace('\\', "\\\\").replace('\'', "\\'");
1605 format!(" $this->assertEquals('{escaped}', {expr});\n")
1606 } else if let Some(n) = assertion.value.as_ref().and_then(|v| v.as_u64()) {
1607 format!(" $this->assertEquals({n}, {expr});\n")
1608 } else {
1609 String::new()
1610 }
1611 }
1612 "not_empty" => format!(" $this->assertNotEmpty({expr});\n"),
1613 "is_empty" => format!(" $this->assertEmpty({expr});\n"),
1614 "is_true" => format!(" $this->assertTrue({expr});\n"),
1615 "is_false" => format!(" $this->assertFalse({expr});\n"),
1616 "greater_than" => {
1617 if let Some(n) = assertion.value.as_ref().and_then(|v| v.as_u64()) {
1618 format!(" $this->assertGreaterThan({n}, {expr});\n")
1619 } else {
1620 String::new()
1621 }
1622 }
1623 "greater_than_or_equal" => {
1624 if let Some(n) = assertion.value.as_ref().and_then(|v| v.as_u64()) {
1625 format!(" $this->assertGreaterThanOrEqual({n}, {expr});\n")
1626 } else {
1627 String::new()
1628 }
1629 }
1630 "contains" => {
1631 if let Some(serde_json::Value::String(s)) = &assertion.value {
1632 let escaped = s.replace('\\', "\\\\").replace('\'', "\\'");
1633 format!(" $this->assertStringContainsString('{escaped}', {expr});\n")
1634 } else {
1635 String::new()
1636 }
1637 }
1638 _ => format!(
1639 " // streaming field '{f}': assertion type '{}' not rendered\n",
1640 assertion.assertion_type
1641 ),
1642 };
1643 if !line.is_empty() {
1644 out.push_str(&line);
1645 }
1646 }
1647 return;
1648 }
1649 }
1650
1651 if let Some(f) = &assertion.field {
1653 if !f.is_empty() && !field_resolver.is_valid_for_result(f) {
1654 out.push_str(&crate::template_env::render(
1655 "php/synthetic_assertion.jinja",
1656 minijinja::context! {
1657 assertion_kind => "skipped",
1658 field_name => f,
1659 },
1660 ));
1661 return;
1662 }
1663 }
1664
1665 if result_is_simple {
1668 if let Some(f) = &assertion.field {
1669 let f_lower = f.to_lowercase();
1670 if !f.is_empty()
1671 && f_lower != "content"
1672 && (f_lower.starts_with("metadata")
1673 || f_lower.starts_with("document")
1674 || f_lower.starts_with("structure"))
1675 {
1676 out.push_str(&crate::template_env::render(
1677 "php/synthetic_assertion.jinja",
1678 minijinja::context! {
1679 assertion_kind => "result_is_simple",
1680 field_name => f,
1681 },
1682 ));
1683 return;
1684 }
1685 }
1686 }
1687
1688 let field_expr = match &assertion.field {
1689 _ if result_is_simple => format!("${result_var}"),
1693 Some(f) if !f.is_empty() => field_resolver.accessor(f, "php", &format!("${result_var}")),
1694 _ => format!("${result_var}"),
1695 };
1696
1697 let field_is_array = assertion.field.as_ref().map_or(result_is_array, |f| {
1700 if f.is_empty() {
1701 result_is_array
1702 } else {
1703 field_resolver.is_array(f)
1704 }
1705 });
1706
1707 let trimmed_field_expr_for = |expected: &serde_json::Value| -> String {
1711 if expected.is_string() {
1712 format!("trim({})", field_expr)
1713 } else {
1714 field_expr.clone()
1715 }
1716 };
1717
1718 let assertion_type = assertion.assertion_type.as_str();
1720 let has_php_val = assertion.value.is_some();
1721 let php_val = match assertion.value.as_ref() {
1725 Some(v) => json_to_php(v),
1726 None if assertion_type == "equals" => "null".to_string(),
1727 None => String::new(),
1728 };
1729 let trimmed_field_expr = trimmed_field_expr_for(assertion.value.as_ref().unwrap_or(&serde_json::Value::Null));
1730 let is_string_val = assertion.value.as_ref().is_some_and(|v| v.is_string());
1731 let values_php: Vec<String> = assertion
1735 .values
1736 .as_ref()
1737 .map(|vals| vals.iter().map(json_to_php).collect::<Vec<_>>())
1738 .or_else(|| assertion.value.as_ref().map(|v| vec![json_to_php(v)]))
1739 .unwrap_or_default();
1740 let contains_any_checks: Vec<String> = assertion
1741 .values
1742 .as_ref()
1743 .map_or(Vec::new(), |vals| vals.iter().map(json_to_php).collect());
1744 let n = assertion.value.as_ref().and_then(|v| v.as_u64()).unwrap_or(0);
1745
1746 let call_expr = if let Some(method_name) = &assertion.method {
1748 build_php_method_call(result_var, method_name, assertion.args.as_ref())
1749 } else {
1750 String::new()
1751 };
1752 let check = assertion.check.as_deref().unwrap_or("is_true");
1753 let has_php_check_val = matches!(assertion.assertion_type.as_str(), "method_result") && assertion.value.is_some();
1754 let php_check_val = if matches!(assertion.assertion_type.as_str(), "method_result") {
1755 assertion.value.as_ref().map(json_to_php).unwrap_or_default()
1756 } else {
1757 String::new()
1758 };
1759 let check_n = assertion.value.as_ref().and_then(|v| v.as_u64()).unwrap_or(0);
1760 let is_bool_val = assertion.value.as_ref().is_some_and(|v| v.is_boolean());
1761 let bool_is_true = assertion.value.as_ref().and_then(|v| v.as_bool()).unwrap_or(false);
1762
1763 if matches!(assertion_type, "not_error" | "error") {
1765 if assertion_type == "not_error" {
1766 }
1768 return;
1770 }
1771
1772 let rendered = crate::template_env::render(
1773 "php/assertion.jinja",
1774 minijinja::context! {
1775 assertion_type => assertion_type,
1776 field_expr => field_expr,
1777 php_val => php_val,
1778 has_php_val => has_php_val,
1779 trimmed_field_expr => trimmed_field_expr,
1780 is_string_val => is_string_val,
1781 field_is_array => field_is_array,
1782 values_php => values_php,
1783 contains_any_checks => contains_any_checks,
1784 n => n,
1785 call_expr => call_expr,
1786 check => check,
1787 php_check_val => php_check_val,
1788 has_php_check_val => has_php_check_val,
1789 check_n => check_n,
1790 is_bool_val => is_bool_val,
1791 bool_is_true => bool_is_true,
1792 },
1793 );
1794 let _ = write!(out, " {}", rendered);
1795}
1796
1797fn build_php_method_call(result_var: &str, method_name: &str, args: Option<&serde_json::Value>) -> String {
1804 let extra_args = if let Some(args_val) = args {
1805 args_val
1806 .as_object()
1807 .map(|obj| {
1808 obj.values()
1809 .map(|v| match v {
1810 serde_json::Value::String(s) => format!("\"{}\"", s.replace('\\', "\\\\").replace('"', "\\\"")),
1811 serde_json::Value::Bool(true) => "true".to_string(),
1812 serde_json::Value::Bool(false) => "false".to_string(),
1813 serde_json::Value::Number(n) => n.to_string(),
1814 serde_json::Value::Null => "null".to_string(),
1815 other => format!("\"{}\"", other.to_string().replace('\\', "\\\\").replace('"', "\\\"")),
1816 })
1817 .collect::<Vec<_>>()
1818 .join(", ")
1819 })
1820 .unwrap_or_default()
1821 } else {
1822 String::new()
1823 };
1824
1825 if extra_args.is_empty() {
1826 format!("${result_var}->{method_name}()")
1827 } else {
1828 format!("${result_var}->{method_name}({extra_args})")
1829 }
1830}
1831
1832fn filter_empty_enum_strings(value: &serde_json::Value) -> serde_json::Value {
1836 match value {
1837 serde_json::Value::Object(map) => {
1838 let filtered: serde_json::Map<String, serde_json::Value> = map
1839 .iter()
1840 .filter_map(|(k, v)| {
1841 if let serde_json::Value::String(s) = v {
1843 if s.is_empty() {
1844 return None;
1845 }
1846 }
1847 Some((k.clone(), filter_empty_enum_strings(v)))
1849 })
1850 .collect();
1851 serde_json::Value::Object(filtered)
1852 }
1853 serde_json::Value::Array(arr) => {
1854 let filtered: Vec<serde_json::Value> = arr.iter().map(filter_empty_enum_strings).collect();
1855 serde_json::Value::Array(filtered)
1856 }
1857 other => other.clone(),
1858 }
1859}
1860
1861fn json_to_php(value: &serde_json::Value) -> String {
1863 match value {
1864 serde_json::Value::String(s) => format!("\"{}\"", escape_php(s)),
1865 serde_json::Value::Bool(true) => "true".to_string(),
1866 serde_json::Value::Bool(false) => "false".to_string(),
1867 serde_json::Value::Number(n) => n.to_string(),
1868 serde_json::Value::Null => "null".to_string(),
1869 serde_json::Value::Array(arr) => {
1870 let items: Vec<String> = arr.iter().map(json_to_php).collect();
1871 format!("[{}]", items.join(", "))
1872 }
1873 serde_json::Value::Object(map) => {
1874 let items: Vec<String> = map
1875 .iter()
1876 .map(|(k, v)| format!("\"{}\" => {}", escape_php(k), json_to_php(v)))
1877 .collect();
1878 format!("[{}]", items.join(", "))
1879 }
1880 }
1881}
1882
1883fn json_to_php_camel_keys(value: &serde_json::Value) -> String {
1888 match value {
1889 serde_json::Value::Object(map) => {
1890 let items: Vec<String> = map
1891 .iter()
1892 .map(|(k, v)| {
1893 let camel_key = k.to_lower_camel_case();
1894 format!("\"{}\" => {}", escape_php(&camel_key), json_to_php_camel_keys(v))
1895 })
1896 .collect();
1897 format!("[{}]", items.join(", "))
1898 }
1899 serde_json::Value::Array(arr) => {
1900 let items: Vec<String> = arr.iter().map(json_to_php_camel_keys).collect();
1901 format!("[{}]", items.join(", "))
1902 }
1903 _ => json_to_php(value),
1904 }
1905}
1906
1907fn build_php_visitor(setup_lines: &mut Vec<String>, visitor_spec: &crate::fixture::VisitorSpec) {
1913 setup_lines.push("$visitor = new class {".to_string());
1914 for (method_name, action) in &visitor_spec.callbacks {
1915 emit_php_visitor_method(setup_lines, method_name, action);
1916 }
1917 setup_lines.push("};".to_string());
1918}
1919
1920fn emit_php_visitor_method(setup_lines: &mut Vec<String>, method_name: &str, action: &CallbackAction) {
1922 let params = match method_name {
1923 "visit_link" => "$ctx, $href, $text, $title",
1924 "visit_image" => "$ctx, $src, $alt, $title",
1925 "visit_heading" => "$ctx, $level, $text, $id",
1926 "visit_code_block" => "$ctx, $lang, $code",
1927 "visit_code_inline"
1928 | "visit_strong"
1929 | "visit_emphasis"
1930 | "visit_strikethrough"
1931 | "visit_underline"
1932 | "visit_subscript"
1933 | "visit_superscript"
1934 | "visit_mark"
1935 | "visit_button"
1936 | "visit_summary"
1937 | "visit_figcaption"
1938 | "visit_definition_term"
1939 | "visit_definition_description" => "$ctx, $text",
1940 "visit_text" => "$ctx, $text",
1941 "visit_list_item" => "$ctx, $ordered, $marker, $text",
1942 "visit_blockquote" => "$ctx, $content, $depth",
1943 "visit_table_row" => "$ctx, $cells, $isHeader",
1944 "visit_custom_element" => "$ctx, $tagName, $html",
1945 "visit_form" => "$ctx, $actionUrl, $method",
1946 "visit_input" => "$ctx, $input_type, $name, $value",
1947 "visit_audio" | "visit_video" | "visit_iframe" => "$ctx, $src",
1948 "visit_details" => "$ctx, $isOpen",
1949 "visit_element_end" | "visit_table_end" | "visit_definition_list_end" | "visit_figure_end" => "$ctx, $output",
1950 "visit_list_start" => "$ctx, $ordered",
1951 "visit_list_end" => "$ctx, $ordered, $output",
1952 _ => "$ctx",
1953 };
1954
1955 let (action_type, action_value, return_form) = match action {
1956 CallbackAction::Skip => ("skip", String::new(), "dict"),
1957 CallbackAction::Continue => ("continue", String::new(), "dict"),
1958 CallbackAction::PreserveHtml => ("preserve_html", String::new(), "dict"),
1959 CallbackAction::Custom { output } => ("custom", escape_php(output), "dict"),
1960 CallbackAction::CustomTemplate { template, return_form } => {
1961 let form = match return_form {
1962 TemplateReturnForm::Dict => "dict",
1963 TemplateReturnForm::BareString => "bare_string",
1964 };
1965 ("custom_template", escape_php(template), form)
1966 }
1967 };
1968
1969 let rendered = crate::template_env::render(
1970 "php/visitor_method.jinja",
1971 minijinja::context! {
1972 method_name => method_name,
1973 params => params,
1974 action_type => action_type,
1975 action_value => action_value,
1976 return_form => return_form,
1977 },
1978 );
1979 for line in rendered.lines() {
1980 setup_lines.push(line.to_string());
1981 }
1982}
1983
1984fn is_php_reserved_type(name: &str) -> bool {
1986 matches!(
1987 name.to_ascii_lowercase().as_str(),
1988 "string"
1989 | "int"
1990 | "integer"
1991 | "float"
1992 | "double"
1993 | "bool"
1994 | "boolean"
1995 | "array"
1996 | "object"
1997 | "null"
1998 | "void"
1999 | "callable"
2000 | "iterable"
2001 | "never"
2002 | "self"
2003 | "parent"
2004 | "static"
2005 | "true"
2006 | "false"
2007 | "mixed"
2008 )
2009}