xref: /php-src/ext/soap/tests/gh15711.phpt (revision 73c4fa0e)
1--TEST--
2GH-15711 (SoapClient can't convert BackedEnum to scalar value)
3--EXTENSIONS--
4soap
5--INI--
6soap.wsdl_cache_enabled=0
7--FILE--
8<?php
9
10enum StringBackedEnum: string
11{
12    case First = 'BackingValue1';
13    case Second = 'BackingValue2';
14    case Third = 'BackingValue3';
15    case Fourth = 'BackingValue4';
16    case Fifth = 'BackingValue5';
17}
18
19enum IntBackedEnum: int
20{
21    case First = 1;
22    case Second = 2;
23}
24
25enum NonBackedEnum
26{
27    case First;
28}
29
30class TestSoapClient extends SoapClient {
31    function __doRequest($request, $location, $action, $version, $one_way = 0): ?string {
32        echo $request;
33    }
34}
35
36$client = new TestSoapClient(__DIR__ . '/gh15711.wsdl', ['classmap' => ['book' => 'book']]);
37
38echo "--- Test with backed enum ---\n";
39
40$book = new stdClass();
41$book->base64 = StringBackedEnum::First;
42$book->string = StringBackedEnum::Second;
43$book->any = StringBackedEnum::Third;
44$book->hexbin = StringBackedEnum::Fourth;
45$book->nmtokens = StringBackedEnum::Fifth;
46$book->integer = IntBackedEnum::First;
47$book->short = IntBackedEnum::Second;
48
49try {
50    $client->dotest($book);
51} catch (Throwable) {}
52
53echo "--- Test with non-backed enum ---\n";
54
55$book = new stdClass();
56$book->base64 = NonBackedEnum::First;
57$book->string = NonBackedEnum::First;
58$book->any = NonBackedEnum::First;
59$book->hexbin = NonBackedEnum::First;
60$book->nmtokens = NonBackedEnum::First;
61$book->integer = NonBackedEnum::First;
62$book->short = NonBackedEnum::First;
63
64try {
65    $client->dotest($book);
66} catch (ValueError $e) {
67    echo "ValueError: ", $e->getMessage(), "\n";
68}
69
70echo "--- Test with mismatched enum backing type ---\n";
71
72$book->integer = StringBackedEnum::First;
73$book->short = StringBackedEnum::First;
74try {
75    $client->dotest($book);
76} catch (ValueError $e) {
77    echo "ValueError: ", $e->getMessage(), "\n";
78}
79
80?>
81--EXPECT--
82--- Test with backed enum ---
83<?xml version="1.0" encoding="UTF-8"?>
84<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://schemas.nothing.com" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><SOAP-ENV:Body><ns1:dotest><dotestReturn xsi:type="ns1:book"><base64 xsi:type="xsd:base64Binary">QmFja2luZ1ZhbHVlMQ==</base64><string xsi:type="xsd:string">BackingValue2</string><any xsi:type="xsd:any"><name xsi:type="xsd:string">Third</name><value xsi:type="xsd:string">BackingValue3</value></any><hexbin xsi:type="xsd:hexBinary">4261636B696E6756616C756534</hexbin><nmtokens>BackingValue5</nmtokens><integer xsi:type="xsd:integer">1</integer><short xsi:type="xsd:short">2</short></dotestReturn></ns1:dotest></SOAP-ENV:Body></SOAP-ENV:Envelope>
85--- Test with non-backed enum ---
86ValueError: Non-backed enums have no default serialization
87--- Test with mismatched enum backing type ---
88ValueError: String-backed enum cannot be serialized as int
89