Exemple #1
0
 public function preload()
 {
     $this->scenario->setFeature($this->getSpecFromMethod());
     $code = $this->getSourceCode();
     $this->parser->parseFeature($code);
     $this->parser->attachMetadata(Annotation::forMethod($this->testClassInstance, $this->testMethod)->raw());
     $this->getMetadata()->getService('di')->injectDependencies($this->testClassInstance);
 }
Exemple #2
0
 protected function enhancePhpunitTest(\PHPUnit_Framework_TestCase $test)
 {
     $className = get_class($test);
     $methodName = $test->getName(false);
     $dependencies = \PHPUnit_Util_Test::getDependencies($className, $methodName);
     $test->setDependencies($dependencies);
     if ($test instanceof TestCaseFormat) {
         $test->getMetadata()->setDependencies($dependencies);
         $test->getMetadata()->setEnv(Annotation::forMethod($test, $methodName)->fetchAll('env'));
     }
 }
 public function preload()
 {
     $this->scenario->setFeature($this->getSpecFromMethod());
     $code = $this->getSourceCode();
     $this->parser->parseFeature($code);
     $this->parser->attachMetadata(Annotation::forMethod($this->testClassInstance, $this->testMethod)->raw());
     $this->getMetadata()->getService('di')->injectDependencies($this->testClassInstance);
     // add example params to feature
     if ($this->getMetadata()->getCurrent('example')) {
         $params = implode(', ', array_values($this->getMetadata()->getCurrent('example')));
         $this->getScenario()->setFeature($this->getScenario()->getFeature() . ' | ' . $params);
     }
 }
Exemple #4
0
 public function preload()
 {
     $this->scenario->setFeature($this->getSpecFromMethod());
     $code = $this->getSourceCode();
     $this->parser->parseFeature($code);
     $this->parser->attachMetadata(Annotation::forMethod($this->testClassInstance, $this->testMethod)->raw());
     $this->getMetadata()->getService('di')->injectDependencies($this->testClassInstance);
     // add example params to feature
     if ($this->getMetadata()->getCurrent('example')) {
         $step = new Comment('', $this->getMetadata()->getCurrent('example'));
         $this->getScenario()->setFeature($this->getScenario()->getFeature() . ' | ' . $step->getArgumentsAsString(100));
     }
 }
Exemple #5
0
 protected function enhancePhpunitTest(\PHPUnit_Framework_TestCase $test)
 {
     $className = get_class($test);
     $methodName = $test->getName(false);
     $dependencies = \PHPUnit_Util_Test::getDependencies($className, $methodName);
     $test->setDependencies($dependencies);
     if ($test instanceof UnitFormat) {
         $feature = $test->getName();
         $feature = preg_replace('/([A-Z]+)([A-Z][a-z])/', '\\1 \\2', $feature);
         $feature = preg_replace('/([a-z\\d])([A-Z])/', '\\1 \\2', $feature);
         $test->getMetadata()->setFeature(strtolower($feature));
         $test->getMetadata()->setDependencies($dependencies);
         $test->getMetadata()->setEnv(Annotation::forMethod($test, $methodName)->fetchAll('env'));
     }
 }
 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);
 }
 protected function addSteps(array $contexts, $group = 'default')
 {
     $this->steps[$group] = [];
     foreach ($contexts as $context) {
         $methods = get_class_methods($context);
         if (!$methods) {
             continue;
         }
         foreach ($methods as $method) {
             $annotation = Annotation::forMethod($context, $method);
             foreach (['Given', 'When', 'Then'] as $type) {
                 $pattern = $annotation->fetch($type);
                 if (!$pattern) {
                     continue;
                 }
                 $pattern = $this->makePlaceholderPattern($pattern);
                 $this->steps[$group][$pattern] = [$context, $method];
             }
         }
     }
 }
 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);
         }
     }
 }
Exemple #9
0
 protected function moduleActionBelongsToPart($module, $action, $part)
 {
     if (!is_array($part)) {
         $part = [strtolower($part)];
     }
     $part = array_map('strtolower', $part);
     $parts = Annotation::forMethod($module, $action)->fetchAll('part');
     $usedParts = array_intersect($parts, $part);
     return !empty($usedParts);
 }
