RoundCube Webmail
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

151 lines
4.5 KiB

  1. <?php
  2. namespace Roundcube\Tests;
  3. use GuzzleHttp\Handler\MockHandler;
  4. use GuzzleHttp\HandlerStack;
  5. use GuzzleHttp\Psr7\Response;
  6. use Masterminds\HTML5;
  7. /*
  8. +-----------------------------------------------------------------------+
  9. | This file is part of the Roundcube Webmail client |
  10. | |
  11. | Copyright (C) The Roundcube Dev Team |
  12. | |
  13. | Licensed under the GNU General Public License version 3 or |
  14. | any later version with exceptions for skins & plugins. |
  15. | See the README file for a full license statement. |
  16. | |
  17. | PURPOSE: |
  18. | Environment initialization script for unit tests |
  19. +-----------------------------------------------------------------------+
  20. | Author: Thomas Bruederli <roundcube@gmail.com> |
  21. | Author: Aleksander Machniak <alec@alec.pl> |
  22. +-----------------------------------------------------------------------+
  23. */
  24. error_reporting(\E_ALL);
  25. if (\PHP_SAPI != 'cli') {
  26. exit('Not in shell mode (php-cli)');
  27. }
  28. if (!defined('INSTALL_PATH')) {
  29. define('INSTALL_PATH', realpath(__DIR__ . '/..') . '/');
  30. }
  31. define('ROUNDCUBE_TEST_MODE', true);
  32. define('ROUNDCUBE_TEST_SESSION', microtime(true));
  33. define('TESTS_DIR', __DIR__ . '/');
  34. if (@is_dir(TESTS_DIR . 'config')) {
  35. define('RCUBE_CONFIG_DIR', TESTS_DIR . 'config');
  36. }
  37. // Some tests depend on the way phpunit is executed
  38. $_SERVER['SCRIPT_NAME'] = 'vendor/bin/phpunit';
  39. require_once INSTALL_PATH . 'program/include/iniset.php';
  40. \rcmail::get_instance(0, 'test')->config->set('devel_mode', false);
  41. // Initialize database and environment
  42. ActionTestCase::init();
  43. /**
  44. * Call protected/private method of a object.
  45. *
  46. * @param object $object Object instance
  47. * @param string $method Method name to call
  48. * @param array $parameters array of parameters to pass into method
  49. * @param string $class Object class
  50. *
  51. * @return mixed method return
  52. */
  53. function invokeMethod($object, $method, array $parameters = [], $class = null)
  54. {
  55. $reflection = new \ReflectionClass($class ?: get_class($object));
  56. $method = $reflection->getMethod($method);
  57. $method->setAccessible(true);
  58. return $method->invokeArgs($object, $parameters);
  59. }
  60. /**
  61. * Get value of a protected/private property of a object.
  62. *
  63. * @param \rcube_sieve_vacation $object Object
  64. * @param string $name Property name
  65. * @param string $class Object class
  66. *
  67. * @return mixed Property value
  68. */
  69. function getProperty($object, $name, $class = null)
  70. {
  71. $reflection = new \ReflectionClass($class ?: get_class($object));
  72. $property = $reflection->getProperty($name);
  73. $property->setAccessible(true);
  74. return $property->getValue($object);
  75. }
  76. /**
  77. * Set protected/private property of a object.
  78. *
  79. * @param \rcube_sieve_vacation $object Object
  80. * @param string $name Property name
  81. * @param mixed $value Property value
  82. * @param string $class Object class
  83. */
  84. function setProperty($object, $name, $value, $class = null): void
  85. {
  86. $reflection = new \ReflectionClass($class ?: get_class($object));
  87. $property = $reflection->getProperty($name);
  88. $property->setAccessible(true);
  89. $property->setValue($object, $value);
  90. }
  91. /**
  92. * Parse HTML content and extract nodes by XPath query
  93. *
  94. * @param string $html HTML content
  95. * @param string $xpath_query XPath query
  96. *
  97. * @return \DOMNodeList List of nodes found
  98. */
  99. function getHTMLNodes($html, $xpath_query)
  100. {
  101. $html5 = new HTML5(['disable_html_ns' => true]);
  102. $doc = $html5->loadHTML($html);
  103. $xpath = new \DOMXPath($doc);
  104. return $xpath->query($xpath_query);
  105. }
  106. /**
  107. * Mock Guzzle HTTP Client
  108. */
  109. function setHttpClientMock(array $responses)
  110. {
  111. foreach ($responses as $idx => $response) {
  112. if (is_array($response)) {
  113. $responses[$idx] = new Response(
  114. $response['code'] ?? 200,
  115. $response['headers'] ?? [],
  116. $response['response'] ?? ''
  117. );
  118. }
  119. }
  120. $mock = new MockHandler($responses);
  121. $handler = HandlerStack::create($mock);
  122. $rcube = \rcube::get_instance();
  123. $rcube->config->set('http_client', ['handler' => $handler]);
  124. }