PageRenderTime 45ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/Tests/unit/Console/ConsoleTest.php

https://bitbucket.org/fsilva/slick-framework
PHP | 332 lines | 211 code | 21 blank | 100 comment | 1 complexity | 91d480fb7fa4f6c98493618c6386e747 MD5 | raw file
  1. <?php
  2. /**
  3. * ConsoleTest
  4. *
  5. * PHP version 5
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. *
  19. * @package Slick
  20. * @subpackage Tests\Unit\Console
  21. * @author Filipe Silva <silvam.filipe@gmail.com>
  22. * @copyright Filipe Silva 2013
  23. * @license Apache License, Version 2.0 (the "License")
  24. * @since Version 1.0.0
  25. */
  26. namespace Console;
  27. use Codeception\Util\Stub;
  28. use Slick\Console as Console;
  29. use Slick\Console\Colors as Colors;
  30. /**
  31. * ConsoleTest
  32. *
  33. * Test case for class Slick\Console
  34. *
  35. * @package Slick
  36. * @subpackage Tests\Unit\Console
  37. * @author Filipe Silva <silvam.filipe@gmail.com>
  38. */
  39. class ConsoleTest extends \Codeception\TestCase\Test
  40. {
  41. /**
  42. * @var \CodeGuy
  43. */
  44. protected $codeGuy;
  45. /**
  46. * The system under test.
  47. * @var \Slick\Console
  48. */
  49. protected $_console = null;
  50. /**
  51. * Setsup the SUT instance
  52. */
  53. protected function _before()
  54. {
  55. parent::_before();
  56. $this->_console = new Console(
  57. array(
  58. 'args' => array(
  59. "--working=".APP_PATH, 'DefaultShell',
  60. '-q', '--verbose', '-w'.APP_PATH,
  61. '--flag=something', '-v',
  62. '-fOther_Thing', '-c1'
  63. ),
  64. 'core' => FRAMEWORK_APTH
  65. )
  66. );
  67. }
  68. /**
  69. * Cleans up after every test.
  70. */
  71. protected function _after()
  72. {
  73. $this->_console->setQuiet(true);
  74. $this->_console = null;
  75. parent::_after();
  76. }
  77. // tests
  78. /**
  79. * Test base implementations
  80. * @test
  81. * @expectedException Slick\Console\Exception\Implementation
  82. */
  83. public function checkImplementation()
  84. {
  85. $this->assertEquals(APP_PATH, Console::$workingPath);
  86. $this->assertTrue($this->_console->verbose);
  87. $this->assertTrue($this->_console->quiet);
  88. $expected = array('DefaultShell');
  89. $this->assertEquals($expected, $this->_console->args);
  90. $flags = $this->_console->flags;
  91. $this->assertContains('fOther_Thing', $flags);
  92. $this->assertTrue(array_key_exists('flag', $flags));
  93. $this->assertTrue(array_key_exists('w', $flags));
  94. $this->assertTrue(array_key_exists('v', $flags));
  95. $this->_console->unimplemented();
  96. }
  97. /**
  98. * Tests for log system
  99. * @test
  100. */
  101. public function logMessages()
  102. {
  103. $this->_console->setQuiet(false);
  104. $this->_console->setColors(false);
  105. ob_start();
  106. $this->_console->log("Info");
  107. $output = ob_get_contents();
  108. ob_clean();
  109. $this->_console->setColors(true);
  110. $this->_console->log("Error log", 'debug');
  111. $colorPutput = ob_get_contents();
  112. ob_end_clean();
  113. $now = strftime('%Y-%m-%d %T');
  114. $expected = "\n[{$now}] INFO: Info\n";
  115. $this->assertEquals($expected, $output);
  116. $levels = $this->_console->logLevels;
  117. $ts = Colors::set("[{$now}] DEBUG: ", $levels['debug']);
  118. $body = Colors::set("Error log", 'light_gray');
  119. $expected = "\n{$ts}{$body}\n";
  120. $this->assertEquals($expected, $colorPutput);
  121. }
  122. /**
  123. * Checks the color
  124. * @test
  125. */
  126. public function doColorReplace()
  127. {
  128. $txt = Colors::replace('Hello world!', 'Hello', 'red');
  129. $color = Colors::set('Hello', 'red');
  130. $this->assertEquals($color.' world!', $txt);
  131. }
  132. /**
  133. * Checks the color
  134. * @test
  135. */
  136. public function outputMultipleLines()
  137. {
  138. $this->_console->setQuiet(false);
  139. $this->_console->setColors(false);
  140. $txt = "Hello world, this is a very long string and therefore should be printed in two or " .
  141. "more separate lines. This usefull to ouput text without worring if it will fit the " .
  142. "console current with an can be ajustable to all the console window sizes.";
  143. ob_start();
  144. $this->_console->out($txt, true);
  145. $output = ob_get_contents();
  146. ob_end_clean();
  147. $this->assertGreaterThan(2, explode("\n", $output));
  148. }
  149. /**
  150. * Test the hability do print out with out do a new line at the end.
  151. * @test
  152. */
  153. public function outputInlineMessage()
  154. {
  155. $this->_console->setQuiet(false);
  156. $this->_console->setColors(false);
  157. $expected = "Testing core ... Done!\n";
  158. ob_start();
  159. $this->_console->out('Testing core ... ', false, false)
  160. ->out('Done!');
  161. $output = ob_get_contents();
  162. ob_end_clean();
  163. $this->assertEquals($expected, $output);
  164. $this->_console->setQuiet(true);
  165. }
  166. /**
  167. * Tests info log ouput
  168. * @test
  169. */
  170. public function logInfoMessage()
  171. {
  172. $this->_console->setQuiet(false);
  173. $this->_console->setColors(false);
  174. ob_start();
  175. $obj = $this->_console->info("Some info");
  176. $output = ob_get_contents();
  177. ob_end_clean();
  178. $now = strftime('%Y-%m-%d %T');
  179. $expected = "\n[{$now}] INFO: Some info\n";
  180. $this->assertEquals($expected, $output);
  181. $this->assertInstanceOf('Slick\Console', $obj);
  182. }
  183. /**
  184. * Tests info log ouput
  185. * @test
  186. */
  187. public function logWarningMessage()
  188. {
  189. $this->_console->setQuiet(false);
  190. $this->_console->setColors(false);
  191. ob_start();
  192. $obj = $this->_console->warning("Some warning");
  193. $output = ob_get_contents();
  194. ob_end_clean();
  195. $now = strftime('%Y-%m-%d %T');
  196. $expected = "\n[{$now}] WARNING: Some warning\n";
  197. $this->assertEquals($expected, $output);
  198. $this->assertInstanceOf('Slick\Console', $obj);
  199. ;
  200. }
  201. /**
  202. * Tests info log ouput
  203. * @test
  204. */
  205. public function logDebugMessage()
  206. {
  207. $this->_console->setQuiet(false);
  208. $this->_console->setColors(false);
  209. ob_start();
  210. $obj = $this->_console->debug("Some debug");
  211. $output = ob_get_contents();
  212. ob_end_clean();
  213. $now = strftime('%Y-%m-%d %T');
  214. $expected = "\n[{$now}] DEBUG: Some debug\n";
  215. $this->assertEquals($expected, $output);
  216. $this->assertInstanceOf('Slick\Console', $obj);
  217. }
  218. /**
  219. * Tests info log ouput
  220. * @test
  221. */
  222. public function logErrorMessage()
  223. {
  224. $this->_console->setQuiet(false);
  225. $this->_console->setColors(false);
  226. ob_start();
  227. $obj = $this->_console->error("Some error");
  228. $output = ob_get_contents();
  229. ob_end_clean();
  230. $now = strftime('%Y-%m-%d %T');
  231. $expected = "\n[{$now}] ERROR: Some error\n";
  232. $this->assertEquals($expected, $output);
  233. $this->assertInstanceOf('Slick\Console', $obj);
  234. }
  235. /**
  236. * Tests the call to an app shell
  237. * @test
  238. */
  239. public function callAppShell()
  240. {
  241. $console = new Console(
  242. array(
  243. 'args' => array(
  244. "--working=".APP_PATH, 'TestShell',
  245. '-q'
  246. ),
  247. 'core' => FRAMEWORK_APTH
  248. )
  249. );
  250. $this->assertInstanceOf('Console\Shell\TestShell', $console->shell);
  251. }
  252. /**
  253. * Tests the call to an app shell
  254. * @test
  255. */
  256. public function callPluginShell()
  257. {
  258. $console = new Console(
  259. array(
  260. 'args' => array(
  261. "--working=".APP_PATH, 'PluginShell',
  262. '-q', '--plugin=Test'
  263. ),
  264. 'core' => FRAMEWORK_APTH
  265. )
  266. );
  267. $this->assertInstanceOf(
  268. 'Test\Console\Shell\PluginShell',
  269. $console->shell
  270. );
  271. }
  272. /**
  273. * Tests the error ouput when the typed shell isn't found
  274. * @test
  275. */
  276. public function callUnknownShell()
  277. {
  278. ob_start();
  279. $console = new Console(
  280. array(
  281. 'args' => array(
  282. "--working=".APP_PATH, 'Unknown'
  283. ),
  284. 'core' => FRAMEWORK_APTH,
  285. 'colors' => false
  286. )
  287. );
  288. $output = ob_get_contents();
  289. ob_end_clean();
  290. $this->assertFalse(is_null($output));
  291. $console->quiet = true;
  292. }
  293. /**
  294. * Testing dispatch event
  295. * @test
  296. */
  297. public function dispatchAShell()
  298. {
  299. $console = new Console(
  300. array(
  301. 'args' => array(
  302. "--working=".APP_PATH, 'TestShell',
  303. '-q', '-c'
  304. ),
  305. 'core' => FRAMEWORK_APTH
  306. )
  307. );
  308. $console->dispatch();
  309. $this->assertTrue($console->shell->runMain);
  310. }
  311. }