ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
CliTest.php
Go to the documentation of this file.
1<?php
2
3namespace Sabre\VObject;
4
5use PHPUnit\Framework\TestCase;
6
12class CliTest extends TestCase {
13
14 function setUp() {
15
16 $this->cli = new CliMock();
17 $this->cli->stderr = fopen('php://memory', 'r+');
18 $this->cli->stdout = fopen('php://memory', 'r+');
19
20 }
21
22 function testInvalidArg() {
23
24 $this->assertEquals(
25 1,
26 $this->cli->main(['vobject', '--hi'])
27 );
28 rewind($this->cli->stderr);
29 $this->assertTrue(strlen(stream_get_contents($this->cli->stderr)) > 100);
30
31 }
32
33 function testQuiet() {
34
35 $this->assertEquals(
36 1,
37 $this->cli->main(['vobject', '-q'])
38 );
39 $this->assertTrue($this->cli->quiet);
40
41 rewind($this->cli->stderr);
42 $this->assertEquals(0, strlen(stream_get_contents($this->cli->stderr)));
43
44 }
45
46 function testHelp() {
47
48 $this->assertEquals(
49 0,
50 $this->cli->main(['vobject', '-h'])
51 );
52 rewind($this->cli->stderr);
53 $this->assertTrue(strlen(stream_get_contents($this->cli->stderr)) > 100);
54
55 }
56
57 function testFormat() {
58
59 $this->assertEquals(
60 1,
61 $this->cli->main(['vobject', '--format=jcard'])
62 );
63
64 rewind($this->cli->stderr);
65 $this->assertTrue(strlen(stream_get_contents($this->cli->stderr)) > 100);
66
67 $this->assertEquals('jcard', $this->cli->format);
68
69 }
70
71 function testFormatInvalid() {
72
73 $this->assertEquals(
74 1,
75 $this->cli->main(['vobject', '--format=foo'])
76 );
77
78 rewind($this->cli->stderr);
79 $this->assertTrue(strlen(stream_get_contents($this->cli->stderr)) > 100);
80
81 $this->assertNull($this->cli->format);
82
83 }
84
86
87 $this->assertEquals(
88 1,
89 $this->cli->main(['vobject', '--inputformat=foo'])
90 );
91
92 rewind($this->cli->stderr);
93 $this->assertTrue(strlen(stream_get_contents($this->cli->stderr)) > 100);
94
95 $this->assertNull($this->cli->format);
96
97 }
98
99
100 function testNoInputFile() {
101
102 $this->assertEquals(
103 1,
104 $this->cli->main(['vobject', 'color'])
105 );
106
107 rewind($this->cli->stderr);
108 $this->assertTrue(strlen(stream_get_contents($this->cli->stderr)) > 100);
109
110 }
111
112 function testTooManyArgs() {
113
114 $this->assertEquals(
115 1,
116 $this->cli->main(['vobject', 'color', 'a', 'b', 'c'])
117 );
118
119 }
120
122
123 $this->assertEquals(
124 1,
125 $this->cli->main(['vobject', 'foo', '-'])
126 );
127
128 }
129
130 function testConvertJson() {
131
132 $inputStream = fopen('php://memory', 'r+');
133
134 fwrite($inputStream, <<<ICS
135BEGIN:VCARD
136VERSION:3.0
137FN:Cowboy Henk
138END:VCARD
139ICS
140 );
141 rewind($inputStream);
142 $this->cli->stdin = $inputStream;
143
144 $this->assertEquals(
145 0,
146 $this->cli->main(['vobject', 'convert', '--format=json', '-'])
147 );
148
149 rewind($this->cli->stdout);
151 $this->assertEquals(
152 '["vcard",[["version",{},"text","4.0"],["prodid",{},"text","-\/\/Sabre\/\/Sabre VObject ' . $version . '\/\/EN"],["fn",{},"text","Cowboy Henk"]]]',
153 stream_get_contents($this->cli->stdout)
154 );
155
156 }
157
159
160 if (version_compare(PHP_VERSION, '5.4.0') < 0) {
161 $this->markTestSkipped('This test required PHP 5.4.0');
162 }
163
164 $inputStream = fopen('php://memory', 'r+');
165
166 fwrite($inputStream, <<<ICS
167BEGIN:VCARD
168VERSION:3.0
169FN:Cowboy Henk
170END:VCARD
171ICS
172 );
173 rewind($inputStream);
174 $this->cli->stdin = $inputStream;
175
176 $this->assertEquals(
177 0,
178 $this->cli->main(['vobject', 'convert', '--format=jcard', '--pretty', '-'])
179 );
180
181 rewind($this->cli->stdout);
182
183 // PHP 5.5.12 changed the output
184
185 $expected = <<<JCARD
186[
187 "vcard",
188 [
189 [
190 "versi
191JCARD;
192
193 $this->assertStringStartsWith(
194 $expected,
195 stream_get_contents($this->cli->stdout)
196 );
197
198 }
199
200 function testConvertJCalFail() {
201
202 $inputStream = fopen('php://memory', 'r+');
203
204 fwrite($inputStream, <<<ICS
205BEGIN:VCARD
206VERSION:3.0
207FN:Cowboy Henk
208END:VCARD
209ICS
210 );
211 rewind($inputStream);
212 $this->cli->stdin = $inputStream;
213
214 $this->assertEquals(
215 2,
216 $this->cli->main(['vobject', 'convert', '--format=jcal', '--inputformat=mimedir', '-'])
217 );
218
219 }
220
221 function testConvertMimeDir() {
222
223 $inputStream = fopen('php://memory', 'r+');
224
225 fwrite($inputStream, <<<JCARD
226[
227 "vcard",
228 [
229 [
230 "version",
231 {
232
233 },
234 "text",
235 "4.0"
236 ],
237 [
238 "prodid",
239 {
240
241 },
242 "text",
243 "-\/\/Sabre\/\/Sabre VObject 3.1.0\/\/EN"
244 ],
245 [
246 "fn",
247 {
248
249 },
250 "text",
251 "Cowboy Henk"
252 ]
253 ]
254]
255JCARD
256 );
257 rewind($inputStream);
258 $this->cli->stdin = $inputStream;
259
260 $this->assertEquals(
261 0,
262 $this->cli->main(['vobject', 'convert', '--format=mimedir', '--inputformat=json', '--pretty', '-'])
263 );
264
265 rewind($this->cli->stdout);
266 $expected = <<<VCF
267BEGIN:VCARD
268VERSION:4.0
269PRODID:-//Sabre//Sabre VObject 3.1.0//EN
270FN:Cowboy Henk
271END:VCARD
272
273VCF;
274
275 $this->assertEquals(
276 strtr($expected, ["\n" => "\r\n"]),
277 stream_get_contents($this->cli->stdout)
278 );
279
280 }
281
282 function testConvertDefaultFormats() {
283
284 $outputFile = SABRE_TEMPDIR . 'bar.json';
285
286 $this->assertEquals(
287 2,
288 $this->cli->main(['vobject', 'convert', 'foo.json', $outputFile])
289 );
290
291 $this->assertEquals('json', $this->cli->inputFormat);
292 $this->assertEquals('json', $this->cli->format);
293
294 }
295
296 function testConvertDefaultFormats2() {
297
298 $outputFile = SABRE_TEMPDIR . 'bar.ics';
299
300 $this->assertEquals(
301 2,
302 $this->cli->main(['vobject', 'convert', 'foo.ics', $outputFile])
303 );
304
305 $this->assertEquals('mimedir', $this->cli->inputFormat);
306 $this->assertEquals('mimedir', $this->cli->format);
307
308 }
309
310 function testVCard3040() {
311
312 $inputStream = fopen('php://memory', 'r+');
313
314 fwrite($inputStream, <<<VCARD
315BEGIN:VCARD
316VERSION:3.0
317PRODID:-//Sabre//Sabre VObject 3.1.0//EN
318FN:Cowboy Henk
319END:VCARD
320
321VCARD
322 );
323 rewind($inputStream);
324 $this->cli->stdin = $inputStream;
325
326 $this->assertEquals(
327 0,
328 $this->cli->main(['vobject', 'convert', '--format=vcard40', '--pretty', '-'])
329 );
330
331 rewind($this->cli->stdout);
332
333 $version = Version::VERSION;
334 $expected = <<<VCF
335BEGIN:VCARD
336VERSION:4.0
337PRODID:-//Sabre//Sabre VObject $version//EN
338FN:Cowboy Henk
339END:VCARD
340
341VCF;
342
343 $this->assertEquals(
344 strtr($expected, ["\n" => "\r\n"]),
345 stream_get_contents($this->cli->stdout)
346 );
347
348 }
349
350 function testVCard4030() {
351
352 $inputStream = fopen('php://memory', 'r+');
353
354 fwrite($inputStream, <<<VCARD
355BEGIN:VCARD
356VERSION:4.0
357PRODID:-//Sabre//Sabre VObject 3.1.0//EN
358FN:Cowboy Henk
359END:VCARD
360
361VCARD
362 );
363 rewind($inputStream);
364 $this->cli->stdin = $inputStream;
365
366 $this->assertEquals(
367 0,
368 $this->cli->main(['vobject', 'convert', '--format=vcard30', '--pretty', '-'])
369 );
370
371 $version = Version::VERSION;
372
373 rewind($this->cli->stdout);
374 $expected = <<<VCF
375BEGIN:VCARD
376VERSION:3.0
377PRODID:-//Sabre//Sabre VObject $version//EN
378FN:Cowboy Henk
379END:VCARD
380
381VCF;
382
383 $this->assertEquals(
384 strtr($expected, ["\n" => "\r\n"]),
385 stream_get_contents($this->cli->stdout)
386 );
387
388 }
389
390 function testVCard4021() {
391
392 $inputStream = fopen('php://memory', 'r+');
393
394 fwrite($inputStream, <<<VCARD
395BEGIN:VCARD
396VERSION:4.0
397PRODID:-//Sabre//Sabre VObject 3.1.0//EN
398FN:Cowboy Henk
399END:VCARD
400
401VCARD
402 );
403 rewind($inputStream);
404 $this->cli->stdin = $inputStream;
405
406 $this->assertEquals(
407 2,
408 $this->cli->main(['vobject', 'convert', '--format=vcard21', '--pretty', '-'])
409 );
410
411 }
412
413 function testValidate() {
414
415 $inputStream = fopen('php://memory', 'r+');
416
417 fwrite($inputStream, <<<VCARD
418BEGIN:VCARD
419VERSION:4.0
420PRODID:-//Sabre//Sabre VObject 3.1.0//EN
421UID:foo
422FN:Cowboy Henk
423END:VCARD
424
425VCARD
426 );
427 rewind($inputStream);
428 $this->cli->stdin = $inputStream;
429 $result = $this->cli->main(['vobject', 'validate', '-']);
430
431 $this->assertEquals(
432 0,
433 $result
434 );
435
436 }
437
438 function testValidateFail() {
439
440 $inputStream = fopen('php://memory', 'r+');
441
442 fwrite($inputStream, <<<VCARD
443BEGIN:VCALENDAR
444VERSION:2.0
445END:VCARD
446
447VCARD
448 );
449 rewind($inputStream);
450 $this->cli->stdin = $inputStream;
451 // vCard 2.0 is not supported yet, so this returns a failure.
452 $this->assertEquals(
453 2,
454 $this->cli->main(['vobject', 'validate', '-'])
455 );
456
457 }
458
459 function testValidateFail2() {
460
461 $inputStream = fopen('php://memory', 'r+');
462
463 fwrite($inputStream, <<<VCARD
464BEGIN:VCALENDAR
465VERSION:5.0
466END:VCALENDAR
467
468VCARD
469 );
470 rewind($inputStream);
471 $this->cli->stdin = $inputStream;
472
473 $this->assertEquals(
474 2,
475 $this->cli->main(['vobject', 'validate', '-'])
476 );
477
478 }
479
480 function testRepair() {
481
482 $inputStream = fopen('php://memory', 'r+');
483
484 fwrite($inputStream, <<<VCARD
485BEGIN:VCARD
486VERSION:5.0
487END:VCARD
488
489VCARD
490 );
491 rewind($inputStream);
492 $this->cli->stdin = $inputStream;
493
494 $this->assertEquals(
495 2,
496 $this->cli->main(['vobject', 'repair', '-'])
497 );
498
499 rewind($this->cli->stdout);
500 $this->assertRegExp("/^BEGIN:VCARD\r\nVERSION:2.1\r\nUID:[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\r\nEND:VCARD\r\n$/", stream_get_contents($this->cli->stdout));
501 }
502
503 function testRepairNothing() {
504
505 $inputStream = fopen('php://memory', 'r+');
506
507 fwrite($inputStream, <<<VCARD
508BEGIN:VCALENDAR
509VERSION:2.0
510PRODID:-//Sabre//Sabre VObject 3.1.0//EN
511BEGIN:VEVENT
512UID:foo
513DTSTAMP:20140122T233226Z
514DTSTART:20140101T120000Z
515END:VEVENT
516END:VCALENDAR
517
518VCARD
519 );
520 rewind($inputStream);
521 $this->cli->stdin = $inputStream;
522
523 $result = $this->cli->main(['vobject', 'repair', '-']);
524
525 rewind($this->cli->stderr);
526 $error = stream_get_contents($this->cli->stderr);
527
528 $this->assertEquals(
529 0,
530 $result,
531 "This should have been error free. stderr output:\n" . $error
532 );
533
534 }
535
542 function testColorCalendar() {
543
544 $inputStream = fopen('php://memory', 'r+');
545
546 $version = Version::VERSION;
547
552 fwrite($inputStream, <<<VCARD
553BEGIN:VCALENDAR
554VERSION:2.0
555PRODID:-//Sabre//Sabre VObject {$version}//EN
556BEGIN:VTIMEZONE
557END:VTIMEZONE
558BEGIN:VEVENT
559ATTENDEE;RSVP=TRUE:mailto:foo@example.org
560REQUEST-STATUS:5;foo
561ATTACH:blabla
562END:VEVENT
563END:VCALENDAR
564
565VCARD
566 );
567 rewind($inputStream);
568 $this->cli->stdin = $inputStream;
569
570 $result = $this->cli->main(['vobject', 'color', '-']);
571
572 rewind($this->cli->stderr);
573 $error = stream_get_contents($this->cli->stderr);
574
575 $this->assertEquals(
576 0,
577 $result,
578 "This should have been error free. stderr output:\n" . $error
579 );
580
581 }
582
589 function testColorVCard() {
590
591 $inputStream = fopen('php://memory', 'r+');
592
593 $version = Version::VERSION;
594
599 fwrite($inputStream, <<<VCARD
600BEGIN:VCARD
601VERSION:4.0
602PRODID:-//Sabre//Sabre VObject {$version}//EN
603ADR:1;2;3;4a,4b;5;6
604group.TEL:123454768
605END:VCARD
606
607VCARD
608 );
609 rewind($inputStream);
610 $this->cli->stdin = $inputStream;
611
612 $result = $this->cli->main(['vobject', 'color', '-']);
613
614 rewind($this->cli->stderr);
615 $error = stream_get_contents($this->cli->stderr);
616
617 $this->assertEquals(
618 0,
619 $result,
620 "This should have been error free. stderr output:\n" . $error
621 );
622
623 }
624}
625
626class CliMock extends Cli {
627
628 public $quiet = false;
629
630 public $format;
631
632 public $pretty;
633
634 public $stdin;
635
636 public $stdout;
637
638 public $stderr;
639
640 public $inputFormat;
641
642 public $outputFormat;
643
644}
$version
Definition: build.php:27
An exception for terminatinating execution or to throw for unit testing.
Tests the cli.
Definition: CliTest.php:12
const VERSION
Full version number.
Definition: Version.php:17
error($a_errmsg)
set error message @access public