Parse.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php
  2. /**
  3. * Tests for the Consumer parsing functions.
  4. *
  5. * PHP versions 4 and 5
  6. *
  7. * LICENSE: See the COPYING file included in this distribution.
  8. *
  9. * @package OpenID
  10. * @author JanRain, Inc. <openid@janrain.com>
  11. * @copyright 2005-2008 Janrain, Inc.
  12. * @license http://www.apache.org/licenses/LICENSE-2.0 Apache
  13. */
  14. require_once 'Tests/Auth/OpenID/TestUtil.php';
  15. require_once 'Auth/OpenID/Parse.php';
  16. require_once 'PHPUnit.php';
  17. class Tests_Auth_OpenID_Link extends PHPUnit_TestCase {
  18. function Tests_Auth_OpenID_Link($case)
  19. {
  20. list($desc, $markup, $links, $case_text) = $case;
  21. $this->desc = $desc;
  22. $this->markup = $markup;
  23. $this->expected_links = $links;
  24. $this->case_text = $case_text;
  25. $this->parser = new Auth_OpenID_Parse();
  26. }
  27. function getName()
  28. {
  29. return $this->desc;
  30. }
  31. function runTest()
  32. {
  33. $parsed = $this->parser->parseLinkAttrs($this->markup);
  34. $i = 0;
  35. foreach ($this->expected_links as $expected) {
  36. list($is_optional_link, $expected_link) = $expected;
  37. if ($is_optional_link &&
  38. ($i >= count($parsed))) {
  39. continue;
  40. }
  41. if (count($parsed) <= $i) {
  42. $i++;
  43. continue;
  44. }
  45. $act_link = $parsed[$i];
  46. $increment = true;
  47. foreach ($expected_link as $attr => $data) {
  48. list($is_optional_attr, $value) = $data;
  49. if ($is_optional_attr) {
  50. $actual_value = null;
  51. if (array_key_exists($attr, $act_link)) {
  52. $actual_value = $act_link[$attr];
  53. } else {
  54. continue;
  55. }
  56. } else {
  57. $actual_value = $act_link[$attr];
  58. }
  59. if ($is_optional_link &&
  60. ($value != $actual_value)) {
  61. $increment = false;
  62. break;
  63. }
  64. $this->assertEquals($value, $actual_value);
  65. }
  66. if ($increment) {
  67. $i++;
  68. }
  69. }
  70. $this->assertEquals($i, count($parsed));
  71. }
  72. }
  73. class NumTestCases extends PHPUnit_TestCase {
  74. function NumTestCases($test_cases, $num_tests)
  75. {
  76. $this->test_cases = $test_cases;
  77. $this->num_tests = $num_tests;
  78. }
  79. function runTest()
  80. {
  81. $this->assertEquals(count($this->test_cases),
  82. $this->num_tests);
  83. }
  84. }
  85. class Tests_Auth_OpenID_Parse extends PHPUnit_TestSuite {
  86. function getName()
  87. {
  88. return "Tests_Auth_OpenID_Parse";
  89. }
  90. function _parseCheck($cond, $where)
  91. {
  92. if (!$cond) {
  93. trigger_error('Parse error in ' . $where, E_USER_ERROR);
  94. }
  95. }
  96. function parseLink($line)
  97. {
  98. $parts = explode(" ", $line);
  99. $optional = intval($parts[0] == 'Link*:');
  100. $this->_parseCheck($optional || ($parts[0] == 'Link:'), __FUNCTION__);
  101. $attrs = array();
  102. foreach (array_slice($parts, 1) as $attr) {
  103. list($k, $v) = explode("=", $attr, 2);
  104. if ($k[strlen($k) - 1] == '*') {
  105. $attr_optional = 1;
  106. $k = substr($k, 0, strlen($k) - 1);
  107. } else {
  108. $attr_optional = 0;
  109. }
  110. $attrs[$k] = array($attr_optional, $v);
  111. }
  112. return array($optional, $attrs);
  113. }
  114. function parseCase($s)
  115. {
  116. list($header, $markup) = explode("\n\n", $s, 2);
  117. $lines = explode("\n", $header);
  118. $name = array_shift($lines);
  119. $this->_parseCheck(strpos($name, 'Name: ') == 0, __FUNCTION__);
  120. $desc = substr($name, 6);
  121. $parsed = array();
  122. foreach ($lines as $line) {
  123. $parsed[] = $this->parseLink($line);
  124. }
  125. return array($desc, $markup, $parsed);
  126. }
  127. function parseTests($s)
  128. {
  129. $tests = array();
  130. $cases = explode("\n\n\n", $s);
  131. $header = array_shift($cases);
  132. list($tests_line, $unused) = explode("\n", $header, 2);
  133. list($k, $v) = explode(": ", $tests_line);
  134. $this->_parseCheck(('Num Tests' == $k), __FUNCTION__);
  135. $num_tests = intval($v);
  136. foreach (array_slice($cases, 0, count($cases) - 1) as $case) {
  137. list($desc, $markup, $links) = $this->parseCase($case);
  138. $tests[] = array($desc, $markup, $links, $case);
  139. }
  140. return array($num_tests, $tests);
  141. }
  142. function Tests_Auth_OpenID_Parse()
  143. {
  144. $test_data = Tests_Auth_OpenID_readdata('linkparse.txt');
  145. list($num_tests, $test_cases) = $this->parseTests($test_data);
  146. $this->addTest(new NumTestCases($test_cases, $num_tests));
  147. foreach ($test_cases as $case) {
  148. $this->addTest(new Tests_Auth_OpenID_Link($case));
  149. }
  150. }
  151. }
  152. ?>