123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650 |
- <?php
- namespace Sabre\VObject;
- /**
- * Tests the cli.
- *
- * Warning: these tests are very rudimentary.
- */
- class CliTest extends \PHPUnit_Framework_TestCase {
- public function setUp() {
- $this->cli = new CliMock();
- $this->cli->stderr = fopen('php://memory','r+');
- $this->cli->stdout = fopen('php://memory','r+');
- }
- public function testInvalidArg() {
- $this->assertEquals(
- 1,
- $this->cli->main(array('vobject', '--hi'))
- );
- rewind($this->cli->stderr);
- $this->assertTrue(strlen(stream_get_contents($this->cli->stderr)) > 100);
- }
- public function testQuiet() {
- $this->assertEquals(
- 1,
- $this->cli->main(array('vobject', '-q'))
- );
- $this->assertTrue($this->cli->quiet);
- rewind($this->cli->stderr);
- $this->assertEquals(0, strlen(stream_get_contents($this->cli->stderr)));
- }
- public function testHelp() {
- $this->assertEquals(
- 0,
- $this->cli->main(array('vobject', '-h'))
- );
- rewind($this->cli->stderr);
- $this->assertTrue(strlen(stream_get_contents($this->cli->stderr)) > 100);
- }
- public function testFormat() {
- $this->assertEquals(
- 1,
- $this->cli->main(array('vobject', '--format=jcard'))
- );
- rewind($this->cli->stderr);
- $this->assertTrue(strlen(stream_get_contents($this->cli->stderr)) > 100);
- $this->assertEquals('jcard', $this->cli->format);
- }
- public function testFormatInvalid() {
- $this->assertEquals(
- 1,
- $this->cli->main(array('vobject', '--format=foo'))
- );
- rewind($this->cli->stderr);
- $this->assertTrue(strlen(stream_get_contents($this->cli->stderr)) > 100);
- $this->assertNull($this->cli->format);
- }
- public function testInputFormatInvalid() {
- $this->assertEquals(
- 1,
- $this->cli->main(array('vobject', '--inputformat=foo'))
- );
- rewind($this->cli->stderr);
- $this->assertTrue(strlen(stream_get_contents($this->cli->stderr)) > 100);
- $this->assertNull($this->cli->format);
- }
- public function testNoInputFile() {
- $this->assertEquals(
- 1,
- $this->cli->main(array('vobject', 'color'))
- );
- rewind($this->cli->stderr);
- $this->assertTrue(strlen(stream_get_contents($this->cli->stderr)) > 100);
- }
- public function testTooManyArgs() {
- $this->assertEquals(
- 1,
- $this->cli->main(array('vobject', 'color', 'a', 'b', 'c'))
- );
- }
- public function testUnknownCommand() {
- $this->assertEquals(
- 1,
- $this->cli->main(array('vobject', 'foo', '-'))
- );
- }
- public function testConvertJson() {
- $inputStream = fopen('php://memory','r+');
- fwrite($inputStream, <<<ICS
- BEGIN:VCARD
- VERSION:3.0
- FN:Cowboy Henk
- END:VCARD
- ICS
- );
- rewind($inputStream);
- $this->cli->stdin = $inputStream;
- $this->assertEquals(
- 0,
- $this->cli->main(array('vobject', 'convert','--format=json', '-'))
- );
- rewind($this->cli->stdout);
- $version = Version::VERSION;
- $this->assertEquals(
- '["vcard",[["version",{},"text","4.0"],["prodid",{},"text","-\/\/Sabre\/\/Sabre VObject '. $version .'\/\/EN"],["fn",{},"text","Cowboy Henk"]]]',
- stream_get_contents($this->cli->stdout)
- );
- }
- public function testConvertJCardPretty() {
- if (version_compare(PHP_VERSION, '5.4.0') < 0) {
- $this->markTestSkipped('This test required PHP 5.4.0');
- }
- $inputStream = fopen('php://memory','r+');
- fwrite($inputStream, <<<ICS
- BEGIN:VCARD
- VERSION:3.0
- FN:Cowboy Henk
- END:VCARD
- ICS
- );
- rewind($inputStream);
- $this->cli->stdin = $inputStream;
- $this->assertEquals(
- 0,
- $this->cli->main(array('vobject', 'convert','--format=jcard', '--pretty', '-'))
- );
- rewind($this->cli->stdout);
- $version = Version::VERSION;
- // PHP 5.5.12 changed the output
- $expected = <<<JCARD
- [
- "vcard",
- [
- [
- "versi
- JCARD;
- $this->assertStringStartsWith(
- $expected,
- stream_get_contents($this->cli->stdout)
- );
- }
- public function testConvertJCalFail() {
- $inputStream = fopen('php://memory','r+');
- fwrite($inputStream, <<<ICS
- BEGIN:VCARD
- VERSION:3.0
- FN:Cowboy Henk
- END:VCARD
- ICS
- );
- rewind($inputStream);
- $this->cli->stdin = $inputStream;
- $this->assertEquals(
- 2,
- $this->cli->main(array('vobject', 'convert','--format=jcal', '--inputformat=mimedir', '-'))
- );
- }
- public function testConvertMimeDir() {
- $inputStream = fopen('php://memory','r+');
- fwrite($inputStream, <<<JCARD
- [
- "vcard",
- [
- [
- "version",
- {
- },
- "text",
- "4.0"
- ],
- [
- "prodid",
- {
- },
- "text",
- "-\/\/Sabre\/\/Sabre VObject 3.1.0\/\/EN"
- ],
- [
- "fn",
- {
- },
- "text",
- "Cowboy Henk"
- ]
- ]
- ]
- JCARD
- );
- rewind($inputStream);
- $this->cli->stdin = $inputStream;
- $this->assertEquals(
- 0,
- $this->cli->main(array('vobject', 'convert','--format=mimedir', '--inputformat=json', '--pretty', '-'))
- );
- rewind($this->cli->stdout);
- $expected = <<<VCF
- BEGIN:VCARD
- VERSION:4.0
- PRODID:-//Sabre//Sabre VObject 3.1.0//EN
- FN:Cowboy Henk
- END:VCARD
- VCF;
- $this->assertEquals(
- strtr($expected, array("\n"=>"\r\n")),
- stream_get_contents($this->cli->stdout)
- );
- }
- public function testConvertDefaultFormats() {
- $inputStream = fopen('php://memory','r+');
- $outputFile = SABRE_TEMPDIR . 'bar.json';
- $this->assertEquals(
- 2,
- $this->cli->main(array('vobject', 'convert','foo.json',$outputFile))
- );
- $this->assertEquals('json', $this->cli->inputFormat);
- $this->assertEquals('json', $this->cli->format);
- }
- public function testConvertDefaultFormats2() {
- $outputFile = SABRE_TEMPDIR . 'bar.ics';
- $this->assertEquals(
- 2,
- $this->cli->main(array('vobject', 'convert','foo.ics',$outputFile))
- );
- $this->assertEquals('mimedir', $this->cli->inputFormat);
- $this->assertEquals('mimedir', $this->cli->format);
- }
- public function testVCard3040() {
- $inputStream = fopen('php://memory','r+');
- fwrite($inputStream, <<<VCARD
- BEGIN:VCARD
- VERSION:3.0
- PRODID:-//Sabre//Sabre VObject 3.1.0//EN
- FN:Cowboy Henk
- END:VCARD
- VCARD
- );
- rewind($inputStream);
- $this->cli->stdin = $inputStream;
- $this->assertEquals(
- 0,
- $this->cli->main(array('vobject', 'convert','--format=vcard40', '--pretty', '-'))
- );
- rewind($this->cli->stdout);
- $version = Version::VERSION;
- $expected = <<<VCF
- BEGIN:VCARD
- VERSION:4.0
- PRODID:-//Sabre//Sabre VObject $version//EN
- FN:Cowboy Henk
- END:VCARD
- VCF;
- $this->assertEquals(
- strtr($expected, array("\n"=>"\r\n")),
- stream_get_contents($this->cli->stdout)
- );
- }
- public function testVCard4030() {
- $inputStream = fopen('php://memory','r+');
- fwrite($inputStream, <<<VCARD
- BEGIN:VCARD
- VERSION:4.0
- PRODID:-//Sabre//Sabre VObject 3.1.0//EN
- FN:Cowboy Henk
- END:VCARD
- VCARD
- );
- rewind($inputStream);
- $this->cli->stdin = $inputStream;
- $this->assertEquals(
- 0,
- $this->cli->main(array('vobject', 'convert','--format=vcard30', '--pretty', '-'))
- );
- $version = Version::VERSION;
- rewind($this->cli->stdout);
- $expected = <<<VCF
- BEGIN:VCARD
- VERSION:3.0
- PRODID:-//Sabre//Sabre VObject $version//EN
- FN:Cowboy Henk
- END:VCARD
- VCF;
- $this->assertEquals(
- strtr($expected, array("\n"=>"\r\n")),
- stream_get_contents($this->cli->stdout)
- );
- }
- public function testVCard4021() {
- $inputStream = fopen('php://memory','r+');
- fwrite($inputStream, <<<VCARD
- BEGIN:VCARD
- VERSION:4.0
- PRODID:-//Sabre//Sabre VObject 3.1.0//EN
- FN:Cowboy Henk
- END:VCARD
- VCARD
- );
- rewind($inputStream);
- $this->cli->stdin = $inputStream;
- // vCard 2.1 is not supported yet, so this returns a failure.
- $this->assertEquals(
- 2,
- $this->cli->main(array('vobject', 'convert','--format=vcard21', '--pretty', '-'))
- );
- }
- function testValidate() {
- $inputStream = fopen('php://memory','r+');
- fwrite($inputStream, <<<VCARD
- BEGIN:VCARD
- VERSION:4.0
- PRODID:-//Sabre//Sabre VObject 3.1.0//EN
- UID:foo
- FN:Cowboy Henk
- END:VCARD
- VCARD
- );
- rewind($inputStream);
- $this->cli->stdin = $inputStream;
- $result = $this->cli->main(array('vobject', 'validate', '-'));
- $this->assertEquals(
- 0,
- $result
- );
- }
- function testValidateFail() {
- $inputStream = fopen('php://memory','r+');
- fwrite($inputStream, <<<VCARD
- BEGIN:VCALENDAR
- VERSION:2.0
- END:VCARD
- VCARD
- );
- rewind($inputStream);
- $this->cli->stdin = $inputStream;
- // vCard 2.1 is not supported yet, so this returns a failure.
- $this->assertEquals(
- 2,
- $this->cli->main(array('vobject', 'validate', '-'))
- );
- }
- function testValidateFail2() {
- $inputStream = fopen('php://memory','r+');
- fwrite($inputStream, <<<VCARD
- BEGIN:VCALENDAR
- VERSION:5.0
- END:VCALENDAR
- VCARD
- );
- rewind($inputStream);
- $this->cli->stdin = $inputStream;
- // vCard 2.1 is not supported yet, so this returns a failure.
- $this->assertEquals(
- 2,
- $this->cli->main(array('vobject', 'validate', '-'))
- );
- }
- function testRepair() {
- $inputStream = fopen('php://memory','r+');
- fwrite($inputStream, <<<VCARD
- BEGIN:VCARD
- VERSION:5.0
- END:VCARD
- VCARD
- );
- rewind($inputStream);
- $this->cli->stdin = $inputStream;
- // vCard 2.1 is not supported yet, so this returns a failure.
- $this->assertEquals(
- 2,
- $this->cli->main(array('vobject', 'repair', '-'))
- );
- rewind($this->cli->stdout);
- $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));
- }
- function testRepairNothing() {
- $inputStream = fopen('php://memory','r+');
- fwrite($inputStream, <<<VCARD
- BEGIN:VCALENDAR
- VERSION:2.0
- PRODID:-//Sabre//Sabre VObject 3.1.0//EN
- BEGIN:VEVENT
- UID:foo
- DTSTAMP:20140122T233226Z
- DTSTART:20140101T120000Z
- END:VEVENT
- END:VCALENDAR
- VCARD
- );
- rewind($inputStream);
- $this->cli->stdin = $inputStream;
- // vCard 2.1 is not supported yet, so this returns a failure.
- $result = $this->cli->main(array('vobject', 'repair', '-'));
- rewind($this->cli->stderr);
- $error = stream_get_contents($this->cli->stderr);
- $this->assertEquals(
- 0,
- $result,
- "This should have been error free. stderr output:\n" . $error
- );
- }
- /**
- * Note: this is a very shallow test, doesn't dig into the actual output,
- * but just makes sure there's no errors thrown.
- *
- * The colorizer is not a critical component, it's mostly a debugging tool.
- */
- function testColorCalendar() {
- $inputStream = fopen('php://memory','r+');
- $version = Version::VERSION;
- /**
- * This object is not valid, but it's designed to hit every part of the
- * colorizer source.
- */
- fwrite($inputStream, <<<VCARD
- BEGIN:VCALENDAR
- VERSION:2.0
- PRODID:-//Sabre//Sabre VObject {$version}//EN
- BEGIN:VTIMEZONE
- END:VTIMEZONE
- BEGIN:VEVENT
- ATTENDEE;RSVP=TRUE:mailto:foo@example.org
- REQUEST-STATUS:5;foo
- ATTACH:blabla
- END:VEVENT
- END:VCALENDAR
- VCARD
- );
- rewind($inputStream);
- $this->cli->stdin = $inputStream;
- // vCard 2.1 is not supported yet, so this returns a failure.
- $result = $this->cli->main(array('vobject', 'color', '-'));
- rewind($this->cli->stderr);
- $error = stream_get_contents($this->cli->stderr);
- $this->assertEquals(
- 0,
- $result,
- "This should have been error free. stderr output:\n" . $error
- );
- }
- /**
- * Note: this is a very shallow test, doesn't dig into the actual output,
- * but just makes sure there's no errors thrown.
- *
- * The colorizer is not a critical component, it's mostly a debugging tool.
- */
- function testColorVCard() {
- $inputStream = fopen('php://memory','r+');
- $version = Version::VERSION;
- /**
- * This object is not valid, but it's designed to hit every part of the
- * colorizer source.
- */
- fwrite($inputStream, <<<VCARD
- BEGIN:VCARD
- VERSION:4.0
- PRODID:-//Sabre//Sabre VObject {$version}//EN
- ADR:1;2;3;4a,4b;5;6
- group.TEL:123454768
- END:VCARD
- VCARD
- );
- rewind($inputStream);
- $this->cli->stdin = $inputStream;
- // vCard 2.1 is not supported yet, so this returns a failure.
- $result = $this->cli->main(array('vobject', 'color', '-'));
- rewind($this->cli->stderr);
- $error = stream_get_contents($this->cli->stderr);
- $this->assertEquals(
- 0,
- $result,
- "This should have been error free. stderr output:\n" . $error
- );
- }
- }
- class CliMock extends Cli {
- public $log = array();
- public $quiet = false;
- public $format;
- public $pretty;
- public $stdin;
- public $stdout;
- public $stderr;
- public $inputFormat;
- public $outputFormat;
- }
|