rest /vendor/codeception/codeception/src/Codeception/TestCase/Cest.php

Language PHP Lines 207
MD5 Hash ee7dd29f51aef0afd7f4f0774fd69dce
Repository https://gitlab.com/jhonn/rest.git View Raw File
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
<?php

namespace Codeception\TestCase;

use Codeception\Events;
use Codeception\Event\TestEvent;
use Codeception\Util\Annotation;

class Cest extends \Codeception\TestCase implements
    Interfaces\ScenarioDriven,
    Interfaces\Descriptive,
    Interfaces\Reported,
    Interfaces\Configurable
{
    use Shared\Actor;
    use Shared\Dependencies;
    use Shared\ScenarioPrint;

    protected $testClassInstance = null;
    protected $testMethod = null;

    public function __construct(array $data = array(), $dataName = '')
    {
        parent::__construct('testCodecept', $data, $dataName);
    }

    public function getName($withDataSet = TRUE)
    {
        return $this->testMethod;
    }

    public function preload()
    {
        $this->scenario->setFeature($this->getSpecFromMethod());
        $code = $this->getRawBody();
        $params = (new \ReflectionMethod($this->testClassInstance, $this->testMethod))->getParameters();
        $scenarioVar = isset($params[1]) ? $params[1]->getName() : null;
        $this->parser->parseFeature($code);
        $this->parser->parseScenarioOptions($code, $scenarioVar);
        $this->fire(Events::TEST_PARSED, new TestEvent($this));
    }

    public function getRawBody()
    {
        $method = new \ReflectionMethod($this->testClassInstance, $this->testMethod);
        $start_line = $method->getStartLine() - 1; // it's actually - 1, otherwise you wont get the function() block
        $end_line = $method->getEndLine();
        $source = file($method->getFileName());
        return implode("", array_slice($source, $start_line, $end_line - $start_line));
    }

    public function testCodecept()
    {
        $this->fire(Events::TEST_BEFORE, new TestEvent($this));

        $this->scenario->run();
        $I = $this->makeIObject();

        try {
            $this->executeBefore($this->testMethod, $I);
            $this->executeTestMethod($I);
            $this->executeAfter($this->testMethod, $I);
        } catch (\Exception $e) {
            // fails and errors are now handled by Codeception\PHPUnit\Listener
            throw $e;
        }

        $this->fire(Events::TEST_AFTER, new TestEvent($this));
    }

    protected function executeBefore($testMethod, $I)
    {
        if (method_exists($this->testClassInstance, '_before')) {
            $this->testClassInstance->_before($I);
        }

        $annotations = \PHPUnit_Util_Test::parseTestMethodAnnotations(get_class($this->testClassInstance), $testMethod);
        if (!empty($annotations['method']['before'])) {
            foreach ($annotations['method']['before'] as $m) {
                $this->executeContextMethod(trim($m), $I);
            }
        }
    }

    protected function executeAfter($testMethod, $I)
    {
        $annotations = \PHPUnit_Util_Test::parseTestMethodAnnotations(get_class($this->testClassInstance), $testMethod);
        if (!empty($annotations['method']['after'])) {
            foreach ($annotations['method']['after'] as $m) {
                $this->executeContextMethod(trim($m), $I);
            }
        }

        if (method_exists($this->testClassInstance, '_after')) {
            $this->testClassInstance->_after($I);
        }
    }

    protected function executeContextMethod($context, $I)
    {
        if (method_exists($this->testClassInstance, $context)) {
            $this->executeBefore($context, $I);
            $contextMethod = new \ReflectionMethod($this->testClassInstance, $context);
            $contextMethod->setAccessible(true);
            $contextMethod->invoke($this->testClassInstance, $I);
            $this->executeAfter($context, $I);
            return;
        }

        throw new \LogicException(
            "Method $context defined in annotation but does not exists in " . get_class($this->testClassInstance)
        );
    }

    protected function makeIObject()
    {
        $className = '\\' . $this->actor;
        $I = new $className($this->scenario);
        $spec = $this->getSpecFromMethod();

        if ($spec) {
            $I->wantTo($spec);
        }
        return $I;
    }

    protected function executeTestMethod($I)
    {
        $testMethodSignature = array($this->testClassInstance, $this->testMethod);
        if (! is_callable($testMethodSignature)) {
            throw new \Exception("Method {$this->testMethod} can't be found in tested class");
        }
        call_user_func($testMethodSignature, $I, $this->scenario);
    }

    public function getTestClass()
    {
        return $this->testClassInstance;
    }

    public function getTestMethod()
    {
        return $this->testMethod;
    }

    public function getSpecFromMethod()
    {
        $text = $this->testMethod;
        $text = preg_replace('/([A-Z]+)([A-Z][a-z])/', '\\1 \\2', $text);
        $text = preg_replace('/([a-z\d])([A-Z])/', '\\1 \\2', $text);
        $text = strtolower($text);
        return $text;
    }

    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;
    }

    public function configEnv($env)
    {
        $this->testClassInstance->env = $env;
        return $this;
    }

    public function getSignature()
    {
        return get_class($this->getTestClass()) . "::" . $this->getTestMethod();
    }

    public function getFileName()
    {
        return $this->testFile;
    }

    public function toString()
    {
        return $this->getFeature(). " (".$this->getSignature().")";
    }
    
    /**
     * @return array
     */
    public function getReportFields()
    {
        return [
            'file' => $this->getFileName(),
            'name' => $this->getTestMethod(),
            'class' => get_class($this->getTestClass()),
            'feature' => $this->getFeature()
        ];
    }

}
Back to Top