DataProviderFailuresAndExceptionsCest.php 8.5 KB

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