PageRenderTime 42ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/Aura/Cli/GetoptTest.php

https://bitbucket.org/harikt/aura.cli
PHP | 624 lines | 464 code | 103 blank | 57 comment | 0 complexity | f97ec915c7c8f99ff879236dc1a4530a MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. namespace Aura\Cli;
  3. /**
  4. * Test class for Getopt.
  5. */
  6. class GetoptTest extends \PHPUnit_Framework_TestCase
  7. {
  8. /**
  9. * @var Getopt
  10. */
  11. protected $getopt;
  12. protected $option_factory;
  13. /**
  14. * Sets up the fixture, for example, opens a network connection.
  15. * This method is called before a test is executed.
  16. */
  17. protected function setUp()
  18. {
  19. parent::setUp();
  20. $this->option_factory = new OptionFactory;
  21. $this->getopt = new Getopt($this->option_factory);
  22. }
  23. /**
  24. * Tears down the fixture, for example, closes a network connection.
  25. * This method is called after a test is executed.
  26. */
  27. protected function tearDown()
  28. {
  29. parent::tearDown();
  30. }
  31. public function testInit($strict = Getopt::STRICT)
  32. {
  33. $opts = [
  34. 'foo_bar' => [
  35. 'long' => 'foo-bar',
  36. 'short' => 'f',
  37. ],
  38. 'baz_dib' => [
  39. 'long' => 'baz-dib',
  40. 'short' => 'b',
  41. ],
  42. ];
  43. $this->getopt->init($opts, $strict);
  44. $expect = 2;
  45. $actual = count($this->getopt->getOptions());
  46. $this->assertEquals($expect, $actual);
  47. }
  48. /**
  49. * @expectedException Aura\Cli\Exception
  50. */
  51. public function testInit_alreadyInitialized()
  52. {
  53. $opts = [
  54. 'foo_bar' => [
  55. 'long' => 'foo-bar',
  56. 'short' => 'f',
  57. ],
  58. 'baz_dib' => [
  59. 'long' => 'baz-dib',
  60. 'short' => 'b',
  61. ],
  62. ];
  63. $this->getopt->init($opts);
  64. $this->getopt->init($opts);
  65. }
  66. /**
  67. * @expectedException \UnexpectedValueException
  68. */
  69. public function testInit_unexpected()
  70. {
  71. $opts = [
  72. 'foo_bar' => true,
  73. 'baz_dib' => true,
  74. ];
  75. $this->getopt->init($opts);
  76. }
  77. public function testGetOptions()
  78. {
  79. $this->testInit();
  80. $expect = [];
  81. $expect['foo_bar'] = $this->option_factory->newInstance([
  82. 'name' => 'foo_bar',
  83. 'long' => 'foo-bar',
  84. 'short' => 'f',
  85. ]);
  86. $expect['baz_dib'] = $this->option_factory->newInstance([
  87. 'name' => 'baz_dib',
  88. 'long' => 'baz-dib',
  89. 'short' => 'b',
  90. ]);
  91. $actual = $this->getopt->getOptions();
  92. $this->assertEquals($expect['foo_bar'], $actual['foo_bar']);
  93. $this->assertEquals($expect['baz_dib'], $actual['baz_dib']);
  94. }
  95. public function testGetOption()
  96. {
  97. $this->testInit();
  98. $expect = $this->option_factory->newInstance([
  99. 'name' => 'foo_bar',
  100. 'long' => 'foo-bar',
  101. 'short' => 'f',
  102. ]);
  103. $actual = $this->getopt->getOption('foo_bar');
  104. $this->assertEquals($expect, $actual);
  105. }
  106. /**
  107. * @expectedException Aura\Cli\Exception\OptionNotDefined
  108. */
  109. public function testGetOption_optionNotDefined()
  110. {
  111. $this->testInit();
  112. $actual = $this->getopt->getOption('no_such_option');
  113. }
  114. public function testGetOption_optionNotDefined_nonStrict()
  115. {
  116. $this->testInit(Getopt::NON_STRICT);
  117. $actual = $this->getopt->getOption('no_such_option');
  118. $this->assertNull($actual);
  119. }
  120. public function testGetLongOption()
  121. {
  122. $this->testInit();
  123. $expect = $this->option_factory->newInstance([
  124. 'name' => 'foo_bar',
  125. 'long' => 'foo-bar',
  126. 'short' => 'f',
  127. ]);
  128. $actual = $this->getopt->getLongOption('foo-bar');
  129. $this->assertEquals($expect, $actual);
  130. }
  131. /**
  132. * @expectedException Aura\Cli\Exception\OptionNotDefined
  133. */
  134. public function testGetLongOption_optionNotDefined()
  135. {
  136. $this->testInit();
  137. $actual = $this->getopt->getLongOption('no_such_option');
  138. }
  139. public function testGetLongOption_optionNotDefined_nonStrict()
  140. {
  141. $this->testInit(Getopt::NON_STRICT);
  142. $actual = $this->getopt->getLongOption('no_such_option');
  143. $this->assertNull($actual);
  144. }
  145. public function testGetShortOption()
  146. {
  147. $this->testInit();
  148. $expect = $this->option_factory->newInstance([
  149. 'name' => 'foo_bar',
  150. 'long' => 'foo-bar',
  151. 'short' => 'f',
  152. ]);
  153. $actual = $this->getopt->getShortOption('f');
  154. $this->assertEquals($expect, $actual);
  155. }
  156. /**
  157. * @expectedException Aura\Cli\Exception\OptionNotDefined
  158. */
  159. public function testGetShortOption_optionNotDefined()
  160. {
  161. $this->testInit();
  162. $actual = $this->getopt->getShortOption('z');
  163. }
  164. public function testGetShortOption_optionNotDefined_nonStrict()
  165. {
  166. $this->testInit(Getopt::NON_STRICT);
  167. $actual = $this->getopt->getShortOption('z');
  168. $this->assertNull($actual);
  169. }
  170. public function testLoad_noOptions()
  171. {
  172. $this->testInit();
  173. $this->getopt->load([
  174. 'abc',
  175. 'def',
  176. ]);
  177. // check options
  178. $expect = [
  179. 'foo_bar' => null,
  180. 'baz_dib' => null,
  181. ];
  182. $actual = $this->getopt->getOptionValues();
  183. $this->assertSame($expect, $actual);
  184. // check params
  185. $expect = [
  186. 'abc',
  187. 'def',
  188. ];
  189. $actual = $this->getopt->getParams();
  190. $this->assertSame($expect, $actual);
  191. }
  192. public function testLoad_longOptions()
  193. {
  194. $this->testInit();
  195. $this->getopt->load([
  196. 'abc',
  197. '--foo-bar=zim',
  198. 'def',
  199. '--baz-dib=gir',
  200. ]);
  201. // check options
  202. $expect = [
  203. 'foo_bar' => 'zim',
  204. 'baz_dib' => 'gir',
  205. ];
  206. $actual = $this->getopt->getOptionValues();
  207. $this->assertSame($expect, $actual);
  208. // check single option values
  209. $expect = 'zim';
  210. $actual = $this->getopt->getOptionValue('foo_bar');
  211. $this->assertSame($expect, $actual);
  212. $expect = 'gir';
  213. $actual = $this->getopt->getOptionValue('baz_dib');
  214. $this->assertSame($expect, $actual);
  215. // check params
  216. $expect = [
  217. 'abc',
  218. 'def',
  219. ];
  220. $actual = $this->getopt->getParams();
  221. $this->assertSame($expect, $actual);
  222. }
  223. public function testLoad_longOptions_nonStrict()
  224. {
  225. $this->testInit(Getopt::NON_STRICT);
  226. $this->getopt->load([
  227. 'abc',
  228. '--no-such-option=zim',
  229. ]);
  230. // check single option values
  231. $actual = $this->getopt->getOptionValue('no_such_option');
  232. $this->assertNull($actual);
  233. }
  234. public function testLoad_shortOptions()
  235. {
  236. $this->testInit();
  237. $this->getopt->load([
  238. 'abc',
  239. '-f',
  240. 'zim',
  241. 'def',
  242. '-b',
  243. 'gir',
  244. ]);
  245. // check options
  246. $expect = [
  247. 'foo_bar' => 'zim',
  248. 'baz_dib' => 'gir',
  249. ];
  250. $actual = $this->getopt->getOptionValues();
  251. $this->assertSame($expect, $actual);
  252. // check single option values
  253. $expect = 'zim';
  254. $actual = $this->getopt->getOptionValue('foo_bar');
  255. $this->assertSame($expect, $actual);
  256. $expect = 'gir';
  257. $actual = $this->getopt->getOptionValue('baz_dib');
  258. $this->assertSame($expect, $actual);
  259. // check params
  260. $expect = [
  261. 'abc',
  262. 'def',
  263. ];
  264. $actual = $this->getopt->getParams();
  265. $this->assertSame($expect, $actual);
  266. }
  267. public function testLoad_shortOptions_nonStrict()
  268. {
  269. $this->testInit(Getopt::NON_STRICT);
  270. $this->getopt->load([
  271. 'abc',
  272. '-z',
  273. ]);
  274. // check single option values
  275. $actual = $this->getopt->getOptionValue('no_such_option');
  276. $this->assertNull($actual);
  277. }
  278. public function testLoad_mixedOptionsAndParams()
  279. {
  280. $this->testInit();
  281. $this->getopt->load([
  282. 'abc',
  283. '--foo-bar=zim',
  284. 'def',
  285. '-b',
  286. 'gir',
  287. '--',
  288. '--no-such-option=123',
  289. '-n',
  290. '456',
  291. 'ghi',
  292. ]);
  293. // check options
  294. $expect = [
  295. 'foo_bar' => 'zim',
  296. 'baz_dib' => 'gir',
  297. ];
  298. $actual = $this->getopt->getOptionValues();
  299. $this->assertSame($expect, $actual);
  300. // check single option values
  301. $expect = 'zim';
  302. $actual = $this->getopt->getOptionValue('foo_bar');
  303. $this->assertSame($expect, $actual);
  304. $expect = 'gir';
  305. $actual = $this->getopt->getOptionValue('baz_dib');
  306. $this->assertSame($expect, $actual);
  307. // check params
  308. $expect = [
  309. 'abc',
  310. 'def',
  311. '--no-such-option=123',
  312. '-n',
  313. '456',
  314. 'ghi',
  315. ];
  316. $actual = $this->getopt->getParams();
  317. $this->assertSame($expect, $actual);
  318. }
  319. public function testLoad_optionalParamsAsFlags()
  320. {
  321. $this->testInit();
  322. $this->getopt->load([
  323. 'abc',
  324. '--foo-bar',
  325. 'def',
  326. '-b',
  327. ]);
  328. // check options
  329. $expect = [
  330. 'foo_bar' => true,
  331. 'baz_dib' => true,
  332. ];
  333. $actual = $this->getopt->getOptionValues();
  334. $this->assertSame($expect, $actual);
  335. // check single option values
  336. $actual = $this->getopt->getOptionValue('foo_bar');
  337. $this->assertTrue($actual);
  338. $actual = $this->getopt->getOptionValue('baz_dib');
  339. $this->assertTrue($actual);
  340. // check params
  341. $expect = [
  342. 'abc',
  343. 'def',
  344. ];
  345. $actual = $this->getopt->getParams();
  346. $this->assertSame($expect, $actual);
  347. }
  348. /**
  349. * @expectedException Aura\Cli\Exception\OptionParamRequired
  350. */
  351. public function testLoad_longOptionParamRequired()
  352. {
  353. $opts = [
  354. 'foo_bar' => [
  355. 'long' => 'foo-bar',
  356. 'short' => 'f',
  357. 'param' => Option::PARAM_REQUIRED,
  358. ],
  359. ];
  360. $this->getopt->init($opts);
  361. $this->getopt->load([
  362. 'abc',
  363. '--foo-bar', // no param, should fail
  364. 'def',
  365. ]);
  366. }
  367. /**
  368. * @expectedException Aura\Cli\Exception\OptionParamRejected
  369. */
  370. public function testLoad_longOptionParamRejected()
  371. {
  372. $opts = [
  373. 'foo_bar' => [
  374. 'long' => 'foo-bar',
  375. 'short' => 'f',
  376. 'param' => Option::PARAM_REJECTED,
  377. ],
  378. ];
  379. $this->getopt->init($opts);
  380. $this->getopt->load([
  381. 'abc',
  382. '--foo-bar=zim', // has param, should fail
  383. 'def',
  384. ]);
  385. }
  386. /**
  387. * @expectedException Aura\Cli\Exception\OptionParamRequired
  388. */
  389. public function testLoad_shortOptionParamRequired()
  390. {
  391. $opts = [
  392. 'foo_bar' => [
  393. 'long' => 'foo-bar',
  394. 'short' => 'f',
  395. 'param' => Option::PARAM_REQUIRED,
  396. ],
  397. ];
  398. $this->getopt->init($opts);
  399. $this->getopt->load([
  400. 'abc',
  401. 'def',
  402. '-f', // no param, should fail
  403. ]);
  404. }
  405. public function testLoad_shortOptionParamRejected()
  406. {
  407. $opts = [
  408. 'foo_bar' => [
  409. 'long' => 'foo-bar',
  410. 'short' => 'f',
  411. 'param' => Option::PARAM_REJECTED,
  412. ],
  413. ];
  414. $this->getopt->init($opts);
  415. $this->getopt->load([
  416. 'abc',
  417. '-f', // has param, should flag as true
  418. 'def',
  419. ]);
  420. $actual = $this->getopt->getOptionValue('foo_bar');
  421. $this->assertTrue($actual);
  422. }
  423. public function testLoad_shortOptionCluster()
  424. {
  425. $opts = [
  426. 'foo_bar' => [
  427. 'short' => 'f',
  428. ],
  429. 'baz_dib' => [
  430. 'short' => 'b',
  431. ],
  432. 'zim_gir' => [
  433. 'short' => 'z',
  434. ],
  435. ];
  436. $this->getopt->init($opts);
  437. $this->getopt->load([
  438. 'abc',
  439. '-fbz', // should flag each as true
  440. 'def',
  441. ]);
  442. $expect = [
  443. 'foo_bar' => true,
  444. 'baz_dib' => true,
  445. 'zim_gir' => true,
  446. ];
  447. $actual = $this->getopt->getOptionValues();
  448. $this->assertSame($expect, $actual);
  449. }
  450. public function testLoad_shortOptionCluster_nonStrict()
  451. {
  452. $this->getopt->init([], Getopt::NON_STRICT);
  453. $this->getopt->load([
  454. 'abc',
  455. '-fbz', // should flag each as true
  456. 'def',
  457. ]);
  458. $expect = [];
  459. $actual = $this->getopt->getOptionValues();
  460. $this->assertSame($expect, $actual);
  461. }
  462. /**
  463. * @expectedException Aura\Cli\Exception\OptionParamRequired
  464. */
  465. public function testLoad_shortOptionClusterRequired()
  466. {
  467. $opts = [
  468. 'foo_bar' => [
  469. 'short' => 'f',
  470. ],
  471. 'baz_dib' => [
  472. 'short' => 'b',
  473. 'param' => Option::PARAM_REQUIRED,
  474. ],
  475. 'zim_gir' => [
  476. 'short' => 'z',
  477. ],
  478. ];
  479. $this->getopt->init($opts);
  480. $this->getopt->load([
  481. 'abc',
  482. '-fbz', // -b requires a param
  483. 'def',
  484. ]);
  485. }
  486. public function testMagicGet()
  487. {
  488. $opts = [
  489. 'foo_bar' => [
  490. 'long' => 'foo-bar',
  491. 'short' => 'f',
  492. ],
  493. ];
  494. $this->getopt->init($opts);
  495. $this->getopt->load([
  496. 'abc',
  497. '--foo-bar=zim', // has param, should flag as true
  498. 'def',
  499. ]);
  500. $expect = 'zim';
  501. $actual = $this->getopt->__get('foo_bar');
  502. $this->assertSame($expect, $actual);
  503. }
  504. public function testMagicGetNonStrict()
  505. {
  506. $opts = [
  507. 'foo_bar' => [
  508. 'long' => 'foo-bar',
  509. 'short' => 'f',
  510. ],
  511. ];
  512. $this->getopt->init($opts, Getopt::NON_STRICT);
  513. $this->getopt->load([
  514. 'abc',
  515. '--foo-bar=zim', // has param, should flag as true
  516. 'def',
  517. ]);
  518. $actual = $this->getopt->__get('no_such_option');
  519. $this->assertNull($actual);
  520. }
  521. }