Exemple #10
0
 protected function createTestFromCestMethod($cestInstance, $methodName, $file)
 {
     if (strpos($methodName, '_') === 0 or $methodName == '__construct') {
         return null;
     }
     $testClass = get_class($cestInstance);
     $cest = new Cest();
     $cest->configName($methodName)->configFile($file)->config('testClassInstance', $cestInstance)->config('testMethod', $methodName)->initConfig();
     $cest->getScenario()->env(Annotation::forMethod($testClass, $methodName)->fetchAll('env'));
     $cest->setDependencies(\PHPUnit_Util_Test::getDependencies($testClass, $methodName));
     return $cest;
 }
Exemple #11
0
 public function getEnvironment()
 {
     return Annotation::forMethod($this, $this->getName(false))->fetchAll('env');
 }
Exemple #12
0
 public function configActor($actor)
 {
     foreach (['actor', 'guy'] as $annotation) {
         $definedActor = Annotation::forMethod($this->testClassInstance, $this->testMethod)->fetch($annotation);
         if (!$definedActor) {
             $definedActor = Annotation::forClass($this->testClassInstance)->fetch($annotation);
         }
         if ($definedActor) {
             $this->actor = $definedActor;
             return $this;
         }
     }
     $this->actor = $actor;
     return $this;
 }
Exemple #13
0
 public function getEnvironment()
 {
     return Annotation::forMethod($this->testClassInstance, $this->testMethod)->fetchAll('env');
 }
 /**
  * Should a method be included as an action?
  *
  * @param \Codeception\Module $module
  * @param \ReflectionMethod $method
  * @param array|null $configuredParts
  * @return bool
  */
 private function includeMethodAsAction($module, $method, $configuredParts = null)
 {
     // Filter out excluded actions
     if (in_array($method->name, $module::$excludeActions)) {
         return false;
     }
     // Keep only the $onlyActions if they are specified
     if ($module::$onlyActions && !in_array($method->name, $module::$onlyActions)) {
         return false;
     }
     // Do not include inherited actions if the static $includeInheritedActions property is set to false.
     // However, if an inherited action is also specified in the static $onlyActions property
     // it should be included as an action.
     if (!$module::$includeInheritedActions) {
         if (!in_array($method->name, $module::$onlyActions)) {
             return false;
         }
         if ($method->getDeclaringClass()->getName() == get_class($module)) {
             return false;
         }
     }
     // Do not include hidden methods, methods with a name starting with an underscore
     if (strpos($method->name, '_') === 0) {
         return false;
     }
     // If a part is configured for the module, only include actions from that part
     if ($configuredParts) {
         $moduleParts = Annotation::forMethod($module, $method->name)->fetchAll('part');
         if (!array_uintersect($moduleParts, $configuredParts, 'strcasecmp')) {
             return false;
         }
     }
     return true;
 }
 protected function createTestFromCestMethod($cestInstance, $methodName, $file, $guy)
 {
     $testClass = get_class($cestInstance);
     if (strpos($methodName, '_') === 0) {
         return;
     }
     $overriddenGuy = Annotation::forMethod($testClass, $methodName)->fetch('guy');
     if (!$overriddenGuy) {
         $overriddenGuy = Annotation::forClass($testClass)->fetch('guy');
     }
     if ($overriddenGuy) {
         $guy = $overriddenGuy;
     }
     $cest = new TestCase\Cest($this->dispatcher, array('name' => $methodName, 'instance' => $cestInstance, 'method' => $methodName, 'file' => $file, 'bootstrap' => $this->settings['bootstrap'], 'guy' => $guy));
     $cest->getScenario()->env(Annotation::forMethod($testClass, $methodName)->fetchAll('env'));
     $cest->getScenario()->groups(\PHPUnit_Util_Test::getGroups($testClass, $methodName));
     $cest->setDependencies(\PHPUnit_Util_Test::getDependencies($testClass, $methodName));
     $cest->preload();
     return $cest;
 }