PageRenderTime 46ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/Console/ConsoleInputDefinitionTest.php

https://github.com/Proudio-Interactive/phpUnderControl
PHP | 316 lines | 185 code | 21 blank | 110 comment | 3 complexity | 4a487681b612a03088a831dfbac80941 MD5 | raw file
  1. <?php
  2. /**
  3. * This file is part of phpUnderControl.
  4. *
  5. * PHP Version 5.2.0
  6. *
  7. * Copyright (c) 2007-2010, Manuel Pichler <mapi@manuel-pichler.de>.
  8. * All rights reserved.
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. *
  14. * * Redistributions of source code must retain the above copyright
  15. * notice, this list of conditions and the following disclaimer.
  16. *
  17. * * Redistributions in binary form must reproduce the above copyright
  18. * notice, this list of conditions and the following disclaimer in
  19. * the documentation and/or other materials provided with the
  20. * distribution.
  21. *
  22. * * Neither the name of Manuel Pichler nor the names of his
  23. * contributors may be used to endorse or promote products derived
  24. * from this software without specific prior written permission.
  25. *
  26. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  27. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  28. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  29. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  30. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  31. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  32. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  33. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  34. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  35. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  36. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  37. * POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. * @category QualityAssurance
  40. * @package Console
  41. * @author Manuel Pichler <mapi@manuel-pichler.de>
  42. * @copyright 2007-2010 Manuel Pichler. All rights reserved.
  43. * @license http://www.opensource.org/licenses/bsd-license.php BSD License
  44. * @version SVN: $Id$
  45. * @link http://www.phpundercontrol.org/
  46. */
  47. require_once dirname( __FILE__ ) . '/../AbstractTest.php';
  48. /**
  49. * Test case for the console input definition class.
  50. *
  51. * @category QualityAssurance
  52. * @package Console
  53. * @author Manuel Pichler <mapi@manuel-pichler.de>
  54. * @copyright 2007-2010 Manuel Pichler. All rights reserved.
  55. * @license http://www.opensource.org/licenses/bsd-license.php BSD License
  56. * @version Release: @package_version@
  57. * @link http://www.phpundercontrol.org/
  58. */
  59. class phpucConsoleInputDefinitionTest extends phpucAbstractTest
  60. {
  61. /**
  62. * Tests the {@link phpucConsoleInputDefinition::addCommand()} method with
  63. * a new command.
  64. *
  65. * @return void
  66. */
  67. public function testAddCommand()
  68. {
  69. $definition = new phpucConsoleInputDefinition();
  70. $this->assertFalse( $definition->hasCommand( 'mapi' ) );
  71. $definition->addCommand( 'mapi', 'Hello World' );
  72. $this->assertTrue( $definition->hasCommand( 'mapi' ) );
  73. }
  74. /**
  75. * Tests that {@link phpucConsoleInputDefinition::addCommand()} fails with
  76. * an exception if the same command identifier is used a second time.
  77. *
  78. * @return void
  79. */
  80. public function testAddCommandTwoTimesFail()
  81. {
  82. $this->setExpectedException(
  83. 'phpucErrorException',
  84. "The command name 'mapi' is already in use."
  85. );
  86. $definition = new phpucConsoleInputDefinition();
  87. $definition->addCommand( 'mapi', 'Hello World 1.' );
  88. $definition->addCommand( 'mapi', 'Hello World 2.' );
  89. }
  90. /**
  91. * Tests that {@link phpucConsoleInputDefinition::addCommand()} fails with
  92. * an exception if an invalid mode value is passed in.
  93. *
  94. * @return void
  95. */
  96. public function testAddCommandWithInvalidModeFail()
  97. {
  98. $definition = new phpucConsoleInputDefinition();
  99. $definition->addCommand(
  100. 'mapi0', 'Hello World.', phpucConsoleInputDefinition::MODE_HIDDEN
  101. );
  102. $definition->addCommand(
  103. 'mapi1', 'Hello World.', phpucConsoleInputDefinition::MODE_NORMAL
  104. );
  105. $this->setExpectedException(
  106. 'phpucErrorException',
  107. 'Invalid value for mode given.'
  108. );
  109. $definition = new phpucConsoleInputDefinition();
  110. $definition->addCommand( 'mapi2', 'Hello World.', -42 );
  111. }
  112. /**
  113. * Tests that {@link phpucConsoleInputDefinition::addArgument()} fails with
  114. * an exception if an unknown command identifier is passed in.
  115. *
  116. * @return void
  117. */
  118. public function testAddArgumentWithUnknownCommandIdentifierFail()
  119. {
  120. $this->setExpectedException(
  121. 'phpucErrorException',
  122. "The command 'manuel' for 'pichler' doesn't exist."
  123. );
  124. $definition = new phpucConsoleInputDefinition();
  125. $definition->addArgument( 'manuel', 'pichler', 'Hello World.' );
  126. }
  127. /**
  128. * Tests that {@link phpucConsoleInputDefinition::addArgument()} fails with
  129. * an exception if an argument with an equal identifier already exists.
  130. *
  131. * @return void
  132. */
  133. public function testAddArgumentTwoTimesFail()
  134. {
  135. $this->setExpectedException(
  136. 'phpucErrorException',
  137. "An argument 'pichler' for command 'manuel' already exists."
  138. );
  139. $definition = new phpucConsoleInputDefinition();
  140. $definition->addCommand( 'manuel', 'Hello World.' );
  141. $definition->addArgument( 'manuel', 'pichler', 'Hello World 1.' );
  142. $definition->addArgument( 'manuel', 'pichler', 'Hello World 2.' );
  143. }
  144. /**
  145. * Tests that {@link phpucConsoleInputDefinition::addArgument()} fails with
  146. * an exception if the mandatory parameter is not of type <b>boolean</b>.
  147. *
  148. * @return void
  149. */
  150. public function testAddArgumentWithInvalidMandatoryValueFail()
  151. {
  152. $this->setExpectedException(
  153. 'phpucErrorException',
  154. 'The mandatory parameter must be of type boolean.'
  155. );
  156. $definition = new phpucConsoleInputDefinition();
  157. $definition->addCommand( 'manuel', 'Hello World.' );
  158. $definition->addArgument( 'manuel', 'pichler', 'Hello World.', null );
  159. }
  160. /**
  161. * Tests that {@link phpucConsoleInputDefinition::addArgument()} stores
  162. * a valid argument.
  163. *
  164. * @return void
  165. */
  166. public function testAddArgument()
  167. {
  168. $definition = new phpucConsoleInputDefinition();
  169. $definition->addCommand( 'manuel', 'Hello World.' );
  170. $this->assertFalse( $definition->hasArgument( 'manuel', 'foo' ) );
  171. $definition->addArgument( 'manuel', 'foo', 'Hello World.' );
  172. $this->assertTrue( $definition->hasArgument( 'manuel', 'foo' ) );
  173. }
  174. /**
  175. * Tests that {@link phpucConsoleInputDefinition::addOption()} fails with an
  176. * exception if an unknown command identifier is passed in.
  177. *
  178. * @return void
  179. */
  180. public function testAddOptionWithUnknownCommandIdentifierFail()
  181. {
  182. $this->setExpectedException(
  183. 'phpucErrorException',
  184. "The command 'manuel' for option 'pichler' doesn't exist."
  185. );
  186. $definition = new phpucConsoleInputDefinition();
  187. $definition->addOption( 'manuel', 'p', 'pichler', 'Hello World.' );
  188. }
  189. /**
  190. * Tests that {@link phpucConsoleInputDefinition::addOption()} fails with an
  191. * exception if an option with an equal identifier already exists.
  192. *
  193. * @return void
  194. */
  195. public function testAddOptionTwoTimesSameShortIdentifierFail()
  196. {
  197. $this->setExpectedException(
  198. 'phpucErrorException',
  199. "An option 'p' already exists for command 'manuel'."
  200. );
  201. $definition = new phpucConsoleInputDefinition();
  202. $definition->addCommand( 'manuel', 'Hello World.' );
  203. $definition->addOption( 'manuel', 'p', 'pichler1', 'Hello World 1.' );
  204. $definition->addOption( 'manuel', 'p', 'pichler2', 'Hello World 2.' );
  205. }
  206. /**
  207. * Tests that {@link phpucConsoleInputDefinition::addOption()} fails with an
  208. * exception if an option with an equal identifier already exists.
  209. *
  210. * @return void
  211. */
  212. public function testAddOptionTwoTimesSameLongIdentifierFail()
  213. {
  214. $this->setExpectedException(
  215. 'phpucErrorException',
  216. "An option 'pichler' already exists for command 'manuel'."
  217. );
  218. $definition = new phpucConsoleInputDefinition();
  219. $definition->addCommand( 'manuel', 'Hello World.' );
  220. $definition->addOption( 'manuel', 'p1', 'pichler', 'Hello World 1.' );
  221. $definition->addOption( 'manuel', 'p2', 'pichler', 'Hello World 2.' );
  222. }
  223. /**
  224. * Tests that {@link phpucConsoleInputDefinition::addOption()} fails with
  225. * an exception if the mandatory parameter is not of type <b>boolean</b>.
  226. *
  227. * @return void
  228. */
  229. public function testAddOptionWithInvalidMandatoryValueFail()
  230. {
  231. $this->setExpectedException(
  232. 'phpucErrorException',
  233. 'The mandatory parameter must be of type boolean.'
  234. );
  235. $definition = new phpucConsoleInputDefinition();
  236. $definition->addCommand( 'manuel', 'Hello World.' );
  237. $definition->addOption(
  238. 'manuel', 'p', 'pichler', 'Hello World.', null, null, null
  239. );
  240. }
  241. /**
  242. * Tests that {@link phpucConsoleInputDefinition::addOption()} fails with an
  243. * exception if an invalid mode value is passed in.
  244. *
  245. * @return void
  246. */
  247. public function testAddOptionWithInvalidModeFail()
  248. {
  249. $definition = new phpucConsoleInputDefinition();
  250. $definition->addCommand( 'manuel', 'Hello World.' );
  251. $definition->addOption(
  252. 'manuel', 'p1', 'pichler1', 'Hello World.', null, null, false,
  253. phpucConsoleInputDefinition::MODE_HIDDEN
  254. );
  255. $definition->addOption(
  256. 'manuel', 'p2', 'pichler2', 'Hello World.', null, null, false,
  257. phpucConsoleInputDefinition::MODE_NORMAL
  258. );
  259. $this->setExpectedException(
  260. 'phpucErrorException',
  261. 'Invalid value for mode given.'
  262. );
  263. $definition->addOption(
  264. 'manuel', 'p3', 'pichler3', 'Hello World.', null, null, false,
  265. -42
  266. );
  267. }
  268. /**
  269. * Tests that {@link phpucConsoleInputDefinition::addOption()} adds an option
  270. * to the internal data structure.
  271. *
  272. * @return void
  273. */
  274. public function testAddOption()
  275. {
  276. $definition = new phpucConsoleInputDefinition();
  277. $definition->addCommand( 'manuel', 'Hello World.' );
  278. $this->assertFalse( $definition->hasOption( 'manuel', 'p1' ) );
  279. $this->assertFalse( $definition->hasOption( 'manuel', 'pichler1' ) );
  280. $definition->addOption(
  281. 'manuel', 'p1', 'pichler1', 'Hello World.', null, null, false,
  282. phpucConsoleInputDefinition::MODE_HIDDEN
  283. );
  284. $this->assertTrue( $definition->hasOption( 'manuel', 'p1' ) );
  285. $this->assertTrue( $definition->hasOption( 'manuel', 'pichler1' ) );
  286. }
  287. }