Parse.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. class Tests_Auth_OpenID_Link extends PHPUnit_Framework_TestCase {
  17. function Tests_Auth_OpenID_Link($case)
  18. {
  19. list($desc, $markup, $links, $case_text) = $case;
  20. $this->desc = $desc;
  21. $this->markup = $markup;
  22. $this->expected_links = $links;
  23. $this->case_text = $case_text;
  24. $this->parser = new Auth_OpenID_Parse();
  25. }
  26. function getName()
  27. {
  28. return $this->desc;
  29. }
  30. function runTest()
  31. {
  32. $parsed = $this->parser->parseLinkAttrs($this->markup);
  33. $i = 0;
  34. foreach ($this->expected_links as $expected) {
  35. list($is_optional_link, $expected_link) = $expected;
  36. if ($is_optional_link &&
  37. ($i >= count($parsed))) {
  38. continue;
  39. }
  40. if (count($parsed) <= $i) {
  41. $i++;
  42. continue;
  43. }
  44. $act_link = $parsed[$i];
  45. $increment = true;
  46. foreach ($expected_link as $attr => $data) {
  47. list($is_optional_attr, $value) = $data;
  48. if ($is_optional_attr) {
  49. $actual_value = null;
  50. if (array_key_exists($attr, $act_link)) {
  51. $actual_value = $act_link[$attr];
  52. } else {
  53. continue;
  54. }
  55. } else {
  56. $actual_value = $act_link[$attr];
  57. }
  58. if ($is_optional_link &&
  59. ($value != $actual_value)) {
  60. $increment = false;
  61. break;
  62. }
  63. $this->assertEquals($value, $actual_value);
  64. }
  65. if ($increment) {
  66. $i++;
  67. }
  68. }
  69. $this->assertEquals($i, count($parsed));
  70. }
  71. }
  72. class NumTestCases extends PHPUnit_Framework_TestCase {
  73. function NumTestCases($test_cases, $num_tests)
  74. {
  75. $this->test_cases = $test_cases;
  76. $this->num_tests = $num_tests;
  77. }
  78. function runTest()
  79. {
  80. $this->assertEquals(count($this->test_cases),
  81. $this->num_tests);
  82. }
  83. }
  84. class Tests_Auth_OpenID_Parse extends PHPUnit_Framework_TestSuite {
  85. function getName()
  86. {
  87. return "Tests_Auth_OpenID_Parse";
  88. }
  89. function _parseCheck($cond, $where)
  90. {
  91. if (!$cond) {
  92. trigger_error('Parse error in ' . $where, E_USER_ERROR);
  93. }
  94. }
  95. function parseLink($line)
  96. {
  97. $parts = explode(" ", $line);
  98. $optional = intval($parts[0] == 'Link*:');
  99. $this->_parseCheck($optional || ($parts[0] == 'Link:'), __FUNCTION__);
  100. $attrs = array();
  101. foreach (array_slice($parts, 1) as $attr) {
  102. list($k, $v) = explode("=", $attr, 2);
  103. if ($k[strlen($k) - 1] == '*') {
  104. $attr_optional = 1;
  105. $k = substr($k, 0, strlen($k) - 1);
  106. } else {
  107. $attr_optional = 0;
  108. }
  109. $attrs[$k] = array($attr_optional, $v);
  110. }
  111. return array($optional, $attrs);
  112. }
  113. function parseCase($s)
  114. {
  115. list($header, $markup) = explode("\n\n", $s, 2);
  116. $lines = explode("\n", $header);
  117. $name = array_shift($lines);
  118. $this->_parseCheck(strpos($name, 'Name: ') == 0, __FUNCTION__);
  119. $desc = substr($name, 6);
  120. $parsed = array();
  121. foreach ($lines as $line) {
  122. $parsed[] = $this->parseLink($line);
  123. }
  124. return array($desc, $markup, $parsed);
  125. }
  126. function parseTests($s)
  127. {
  128. $tests = array();
  129. $cases = explode("\n\n\n", $s);
  130. $header = array_shift($cases);
  131. list($tests_line, $unused) = explode("\n", $header, 2);
  132. list($k, $v) = explode(": ", $tests_line);
  133. $this->_parseCheck(('Num Tests' == $k), __FUNCTION__);
  134. $num_tests = intval($v);
  135. foreach (array_slice($cases, 0, count($cases) - 1) as $case) {
  136. list($desc, $markup, $links) = $this->parseCase($case);
  137. $tests[] = array($desc, $markup, $links, $case);
  138. }
  139. return array($num_tests, $tests);
  140. }
  141. function Tests_Auth_OpenID_Parse()
  142. {
  143. $test_data = Tests_Auth_OpenID_readdata('linkparse.txt');
  144. list($num_tests, $test_cases) = $this->parseTests($test_data);
  145. $this->addTest(new NumTestCases($test_cases, $num_tests));
  146. foreach ($test_cases as $case) {
  147. $this->addTest(new Tests_Auth_OpenID_Link($case));
  148. }
  149. }
  150. }