remote.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. /**
  3. * base include file for SimpleTest
  4. * @package SimpleTest
  5. * @subpackage UnitTester
  6. * @version $Id$
  7. */
  8. /**#@+
  9. * include other SimpleTest class files
  10. */
  11. require_once(dirname(__FILE__) . '/browser.php');
  12. require_once(dirname(__FILE__) . '/xml.php');
  13. require_once(dirname(__FILE__) . '/test_case.php');
  14. /**#@-*/
  15. /**
  16. * Runs an XML formated test on a remote server.
  17. * @package SimpleTest
  18. * @subpackage UnitTester
  19. */
  20. class RemoteTestCase {
  21. private $url;
  22. private $dry_url;
  23. private $size;
  24. /**
  25. * Sets the location of the remote test.
  26. * @param string $url Test location.
  27. * @param string $dry_url Location for dry run.
  28. * @access public
  29. */
  30. function __construct($url, $dry_url = false) {
  31. $this->url = $url;
  32. $this->dry_url = $dry_url ? $dry_url : $url;
  33. $this->size = false;
  34. }
  35. /**
  36. * Accessor for the test name for subclasses.
  37. * @return string Name of the test.
  38. * @access public
  39. */
  40. function getLabel() {
  41. return $this->url;
  42. }
  43. /**
  44. * Runs the top level test for this class. Currently
  45. * reads the data as a single chunk. I'll fix this
  46. * once I have added iteration to the browser.
  47. * @param SimpleReporter $reporter Target of test results.
  48. * @returns boolean True if no failures.
  49. * @access public
  50. */
  51. function run($reporter) {
  52. $browser = $this->createBrowser();
  53. $xml = $browser->get($this->url);
  54. if (! $xml) {
  55. trigger_error('Cannot read remote test URL [' . $this->url . ']');
  56. return false;
  57. }
  58. $parser = $this->createParser($reporter);
  59. if (! $parser->parse($xml)) {
  60. trigger_error('Cannot parse incoming XML from [' . $this->url . ']');
  61. return false;
  62. }
  63. return true;
  64. }
  65. /**
  66. * Creates a new web browser object for fetching
  67. * the XML report.
  68. * @return SimpleBrowser New browser.
  69. * @access protected
  70. */
  71. protected function createBrowser() {
  72. return new SimpleBrowser();
  73. }
  74. /**
  75. * Creates the XML parser.
  76. * @param SimpleReporter $reporter Target of test results.
  77. * @return SimpleTestXmlListener XML reader.
  78. * @access protected
  79. */
  80. protected function createParser($reporter) {
  81. return new SimpleTestXmlParser($reporter);
  82. }
  83. /**
  84. * Accessor for the number of subtests.
  85. * @return integer Number of test cases.
  86. * @access public
  87. */
  88. function getSize() {
  89. if ($this->size === false) {
  90. $browser = $this->createBrowser();
  91. $xml = $browser->get($this->dry_url);
  92. if (! $xml) {
  93. trigger_error('Cannot read remote test URL [' . $this->dry_url . ']');
  94. return false;
  95. }
  96. $reporter = new SimpleReporter();
  97. $parser = $this->createParser($reporter);
  98. if (! $parser->parse($xml)) {
  99. trigger_error('Cannot parse incoming XML from [' . $this->dry_url . ']');
  100. return false;
  101. }
  102. $this->size = $reporter->getTestCaseCount();
  103. }
  104. return $this->size;
  105. }
  106. }
  107. ?>