DataProviderFailuresAndExceptionsCest.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. class DataProviderFailuresAndExceptionsCest
  3. {
  4. /**
  5. * @param CliGuy $I
  6. */
  7. protected function moveToPath(\CliGuy $I)
  8. {
  9. $I->amInPath('tests/data/dataprovider_failures_and_exceptions');
  10. }
  11. /**
  12. * This looks at only the contents of stdout when there is a failure in parsing a dataProvider annotation.
  13. * When there is a failure all the useful information should go to stderr, so stdout is left with
  14. * only the version headers.
  15. *
  16. * @param CliGuy $I
  17. * @before moveToPath
  18. */
  19. public function runTestWithDataProvidersFailureStdout(\CliGuy $I)
  20. {
  21. /**
  22. * On windows /dev/null is NUL so detect running OS and return the appropriate string for redirection.
  23. * As some systems have php_uname and co disabled, we use the DIRECTORY_SEPARATOR constant to
  24. * figure out if we are running on windows or not.
  25. */
  26. $devNull = (DIRECTORY_SEPARATOR === '\\')?'NUL':'/dev/null';
  27. $I->executeCommand('run -n -v unit DataProvidersFailureCest 2> '.$devNull,false);
  28. // We should only see the version headers in stdout when there is this kind of failure.
  29. $I->canSeeShellOutputMatches('/^Codeception PHP Testing Framework v[0-9\.]+\nPowered by PHPUnit .+ by Sebastian Bergmann and contributors\./');
  30. $I->seeResultCodeIs(1);
  31. }
  32. /**
  33. * This redirects stderr to stdout so that we can test the contents of stderr. Stderr is where all the interesting
  34. * information should be when there is a failure.
  35. *
  36. * @param CliGuy $I
  37. * @before moveToPath
  38. */
  39. public function runTestWithDataProvidersFailureStderr(\CliGuy $I)
  40. {
  41. $I->executeCommand('run -n unit DataProvidersFailureCest 2>&1',false);
  42. $I->seeInShellOutput('Couldn\'t parse test');
  43. $I->seeInShellOutput('DataProvider \'rectangle\' for DataProvidersFailureCest->testIsTriangle');
  44. $I->seeInShellOutput('Make sure that the dataprovider exist within the test class.');
  45. // For Unit tests PHPUnit throws the errors, this confirms that we haven't ended up running PHPUnit test Loader
  46. $I->dontSeeInShellOutput('PHPUnit_Framework_Warning');
  47. $I->dontSeeInShellOutput('The data provider specified for DataProvidersFailureCest::testIsTriangle');
  48. $I->dontSeeInShellOutput('Method rectangle does not exist');
  49. $I->dontSeeInShellOutput('FAILURES!');
  50. $I->dontSeeInShellOutput('WARNINGS!');
  51. $I->dontSeeInShellOutput('OK');
  52. $I->dontSeeInShellOutput('Tests: 1, Assertions: 0, Warnings: 1.');
  53. // In normal mode the Exception trace should not appear.
  54. $I->dontSeeInShellOutput('Exception trace');
  55. $I->seeResultCodeIs(1);
  56. }
  57. /**
  58. * This adds the -v to the stderr test which should just add the Exception Trace to the output.
  59. *
  60. * @param CliGuy $I
  61. * @before moveToPath
  62. */
  63. public function runTestWithDataProvidersFailureStderrVerbose(\CliGuy $I)
  64. {
  65. $I->executeCommand('run -n unit DataProvidersFailureCest -v 2>&1',false);
  66. $I->seeInShellOutput('Couldn\'t parse test');
  67. $I->seeInShellOutput('DataProvider \'rectangle\' for DataProvidersFailureCest->testIsTriangle');
  68. $I->seeInShellOutput('Make sure that the dataprovider exist within the test class.');
  69. // For Unit tests PHPUnit throws the errors, this confirms that we haven't ended up running PHPUnit test Loader
  70. $I->dontSeeInShellOutput('PHPUnit_Framework_Warning');
  71. $I->dontSeeInShellOutput('The data provider specified for DataProvidersFailureCest::testIsTriangle');
  72. $I->dontSeeInShellOutput('Method rectangle does not exist');
  73. $I->dontSeeInShellOutput('FAILURES!');
  74. $I->dontSeeInShellOutput('WARNINGS!');
  75. $I->dontSeeInShellOutput('OK');
  76. $I->dontSeeInShellOutput('Tests: 1, Assertions: 0, Warnings: 1.');
  77. // In verbose mode the Exception trace should be output.
  78. $I->seeInShellOutput('[Codeception\Exception\TestParseException]');
  79. $I->seeInShellOutput('Exception trace');
  80. $I->seeResultCodeIs(1);
  81. }
  82. /**
  83. * This looks at only the contents of stdout when there is an exception thrown when executing a dataProvider
  84. * function.
  85. * When exception thrown all the useful information should go to stderr, so stdout is left with nothing.
  86. *
  87. * @param CliGuy $I
  88. * @before moveToPath
  89. */
  90. public function runTestWithDataProvidersExceptionStdout(\CliGuy $I)
  91. {
  92. /**
  93. * On windows /dev/null is NUL so detect running OS and return the appropriate string for redirection.
  94. * As some systems have php_uname and co disabled, we use the DIRECTORY_SEPARATOR constant to
  95. * figure out if we are running on windows or not.
  96. */
  97. $devNull = (DIRECTORY_SEPARATOR === '\\')?'NUL':'/dev/null';
  98. $I->executeCommand('run -n unit DataProvidersExceptionCest -v 2> '.$devNull, false);
  99. // Depending on the test environment, we either see nothing or just the headers here.
  100. $I->canSeeShellOutputMatches('/^Codeception PHP Testing Framework v[0-9\.]+\nPowered by PHPUnit .+ by Sebastian Bergmann and contributors\./');
  101. $I->seeResultCodeIs(1);
  102. }
  103. /**
  104. * This redirects stderr to stdout so that we can test the contents of stderr. Stderr is where all the interesting
  105. * information should be when there is a failure.
  106. *
  107. * @param CliGuy $I
  108. * @before moveToPath
  109. */
  110. public function runTestWithDataProvidersExceptionStderr(\CliGuy $I)
  111. {
  112. $I->executeCommand('run -n unit DataProvidersExceptionCest 2>&1', false);
  113. // For Unit tests PHPUnit throws the errors, this confirms that we haven't ended up running PHPUnit test Loader
  114. $I->dontSeeInShellOutput('There was 1 warning');
  115. $I->dontSeeInShellOutput('PHPUnit_Framework_Warning');
  116. $I->dontSeeInShellOutput('The data provider specified for DataProvidersExceptionTest::testIsTriangle');
  117. $I->dontSeeInShellOutput('FAILURES!');
  118. $I->dontSeeInShellOutput('WARNINGS!');
  119. $I->dontSeeInShellOutput('OK');
  120. // We should not see the messages related to a failure to parse the dataProvider function
  121. $I->dontSeeInShellOutput('[Codeception\Exception\TestParseException]');
  122. $I->dontSeeInShellOutput('Couldn\'t parse test');
  123. $I->dontSeeInShellOutput('DataProvider \'rectangle\' for DataProvidersFailureCest->testIsTriangle ');
  124. // We should just see the message
  125. $I->seeInShellOutput('Something went wrong!!!');
  126. // We don't have the verbose flag set, so there should be no trace.
  127. $I->dontSeeInShellOutput('Exception trace:');
  128. $I->seeResultCodeIs(1);
  129. }
  130. /**
  131. * This adds the -v to the stderr test which should just add the Exception Trace to the output of stderr.
  132. *
  133. * @param CliGuy $I
  134. * @before moveToPath
  135. */
  136. public function runTestWithDataProvidersExceptionStderrVerbose(\CliGuy $I)
  137. {
  138. $I->executeCommand('run -n unit DataProvidersExceptionCest -v 2>&1', false);
  139. // For Unit tests PHPUnit throws the errors, this confirms that we haven't ended up running PHPUnit test Loader
  140. $I->dontSeeInShellOutput('There was 1 warning');
  141. $I->dontSeeInShellOutput('PHPUnit_Framework_Warning');
  142. $I->dontSeeInShellOutput('The data provider specified for DataProvidersExceptionTest::testIsTriangle');
  143. $I->dontSeeInShellOutput('FAILURES!');
  144. $I->dontSeeInShellOutput('WARNINGS!');
  145. $I->dontSeeInShellOutput('OK');
  146. // We should not see the messages related to a failure to parse the dataProvider function
  147. $I->dontSeeInShellOutput('[Codeception\Exception\TestParseException]');
  148. $I->dontSeeInShellOutput('Couldn\'t parse test');
  149. $I->dontSeeInShellOutput('DataProvider \'rectangle\' for DataProvidersFailureCest->testIsTriangle is ');
  150. // We should just see the message
  151. $I->seeInShellOutput('Something went wrong!!!');
  152. // We have the verbose flag set, so there should be a trace.
  153. $I->seeInShellOutput('[Exception]');
  154. $I->seeInShellOutput('Exception trace:');
  155. $I->seeResultCodeIs(1);
  156. }
  157. }