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