1<?php
2
3namespace Sabre\VObject\Splitter;
4
5class VCardTest extends \PHPUnit_Framework_TestCase {
6
7    function createStream($data) {
8
9        $stream = fopen('php://memory', 'r+');
10        fwrite($stream, $data);
11        rewind($stream);
12        return $stream;
13
14    }
15
16    function testVCardImportValidVCard() {
17        $data = <<<EOT
18BEGIN:VCARD
19UID:foo
20END:VCARD
21EOT;
22        $tempFile = $this->createStream($data);
23
24        $objects = new VCard($tempFile);
25
26        $count = 0;
27        while ($objects->getNext()) {
28            $count++;
29        }
30        $this->assertEquals(1, $count);
31
32    }
33
34    /**
35     * @expectedException Sabre\VObject\ParseException
36     */
37    function testVCardImportWrongType() {
38        $event[] = <<<EOT
39BEGIN:VEVENT
40UID:foo1
41DTSTAMP:20140122T233226Z
42DTSTART:20140101T050000Z
43END:VEVENT
44EOT;
45
46$event[] = <<<EOT
47BEGIN:VEVENT
48UID:foo2
49DTSTAMP:20140122T233226Z
50DTSTART:20140101T060000Z
51END:VEVENT
52EOT;
53
54        $data = <<<EOT
55BEGIN:VCALENDAR
56$event[0]
57$event[1]
58END:VCALENDAR
59
60EOT;
61        $tempFile = $this->createStream($data);
62
63        $splitter = new VCard($tempFile);
64
65        while ($object = $splitter->getNext()) {
66        }
67
68    }
69
70    function testVCardImportValidVCardsWithCategories() {
71        $data = <<<EOT
72BEGIN:VCARD
73UID:card-in-foo1-and-foo2
74CATEGORIES:foo1,foo2
75END:VCARD
76BEGIN:VCARD
77UID:card-in-foo1
78CATEGORIES:foo1
79END:VCARD
80BEGIN:VCARD
81UID:card-in-foo3
82CATEGORIES:foo3
83END:VCARD
84BEGIN:VCARD
85UID:card-in-foo1-and-foo3
86CATEGORIES:foo1\,foo3
87END:VCARD
88EOT;
89        $tempFile = $this->createStream($data);
90
91        $splitter = new VCard($tempFile);
92
93        $count = 0;
94        while ($object = $splitter->getNext()) {
95            $count++;
96        }
97        $this->assertEquals(4, $count);
98
99    }
100
101    function testVCardImportEndOfData() {
102        $data = <<<EOT
103BEGIN:VCARD
104UID:foo
105END:VCARD
106EOT;
107        $tempFile = $this->createStream($data);
108
109        $objects = new VCard($tempFile);
110        $object = $objects->getNext();
111
112        $this->assertNull($objects->getNext());
113
114
115    }
116
117    /**
118     * @expectedException \Sabre\VObject\ParseException
119     */
120    function testVCardImportCheckInvalidArgumentException() {
121        $data = <<<EOT
122BEGIN:FOO
123END:FOO
124EOT;
125        $tempFile = $this->createStream($data);
126
127        $objects = new VCard($tempFile);
128        while ($objects->getNext()) { }
129
130    }
131
132    function testVCardImportMultipleValidVCards() {
133        $data = <<<EOT
134BEGIN:VCARD
135UID:foo
136END:VCARD
137BEGIN:VCARD
138UID:foo
139END:VCARD
140EOT;
141        $tempFile = $this->createStream($data);
142
143        $objects = new VCard($tempFile);
144
145        $count = 0;
146        while ($objects->getNext()) {
147            $count++;
148        }
149        $this->assertEquals(2, $count);
150
151    }
152
153    function testImportMultipleSeparatedWithNewLines() {
154        $data = <<<EOT
155BEGIN:VCARD
156UID:foo
157END:VCARD
158
159
160BEGIN:VCARD
161UID:foo
162END:VCARD
163
164
165EOT;
166        $tempFile = $this->createStream($data);
167        $objects = new VCard($tempFile);
168
169        $count = 0;
170        while ($objects->getNext()) {
171            $count++;
172        }
173        $this->assertEquals(2, $count);
174    }
175
176    function testVCardImportVCardWithoutUID() {
177        $data = <<<EOT
178BEGIN:VCARD
179END:VCARD
180EOT;
181        $tempFile = $this->createStream($data);
182
183        $objects = new VCard($tempFile);
184
185        $count = 0;
186        while ($objects->getNext()) {
187            $count++;
188        }
189
190        $this->assertEquals(1, $count);
191    }
192
193}
194