xml_test.php 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php
  2. // $Id: xml_test.php 1787 2008-04-26 20:35:39Z pp11 $
  3. require_once(dirname(__FILE__) . '/../autorun.php');
  4. require_once(dirname(__FILE__) . '/../xml.php');
  5. Mock::generate('SimpleScorer');
  6. if (! function_exists('xml_parser_create')) {
  7. SimpleTest::ignore('TestOfXmlStructureParsing');
  8. SimpleTest::ignore('TestOfXmlResultsParsing');
  9. }
  10. class TestOfNestingTags extends UnitTestCase {
  11. function testGroupSize() {
  12. $nesting = new NestingGroupTag(array('SIZE' => 2));
  13. $this->assertEqual($nesting->getSize(), 2);
  14. }
  15. }
  16. class TestOfXmlStructureParsing extends UnitTestCase {
  17. function testValidXml() {
  18. $listener = new MockSimpleScorer();
  19. $listener->expectNever('paintGroupStart');
  20. $listener->expectNever('paintGroupEnd');
  21. $listener->expectNever('paintCaseStart');
  22. $listener->expectNever('paintCaseEnd');
  23. $parser = new SimpleTestXmlParser($listener);
  24. $this->assertTrue($parser->parse("<?xml version=\"1.0\"?>\n"));
  25. $this->assertTrue($parser->parse("<run>\n"));
  26. $this->assertTrue($parser->parse("</run>\n"));
  27. }
  28. function testEmptyGroup() {
  29. $listener = new MockSimpleScorer();
  30. $listener->expectOnce('paintGroupStart', array('a_group', 7));
  31. $listener->expectOnce('paintGroupEnd', array('a_group'));
  32. $parser = new SimpleTestXmlParser($listener);
  33. $parser->parse("<?xml version=\"1.0\"?>\n");
  34. $parser->parse("<run>\n");
  35. $this->assertTrue($parser->parse("<group size=\"7\">\n"));
  36. $this->assertTrue($parser->parse("<name>a_group</name>\n"));
  37. $this->assertTrue($parser->parse("</group>\n"));
  38. $parser->parse("</run>\n");
  39. }
  40. function testEmptyCase() {
  41. $listener = new MockSimpleScorer();
  42. $listener->expectOnce('paintCaseStart', array('a_case'));
  43. $listener->expectOnce('paintCaseEnd', array('a_case'));
  44. $parser = new SimpleTestXmlParser($listener);
  45. $parser->parse("<?xml version=\"1.0\"?>\n");
  46. $parser->parse("<run>\n");
  47. $this->assertTrue($parser->parse("<case>\n"));
  48. $this->assertTrue($parser->parse("<name>a_case</name>\n"));
  49. $this->assertTrue($parser->parse("</case>\n"));
  50. $parser->parse("</run>\n");
  51. }
  52. function testEmptyMethod() {
  53. $listener = new MockSimpleScorer();
  54. $listener->expectOnce('paintCaseStart', array('a_case'));
  55. $listener->expectOnce('paintCaseEnd', array('a_case'));
  56. $listener->expectOnce('paintMethodStart', array('a_method'));
  57. $listener->expectOnce('paintMethodEnd', array('a_method'));
  58. $parser = new SimpleTestXmlParser($listener);
  59. $parser->parse("<?xml version=\"1.0\"?>\n");
  60. $parser->parse("<run>\n");
  61. $parser->parse("<case>\n");
  62. $parser->parse("<name>a_case</name>\n");
  63. $this->assertTrue($parser->parse("<test>\n"));
  64. $this->assertTrue($parser->parse("<name>a_method</name>\n"));
  65. $this->assertTrue($parser->parse("</test>\n"));
  66. $parser->parse("</case>\n");
  67. $parser->parse("</run>\n");
  68. }
  69. function testNestedGroup() {
  70. $listener = new MockSimpleScorer();
  71. $listener->expectAt(0, 'paintGroupStart', array('a_group', 7));
  72. $listener->expectAt(1, 'paintGroupStart', array('b_group', 3));
  73. $listener->expectCallCount('paintGroupStart', 2);
  74. $listener->expectAt(0, 'paintGroupEnd', array('b_group'));
  75. $listener->expectAt(1, 'paintGroupEnd', array('a_group'));
  76. $listener->expectCallCount('paintGroupEnd', 2);
  77. $parser = new SimpleTestXmlParser($listener);
  78. $parser->parse("<?xml version=\"1.0\"?>\n");
  79. $parser->parse("<run>\n");
  80. $this->assertTrue($parser->parse("<group size=\"7\">\n"));
  81. $this->assertTrue($parser->parse("<name>a_group</name>\n"));
  82. $this->assertTrue($parser->parse("<group size=\"3\">\n"));
  83. $this->assertTrue($parser->parse("<name>b_group</name>\n"));
  84. $this->assertTrue($parser->parse("</group>\n"));
  85. $this->assertTrue($parser->parse("</group>\n"));
  86. $parser->parse("</run>\n");
  87. }
  88. }
  89. class AnyOldSignal {
  90. public $stuff = true;
  91. }
  92. class TestOfXmlResultsParsing extends UnitTestCase {
  93. function sendValidStart(&$parser) {
  94. $parser->parse("<?xml version=\"1.0\"?>\n");
  95. $parser->parse("<run>\n");
  96. $parser->parse("<case>\n");
  97. $parser->parse("<name>a_case</name>\n");
  98. $parser->parse("<test>\n");
  99. $parser->parse("<name>a_method</name>\n");
  100. }
  101. function sendValidEnd(&$parser) {
  102. $parser->parse("</test>\n");
  103. $parser->parse("</case>\n");
  104. $parser->parse("</run>\n");
  105. }
  106. function testPass() {
  107. $listener = new MockSimpleScorer();
  108. $listener->expectOnce('paintPass', array('a_message'));
  109. $parser = new SimpleTestXmlParser($listener);
  110. $this->sendValidStart($parser);
  111. $this->assertTrue($parser->parse("<pass>a_message</pass>\n"));
  112. $this->sendValidEnd($parser);
  113. }
  114. function testFail() {
  115. $listener = new MockSimpleScorer();
  116. $listener->expectOnce('paintFail', array('a_message'));
  117. $parser = new SimpleTestXmlParser($listener);
  118. $this->sendValidStart($parser);
  119. $this->assertTrue($parser->parse("<fail>a_message</fail>\n"));
  120. $this->sendValidEnd($parser);
  121. }
  122. function testException() {
  123. $listener = new MockSimpleScorer();
  124. $listener->expectOnce('paintError', array('a_message'));
  125. $parser = new SimpleTestXmlParser($listener);
  126. $this->sendValidStart($parser);
  127. $this->assertTrue($parser->parse("<exception>a_message</exception>\n"));
  128. $this->sendValidEnd($parser);
  129. }
  130. function testSkip() {
  131. $listener = new MockSimpleScorer();
  132. $listener->expectOnce('paintSkip', array('a_message'));
  133. $parser = new SimpleTestXmlParser($listener);
  134. $this->sendValidStart($parser);
  135. $this->assertTrue($parser->parse("<skip>a_message</skip>\n"));
  136. $this->sendValidEnd($parser);
  137. }
  138. function testSignal() {
  139. $signal = new AnyOldSignal();
  140. $signal->stuff = "Hello";
  141. $listener = new MockSimpleScorer();
  142. $listener->expectOnce('paintSignal', array('a_signal', $signal));
  143. $parser = new SimpleTestXmlParser($listener);
  144. $this->sendValidStart($parser);
  145. $this->assertTrue($parser->parse(
  146. "<signal type=\"a_signal\"><![CDATA[" .
  147. serialize($signal) . "]]></signal>\n"));
  148. $this->sendValidEnd($parser);
  149. }
  150. function testMessage() {
  151. $listener = new MockSimpleScorer();
  152. $listener->expectOnce('paintMessage', array('a_message'));
  153. $parser = new SimpleTestXmlParser($listener);
  154. $this->sendValidStart($parser);
  155. $this->assertTrue($parser->parse("<message>a_message</message>\n"));
  156. $this->sendValidEnd($parser);
  157. }
  158. function testFormattedMessage() {
  159. $listener = new MockSimpleScorer();
  160. $listener->expectOnce('paintFormattedMessage', array("\na\tmessage\n"));
  161. $parser = new SimpleTestXmlParser($listener);
  162. $this->sendValidStart($parser);
  163. $this->assertTrue($parser->parse("<formatted><![CDATA[\na\tmessage\n]]></formatted>\n"));
  164. $this->sendValidEnd($parser);
  165. }
  166. }
  167. ?>