ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
CliTest.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Sabre\VObject;
4 
6 
12 class 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 
121  function testUnknownCommand() {
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
135 BEGIN:VCARD
136 VERSION:3.0
137 FN:Cowboy Henk
138 END:VCARD
139 ICS
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
167 BEGIN:VCARD
168 VERSION:3.0
169 FN:Cowboy Henk
170 END:VCARD
171 ICS
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
191 JCARD;
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
205 BEGIN:VCARD
206 VERSION:3.0
207 FN:Cowboy Henk
208 END:VCARD
209 ICS
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 ]
255 JCARD
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
267 BEGIN:VCARD
268 VERSION:4.0
269 PRODID:-//Sabre//Sabre VObject 3.1.0//EN
270 FN:Cowboy Henk
271 END:VCARD
272 
273 VCF;
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
315 BEGIN:VCARD
316 VERSION:3.0
317 PRODID:-//Sabre//Sabre VObject 3.1.0//EN
318 FN:Cowboy Henk
319 END:VCARD
320 
321 VCARD
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
335 BEGIN:VCARD
336 VERSION:4.0
337 PRODID:-//Sabre//Sabre VObject $version//EN
338 FN:Cowboy Henk
339 END:VCARD
340 
341 VCF;
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
355 BEGIN:VCARD
356 VERSION:4.0
357 PRODID:-//Sabre//Sabre VObject 3.1.0//EN
358 FN:Cowboy Henk
359 END:VCARD
360 
361 VCARD
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
375 BEGIN:VCARD
376 VERSION:3.0
377 PRODID:-//Sabre//Sabre VObject $version//EN
378 FN:Cowboy Henk
379 END:VCARD
380 
381 VCF;
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
395 BEGIN:VCARD
396 VERSION:4.0
397 PRODID:-//Sabre//Sabre VObject 3.1.0//EN
398 FN:Cowboy Henk
399 END:VCARD
400 
401 VCARD
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
418 BEGIN:VCARD
419 VERSION:4.0
420 PRODID:-//Sabre//Sabre VObject 3.1.0//EN
421 UID:foo
422 FN:Cowboy Henk
423 END:VCARD
424 
425 VCARD
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
443 BEGIN:VCALENDAR
444 VERSION:2.0
445 END:VCARD
446 
447 VCARD
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
464 BEGIN:VCALENDAR
465 VERSION:5.0
466 END:VCALENDAR
467 
468 VCARD
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
485 BEGIN:VCARD
486 VERSION:5.0
487 END:VCARD
489 VCARD
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
508 BEGIN:VCALENDAR
509 VERSION:2.0
510 PRODID:-//Sabre//Sabre VObject 3.1.0//EN
511 BEGIN:VEVENT
512 UID:foo
513 DTSTAMP:20140122T233226Z
514 DTSTART:20140101T120000Z
515 END:VEVENT
516 END:VCALENDAR
517 
518 VCARD
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
553 BEGIN:VCALENDAR
554 VERSION:2.0
555 PRODID:-//Sabre//Sabre VObject {$version}//EN
556 BEGIN:VTIMEZONE
557 END:VTIMEZONE
558 BEGIN:VEVENT
559 ATTENDEE;RSVP=TRUE:mailto:foo@example.org
560 REQUEST-STATUS:5;foo
561 ATTACH:blabla
562 END:VEVENT
563 END:VCALENDAR
564 
565 VCARD
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
600 BEGIN:VCARD
601 VERSION:4.0
602 PRODID:-//Sabre//Sabre VObject {$version}//EN
603 ADR:1;2;3;4a,4b;5;6
604 group.TEL:123454768
605 END:VCARD
606 
607 VCARD
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 
626 class 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 }
const VERSION
Full version number.
Definition: Version.php:17
if(! $in) print Initializing normalization quick check tables n
$version
Definition: build.php:27
foreach($paths as $path) duplicate contact merge tool n
Tests the cli.
Definition: CliTest.php:12