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