TestLoaderTest.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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->assertEquals(1, count($this->testLoader->getTests()));
  23. }
  24. public function testAddTest()
  25. {
  26. $this->testLoader->loadTest('SimpleTest.php');
  27. $this->assertEquals(1, count($this->testLoader->getTests()));
  28. }
  29. public function testAddCeptAbsolutePath()
  30. {
  31. $this->testLoader->loadTest(codecept_data_dir('SimpleCept.php'));
  32. $this->assertEquals(1, count($this->testLoader->getTests()));
  33. }
  34. public function testAddCeptWithoutExtension()
  35. {
  36. $this->testLoader->loadTest('SimpleCept');
  37. $this->assertEquals(1, count($this->testLoader->getTests()));
  38. }
  39. /**
  40. * @group core
  41. */
  42. public function testLoadFileWithFewCases()
  43. {
  44. $this->testLoader->loadTest('SimpleNamespacedTest.php');
  45. $this->assertEquals(3, count($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->assertNotSame(false, array_search($name, $testNames), "$name not found in tests");
  78. }
  79. }