ParserTest.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <?php
  2. use Codeception\Lib\Parser;
  3. /**
  4. * @group core
  5. * Class ParserTest
  6. */
  7. class ParserTest extends \Codeception\Test\Unit
  8. {
  9. /**
  10. * @var Parser
  11. */
  12. protected $parser;
  13. /**
  14. * @var \Codeception\Scenario
  15. */
  16. protected $scenario;
  17. protected $testMetadata;
  18. protected function _before()
  19. {
  20. $cept = new \Codeception\Test\Cept('demo', 'DemoCept.php');
  21. $this->testMetadata = $cept->getMetadata();
  22. $this->scenario = new Codeception\Scenario($cept);
  23. $this->parser = new Parser($this->scenario, $this->testMetadata);
  24. }
  25. public function testParsingFeature()
  26. {
  27. $code = "<?php\n \\\$I->wantTo('run this test'); ";
  28. $this->parser->parseFeature($code);
  29. $this->assertEquals('run this test', $this->scenario->getFeature());
  30. $code = "<?php\n \\\$I->wantToTest('this run'); ";
  31. $this->parser->parseFeature($code);
  32. $this->assertEquals('test this run', $this->scenario->getFeature());
  33. }
  34. public function testParsingWithWhitespace()
  35. {
  36. $code = "<?php\n \\\$I->wantTo( 'run this test' ); ";
  37. $this->parser->parseFeature($code);
  38. $this->assertEquals('run this test', $this->scenario->getFeature());
  39. }
  40. public function testScenarioOptions()
  41. {
  42. $code = <<<EOF
  43. <?php
  44. // @group davert
  45. // @env windows
  46. \$I = new AcceptanceTeser(\$scenario);
  47. EOF;
  48. $this->parser->parseScenarioOptions($code);
  49. $this->assertContains('davert', $this->testMetadata->getGroups());
  50. $this->assertContains('windows', $this->testMetadata->getEnv());
  51. }
  52. public function testCommentedInBlockScenarioOptions()
  53. {
  54. $code = <<<EOF
  55. <?php
  56. /**
  57. * @skip
  58. */
  59. EOF;
  60. $this->parser->parseScenarioOptions($code);
  61. $this->assertTrue($this->testMetadata->isBlocked());
  62. }
  63. public function testFeatureCommented()
  64. {
  65. $code = "<?php\n //\\\$I->wantTo('run this test'); ";
  66. $this->parser->parseFeature($code);
  67. $this->assertNull($this->scenario->getFeature());
  68. $code = "<?php\n /*\n \\\$I->wantTo('run this test'); \n */";
  69. $this->parser->parseFeature($code);
  70. $this->assertNull($this->scenario->getFeature());
  71. }
  72. public function testScenarioSkipOptionsHandled()
  73. {
  74. $code = "<?php\n // @skip pass along";
  75. $this->parser->parseScenarioOptions($code);
  76. $this->assertTrue($this->testMetadata->isBlocked());
  77. }
  78. public function testScenarioIncompleteOptionHandled()
  79. {
  80. $code = "<?php\n // @incomplete not ready yet";
  81. $this->parser->parseScenarioOptions($code);
  82. $this->assertTrue($this->testMetadata->isBlocked());
  83. }
  84. public function testSteps()
  85. {
  86. $code = file_get_contents(\Codeception\Configuration::projectDir().'tests/cli/UnitCept.php');
  87. $this->assertContains('$I->seeInThisFile', $code);
  88. $this->parser->parseSteps($code);
  89. $text = $this->scenario->getText();
  90. $this->assertContains("I see in this file", $text);
  91. }
  92. public function testStepsWithFriends()
  93. {
  94. $code = file_get_contents(\Codeception\Configuration::projectDir().'tests/web/FriendsCept.php');
  95. $this->assertContains('$I->haveFriend', $code);
  96. $this->parser->parseSteps($code);
  97. $text = $this->scenario->getText();
  98. $this->assertContains("jon does", $text);
  99. $this->assertContains("I have friend", $text);
  100. $this->assertContains("back to me", $text);
  101. }
  102. public function testParseFile()
  103. {
  104. $classes = Parser::getClassesFromFile(codecept_data_dir('SimpleTest.php'));
  105. $this->assertEquals(['SampleTest'], $classes);
  106. }
  107. public function testParseFileWithClass()
  108. {
  109. if (version_compare(PHP_VERSION, '5.5.0', '<')) {
  110. $this->markTestSkipped('only for php 5.5');
  111. }
  112. $classes = Parser::getClassesFromFile(codecept_data_dir('php55Test'));
  113. $this->assertEquals(['php55Test'], $classes);
  114. }
  115. public function testParseFileWithAnonymousClass()
  116. {
  117. if (version_compare(PHP_VERSION, '7.0.0', '<')) {
  118. $this->markTestSkipped('only for php 7');
  119. }
  120. $classes = Parser::getClassesFromFile(codecept_data_dir('php70Test'));
  121. $this->assertEquals(['php70Test'], $classes);
  122. }
  123. /*
  124. * https://github.com/Codeception/Codeception/issues/1779
  125. */
  126. public function testParseFileWhichUnsetsFileVariable()
  127. {
  128. $classes = Parser::getClassesFromFile(codecept_data_dir('unsetFile.php'));
  129. $this->assertEquals([], $classes);
  130. }
  131. /**
  132. * @group core
  133. * @throws \Codeception\Exception\TestParseException
  134. */
  135. public function testModernValidation()
  136. {
  137. if (PHP_MAJOR_VERSION < 7) {
  138. $this->markTestSkipped();
  139. }
  140. $this->setExpectedException('Codeception\Exception\TestParseException');
  141. Parser::load(codecept_data_dir('Invalid.php'));
  142. }
  143. /**
  144. * @group core
  145. */
  146. public function testClassesFromFile()
  147. {
  148. $classes = Parser::getClassesFromFile(codecept_data_dir('DummyClass.php'));
  149. $this->assertContains('DummyClass', $classes);
  150. $classes = Parser::getClassesFromFile(codecept_data_dir('SimpleWithDependencyInjectionCest.php'));
  151. $this->assertContains('simpleDI\\LoadedTestWithDependencyInjectionCest', $classes);
  152. $this->assertContains('simpleDI\\AnotherCest', $classes);
  153. }
  154. }