Example #1
0
 public function parseScenarioOptions($code)
 {
     $annotations = ['group', 'env', 'skip', 'incomplete', 'ignore'];
     $comments = $this->matchComments($code);
     foreach ($annotations as $annotation) {
         $values = Annotation::fetchAllFromComment($annotation, $comments);
         foreach ($values as $value) {
             call_user_func([$this->scenario, $annotation], $value);
         }
     }
     // deprecated - parsing $scenario->xxx calls
     $metaData = ['group', 'env'];
     $phpCode = $this->stripComments($code);
     $scenario = $this->scenario;
     $feature = $scenario->getFeature();
     foreach ($metaData as $call) {
         $res = preg_match_all("~\\\$scenario->{$call}.*?;~", $phpCode, $matches);
         if (!$res) {
             continue;
         }
         foreach ($matches[0] as $line) {
             // run $scenario->group or $scenario->env
             \Codeception\Lib\Deprecation::add("\$scenario->{$call}() is deprecated in favor of annotation: // @{$call}", $this->scenario->getFeature());
             eval($line);
         }
     }
 }
Example #2
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);
 }
Example #3
0
 public function attachMetadata($comments)
 {
     $this->metadata->setGroups(Annotation::fetchAllFromComment('group', $comments));
     $this->metadata->setEnv(Annotation::fetchAllFromComment('env', $comments));
     $this->metadata->setDependencies(Annotation::fetchAllFromComment('depends', $comments));
     $this->metadata->setSkip($this->firstOrNull(Annotation::fetchAllFromComment('skip', $comments)));
     $this->metadata->setIncomplete($this->firstOrNull(Annotation::fetchAllFromComment('incomplete', $comments)));
 }
Example #4
0
 protected function executeAfter($testMethod, $I)
 {
     $after = Annotation::forClass($this->testClassInstance)->method($testMethod)->fetch('after');
     if (!$after) {
         return;
     }
     $this->executeContextMethod($after, $I);
 }
Example #5
0
 public function attachMetadata($comments)
 {
     $annotations = ['group', 'env', 'skip', 'incomplete', 'ignore'];
     foreach ($annotations as $annotation) {
         $values = Annotation::fetchAllFromComment($annotation, $comments);
         foreach ($values as $value) {
             call_user_func([$this->scenario, $annotation], $value);
         }
     }
 }
Example #6
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'));
     }
 }
Example #7
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')) {
         $params = implode(', ', array_values($this->getMetadata()->getCurrent('example')));
         $this->getScenario()->setFeature($this->getScenario()->getFeature() . ' | ' . $params);
     }
 }
Example #8
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));
     }
 }
Example #9
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);
 }
Example #11
0
 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];
             }
         }
     }
 }
Example #12
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 testValueToSupportAnnotationStyle()
 {
     $values = Annotation::arrayValue('( code="200", user="******", email = "*****@*****.**")');
     $this->assertEquals(['code' => '200', 'user' => 'davert', 'email' => '*****@*****.**'], $values);
 }
Example #14
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);
 }
Example #15
0
 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;
 }
Example #16
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;
 }
 /**
  * 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;
 }
Example #18
0
 public function getEnvironment()
 {
     return Annotation::forMethod($this, $this->getName(false))->fetchAll('env');
 }
Example #19
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;
 }
Example #20
0
 public function getEnvironment()
 {
     return Annotation::forMethod($this->testClassInstance, $this->testMethod)->fetchAll('env');
 }
Example #21
0
 public function testMultipleMethodAnnotations()
 {
     $this->assertEquals(array('$var1', '$var2'), Annotation::forClass(__CLASS__)->method('testMethodAnnotation')->fetchAll('param'));
 }