PageRenderTime 40ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/Tests/Auth/OpenID/Parse.php

http://github.com/openid/php-openid
PHP | 185 lines | 138 code | 35 blank | 12 comment | 18 complexity | 26f759c5c9985992ac147a73a6b80eeb MD5 | raw file
Possible License(s): Apache-2.0
  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 __construct($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 __construct($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 = [];
  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] = [$attr_optional, $v];
  110. }
  111. return [$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 = [];
  121. foreach ($lines as $line) {
  122. $parsed[] = $this->parseLink($line);
  123. }
  124. return [$desc, $markup, $parsed];
  125. }
  126. function parseTests($s)
  127. {
  128. $tests = [];
  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[] = [$desc, $markup, $links, $case];
  138. }
  139. return [$num_tests, $tests];
  140. }
  141. function __construct()
  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. }