TestUtil.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <?php
  2. /**
  3. * Utilites for test functions
  4. */
  5. require_once 'PHPUnit.php';
  6. function Tests_Auth_OpenID_datafile($name, $reader)
  7. {
  8. $path = dirname(realpath(__FILE__));
  9. $sep = DIRECTORY_SEPARATOR;
  10. $filename = $path . $sep . 'data' . $sep . $name;
  11. $data = $reader($filename);
  12. if ($data === false) {
  13. $msg = "Failed to open data file: $name";
  14. trigger_error($msg, E_USER_ERROR);
  15. }
  16. return $data;
  17. }
  18. function Tests_Auth_OpenID_readdata($name)
  19. {
  20. return Tests_Auth_OpenID_datafile($name, 'file_get_contents');
  21. }
  22. function Tests_Auth_OpenID_readlines($name)
  23. {
  24. return Tests_Auth_OpenID_datafile($name, 'file');
  25. }
  26. class OpenIDTestMixin extends PHPUnit_TestCase {
  27. function failUnlessOpenIDValueEquals($msg, $key, $expected, $ns=null)
  28. {
  29. if ($ns === null) {
  30. $ns = Auth_OpenID_OPENID_NS;
  31. }
  32. $actual = $msg->getArg($ns, $key);
  33. $error_format = 'Wrong value for openid.%s: expected=%s, actual=%s';
  34. $error_message = sprintf($error_format,
  35. $key, $expected, $actual);
  36. $this->assertEquals($expected, $actual, $error_message);
  37. }
  38. function failIfOpenIDKeyExists($msg, $key, $ns=null)
  39. {
  40. if ($ns === null) {
  41. $ns = Auth_OpenID_OPENID_NS;
  42. }
  43. $actual = $msg->getArg($ns, $key);
  44. $error_message = sprintf('openid.%s unexpectedly present: %s',
  45. $key, $actual);
  46. $this->assertFalse($msg->hasKey($ns, $key),
  47. $error_message);
  48. }
  49. }
  50. ?>