/framework/Kolab_Filter/test/Horde/Kolab/Filter/StoryTestCase.php

https://github.com/ewandor/horde · PHP · 177 lines · 114 code · 8 blank · 55 comment · 0 complexity · 902b11ef0204660d8338b729803ad860 MD5 · raw file

  1. <?php
  2. /**
  3. * Base for scenario based testing of this package.
  4. *
  5. * PHP version 5
  6. *
  7. * @category Kolab
  8. * @package Kolab_Filter
  9. * @subpackage UnitTests
  10. * @author Gunnar Wrobel <wrobel@pardus.de>
  11. * @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
  12. * @link http://pear.horde.org/index.php?package=Kolab_Filter
  13. */
  14. /**
  15. * Base for scenario based testing of this package.
  16. *
  17. * Copyright 2010 Klarälvdalens Datakonsult AB
  18. *
  19. * See the enclosed file COPYING for license information (LGPL). If you
  20. * did not receive this file, see http://www.horde.org/licenses/lgpl21.
  21. *
  22. * @category Kolab
  23. * @package Kolab_Filter
  24. * @subpackage UnitTests
  25. * @author Gunnar Wrobel <wrobel@pardus.de>
  26. * @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
  27. * @link http://pear.horde.org/index.php?package=Kolab_Filter
  28. */
  29. class Horde_Kolab_Filter_StoryTestCase
  30. extends PHPUnit_Extensions_Story_TestCase
  31. {
  32. public function setUp()
  33. {
  34. $this->markTestSkipped('Kolab_Filter is not H4 compatible yet.');
  35. }
  36. /**
  37. * Handle a "given" step.
  38. *
  39. * @param array &$world Joined "world" of variables.
  40. * @param string $action The description of the step.
  41. * @param array $arguments Additional arguments to the step.
  42. *
  43. * @return mixed The outcome of the step.
  44. */
  45. public function runGiven(&$world, $action, $arguments)
  46. {
  47. switch($action) {
  48. case 'an incoming message on host':
  49. $world['hostname'] = $arguments[0];
  50. $world['type'] = 'Incoming';
  51. break;
  52. case 'the SMTP sender address is':
  53. $world['sender'] = $arguments[0];
  54. break;
  55. case 'the SMTP recipient address is':
  56. $world['recipient'] = $arguments[0];
  57. break;
  58. case 'the client address is':
  59. $world['client'] = $arguments[0];
  60. break;
  61. case 'the hostname is':
  62. $world['hostname'] = $arguments[0];
  63. break;
  64. case 'the unmodified message content is':
  65. $world['infile'] = $arguments[0];
  66. $world['fp'] = fopen($world['infile'], 'r');
  67. break;
  68. case 'the modified message template is':
  69. $world['infile'] = $arguments[0];
  70. $world['fp'] = fopen($world['infile'], 'r');
  71. stream_filter_register(
  72. 'addresses', 'Horde_Kolab_Filter_Helper_AddressFilter'
  73. );
  74. stream_filter_append(
  75. $world['fp'],
  76. 'addresses',
  77. STREAM_FILTER_READ,
  78. array(
  79. 'recipient' => $world['recipient'],
  80. 'sender' => $world['sender']
  81. )
  82. );
  83. break;
  84. default:
  85. return $this->notImplemented($action);
  86. }
  87. }
  88. /**
  89. * Handle a "when" step.
  90. *
  91. * @param array &$world Joined "world" of variables.
  92. * @param string $action The description of the step.
  93. * @param array $arguments Additional arguments to the step.
  94. *
  95. * @return mixed The outcome of the step.
  96. */
  97. public function runWhen(&$world, $action, $arguments)
  98. {
  99. switch($action) {
  100. case 'handling the message':
  101. global $conf;
  102. $conf['server']['mock'] = true;
  103. //@todo: Fix guid => dn here
  104. $conf['server']['data'] = array('dn=example' => array('dn' => 'dn=example', 'data' => array('mail' => array('me@example.org'), 'kolabHomeServer' => array('localhost'), 'objectClass' => array('kolabInetOrgPerson'), 'guid' => 'dn=example')));
  105. $_SERVER['argv'] = $this->_prepareArguments($world);
  106. $filter = new Horde_Kolab_Filter();
  107. ob_start();
  108. $result = $filter->main($world['type'], $world['fp'], 'echo');
  109. $world['output'] = ob_get_contents();
  110. ob_end_clean();
  111. break;
  112. default:
  113. return $this->notImplemented($action);
  114. }
  115. }
  116. /**
  117. * Handle a "then" step.
  118. *
  119. * @param array &$world Joined "world" of variables.
  120. * @param string $action The description of the step.
  121. * @param array $arguments Additional arguments to the step.
  122. *
  123. * @return mixed The outcome of the step.
  124. */
  125. public function runThen(&$world, $action, $arguments)
  126. {
  127. switch($action) {
  128. case 'the result will be the same as the content in':
  129. $out = file_get_contents($arguments[0]);
  130. $this->_cleanAndCompareOutput($out, $world['output']);
  131. break;
  132. default:
  133. return $this->notImplemented($action);
  134. }
  135. }
  136. private function _prepareArguments(&$world)
  137. {
  138. $recipient = isset($world['recipient']) ? $world['recipient'] : '';
  139. $sender = isset($world['sender']) ? $world['sender'] : '';
  140. $user = isset($world['user']) ? $world['user'] : '';
  141. $hostname = isset($world['hostname']) ? $world['hostname'] : '';
  142. $client = isset($world['client']) ? $world['client'] : '';
  143. return array(
  144. $_SERVER['argv'][0],
  145. '--sender=' . $sender,
  146. '--recipient=' . $recipient,
  147. '--user=' . $user,
  148. '--host=' . $hostname,
  149. '--client=' . $client
  150. );
  151. }
  152. private function _cleanAndCompareOutput($received, $expected)
  153. {
  154. $replace = array(
  155. '/^Received:.*$/m' => '',
  156. '/^Date:.*$/m' => '',
  157. '/DTSTAMP:.*$/m' => '',
  158. '/^--+=.*$/m' => '----',
  159. '/^Message-ID.*$/m' => '----',
  160. '/boundary=.*$/m' => '----',
  161. '/\s/' => '',
  162. );
  163. foreach ($replace as $pattern => $replacement) {
  164. $received = preg_replace($pattern, $replacement, $received);
  165. $expected = preg_replace($pattern, $replacement, $expected);
  166. }
  167. $this->assertEquals($received, $expected);
  168. }
  169. }