/ConsoleTools/tests/question_dialog_test.php

https://github.com/F5/zetacomponents · PHP · 279 lines · 185 code · 55 blank · 39 comment · 2 complexity · 53a365582bbc56febc1e07bb805fbbc3 MD5 · raw file

  1. <?php
  2. /**
  3. * ezcConsoleQuestionDialogTest class.
  4. *
  5. * Licensed to the Apache Software Foundation (ASF) under one
  6. * or more contributor license agreements. See the NOTICE file
  7. * distributed with this work for additional information
  8. * regarding copyright ownership. The ASF licenses this file
  9. * to you under the Apache License, Version 2.0 (the
  10. * "License"); you may not use this file except in compliance
  11. * with the License. You may obtain a copy of the License at
  12. *
  13. * http://www.apache.org/licenses/LICENSE-2.0
  14. *
  15. * Unless required by applicable law or agreed to in writing,
  16. * software distributed under the License is distributed on an
  17. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  18. * KIND, either express or implied. See the License for the
  19. * specific language governing permissions and limitations
  20. * under the License.
  21. *
  22. * @package ConsoleTools
  23. * @subpackage Tests
  24. * @version //autogentag//
  25. * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
  26. */
  27. /**
  28. * Require generic test case for ezcConsoleDialog implementations.
  29. */
  30. require_once dirname( __FILE__ ) . "/dialog_test.php";
  31. /**
  32. * Test suite for ezcConsoleQuestionDialog class.
  33. *
  34. * @package ConsoleTools
  35. * @subpackage Tests
  36. */
  37. class ezcConsoleQuestionDialogTest extends ezcConsoleDialogTest
  38. {
  39. public static function suite()
  40. {
  41. return new PHPUnit_Framework_TestSuite( "ezcConsoleQuestionDialogTest" );
  42. }
  43. public function testGetAccessSuccess()
  44. {
  45. $output = new ezcConsoleOutput();
  46. $dialog = new ezcConsoleQuestionDialog( $output );
  47. $this->assertSame( $output, $dialog->output );
  48. $this->assertEquals( new ezcConsoleQuestionDialogOptions(), $dialog->options );
  49. }
  50. public function testGetAccessFailure()
  51. {
  52. $output = new ezcConsoleOutput();
  53. $dialog = new ezcConsoleQuestionDialog( $output );
  54. $exceptionCaught = false;
  55. try
  56. {
  57. echo $dialog->foo;
  58. }
  59. catch ( ezcBasePropertyNotFoundException $e )
  60. {
  61. $exceptionCaught = true;
  62. }
  63. $this->assertTrue( $exceptionCaught, "Exception not thrown on access of nonexistent property foo." );
  64. }
  65. public function testSetAccessSuccess()
  66. {
  67. $output = new ezcConsoleOutput();
  68. $dialog = new ezcConsoleQuestionDialog( $output );
  69. $outputNew = new ezcConsoleOutput();
  70. $optionsNew = new ezcConsoleQuestionDialogOptions();
  71. $dialog->output = $outputNew;
  72. $dialog->options = $optionsNew;
  73. $this->assertSame( $outputNew, $dialog->output );
  74. $this->assertSame( $optionsNew, $dialog->options );
  75. }
  76. public function testSetAccessFailure()
  77. {
  78. $output = new ezcConsoleOutput();
  79. $dialog = new ezcConsoleQuestionDialog( $output );
  80. $exceptionCaught = false;
  81. try
  82. {
  83. $dialog->output = "Foo";
  84. }
  85. catch ( ezcBaseValueException $e )
  86. {
  87. $exceptionCaught = true;
  88. }
  89. $this->assertTrue( $exceptionCaught, "Exception not thrown on invalid value for output." );
  90. $exceptionCaught = false;
  91. try
  92. {
  93. $dialog->options = "Foo";
  94. }
  95. catch ( ezcBaseValueException $e )
  96. {
  97. $exceptionCaught = true;
  98. }
  99. $this->assertTrue( $exceptionCaught, "Exception not thrown on invalid value for options." );
  100. $exceptionCaught = false;
  101. try
  102. {
  103. $dialog->foo = "bar";
  104. }
  105. catch ( ezcBasePropertyNotFoundException $e )
  106. {
  107. $exceptionCaught = true;
  108. }
  109. $this->assertTrue( $exceptionCaught, "Exception not thrown on access of nonexistent property foo." );
  110. $this->assertSame( $output, $dialog->output );
  111. $this->assertEquals( new ezcConsoleQuestionDialogOptions(), $dialog->options );
  112. }
  113. public function testIssetAccess()
  114. {
  115. $output = new ezcConsoleOutput();
  116. $dialog = new ezcConsoleQuestionDialog( $output );
  117. $this->assertTrue( isset( $dialog->options ), "Property options is not set." );
  118. $this->assertTrue( isset( $dialog->output ), "Property options is not set." );
  119. $this->assertFalse( isset( $dialog->foo ), "Property foo is set." );
  120. }
  121. public function testBasicMethods()
  122. {
  123. $output = new ezcConsoleOutput();
  124. $dialog = new ezcConsoleQuestionDialog( $output );
  125. $this->assertFalse( $dialog->hasValidResult(), "Fresh dialog has valid result." );
  126. $exceptionCaught = false;
  127. try
  128. {
  129. $dialog->getResult();
  130. }
  131. catch ( ezcConsoleNoValidDialogResultException $e )
  132. {
  133. $exceptionCaught = true;
  134. }
  135. $this->assertTrue( $exceptionCaught, "Excption not thrown on getResult() without result." );
  136. $dialog->reset();
  137. $exceptionCaught = false;
  138. try
  139. {
  140. $dialog->getResult();
  141. }
  142. catch ( ezcConsoleNoValidDialogResultException $e )
  143. {
  144. $exceptionCaught = true;
  145. }
  146. $this->assertTrue( $exceptionCaught, "Excption not thrown on getResult() without result." );
  147. }
  148. public function testYesNoQuestionFactory()
  149. {
  150. $output = new ezcConsoleOutput();
  151. $dialog = ezcConsoleQuestionDialog::YesNoQuestion( $output, "Is Jean-Luc a borg?", "y" );
  152. $this->assertInstanceOf( "ezcConsoleQuestionDialogOptions", $dialog->options );
  153. $this->assertEquals( "Is Jean-Luc a borg?", $dialog->options->text );
  154. $this->assertTrue( $dialog->options->showResults );
  155. $this->assertInstanceOf( "ezcConsoleQuestionDialogCollectionValidator", $dialog->options->validator );
  156. $this->assertEquals( array( "y", "n" ), $dialog->options->validator->collection );
  157. $this->assertEquals( "y", $dialog->options->validator->default );
  158. $this->assertEquals( ezcConsoleQuestionDialogCollectionValidator::CONVERT_LOWER, $dialog->options->validator->conversion );
  159. }
  160. public function testDialog1()
  161. {
  162. $this->runDialog( __METHOD__ );
  163. $res[] = $this->readPipe( $this->pipes[1] );
  164. fputs( $this->pipes[0], "A\n" );
  165. $res[] = $this->readPipe( $this->pipes[1] );
  166. fputs( $this->pipes[0], "Y\n" );
  167. $res[] = $this->readPipe( $this->pipes[1] );
  168. // $this->saveDialogResult( __METHOD__, $res );
  169. $this->assertEquals( $this->res, $res );
  170. }
  171. public function testDialog2()
  172. {
  173. $this->runDialog( __METHOD__ );
  174. $res[] = $this->readPipe( $this->pipes[1] );
  175. fputs( $this->pipes[0], "A\n" );
  176. $res[] = $this->readPipe( $this->pipes[1] );
  177. fputs( $this->pipes[0], "3.14\n" );
  178. $res[] = $this->readPipe( $this->pipes[1] );
  179. fputs( $this->pipes[0], "true\n" );
  180. $res[] = $this->readPipe( $this->pipes[1] );
  181. fputs( $this->pipes[0], "23\n" );
  182. $res[] = $this->readPipe( $this->pipes[1] );
  183. // $this->saveDialogResult( __METHOD__, $res );
  184. $this->assertEquals( $this->res, $res );
  185. }
  186. public function testDialog3()
  187. {
  188. $this->runDialog( __METHOD__ );
  189. $res[] = $this->readPipe( $this->pipes[1] );
  190. fputs( $this->pipes[0], "A\n" );
  191. $res[] = $this->readPipe( $this->pipes[1] );
  192. fputs( $this->pipes[0], "y\n" );
  193. $res[] = $this->readPipe( $this->pipes[1] );
  194. // $this->saveDialogResult( __METHOD__, $res );
  195. $this->assertEquals( $this->res, $res );
  196. }
  197. public function testDialog4()
  198. {
  199. $this->runDialog( __METHOD__ );
  200. $res[] = $this->readPipe( $this->pipes[1] );
  201. fputs( $this->pipes[0], "foo\n" );
  202. $res[] = $this->readPipe( $this->pipes[1] );
  203. fputs( $this->pipes[0], "foo.bar@\n" );
  204. $res[] = $this->readPipe( $this->pipes[1] );
  205. fputs( $this->pipes[0], "foo.bar@example\n" );
  206. $res[] = $this->readPipe( $this->pipes[1] );
  207. fputs( $this->pipes[0], "foo.bar@example.com\n" );
  208. $res[] = $this->readPipe( $this->pipes[1] );
  209. // $this->saveDialogResult( __METHOD__, $res );
  210. $this->assertEquals( $this->res, $res );
  211. }
  212. public function testDialog5()
  213. {
  214. $this->runDialog( __METHOD__ );
  215. $res[] = $this->readPipe( $this->pipes[1] );
  216. fputs( $this->pipes[0], "foo\n" );
  217. $res[] = $this->readPipe( $this->pipes[1] );
  218. fclose( $this->pipes[0] );
  219. $res[] = $this->readPipe( $this->pipes[1] );
  220. // $this->saveDialogResult( __METHOD__, $res );
  221. $this->assertEquals( $this->res, $res );
  222. }
  223. }
  224. ?>