PHPUnitAssertions.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. namespace Sabre\VObject;
  3. /**
  4. * PHPUnit Assertions.
  5. *
  6. * This trait can be added to your unittest to make it easier to test iCalendar
  7. * and/or vCards.
  8. *
  9. * @copyright Copyright (C) fruux GmbH (https://fruux.com/)
  10. * @author Evert Pot (http://evertpot.com/)
  11. * @license http://sabre.io/license/ Modified BSD License
  12. */
  13. trait PHPUnitAssertions
  14. {
  15. /**
  16. * This method tests whether two vcards or icalendar objects are
  17. * semantically identical.
  18. *
  19. * It supports objects being supplied as strings, streams or
  20. * Sabre\VObject\Component instances.
  21. *
  22. * PRODID is removed from both objects as this is often changes and would
  23. * just get in the way.
  24. *
  25. * CALSCALE will automatically get removed if it's set to GREGORIAN.
  26. *
  27. * Any property that has the value **ANY** will be treated as a wildcard.
  28. *
  29. * @param resource|string|Component $expected
  30. * @param resource|string|Component $actual
  31. * @param string $message
  32. */
  33. public function assertVObjectEqualsVObject($expected, $actual, $message = '')
  34. {
  35. $getObj = function ($input) {
  36. if (is_resource($input)) {
  37. $input = stream_get_contents($input);
  38. }
  39. if (is_string($input)) {
  40. $input = Reader::read($input);
  41. }
  42. if (!$input instanceof Component) {
  43. $this->fail('Input must be a string, stream or VObject component');
  44. }
  45. unset($input->PRODID);
  46. if ($input instanceof Component\VCalendar && 'GREGORIAN' === (string) $input->CALSCALE) {
  47. unset($input->CALSCALE);
  48. }
  49. return $input;
  50. };
  51. $expected = $getObj($expected)->serialize();
  52. $actual = $getObj($actual)->serialize();
  53. // Finding wildcards in expected.
  54. preg_match_all('|^([A-Z]+):\\*\\*ANY\\*\\*\r$|m', $expected, $matches, PREG_SET_ORDER);
  55. foreach ($matches as $match) {
  56. $actual = preg_replace(
  57. '|^'.preg_quote($match[1], '|').':(.*)\r$|m',
  58. $match[1].':**ANY**'."\r",
  59. $actual
  60. );
  61. }
  62. $this->assertEquals(
  63. $expected,
  64. $actual,
  65. $message
  66. );
  67. }
  68. }