BrokerTester.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. namespace Sabre\VObject\ITip;
  3. use Sabre\VObject\Reader;
  4. /**
  5. * Utilities for testing the broker
  6. *
  7. * @copyright Copyright (C) fruux GmbH (https://fruux.com/)
  8. * @author Evert Pot (http://evertpot.com/)
  9. * @license http://sabre.io/license/ Modified BSD License
  10. */
  11. abstract class BrokerTester extends \Sabre\VObject\TestCase {
  12. function parse($oldMessage, $newMessage, $expected = array(), $currentUser = 'mailto:one@example.org') {
  13. $broker = new Broker();
  14. $result = $broker->parseEvent($newMessage, $currentUser, $oldMessage);
  15. $this->assertEquals(count($expected), count($result));
  16. foreach($expected as $index=>$ex) {
  17. $message = $result[$index];
  18. foreach($ex as $key=>$val) {
  19. if ($key==='message') {
  20. $this->assertVObjEquals(
  21. $val,
  22. $message->message->serialize()
  23. );
  24. } else {
  25. $this->assertEquals($val, $message->$key);
  26. }
  27. }
  28. }
  29. }
  30. function process($input, $existingObject = null, $expected = false) {
  31. $version = \Sabre\VObject\Version::VERSION;
  32. $vcal = Reader::read($input);
  33. foreach($vcal->getComponents() as $mainComponent) {
  34. break;
  35. }
  36. $message = new Message();
  37. $message->message = $vcal;
  38. $message->method = isset($vcal->METHOD)?$vcal->METHOD->getValue():null;
  39. $message->component = $mainComponent->name;
  40. $message->uid = $mainComponent->uid->getValue();
  41. $message->sequence = isset($vcal->VEVENT[0])?(string)$vcal->VEVENT[0]->SEQUENCE:null;
  42. if ($message->method === 'REPLY') {
  43. $message->sender = $mainComponent->ATTENDEE->getValue();
  44. $message->senderName = isset($mainComponent->ATTENDEE['CN'])?$mainComponent->ATTENDEE['CN']->getValue():null;
  45. $message->recipient = $mainComponent->ORGANIZER->getValue();
  46. $message->recipientName = isset($mainComponent->ORGANIZER['CN'])?$mainComponent->ORGANIZER['CN']:null;
  47. }
  48. $broker = new Broker();
  49. if (is_string($existingObject)) {
  50. $existingObject = str_replace(
  51. '%foo%',
  52. "VERSION:2.0\nPRODID:-//Sabre//Sabre VObject $version//EN\nCALSCALE:GREGORIAN",
  53. $existingObject
  54. );
  55. $existingObject = Reader::read($existingObject);
  56. }
  57. $result = $broker->processMessage($message, $existingObject);
  58. if (is_string($expected)) {
  59. $expected = str_replace(
  60. '%foo%',
  61. "VERSION:2.0\nPRODID:-//Sabre//Sabre VObject $version//EN\nCALSCALE:GREGORIAN",
  62. $expected
  63. );
  64. $expected = str_replace("\n", "\r\n", $expected);
  65. }
  66. if ($result instanceof \Sabre\VObject\Component\VCalendar) {
  67. $result = $result->serialize();
  68. $result = rtrim($result,"\r\n");
  69. }
  70. $this->assertEquals(
  71. $expected,
  72. $result
  73. );
  74. }
  75. }