TestLoaderTest.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. /**
  3. * Class TestLoaderTest
  4. * @group load
  5. */
  6. class TestLoaderTest extends \PHPUnit\Framework\TestCase
  7. {
  8. /**
  9. * @var \Codeception\Test\Loader
  10. */
  11. protected $testLoader;
  12. protected function setUp()
  13. {
  14. $this->testLoader = new \Codeception\Test\Loader(['path' => \Codeception\Configuration::dataDir()]);
  15. }
  16. /**
  17. * @group core
  18. */
  19. public function testAddCept()
  20. {
  21. $this->testLoader->loadTest('SimpleCept.php');
  22. $this->assertCount(1, $this->testLoader->getTests());
  23. }
  24. public function testAddTest()
  25. {
  26. $this->testLoader->loadTest('SimpleTest.php');
  27. $this->assertCount(1, $this->testLoader->getTests());
  28. }
  29. public function testAddCeptAbsolutePath()
  30. {
  31. $this->testLoader->loadTest(codecept_data_dir('SimpleCept.php'));
  32. $this->assertCount(1, $this->testLoader->getTests());
  33. }
  34. public function testAddCeptWithoutExtension()
  35. {
  36. $this->testLoader->loadTest('SimpleCept');
  37. $this->assertCount(1, $this->testLoader->getTests());
  38. }
  39. /**
  40. * @group core
  41. */
  42. public function testLoadFileWithFewCases()
  43. {
  44. $this->testLoader->loadTest('SimpleNamespacedTest.php');
  45. $this->assertCount(3, $this->testLoader->getTests());
  46. }
  47. /**
  48. * @group core
  49. */
  50. public function testLoadAllTests()
  51. {
  52. // to autoload dependencies
  53. Codeception\Util\Autoload::addNamespace(
  54. 'Math',
  55. codecept_data_dir().'claypit/tests/_support/Math'
  56. );
  57. Codeception\Util\Autoload::addNamespace('Codeception\Module', codecept_data_dir().'claypit/tests/_support');
  58. $this->testLoader = new \Codeception\Test\Loader(['path' => codecept_data_dir().'claypit/tests']);
  59. $this->testLoader->loadTests();
  60. $testNames = $this->getTestNames($this->testLoader->getTests());
  61. $this->assertContainsTestName('AnotherCept', $testNames);
  62. $this->assertContainsTestName('MageGuildCest:darkPower', $testNames);
  63. $this->assertContainsTestName('FailingTest:testMe', $testNames);
  64. $this->assertContainsTestName('MathCest:testAddition', $testNames);
  65. $this->assertContainsTestName('MathTest:testAll', $testNames);
  66. }
  67. protected function getTestNames($tests)
  68. {
  69. $testNames = [];
  70. foreach ($tests as $test) {
  71. $testNames[] = \Codeception\Test\Descriptor::getTestSignature($test);
  72. }
  73. return $testNames;
  74. }
  75. protected function assertContainsTestName($name, $testNames)
  76. {
  77. $this->assertContains($name, $testNames, "$name not found in tests");
  78. }
  79. public function testDataProviderReturningArray()
  80. {
  81. $this->testLoader->loadTest('SimpleWithDataProviderArrayCest.php');
  82. $tests = $this->testLoader->getTests();
  83. /** @var \PHPUnit\Framework\DataProviderTestSuite $firstTest */
  84. $firstTest = $tests[0];
  85. $this->assertEquals(5, $firstTest->count());
  86. }
  87. public function testDataProviderReturningGenerator()
  88. {
  89. $this->testLoader->loadTest('SimpleWithDataProviderYieldGeneratorCest.php');
  90. $tests = $this->testLoader->getTests();
  91. /** @var \PHPUnit\Framework\DataProviderTestSuite $firstTest */
  92. $firstTest = $tests[0];
  93. $this->assertEquals(5, $firstTest->count());
  94. }
  95. }