PageRenderTime 45ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/Tests/CodeGen/ASTCreatorTest.php

https://github.com/krevels/pibx
PHP | 334 lines | 253 code | 49 blank | 32 comment | 0 complexity | d96ef77495972b4cba35cecbba38738b MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /**
  3. * Copyright (c) 2010, Christoph Gockel.
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without modification,
  7. * are permitted provided that the following conditions are met:
  8. *
  9. * * Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. * * Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. * * Neither the name of PiBX nor the names of its contributors may be used
  15. * to endorse or promote products derived from this software without specific
  16. * prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
  22. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  25. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. require_once dirname(__FILE__) . '/../bootstrap.php';
  30. require_once 'PHPUnit/Framework.php';
  31. require_once 'PiBX/CodeGen/ASTCreator.php';
  32. require_once 'PiBX/CodeGen/SchemaParser.php';
  33. /**
  34. * Description of ASTCreatorTest
  35. *
  36. * @author Christoph Gockel
  37. */
  38. class PiBX_CodeGen_ASTCreatorTest extends PHPUnit_Framework_TestCase {
  39. public function testSimpleTypeWithEnumeration() {
  40. $data = <<<XML
  41. <?xml version="1.0"?>
  42. <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  43. <xs:simpleType name="bookCategoryType">
  44. <xs:restriction base="string">
  45. <xs:enumeration value="magazine"/>
  46. <xs:enumeration value="novel"/>
  47. <xs:enumeration value="fiction"/>
  48. <xs:enumeration value="other"/>
  49. </xs:restriction>
  50. </xs:simpleType>
  51. </xs:schema>
  52. XML;
  53. $expectedType = new PiBX_AST_Type('bookCategoryType');
  54. $expectedType->setAsRoot();
  55. $expectedType->setNamespaces(array('xs' => 'http://www.w3.org/2001/XMLSchema'));
  56. $enumeration = new PiBX_AST_Enumeration();
  57. $enum = new PiBX_AST_EnumerationValue('magazine', 'string');
  58. $enumeration->add($enum);
  59. $enum = new PiBX_AST_EnumerationValue('novel', 'string');
  60. $enumeration->add($enum);
  61. $enum = new PiBX_AST_EnumerationValue('fiction', 'string');
  62. $enumeration->add($enum);
  63. $enum = new PiBX_AST_EnumerationValue('other', 'string');
  64. $enumeration->add($enum);
  65. $expectedType->add($enumeration);
  66. $schema = simplexml_load_string($data);
  67. $parser = new PiBX_CodeGen_SchemaParser();
  68. $parser->setSchema($schema);
  69. $tree = $parser->parse();
  70. $creator = new PiBX_CodeGen_ASTCreator(new PiBX_CodeGen_TypeUsage());
  71. $tree->accept($creator);
  72. $typeList = $creator->getTypeList();
  73. list($type) = $typeList;
  74. $this->assertEquals($expectedType, $type);
  75. }
  76. public function testComplexTypeWithUnboundedSequence() {
  77. $data = <<<XML
  78. <?xml version="1.0"?>
  79. <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  80. <xs:element name="Collection">
  81. <xs:complexType>
  82. <xs:sequence>
  83. <xs:element name ="books">
  84. <xs:complexType>
  85. <xs:sequence>
  86. <xs:element name="book" type="bookType" minOccurs="1" maxOccurs="unbounded"/>
  87. </xs:sequence>
  88. </xs:complexType>
  89. </xs:element>
  90. </xs:sequence>
  91. </xs:complexType>
  92. </xs:element>
  93. </xs:schema>
  94. XML;
  95. $expectedType = new PiBX_AST_Type('Collection');
  96. $expectedType->setAsRoot();
  97. $expectedType->setAttributeCount(1);
  98. $expectedType->setNamespaces(array('xs' => 'http://www.w3.org/2001/XMLSchema'));
  99. $ta = new PiBX_AST_TypeAttribute('books');
  100. $c = new PiBX_AST_Collection();
  101. $ci = new PiBX_AST_CollectionItem('book');
  102. $ci->setType('bookType');
  103. $expectedType->add($ta);
  104. $ta->add($c);
  105. $c->add($ci);
  106. $schema = simplexml_load_string($data);
  107. $parser = new PiBX_CodeGen_SchemaParser();
  108. $parser->setSchema($schema);
  109. $tree = $parser->parse();
  110. $creator = new PiBX_CodeGen_ASTCreator(new PiBX_CodeGen_TypeUsage());
  111. $tree->accept($creator);
  112. $typeList = $creator->getTypeList();
  113. list($type) = $typeList;
  114. $this->assertEquals($expectedType, $type);
  115. }
  116. public function testComplexTypeWithElements() {
  117. $data = <<<XML
  118. <?xml version="1.0"?>
  119. <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  120. <xs:complexType name="complexTypeWithElements">
  121. <xs:sequence>
  122. <xs:element name="element1" type="xs:string"/>
  123. <xs:element name="element2" type="xs:long"/>
  124. <xs:element name="element3" type="xs:string"/>
  125. <xs:element name="element4" type="xs:string" minOccurs="0"/>
  126. <xs:element name="element5" type="xs:date"/>
  127. </xs:sequence>
  128. </xs:complexType>
  129. </xs:schema>
  130. XML;
  131. $expectedType = new PiBX_AST_Type('complexTypeWithElements');
  132. $expectedType->setAsRoot();
  133. $expectedType->setAttributeCount(5);
  134. $expectedType->setNamespaces(array('xs' => 'http://www.w3.org/2001/XMLSchema'));
  135. $attr = new PiBX_AST_TypeAttribute('element1');
  136. $attr->setType('string');
  137. $expectedType->add($attr);
  138. $attr = new PiBX_AST_TypeAttribute('element2');
  139. $attr->setType('long');
  140. $expectedType->add($attr);
  141. $attr = new PiBX_AST_TypeAttribute('element3');
  142. $attr->setType('string');
  143. $expectedType->add($attr);
  144. $attr = new PiBX_AST_TypeAttribute('element4');
  145. $attr->setType('string');
  146. $expectedType->add($attr);
  147. $attr = new PiBX_AST_TypeAttribute('element5');
  148. $attr->setType('date');
  149. $expectedType->add($attr);
  150. $schema = simplexml_load_string($data);
  151. $parser = new PiBX_CodeGen_SchemaParser();
  152. $parser->setSchema($schema);
  153. $tree = $parser->parse();
  154. $creator = new PiBX_CodeGen_ASTCreator(new PiBX_CodeGen_TypeUsage());
  155. $tree->accept($creator);
  156. $typeList = $creator->getTypeList();
  157. list($type) = $typeList;
  158. $this->assertEquals($expectedType, $type);
  159. }
  160. public function testComplexTypeWithElementsAndSequence() {
  161. $data = <<<XML
  162. <?xml version="1.0"?>
  163. <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  164. <xs:complexType name="complexTypeWithElements">
  165. <xs:sequence>
  166. <xs:element name="element1" type="xs:string"/>
  167. <xs:element name="element2" type="xs:long"/>
  168. <xs:element name="element3" type="xs:string"/>
  169. <xs:element name="element4" type="xs:string" minOccurs="0"/>
  170. <xs:element name="elements" >
  171. <xs:complexType>
  172. <xs:sequence>
  173. <xs:element name="element" type="xs:string" minOccurs="1" maxOccurs="unbounded"/>
  174. </xs:sequence>
  175. </xs:complexType>
  176. </xs:element>
  177. <xs:element name="element6" type="xs:date"/>
  178. </xs:sequence>
  179. </xs:complexType>
  180. </xs:schema>
  181. XML;
  182. $expectedType = new PiBX_AST_Type('complexTypeWithElements');
  183. $expectedType->setAsRoot();
  184. $expectedType->setAttributeCount(6);
  185. $expectedType->setNamespaces(array('xs' => 'http://www.w3.org/2001/XMLSchema'));
  186. $attr = new PiBX_AST_TypeAttribute('element1');
  187. $attr->setType('string');
  188. $expectedType->add($attr);
  189. $attr = new PiBX_AST_TypeAttribute('element2');
  190. $attr->setType('long');
  191. $expectedType->add($attr);
  192. $attr = new PiBX_AST_TypeAttribute('element3');
  193. $attr->setType('string');
  194. $expectedType->add($attr);
  195. $attr = new PiBX_AST_TypeAttribute('element4');
  196. $attr->setType('string');
  197. $expectedType->add($attr);
  198. $attr = new PiBX_AST_TypeAttribute('elements');
  199. $collection = new PiBX_AST_Collection('');
  200. $collectionItem = new PiBX_AST_CollectionItem('element');
  201. $collectionItem->setType('string');
  202. $collection->add($collectionItem);
  203. $attr->add($collection);
  204. $expectedType->add($attr);
  205. $attr = new PiBX_AST_TypeAttribute('element6');
  206. $attr->setType('date');
  207. $expectedType->add($attr);
  208. $schema = simplexml_load_string($data);
  209. $parser = new PiBX_CodeGen_SchemaParser();
  210. $parser->setSchema($schema);
  211. $tree = $parser->parse();
  212. $creator = new PiBX_CodeGen_ASTCreator(new PiBX_CodeGen_TypeUsage());
  213. $tree->accept($creator);
  214. $typeList = $creator->getTypeList();
  215. list($type) = $typeList;
  216. $this->assertEquals($expectedType, $type);
  217. }
  218. public function testBooksExampleXSD() {
  219. $data = file_get_contents(dirname(__FILE__) . '/../_files/Books/books.xsd');
  220. $expectedTypeList = array();
  221. $expectedType1 = new PiBX_AST_Type('Collection');
  222. $expectedType1->setAsRoot();
  223. $expectedType1->setAttributeCount(1);
  224. $expectedType1->setNamespaces(array('xs' => 'http://www.w3.org/2001/XMLSchema'));
  225. $ta = new PiBX_AST_TypeAttribute('books');
  226. $c = new PiBX_AST_Collection();
  227. $ci = new PiBX_AST_CollectionItem('book');
  228. $ci->setType('bookType');
  229. $expectedType1->add($ta);
  230. $ta->add($c);
  231. $c->add($ci);
  232. $expectedTypeList[] = $expectedType1;
  233. $expectedType2 = new PiBX_AST_Type('bookType');
  234. $expectedType2->setAttributeCount(8);
  235. $expectedType2->add(new PiBX_AST_TypeAttribute('name', 'string'));
  236. $expectedType2->add(new PiBX_AST_TypeAttribute('ISBN', 'long'));
  237. $expectedType2->add(new PiBX_AST_TypeAttribute('price', 'string'));
  238. $ta = new PiBX_AST_TypeAttribute('authors');
  239. $c = new PiBX_AST_Collection();
  240. $ci = new PiBX_AST_CollectionItem('authorName', 'string');
  241. $c->add($ci);
  242. $ta->add($c);
  243. $expectedType2->add($ta);
  244. $expectedType2->add(new PiBX_AST_TypeAttribute('description', 'string'));
  245. $ta = new PiBX_AST_TypeAttribute('promotion');
  246. $s = new PiBX_AST_Structure();
  247. $s->setStructureType(PiBX_AST_StructureType::CHOICE());
  248. $s->add(new PiBX_AST_StructureElement('Discount', 'string'));
  249. $s->add(new PiBX_AST_StructureElement('None', 'string'));
  250. $ta->add($s);
  251. $expectedType2->add($ta);
  252. $expectedType2->add(new PiBX_AST_TypeAttribute('publicationDate', 'date'));
  253. $ta = new PiBX_AST_TypeAttribute('bookCategory');
  254. $e = new PiBX_AST_Enumeration();
  255. $e->add(new PiBX_AST_EnumerationValue('magazine', 'string'));
  256. $e->add($ev = new PiBX_AST_EnumerationValue('novel', 'string'));
  257. $e->add($ev = new PiBX_AST_EnumerationValue('fiction', 'string'));
  258. $e->add($ev = new PiBX_AST_EnumerationValue('other', 'string'));
  259. $ta->add($e);
  260. $expectedType2->add($ta);
  261. $ta = new PiBX_AST_TypeAttribute('itemId', 'string');
  262. $ta->setStyle('attribute');
  263. $expectedType2->add($ta);
  264. $expectedTypeList[] = $expectedType2;
  265. $enumeration = new PiBX_AST_Enumeration();
  266. $enumeration->add(new PiBX_AST_EnumerationValue('magazine', 'string'));
  267. $enumeration->add(new PiBX_AST_EnumerationValue('novel', 'string'));
  268. $enumeration->add(new PiBX_AST_EnumerationValue('fiction', 'string'));
  269. $enumeration->add(new PiBX_AST_EnumerationValue('other', 'string'));
  270. $expectedType3 = new PiBX_AST_Type('bookCategoryType');
  271. $expectedType3->add($enumeration);
  272. $expectedTypeList[] = $expectedType3;
  273. $schema = simplexml_load_string($data);
  274. $parser = new PiBX_CodeGen_SchemaParser();
  275. $parser->setSchema($schema);
  276. $tree = $parser->parse();
  277. $creator = new PiBX_CodeGen_ASTCreator(new PiBX_CodeGen_TypeUsage());
  278. $tree->accept($creator);
  279. $typeList = $creator->getTypeList();
  280. $this->assertEquals(3, count($typeList));
  281. $this->assertEquals($expectedType1, $typeList[0]);
  282. $this->assertEquals($expectedType2, $typeList[1]);
  283. $this->assertEquals($expectedType3, $typeList[2]);
  284. }
  285. }