CliTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650
  1. <?php
  2. namespace Sabre\VObject;
  3. /**
  4. * Tests the cli.
  5. *
  6. * Warning: these tests are very rudimentary.
  7. */
  8. class CliTest extends \PHPUnit_Framework_TestCase {
  9. public function setUp() {
  10. $this->cli = new CliMock();
  11. $this->cli->stderr = fopen('php://memory','r+');
  12. $this->cli->stdout = fopen('php://memory','r+');
  13. }
  14. public function testInvalidArg() {
  15. $this->assertEquals(
  16. 1,
  17. $this->cli->main(array('vobject', '--hi'))
  18. );
  19. rewind($this->cli->stderr);
  20. $this->assertTrue(strlen(stream_get_contents($this->cli->stderr)) > 100);
  21. }
  22. public function testQuiet() {
  23. $this->assertEquals(
  24. 1,
  25. $this->cli->main(array('vobject', '-q'))
  26. );
  27. $this->assertTrue($this->cli->quiet);
  28. rewind($this->cli->stderr);
  29. $this->assertEquals(0, strlen(stream_get_contents($this->cli->stderr)));
  30. }
  31. public function testHelp() {
  32. $this->assertEquals(
  33. 0,
  34. $this->cli->main(array('vobject', '-h'))
  35. );
  36. rewind($this->cli->stderr);
  37. $this->assertTrue(strlen(stream_get_contents($this->cli->stderr)) > 100);
  38. }
  39. public function testFormat() {
  40. $this->assertEquals(
  41. 1,
  42. $this->cli->main(array('vobject', '--format=jcard'))
  43. );
  44. rewind($this->cli->stderr);
  45. $this->assertTrue(strlen(stream_get_contents($this->cli->stderr)) > 100);
  46. $this->assertEquals('jcard', $this->cli->format);
  47. }
  48. public function testFormatInvalid() {
  49. $this->assertEquals(
  50. 1,
  51. $this->cli->main(array('vobject', '--format=foo'))
  52. );
  53. rewind($this->cli->stderr);
  54. $this->assertTrue(strlen(stream_get_contents($this->cli->stderr)) > 100);
  55. $this->assertNull($this->cli->format);
  56. }
  57. public function testInputFormatInvalid() {
  58. $this->assertEquals(
  59. 1,
  60. $this->cli->main(array('vobject', '--inputformat=foo'))
  61. );
  62. rewind($this->cli->stderr);
  63. $this->assertTrue(strlen(stream_get_contents($this->cli->stderr)) > 100);
  64. $this->assertNull($this->cli->format);
  65. }
  66. public function testNoInputFile() {
  67. $this->assertEquals(
  68. 1,
  69. $this->cli->main(array('vobject', 'color'))
  70. );
  71. rewind($this->cli->stderr);
  72. $this->assertTrue(strlen(stream_get_contents($this->cli->stderr)) > 100);
  73. }
  74. public function testTooManyArgs() {
  75. $this->assertEquals(
  76. 1,
  77. $this->cli->main(array('vobject', 'color', 'a', 'b', 'c'))
  78. );
  79. }
  80. public function testUnknownCommand() {
  81. $this->assertEquals(
  82. 1,
  83. $this->cli->main(array('vobject', 'foo', '-'))
  84. );
  85. }
  86. public function testConvertJson() {
  87. $inputStream = fopen('php://memory','r+');
  88. fwrite($inputStream, <<<ICS
  89. BEGIN:VCARD
  90. VERSION:3.0
  91. FN:Cowboy Henk
  92. END:VCARD
  93. ICS
  94. );
  95. rewind($inputStream);
  96. $this->cli->stdin = $inputStream;
  97. $this->assertEquals(
  98. 0,
  99. $this->cli->main(array('vobject', 'convert','--format=json', '-'))
  100. );
  101. rewind($this->cli->stdout);
  102. $version = Version::VERSION;
  103. $this->assertEquals(
  104. '["vcard",[["version",{},"text","4.0"],["prodid",{},"text","-\/\/Sabre\/\/Sabre VObject '. $version .'\/\/EN"],["fn",{},"text","Cowboy Henk"]]]',
  105. stream_get_contents($this->cli->stdout)
  106. );
  107. }
  108. public function testConvertJCardPretty() {
  109. if (version_compare(PHP_VERSION, '5.4.0') < 0) {
  110. $this->markTestSkipped('This test required PHP 5.4.0');
  111. }
  112. $inputStream = fopen('php://memory','r+');
  113. fwrite($inputStream, <<<ICS
  114. BEGIN:VCARD
  115. VERSION:3.0
  116. FN:Cowboy Henk
  117. END:VCARD
  118. ICS
  119. );
  120. rewind($inputStream);
  121. $this->cli->stdin = $inputStream;
  122. $this->assertEquals(
  123. 0,
  124. $this->cli->main(array('vobject', 'convert','--format=jcard', '--pretty', '-'))
  125. );
  126. rewind($this->cli->stdout);
  127. $version = Version::VERSION;
  128. // PHP 5.5.12 changed the output
  129. $expected = <<<JCARD
  130. [
  131. "vcard",
  132. [
  133. [
  134. "versi
  135. JCARD;
  136. $this->assertStringStartsWith(
  137. $expected,
  138. stream_get_contents($this->cli->stdout)
  139. );
  140. }
  141. public function testConvertJCalFail() {
  142. $inputStream = fopen('php://memory','r+');
  143. fwrite($inputStream, <<<ICS
  144. BEGIN:VCARD
  145. VERSION:3.0
  146. FN:Cowboy Henk
  147. END:VCARD
  148. ICS
  149. );
  150. rewind($inputStream);
  151. $this->cli->stdin = $inputStream;
  152. $this->assertEquals(
  153. 2,
  154. $this->cli->main(array('vobject', 'convert','--format=jcal', '--inputformat=mimedir', '-'))
  155. );
  156. }
  157. public function testConvertMimeDir() {
  158. $inputStream = fopen('php://memory','r+');
  159. fwrite($inputStream, <<<JCARD
  160. [
  161. "vcard",
  162. [
  163. [
  164. "version",
  165. {
  166. },
  167. "text",
  168. "4.0"
  169. ],
  170. [
  171. "prodid",
  172. {
  173. },
  174. "text",
  175. "-\/\/Sabre\/\/Sabre VObject 3.1.0\/\/EN"
  176. ],
  177. [
  178. "fn",
  179. {
  180. },
  181. "text",
  182. "Cowboy Henk"
  183. ]
  184. ]
  185. ]
  186. JCARD
  187. );
  188. rewind($inputStream);
  189. $this->cli->stdin = $inputStream;
  190. $this->assertEquals(
  191. 0,
  192. $this->cli->main(array('vobject', 'convert','--format=mimedir', '--inputformat=json', '--pretty', '-'))
  193. );
  194. rewind($this->cli->stdout);
  195. $expected = <<<VCF
  196. BEGIN:VCARD
  197. VERSION:4.0
  198. PRODID:-//Sabre//Sabre VObject 3.1.0//EN
  199. FN:Cowboy Henk
  200. END:VCARD
  201. VCF;
  202. $this->assertEquals(
  203. strtr($expected, array("\n"=>"\r\n")),
  204. stream_get_contents($this->cli->stdout)
  205. );
  206. }
  207. public function testConvertDefaultFormats() {
  208. $inputStream = fopen('php://memory','r+');
  209. $outputFile = SABRE_TEMPDIR . 'bar.json';
  210. $this->assertEquals(
  211. 2,
  212. $this->cli->main(array('vobject', 'convert','foo.json',$outputFile))
  213. );
  214. $this->assertEquals('json', $this->cli->inputFormat);
  215. $this->assertEquals('json', $this->cli->format);
  216. }
  217. public function testConvertDefaultFormats2() {
  218. $outputFile = SABRE_TEMPDIR . 'bar.ics';
  219. $this->assertEquals(
  220. 2,
  221. $this->cli->main(array('vobject', 'convert','foo.ics',$outputFile))
  222. );
  223. $this->assertEquals('mimedir', $this->cli->inputFormat);
  224. $this->assertEquals('mimedir', $this->cli->format);
  225. }
  226. public function testVCard3040() {
  227. $inputStream = fopen('php://memory','r+');
  228. fwrite($inputStream, <<<VCARD
  229. BEGIN:VCARD
  230. VERSION:3.0
  231. PRODID:-//Sabre//Sabre VObject 3.1.0//EN
  232. FN:Cowboy Henk
  233. END:VCARD
  234. VCARD
  235. );
  236. rewind($inputStream);
  237. $this->cli->stdin = $inputStream;
  238. $this->assertEquals(
  239. 0,
  240. $this->cli->main(array('vobject', 'convert','--format=vcard40', '--pretty', '-'))
  241. );
  242. rewind($this->cli->stdout);
  243. $version = Version::VERSION;
  244. $expected = <<<VCF
  245. BEGIN:VCARD
  246. VERSION:4.0
  247. PRODID:-//Sabre//Sabre VObject $version//EN
  248. FN:Cowboy Henk
  249. END:VCARD
  250. VCF;
  251. $this->assertEquals(
  252. strtr($expected, array("\n"=>"\r\n")),
  253. stream_get_contents($this->cli->stdout)
  254. );
  255. }
  256. public function testVCard4030() {
  257. $inputStream = fopen('php://memory','r+');
  258. fwrite($inputStream, <<<VCARD
  259. BEGIN:VCARD
  260. VERSION:4.0
  261. PRODID:-//Sabre//Sabre VObject 3.1.0//EN
  262. FN:Cowboy Henk
  263. END:VCARD
  264. VCARD
  265. );
  266. rewind($inputStream);
  267. $this->cli->stdin = $inputStream;
  268. $this->assertEquals(
  269. 0,
  270. $this->cli->main(array('vobject', 'convert','--format=vcard30', '--pretty', '-'))
  271. );
  272. $version = Version::VERSION;
  273. rewind($this->cli->stdout);
  274. $expected = <<<VCF
  275. BEGIN:VCARD
  276. VERSION:3.0
  277. PRODID:-//Sabre//Sabre VObject $version//EN
  278. FN:Cowboy Henk
  279. END:VCARD
  280. VCF;
  281. $this->assertEquals(
  282. strtr($expected, array("\n"=>"\r\n")),
  283. stream_get_contents($this->cli->stdout)
  284. );
  285. }
  286. public function testVCard4021() {
  287. $inputStream = fopen('php://memory','r+');
  288. fwrite($inputStream, <<<VCARD
  289. BEGIN:VCARD
  290. VERSION:4.0
  291. PRODID:-//Sabre//Sabre VObject 3.1.0//EN
  292. FN:Cowboy Henk
  293. END:VCARD
  294. VCARD
  295. );
  296. rewind($inputStream);
  297. $this->cli->stdin = $inputStream;
  298. // vCard 2.1 is not supported yet, so this returns a failure.
  299. $this->assertEquals(
  300. 2,
  301. $this->cli->main(array('vobject', 'convert','--format=vcard21', '--pretty', '-'))
  302. );
  303. }
  304. function testValidate() {
  305. $inputStream = fopen('php://memory','r+');
  306. fwrite($inputStream, <<<VCARD
  307. BEGIN:VCARD
  308. VERSION:4.0
  309. PRODID:-//Sabre//Sabre VObject 3.1.0//EN
  310. UID:foo
  311. FN:Cowboy Henk
  312. END:VCARD
  313. VCARD
  314. );
  315. rewind($inputStream);
  316. $this->cli->stdin = $inputStream;
  317. $result = $this->cli->main(array('vobject', 'validate', '-'));
  318. $this->assertEquals(
  319. 0,
  320. $result
  321. );
  322. }
  323. function testValidateFail() {
  324. $inputStream = fopen('php://memory','r+');
  325. fwrite($inputStream, <<<VCARD
  326. BEGIN:VCALENDAR
  327. VERSION:2.0
  328. END:VCARD
  329. VCARD
  330. );
  331. rewind($inputStream);
  332. $this->cli->stdin = $inputStream;
  333. // vCard 2.1 is not supported yet, so this returns a failure.
  334. $this->assertEquals(
  335. 2,
  336. $this->cli->main(array('vobject', 'validate', '-'))
  337. );
  338. }
  339. function testValidateFail2() {
  340. $inputStream = fopen('php://memory','r+');
  341. fwrite($inputStream, <<<VCARD
  342. BEGIN:VCALENDAR
  343. VERSION:5.0
  344. END:VCALENDAR
  345. VCARD
  346. );
  347. rewind($inputStream);
  348. $this->cli->stdin = $inputStream;
  349. // vCard 2.1 is not supported yet, so this returns a failure.
  350. $this->assertEquals(
  351. 2,
  352. $this->cli->main(array('vobject', 'validate', '-'))
  353. );
  354. }
  355. function testRepair() {
  356. $inputStream = fopen('php://memory','r+');
  357. fwrite($inputStream, <<<VCARD
  358. BEGIN:VCARD
  359. VERSION:5.0
  360. END:VCARD
  361. VCARD
  362. );
  363. rewind($inputStream);
  364. $this->cli->stdin = $inputStream;
  365. // vCard 2.1 is not supported yet, so this returns a failure.
  366. $this->assertEquals(
  367. 2,
  368. $this->cli->main(array('vobject', 'repair', '-'))
  369. );
  370. rewind($this->cli->stdout);
  371. $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));
  372. }
  373. function testRepairNothing() {
  374. $inputStream = fopen('php://memory','r+');
  375. fwrite($inputStream, <<<VCARD
  376. BEGIN:VCALENDAR
  377. VERSION:2.0
  378. PRODID:-//Sabre//Sabre VObject 3.1.0//EN
  379. BEGIN:VEVENT
  380. UID:foo
  381. DTSTAMP:20140122T233226Z
  382. DTSTART:20140101T120000Z
  383. END:VEVENT
  384. END:VCALENDAR
  385. VCARD
  386. );
  387. rewind($inputStream);
  388. $this->cli->stdin = $inputStream;
  389. // vCard 2.1 is not supported yet, so this returns a failure.
  390. $result = $this->cli->main(array('vobject', 'repair', '-'));
  391. rewind($this->cli->stderr);
  392. $error = stream_get_contents($this->cli->stderr);
  393. $this->assertEquals(
  394. 0,
  395. $result,
  396. "This should have been error free. stderr output:\n" . $error
  397. );
  398. }
  399. /**
  400. * Note: this is a very shallow test, doesn't dig into the actual output,
  401. * but just makes sure there's no errors thrown.
  402. *
  403. * The colorizer is not a critical component, it's mostly a debugging tool.
  404. */
  405. function testColorCalendar() {
  406. $inputStream = fopen('php://memory','r+');
  407. $version = Version::VERSION;
  408. /**
  409. * This object is not valid, but it's designed to hit every part of the
  410. * colorizer source.
  411. */
  412. fwrite($inputStream, <<<VCARD
  413. BEGIN:VCALENDAR
  414. VERSION:2.0
  415. PRODID:-//Sabre//Sabre VObject {$version}//EN
  416. BEGIN:VTIMEZONE
  417. END:VTIMEZONE
  418. BEGIN:VEVENT
  419. ATTENDEE;RSVP=TRUE:mailto:foo@example.org
  420. REQUEST-STATUS:5;foo
  421. ATTACH:blabla
  422. END:VEVENT
  423. END:VCALENDAR
  424. VCARD
  425. );
  426. rewind($inputStream);
  427. $this->cli->stdin = $inputStream;
  428. // vCard 2.1 is not supported yet, so this returns a failure.
  429. $result = $this->cli->main(array('vobject', 'color', '-'));
  430. rewind($this->cli->stderr);
  431. $error = stream_get_contents($this->cli->stderr);
  432. $this->assertEquals(
  433. 0,
  434. $result,
  435. "This should have been error free. stderr output:\n" . $error
  436. );
  437. }
  438. /**
  439. * Note: this is a very shallow test, doesn't dig into the actual output,
  440. * but just makes sure there's no errors thrown.
  441. *
  442. * The colorizer is not a critical component, it's mostly a debugging tool.
  443. */
  444. function testColorVCard() {
  445. $inputStream = fopen('php://memory','r+');
  446. $version = Version::VERSION;
  447. /**
  448. * This object is not valid, but it's designed to hit every part of the
  449. * colorizer source.
  450. */
  451. fwrite($inputStream, <<<VCARD
  452. BEGIN:VCARD
  453. VERSION:4.0
  454. PRODID:-//Sabre//Sabre VObject {$version}//EN
  455. ADR:1;2;3;4a,4b;5;6
  456. group.TEL:123454768
  457. END:VCARD
  458. VCARD
  459. );
  460. rewind($inputStream);
  461. $this->cli->stdin = $inputStream;
  462. // vCard 2.1 is not supported yet, so this returns a failure.
  463. $result = $this->cli->main(array('vobject', 'color', '-'));
  464. rewind($this->cli->stderr);
  465. $error = stream_get_contents($this->cli->stderr);
  466. $this->assertEquals(
  467. 0,
  468. $result,
  469. "This should have been error free. stderr output:\n" . $error
  470. );
  471. }
  472. }
  473. class CliMock extends Cli {
  474. public $log = array();
  475. public $quiet = false;
  476. public $format;
  477. public $pretty;
  478. public $stdin;
  479. public $stdout;
  480. public $stderr;
  481. public $inputFormat;
  482. public $outputFormat;
  483. }