Exemplo n.º 1
0
 public function loadTests($file)
 {
     Parser::load($file);
     $testClasses = Parser::getClassesFromFile($file);
     foreach ($testClasses as $testClass) {
         if (substr($testClass, -strlen('Cest')) !== 'Cest') {
             continue;
         }
         if (!(new \ReflectionClass($testClass))->isInstantiable()) {
             continue;
         }
         $unit = new $testClass();
         $methods = get_class_methods($testClass);
         foreach ($methods as $method) {
             if (strpos($method, '_') === 0) {
                 continue;
             }
             $rawExamples = Annotation::forMethod($unit, $method)->fetchAll('example');
             if (count($rawExamples)) {
                 $examples = array_map(function ($v) {
                     return Annotation::arrayValue($v);
                 }, $rawExamples);
                 $dataProvider = new \PHPUnit_Framework_TestSuite_DataProvider();
                 foreach ($examples as $k => $example) {
                     if ($example === null) {
                         throw new TestParseException($file, "Example for {$testClass}->{$method} contains invalid data:\n" . $rawExamples[$k] . "\n" . "Make sure this is a valid JSON (Hint: \"-char for strings) or a single-line annotation in Doctrine-style");
                     }
                     $test = new CestFormat($unit, $method, $file);
                     $test->getMetadata()->setCurrent(['example' => $example]);
                     $groups = Annotation::forMethod($unit, $method)->fetchAll('group');
                     $dataProvider->addTest($test, $groups);
                 }
                 $this->tests[] = $dataProvider;
                 continue;
             }
             $this->tests[] = new CestFormat($unit, $method, $file);
         }
     }
 }
 public function before(\Codeception\Event\SuiteEvent $se)
 {
     $suite = $se->getSuite();
     $tests = $suite->tests();
     foreach ($tests as $id => $test) {
         if (is_a($test, 'Codeception\\Test\\Cest')) {
             $testClass = $test->getTestClass();
             $testClassName = get_class($testClass);
             $testMethod = $test->getTestMethod();
             $testFile = $test->getMetadata()->getFilename();
             $testActor = $test->getMetadata()->getCurrent('actor');
             $dataMethod = Annotation::forMethod($testClass, $testMethod)->fetch('dataprovider');
             try {
                 if (empty($dataMethod)) {
                     continue;
                 }
                 if (false === is_callable([$testClass, $dataMethod])) {
                     throw new \Exception();
                 }
                 $dataProvider = new \PHPUnit_Framework_TestSuite_DataProvider();
                 $examples = $testClassName::$dataMethod();
                 foreach ($examples as $example) {
                     if ($example === null) {
                         throw new TestParseException($testFile, "Invalid values format returned by DataProvider {$dataMethod} for {$testClassName}->{$testMethod}.");
                     }
                     $dataTest = new CestFormat($testClass, $testMethod, $testFile);
                     $dataTest->getMetadata()->setServices(['di' => $test->getMetadata()->getService('di'), 'dispatcher' => $test->getMetadata()->getService('dispatcher'), 'modules' => $test->getMetadata()->getService('modules')]);
                     $dataTest->getMetadata()->setCurrent(['actor' => $testActor, 'example' => $example]);
                     $step = new Comment('', $dataTest->getMetadata()->getCurrent('example'));
                     $dataTest->getScenario()->setFeature($dataTest->getSpecFromMethod() . ' | ' . $step->getArgumentsAsString(100));
                     $groups = Annotation::forMethod($testClass, $testMethod)->fetchAll('group');
                     $dataProvider->addTest($dataTest, $groups);
                 }
                 $tests[$id] = $dataProvider;
             } catch (\Exception $e) {
                 throw new TestParseException($testFile, "DataProvider {$dataMethod} for {$testClassName}->{$testMethod} is invalid or not callable." . PHP_EOL . "Make sure this is a public static function.");
             }
         }
     }
     $suite->setTests($tests);
 }