RunCest.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  1. <?php
  2. class RunCest
  3. {
  4. public function _before(\CliGuy $I)
  5. {
  6. $I->amInPath('tests/data/sandbox');
  7. }
  8. public function runOneFile(\CliGuy $I)
  9. {
  10. $I->wantTo('execute one test');
  11. $I->executeCommand('run tests/dummy/FileExistsCept.php');
  12. $I->seeInShellOutput("OK (");
  13. }
  14. public function runOneFileWithColors(\CliGuy $I)
  15. {
  16. $I->wantTo('execute one test');
  17. $I->executeCommand('run --colors tests/dummy/FileExistsCept.php');
  18. $I->seeInShellOutput("OK (");
  19. $I->seeInShellOutput("\033[35;1mFileExistsCept:\033[39;22m Check config exists");
  20. }
  21. /**
  22. * @group reports
  23. * @group core
  24. *
  25. * @param CliGuy $I
  26. */
  27. public function runHtml(\CliGuy $I)
  28. {
  29. $I->wantTo('execute tests with html output');
  30. $I->executeCommand('run dummy --html');
  31. $I->seeFileFound('report.html', 'tests/_output');
  32. }
  33. /**
  34. * @group reports
  35. *
  36. * @param CliGuy $I
  37. */
  38. public function runJsonReport(\CliGuy $I)
  39. {
  40. $I->wantTo('check json reports');
  41. $I->executeCommand('run dummy --json');
  42. $I->seeFileFound('report.json', 'tests/_output');
  43. $I->seeInThisFile('"suite":');
  44. $I->seeInThisFile('"dummy"');
  45. $I->assertNotNull(json_decode(file_get_contents('tests/_output/report.json')));
  46. }
  47. /**
  48. * @group reports
  49. *
  50. * @param CliGuy $I
  51. */
  52. public function runTapReport(\CliGuy $I)
  53. {
  54. $I->wantTo('check tap reports');
  55. $I->executeCommand('run dummy --tap');
  56. $I->seeFileFound('report.tap.log', 'tests/_output');
  57. }
  58. /**
  59. * @group reports
  60. *
  61. * @param CliGuy $I
  62. */
  63. public function runXmlReport(\CliGuy $I)
  64. {
  65. $I->wantTo('check xml reports');
  66. $I->executeCommand('run dummy --xml');
  67. $I->seeFileFound('report.xml', 'tests/_output');
  68. $I->seeInThisFile('<?xml');
  69. $I->seeInThisFile('<testsuite name="dummy"');
  70. $I->seeInThisFile('<testcase name="FileExists"');
  71. $I->seeInThisFile('feature="');
  72. }
  73. /**
  74. * @group reports
  75. * @param CliGuy $I
  76. */
  77. public function runXmlReportsInStrictMode(\CliGuy $I)
  78. {
  79. $I->wantTo('check xml in strict mode');
  80. $I->executeCommand('run dummy --xml -c codeception_strict_xml.yml');
  81. $I->seeFileFound('report.xml', 'tests/_output');
  82. $I->seeInThisFile('<?xml');
  83. $I->seeInThisFile('<testsuite name="dummy"');
  84. $I->seeInThisFile('<testcase name="FileExists"');
  85. $I->dontSeeInThisFile('feature="');
  86. }
  87. /**
  88. * @group reports
  89. *
  90. * @param CliGuy $I
  91. */
  92. public function runCustomReport(\CliGuy $I)
  93. {
  94. if (\PHPUnit\Runner\Version::series() >= 7) {
  95. throw new \Codeception\Exception\Skip('Not for PHPUnit 7');
  96. }
  97. $I->executeCommand('run dummy --report -c codeception_custom_report.yml');
  98. $I->seeInShellOutput('FileExistsCept: Check config exists');
  99. $I->dontSeeInShellOutput('Ok');
  100. }
  101. public function runOneGroup(\CliGuy $I)
  102. {
  103. $I->executeCommand('run skipped -g notorun');
  104. $I->seeInShellOutput('Skipped Tests (1)');
  105. $I->seeInShellOutput("IncompleteMeCept");
  106. $I->dontSeeInShellOutput("SkipMeCept");
  107. }
  108. public function skipRunOneGroup(\CliGuy $I)
  109. {
  110. $I->executeCommand('run skipped --skip-group notorun');
  111. $I->seeInShellOutput('Skipped Tests (2)');
  112. $I->seeInShellOutput("SkipMeCept");
  113. $I->dontSeeInShellOutput("IncompleteMeCept");
  114. }
  115. public function skipGroupOfCest(\CliGuy $I)
  116. {
  117. $I->executeCommand('run dummy');
  118. $I->seeInShellOutput('Optimistic');
  119. $I->seeInShellOutput('Dummy Tests (6)');
  120. $I->executeCommand('run dummy --skip-group ok');
  121. $I->seeInShellOutput('Pessimistic');
  122. $I->seeInShellOutput('Dummy Tests (5)');
  123. $I->dontSeeInShellOutput('Optimistic');
  124. }
  125. public function runTwoSuites(\CliGuy $I)
  126. {
  127. $I->executeCommand('run skipped,dummy --no-exit');
  128. $I->seeInShellOutput("Skipped Tests (3)");
  129. $I->seeInShellOutput("Dummy Tests (6)");
  130. $I->dontSeeInShellOutput("Remote Tests");
  131. }
  132. public function skipSuites(\CliGuy $I)
  133. {
  134. $I->executeCommand(
  135. 'run dummy --skip skipped --skip remote --skip remote_server --skip order --skip unit '
  136. . '--skip powers --skip math --skip messages'
  137. );
  138. $I->seeInShellOutput("Dummy Tests");
  139. $I->dontSeeInShellOutput("Remote Tests");
  140. $I->dontSeeInShellOutput("Remote_server Tests");
  141. $I->dontSeeInShellOutput("Order Tests");
  142. }
  143. public function runOneTestFromUnit(\CliGuy $I)
  144. {
  145. $I->executeCommand('run tests/dummy/AnotherTest.php:testFirst');
  146. $I->seeInShellOutput("AnotherTest: First");
  147. $I->seeInShellOutput('OK');
  148. $I->dontSeeInShellOutput('AnotherTest: Second');
  149. }
  150. public function runOneTestFromCest(\CliGuy $I)
  151. {
  152. $I->executeCommand('run tests/dummy/AnotherCest.php:optimistic');
  153. $I->seeInShellOutput("Optimistic");
  154. $I->dontSeeInShellOutput('Pessimistic');
  155. }
  156. public function runTestWithDataProviders(\CliGuy $I)
  157. {
  158. $I->executeCommand('run tests/unit/DataProvidersTest.php');
  159. $I->seeInShellOutput('Is triangle | "real triangle"');
  160. $I->seeInShellOutput('Is triangle | #0');
  161. $I->seeInShellOutput('Is triangle | #1');
  162. $I->seeInShellOutput('DataProvidersTest');
  163. $I->seeInShellOutput("OK");
  164. }
  165. public function runOneGroupWithDataProviders(\CliGuy $I)
  166. {
  167. $I->executeCommand('run unit -g data-providers');
  168. $I->seeInShellOutput('Is triangle | "real triangle"');
  169. $I->seeInShellOutput('Is triangle | #0');
  170. $I->seeInShellOutput('Is triangle | #1');
  171. $I->seeInShellOutput('DataProvidersTest');
  172. $I->seeInShellOutput("OK");
  173. }
  174. public function runTestWithFailFast(\CliGuy $I)
  175. {
  176. $I->executeCommand('run unit --skip-group error --no-exit');
  177. $I->seeInShellOutput('FailingTest: Me');
  178. $I->seeInShellOutput("PassingTest: Me");
  179. $I->executeCommand('run unit --fail-fast --skip-group error --no-exit');
  180. $I->seeInShellOutput('There was 1 failure');
  181. $I->dontSeeInShellOutput("PassingTest: Me");
  182. }
  183. public function runWithCustomOutputPath(\CliGuy $I)
  184. {
  185. $I->executeCommand('run dummy --xml myverycustom.xml --html myownhtmlreport.html');
  186. $I->seeFileFound('myverycustom.xml', 'tests/_output');
  187. $I->seeInThisFile('<?xml');
  188. $I->seeInThisFile('<testsuite name="dummy"');
  189. $I->seeInThisFile('<testcase name="FileExists"');
  190. $I->seeFileFound('myownhtmlreport.html', 'tests/_output');
  191. $I->dontSeeFileFound('report.xml', 'tests/_output');
  192. $I->dontSeeFileFound('report.html', 'tests/_output');
  193. }
  194. public function runTestsWithDependencyInjections(\CliGuy $I)
  195. {
  196. $I->executeCommand('run math');
  197. $I->seeInShellOutput('MathCest: Test addition');
  198. $I->seeInShellOutput('MathCest: Test subtraction');
  199. $I->seeInShellOutput('MathCest: Test square');
  200. $I->seeInShellOutput('MathTest: All');
  201. $I->seeInShellOutput('OK (');
  202. $I->dontSeeInShellOutput('fail');
  203. $I->dontSeeInShellOutput('error');
  204. }
  205. public function runErrorTest(\CliGuy $I)
  206. {
  207. $I->executeCommand('run unit ErrorTest --no-exit');
  208. $I->seeInShellOutput('There was 1 error');
  209. $I->seeInShellOutput('Array to string conversion');
  210. $I->seeInShellOutput('ErrorTest.php');
  211. }
  212. public function runTestWithException(\CliGuy $I)
  213. {
  214. $I->executeCommand('run unit ExceptionTest --no-exit -v');
  215. $I->seeInShellOutput('There was 1 error');
  216. $I->seeInShellOutput('Helllo!');
  217. $I->expect('Exceptions are not wrapped into ExceptionWrapper');
  218. $I->dontSeeInShellOutput('PHPUnit_Framework_ExceptionWrapper');
  219. $I->seeInShellOutput('RuntimeException');
  220. }
  221. public function runTestsWithSteps(\CliGuy $I)
  222. {
  223. $I->executeCommand('run scenario SuccessCept --steps');
  224. $I->seeInShellOutput(<<<EOF
  225. Scenario --
  226. I am in path "."
  227. I see file found "scenario.suite.yml"
  228. PASSED
  229. EOF
  230. );
  231. }
  232. /**
  233. * @param CliGuy $I
  234. */
  235. public function runTestWithFailedScenario(\CliGuy $I, $scenario)
  236. {
  237. if (!extension_loaded('xdebug') && !defined('HHVM_VERSION')) {
  238. $scenario->skip("Xdebug not loaded");
  239. }
  240. $I->executeCommand('run scenario FailedCept --steps --no-exit');
  241. $I->seeInShellOutput(<<<EOF
  242. FailedCept: Fail when file is not found
  243. Signature: FailedCept
  244. Test: tests/scenario/FailedCept.php
  245. Scenario --
  246. I am in path "."
  247. I see file found "games.zip"
  248. FAIL
  249. EOF
  250. );
  251. $I->expect('to see scenario trace');
  252. $I->seeInShellOutput(<<<EOF
  253. Scenario Steps:
  254. 2. \$I->seeFileFound("games.zip") at tests/scenario/FailedCept.php:5
  255. 1. \$I->amInPath(".") at tests/scenario/FailedCept.php:4
  256. EOF
  257. );
  258. }
  259. /**
  260. * @param CliGuy $I
  261. */
  262. public function runTestWithSubSteps(\CliGuy $I, $scenario)
  263. {
  264. if (!extension_loaded('xdebug') && !defined('HHVM_VERSION')) {
  265. $scenario->skip("Xdebug not loaded");
  266. }
  267. $file = "codeception" . DIRECTORY_SEPARATOR . "c3";
  268. $I->executeCommand('run scenario SubStepsCept --steps');
  269. $I->seeInShellOutput(<<<EOF
  270. Scenario --
  271. I am in path "."
  272. I see code coverage files are present
  273. EOF
  274. );
  275. // I split this assertion into two, because extra space is printed after "present" on HHVM
  276. $I->seeInShellOutput(<<<EOF
  277. I see file found "c3.php"
  278. I see file found "composer.json"
  279. I see in this file "$file"
  280. EOF
  281. );
  282. }
  283. public function runDependentCest(CliGuy $I)
  284. {
  285. $I->executeCommand('run order DependentCest --no-exit');
  286. $I->seeInShellOutput('Skipped: 1');
  287. }
  288. public function runDependentTest(CliGuy $I)
  289. {
  290. $I->executeCommand('run unit DependsTest --no-exit');
  291. $I->seeInShellOutput('Skipped: 1');
  292. $I->executeCommand('run unit --no-exit');
  293. $I->seeInShellOutput('Skipped: 2');
  294. }
  295. public function runGherkinTest(CliGuy $I)
  296. {
  297. $I->executeCommand('run scenario File.feature --steps');
  298. $I->seeInShellOutput(<<<EOF
  299. In order to test a feature
  300. As a user
  301. I need to be able to see output
  302. EOF
  303. );
  304. $I->seeInShellOutput('Given i have terminal opened');
  305. $I->seeInShellOutput('When i am in current directory');
  306. $I->seeInShellOutput('Then there is a file "scenario.suite.yml"');
  307. $I->seeInShellOutput('And there are keywords in "scenario.suite.yml"');
  308. $I->seeInShellOutput(<<<EOF
  309. | class_name | ScenarioGuy |
  310. | enabled | Filesystem |
  311. EOF
  312. );
  313. $I->seeInShellOutput('PASSED');
  314. }
  315. public function reportsCorrectFailedStep(CliGuy $I)
  316. {
  317. $I->executeCommand('run scenario File.feature -v');
  318. $I->seeInShellOutput('OK, but incomplete');
  319. $I->seeInShellOutput('Step definition for `I have only idea of what\'s going on here` not found in contexts');
  320. }
  321. public function runFailingGherkinTest(CliGuy $I)
  322. {
  323. $I->executeCommand('run scenario Fail.feature -v --no-exit');
  324. $I->seeInShellOutput('Step I see file "games.zip"');
  325. $I->seeInShellOutput('Step I see file "tools.zip"');
  326. }
  327. public function runGherkinScenarioWithMultipleStepDefinitions(CliGuy $I)
  328. {
  329. $I->executeCommand('run scenario "File.feature:Check file once more" --steps');
  330. $I->seeInShellOutput('When there is a file "scenario.suite.yml"');
  331. $I->seeInShellOutput('Then i see file "scenario.suite.yml"');
  332. $I->dontSeeInShellOutput('Step definition for `I see file "scenario.suite.yml"` not found in contexts');
  333. $I->seeInShellOutput('PASSED');
  334. }
  335. public function runGherkinScenarioOutline(CliGuy $I)
  336. {
  337. $I->executeCommand('run scenario FileExamples.feature -v');
  338. $I->seeInShellOutput('OK (3 tests');
  339. }
  340. /**
  341. * @param CliGuy $I
  342. * @after checkExampleFiles
  343. */
  344. public function runTestWithAnnotationExamples(CliGuy $I)
  345. {
  346. $I->executeCommand('run scenario ExamplesCest:filesExistsAnnotation --steps');
  347. }
  348. /**
  349. * @param CliGuy $I
  350. * @after checkExampleFiles
  351. */
  352. public function runTestWithJsonExamples(CliGuy $I)
  353. {
  354. $I->executeCommand('run scenario ExamplesCest:filesExistsByJson --steps');
  355. }
  356. /**
  357. * @param CliGuy $I
  358. * @after checkExampleFiles
  359. */
  360. public function runTestWithArrayExamples(CliGuy $I)
  361. {
  362. $I->executeCommand('run scenario ExamplesCest:filesExistsByArray --steps');
  363. }
  364. protected function checkExampleFiles(CliGuy $I)
  365. {
  366. $I->seeInShellOutput('OK (3 tests');
  367. $I->seeInShellOutput('I see file found "scenario.suite.yml"');
  368. $I->seeInShellOutput('I see file found "dummy.suite.yml"');
  369. $I->seeInShellOutput('I see file found "unit.suite.yml"');
  370. }
  371. public function runTestWithComplexExample(CliGuy $I)
  372. {
  373. $I->executeCommand('run scenario ExamplesCest:filesExistsComplexJson --debug');
  374. $I->seeInShellOutput('Files exists complex json | {"path":"."');
  375. $I->seeInShellOutput('OK (1 test');
  376. $I->seeInShellOutput('I see file found "scenario.suite.yml"');
  377. $I->seeInShellOutput('I see file found "dummy.suite.yml"');
  378. $I->seeInShellOutput('I see file found "unit.suite.yml"');
  379. }
  380. public function overrideConfigOptionsToChangeReporter(CliGuy $I)
  381. {
  382. if (!class_exists('PHPUnit_Util_Log_TeamCity')) {
  383. throw new \Codeception\Exception\Skip('Reporter does not exist for this PHPUnit version');
  384. }
  385. $I->executeCommand('run scenario --report -o "reporters: report: PHPUnit_Util_Log_TeamCity" --no-exit');
  386. $I->seeInShellOutput('##teamcity[testStarted');
  387. $I->dontSeeInShellOutput('............Ok');
  388. }
  389. public function overrideModuleOptions(CliGuy $I)
  390. {
  391. $I->executeCommand('run powers PowerIsRisingCept --no-exit');
  392. $I->seeInShellOutput('FAILURES');
  393. $I->executeCommand('run powers PowerIsRisingCept -o "modules: config: PowerHelper: has_power: true" --no-exit');
  394. $I->dontSeeInShellOutput('FAILURES');
  395. }
  396. public function runTestWithAnnotationExamplesFromGroupFileTest(CliGuy $I)
  397. {
  398. $I->executeCommand('run scenario -g groupFileTest1 --steps');
  399. $I->seeInShellOutput('OK (3 tests');
  400. }
  401. public function testsWithConditionalFails(CliGuy $I)
  402. {
  403. $I->executeCommand('run scenario ConditionalCept --no-exit');
  404. $I->seeInShellOutput('There were 3 failures');
  405. $I->seeInShellOutput('Fail File "not-a-file" not found');
  406. $I->seeInShellOutput('Fail File "not-a-dir" not found');
  407. $I->seeInShellOutput('Fail File "nothing" not found');
  408. }
  409. public function runTestWithAnnotationDataprovider(CliGuy $I)
  410. {
  411. $I->executeCommand('run scenario -g dataprovider --steps');
  412. $I->seeInShellOutput('OK (15 tests');
  413. }
  414. public function runFailedTestAndCheckOutput(CliGuy $I)
  415. {
  416. $I->executeCommand('run scenario FailedCept', false);
  417. $testPath = implode(DIRECTORY_SEPARATOR, ['tests', 'scenario', 'FailedCept.php']);
  418. $I->seeInShellOutput('1) FailedCept: Fail when file is not found');
  419. $I->seeInShellOutput('Test ' . $testPath);
  420. $I->seeInShellOutput('Step See file found "games.zip"');
  421. $I->seeInShellOutput('Fail File "games.zip" not found at ""');
  422. }
  423. public function runTestWithCustomSetupMethod(CliGuy $I)
  424. {
  425. $I->executeCommand('run powers PowerUpCest');
  426. $I->dontSeeInShellOutput('FAILURES');
  427. }
  428. public function runCestWithTwoFailedTest(CliGuy $I)
  429. {
  430. $I->executeCommand('run scenario PartialFailedCest', false);
  431. $I->seeInShellOutput('See file found "testcasetwo.txt"');
  432. $I->seeInShellOutput('See file found "testcasethree.txt"');
  433. $I->seeInShellOutput('Tests: 3,');
  434. $I->seeInShellOutput('Failures: 2.');
  435. }
  436. public function runWarningTests(CliGuy $I)
  437. {
  438. $I->executeCommand('run unit WarningTest.php', false);
  439. $I->seeInShellOutput('There was 1 warning');
  440. $I->seeInShellOutput('WarningTest::testWarningInvalidDataProvider');
  441. $I->seeInShellOutput('Tests: 1,');
  442. $I->seeInShellOutput('Warnings: 1.');
  443. }
  444. /**
  445. * @group shuffle
  446. * @param CliGuy $I
  447. */
  448. public function showSeedNumberOnShuffle(CliGuy $I)
  449. {
  450. $I->executeCommand('run unit -o "settings: shuffle: true"', false);
  451. $I->seeInShellOutput('Seed');
  452. $I->executeCommand('run unit', false);
  453. $I->dontSeeInShellOutput('Seed');
  454. }
  455. /**
  456. * @group shuffle
  457. * @param CliGuy $I
  458. */
  459. public function showSameOrderOfFilesOnSeed(CliGuy $I, \Codeception\Scenario $s)
  460. {
  461. if (DIRECTORY_SEPARATOR === '\\') {
  462. $s->skip('Failing on Windows. Need to investigate');
  463. }
  464. $I->executeCommand('run unit -o "settings: shuffle: true"', false);
  465. $I->seeInShellOutput('Seed');
  466. $output = $I->grabFromOutput('/---\n((.|\n)*?)---/m');
  467. $output = preg_replace('~\(\d\.\d+s\)~m', '', $output);
  468. $seed = $I->grabFromOutput('~\[Seed\] (.*)~');
  469. $I->executeCommand('run unit -o "settings: shuffle: true" --seed ' . $seed, false);
  470. $newOutput = $I->grabFromOutput('/---\n((.|\n)*?)---/m');
  471. $newOutput = preg_replace('~\(\d\.\d+s\)~m', '', $newOutput);
  472. $I->assertEquals($output, $newOutput, 'order of tests is the same');
  473. $I->executeCommand('run unit -o "settings: shuffle: true"', false);
  474. $newOutput = $I->grabFromOutput('/---\n((.|\n)*?)---/m');
  475. $newOutput = preg_replace('~\(\d\.\d+s\)~m', '', $newOutput);
  476. $I->assertNotEquals($output, $newOutput, 'order of tests is the same');
  477. }
  478. }