1#!/usr/bin/perl -w
2package Pod::WSDL;
3use Test::More tests => 54;
4BEGIN {use_ok('Pod::WSDL');}
5use lib length $0 > 10 ? substr $0, 0, length($0) - 16 : '.';
6use strict;
7use warnings;
8use XML::XPath;
9
10my $tmpXP;
11
12# test standardtypes
13my @xsdTypes = qw(
14	anyType
15	anySimpleType
16	string
17	normalizedString
18	token
19	anyUri  
20	language
21	Name
22	QName
23	NCName
24	boolean
25	float
26	double
27	decimal
28	int
29	positiveInteger
30	nonPositiveInteger
31	negativeInteger
32	nonNegativeInteger
33	long
34	short
35	byte
36	unsignedInt
37	unsignedLong
38	unsignedShort
39	unsignedByte
40	duration
41	dateTime
42	time
43	date
44	gYearMonth
45	gYear
46	gMonthDay
47	gDay
48	gMonth
49	hexBinary
50	base64Binary
51);
52
53my $p = new Pod::WSDL(source => 'My::TypeTest',
54	               location => 'http://localhost/My/TypeTest',
55	               pretty => 1,
56	               withDocumentation => 1);
57
58my $xmlOutput = $p->WSDL;
59my $xp = XML::XPath->new(xml => $xmlOutput);
60
61my $foundMethod = 0;
62
63for my $m (@{$p->methods}) {
64	if ($m->name eq 'testXSDTypes') {
65		$foundMethod = 1;
66		for (0 .. @xsdTypes - 1) {
67			ok($m->params->[$_]->type eq $xsdTypes[$_], "Recognized xsd type '$xsdTypes[$_]' on method 'testXSDTypes' correctly")
68		}
69	}
70}
71
72fail('Did not find method testXSDTypes in package My::TypeTest') unless $foundMethod;
73
74# test own complex types
75$foundMethod = 0;
76for my $m (@{$p->methods}) {
77	if ($m->name eq 'testComplexTypes') {
78		$foundMethod = 1;
79		ok($m->params->[0]->type eq 'My::Foo', "Recognized own complex type 'My::Foo' on method 'testComplexTypes' correctly")
80	}
81}
82
83fail('Did not find method testComplexTypes in package My::TypeTest') unless $foundMethod;
84
85ok($xp->exists('/wsdl:definitions/wsdl:types/schema[@targetNamespace = "http://localhost/My/TypeTest"]'), 'Found schema with targetNamespace "http://localhost/My/TypeTest" in xml output.');
86ok($xp->exists('/wsdl:definitions/wsdl:types/schema/import[@namespace = "http://schemas.xmlsoap.org/soap/encoding/"]'), 'Found import of namespace "http://schemas.xmlsoap.org/soap/encoding/" in schema in xml output.');
87ok($xp->exists('/wsdl:definitions/wsdl:types/schema/complexType[@name = "MyFoo"]'), 'Found complex type MyFoo in xml output.');
88ok($xp->exists('/wsdl:definitions/wsdl:types/schema/complexType/sequence/element[@name = "_bar" and @type = "xsd:negativeInteger"]'), 'Found element with name "_bar" and type xsd:negativeInteger in complex type MyFoo in xml output.');
89ok($xp->exists('/wsdl:definitions/wsdl:types/schema/complexType/sequence/element/annotation/documentation[text() = "a bar"]'), 'Found documentation for element with name "_bar" in complex type MyFoo in xml output.');
90
91# test array types
92ok($xp->exists('/wsdl:definitions/wsdl:types/schema/complexType[@name = "ArrayOfString"]'), 'Found array type ArrayOfString in xml output.');
93ok($xp->exists('/wsdl:definitions/wsdl:types/schema/complexType[@name = "ArrayOfString"]/complexContent'), 'ArrayOfString has complexContent child.');
94ok($xp->exists('/wsdl:definitions/wsdl:types/schema/complexType[@name = "ArrayOfString"]/complexContent/restriction[@base = "soapenc:Array"]'), 'complexContent has restriction child with base="soapenc:Array".');
95ok($xp->exists('/wsdl:definitions/wsdl:types/schema/complexType[@name = "ArrayOfString"]/complexContent/restriction[@base = "soapenc:Array"]/attribute[@ref="soapenc:arrayType" and @wsdl:arrayType="soapenc:string[]"]'), 'restriction has attribute child with ref="soapenc:arrayType" and wsdl:arrayType="soapenc:string[].');
96
97ok($xp->exists('/wsdl:definitions/wsdl:types/schema/complexType[@name = "ArrayOfMyFoo"]'), 'Found array type ArrayOfMyFoo in xml output.');
98ok($xp->exists('/wsdl:definitions/wsdl:types/schema/complexType[@name = "ArrayOfMyFoo"]/complexContent'), 'ArrayOfMyFoo has complexContent child.');
99ok($xp->exists('/wsdl:definitions/wsdl:types/schema/complexType[@name = "ArrayOfMyFoo"]/complexContent/restriction[@base = "soapenc:Array"]'), 'complexContent has restriction child with base="soapenc:Array".');
100ok($xp->exists('/wsdl:definitions/wsdl:types/schema/complexType[@name = "ArrayOfMyFoo"]/complexContent/restriction[@base = "soapenc:Array"]/attribute[@ref="soapenc:arrayType" and @wsdl:arrayType="tns1:MyFoo[]"]'), 'restriction has attribute child with ref="soapenc:arrayType" and wsdl:arrayType="tns1:MyFoo[].');
101
102# test nillable attributes
103ok($xp->exists('/wsdl:definitions/wsdl:types/schema/complexType/sequence/element[@name = "_boerk" and @type = "xsd:boolean" and @nillable = "true"]'), 'Found nillable element with name "_boerk" in complex type MyFoo in xml output.');
104
105# test non existing types
106eval {
107$p = new Pod::WSDL(source => 'My::WrongTypeTest',
108	               location => 'http://localhost/My/WrongTypeTest',
109	               pretty => 1,
110	               withDocumentation => 1);
111};
112
113ok($@ =~ /Can't find any file 'Non::Existent::Type' and can't locate it as a module in \@INC either \(\@INC contains/, 'Pod::WSDL dies on encountering unknown type');
114
115__END__
116This is just to help making tests ...
117
118<wsdl:types>
119    <schema targetNamespace="http://localhost/My/TypeTest" xmlns="http://www.w3.org/2001/XMLSchema">
120        <import namespace="http://schemas.xmlsoap.org/soap/encoding/" />
121        <complexType name="ArrayOfString">
122                <complexContent>
123                        <restriction base="soapenc:Array">
124                                <attribute ref="soapenc:arrayType" wsdl:arrayType="soapenc:string[]" />
125                        </restriction>
126                </complexContent>
127        </complexType>
128        <complexType name="MyFoo">
129                <sequence>
130                        <element name="_bar" type="xsd:negativeInteger">
131                                <wsdl:documentation>a bar</wsdl:documentation>
132                        </element>
133                        <element name="_boerk" nillable="true" type="xsd:boolean">
134                                <wsdl:documentation>a nillable _boerk</wsdl:documentation>
135                        </element>
136                </sequence>
137        </complexType>
138        <complexType name="ArrayOfMyFoo">
139                <complexContent>
140                        <restriction base="soapenc:Array">
141                                <attribute ref="soapenc:arrayType" wsdl:arrayType="tns1:MyFoo[]" />
142                        </restriction>
143                </complexContent>
144        </complexType>
145    </schema>
146</wsdl:types>
147