Skip to content

Commit 56aece9

Browse files
committed
refactored test classes
1 parent 23b0cf6 commit 56aece9

22 files changed

Lines changed: 346 additions & 289 deletions

File tree

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
<?php
2-
namespace Codeception;
2+
namespace Codeception\Lib;
33

4-
use Codeception\Util\Debug;
4+
use Codeception\Scenario;
5+
use Codeception\Step;
56

67
class Parser {
78

@@ -96,4 +97,39 @@ protected function addCommentStep($comment)
9697
$this->scenario->addStep(new \Codeception\Step\Comment($comment,array()));
9798
}
9899

100+
public static function getClassesFromFile($file)
101+
{
102+
require_once $file;
103+
$sourceCode = file_get_contents($file);
104+
$classes = array();
105+
$tokens = token_get_all($sourceCode);
106+
$namespace = '';
107+
108+
for ($i = 0; $i < count($tokens); $i++) {
109+
if ($tokens[$i][0] === T_NAMESPACE) {
110+
$namespace = '';
111+
for ($j = $i + 1; $j < count($tokens); $j++) {
112+
if ($tokens[$j][0] === T_STRING) {
113+
$namespace .= $tokens[$j][1] . '\\';
114+
} else {
115+
if ($tokens[$j] === '{' || $tokens[$j] === ';') {
116+
break;
117+
}
118+
}
119+
}
120+
}
121+
122+
if ($tokens[$i][0] === T_CLASS) {
123+
for ($j = $i + 1; $j < count($tokens); $j++) {
124+
if ($tokens[$j] === '{') {
125+
$classes[] = $namespace . $tokens[$i + 2][1];
126+
break;
127+
}
128+
}
129+
}
130+
}
131+
132+
return $classes;
133+
}
134+
99135
}

src/Codeception/PHPUnit/Log/JUnit.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,6 @@ public function startTest(\PHPUnit_Framework_Test $test)
1010

1111
$this->currentTestCase = $this->document->createElement('testcase');
1212

13-
if ($test instanceof \Codeception\TestCase\Cept) {
14-
$this->currentTestCase->setAttribute('file', $test->getFileName());
15-
return;
16-
}
17-
1813
if ($test instanceof \Codeception\TestCase) {
1914
$class = new \ReflectionClass($test->getTestClass());
2015
$methodName = $test->getTestMethod();
@@ -33,7 +28,7 @@ public function endTest(\PHPUnit_Framework_Test $test, $time) {
3328

3429
if ($test instanceof \Codeception\TestCase\Cept) {
3530
$this->currentTestCase->setAttribute(
36-
'name', $test->toString()
31+
'name', htmlspecialchars($test->toString())
3732
);
3833
}
3934
return parent::endTest($test, $time);

src/Codeception/Subscriber/Console.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
use Codeception\Event\TestEvent;
99
use Codeception\Exception\ConditionalAssertionFailed;
1010
use Codeception\SuiteManager;
11-
use Codeception\TestCase\ScenarioDriven;
11+
use Codeception\TestCase\Interfaces\ScenarioDriven;
1212
use Codeception\TestCase;
1313
use Codeception\Lib\Console\Message;
1414
use Codeception\Lib\Console\Output;
@@ -101,7 +101,7 @@ public function startTest(TestEvent $e)
101101
public function before(TestEvent $e)
102102
{
103103
$test = $e->getTest();
104-
$filename = $test->getFileName();
104+
$filename = $test->getSignature();
105105

106106
if ($test->getFeature()) {
107107
$this->message("Trying to <focus>%s</focus> (%s) ")

src/Codeception/SuiteManager.php

Lines changed: 22 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Codeception\Event\Suite;
66
use Codeception\Event\SuiteEvent;
77
use Codeception\Lib\Generator\Actor;
8+
use Codeception\Lib\Parser;
89
use Codeception\Util\Annotation;
910
use Symfony\Component\EventDispatcher\EventDispatcher;
1011
use Symfony\Component\Finder\Finder;
@@ -97,7 +98,7 @@ protected function createSuite($name)
9798

9899
public function addTest($path)
99100
{
100-
$testClasses = $this->getClassesFromFile($path);
101+
$testClasses = Parser::getClassesFromFile($path);
101102

102103
foreach ($testClasses as $testClass) {
103104
$reflected = new \ReflectionClass($testClass);
@@ -124,11 +125,12 @@ public function addCept($file)
124125
$name = $this->relativeName($file);
125126
$this->tests[$name] = $file;
126127

127-
$cept = new TestCase\Cept($this->dispatcher, array(
128-
'name' => $name,
129-
'file' => $file,
130-
'bootstrap' => $this->settings['bootstrap']
131-
));
128+
$cept = new TestCase\Cept();
129+
$cept->configDispatcher($this->dispatcher)
130+
->configName($name)
131+
->configFile($file)
132+
->configBootstrap($this->settings['bootstrap'])
133+
->initConfig();
132134

133135
$cept->preload();
134136

@@ -143,7 +145,7 @@ public function addCest($file)
143145
$name = $this->relativeName($file);
144146
$this->tests[$name] = $file;
145147

146-
$testClasses = $this->getClassesFromFile($file);
148+
$testClasses = Parser::getClassesFromFile($file);
147149

148150
foreach ($testClasses as $testClass) {
149151
$reflected = new \ReflectionClass($testClass);
@@ -223,43 +225,6 @@ public function loadTests()
223225
}
224226
}
225227

226-
protected function getClassesFromFile($file)
227-
{
228-
$loaded_classes = get_declared_classes();
229-
require_once $file;
230-
231-
$sourceCode = file_get_contents($file);
232-
$classes = array();
233-
$tokens = token_get_all($sourceCode);
234-
$namespace = '';
235-
236-
for ($i = 0; $i < count($tokens); $i++) {
237-
if ($tokens[$i][0] === T_NAMESPACE) {
238-
$namespace = '';
239-
for ($j = $i + 1; $j < count($tokens); $j++) {
240-
if ($tokens[$j][0] === T_STRING) {
241-
$namespace .= $tokens[$j][1] . '\\';
242-
} else {
243-
if ($tokens[$j] === '{' || $tokens[$j] === ';') {
244-
break;
245-
}
246-
}
247-
}
248-
}
249-
250-
if ($tokens[$i][0] === T_CLASS) {
251-
for ($j = $i + 1; $j < count($tokens); $j++) {
252-
if ($tokens[$j] === '{') {
253-
$classes[] = $namespace . $tokens[$i + 2][1];
254-
break;
255-
}
256-
}
257-
}
258-
}
259-
260-
return $classes;
261-
}
262-
263228
protected function createTestFromPhpUnitMethod(\ReflectionClass $class, \ReflectionMethod $method)
264229
{
265230
if (!\PHPUnit_Framework_TestSuite::isTestMethod($method)) {
@@ -296,9 +261,10 @@ protected function enhancePhpunitTest(\PHPUnit_Framework_TestCase $test)
296261
? $this->settings['namespace'] . '\\' . $this->settings['class_name']
297262
: $this->settings['class_name'];
298263

299-
$test->setBootstrap($this->settings['bootstrap']);
300-
$test->setDispatcher($this->dispatcher);
301-
$test->setGuyClass($guy);
264+
$test->configBootstrap($this->settings['bootstrap'])
265+
->configDispatcher($this->dispatcher)
266+
->configActor($guy)
267+
->initConfig();
302268

303269
$test->getScenario()->groups(\PHPUnit_Util_Test::getGroups($className, $methodName));
304270
$test->getScenario()->env(Annotation::forMethod($className, $methodName)->fetchAll('env'));
@@ -311,22 +277,15 @@ protected function createTestFromCestMethod($cestInstance, $methodName, $file, $
311277
return;
312278
}
313279

314-
$overriddenGuy = Annotation::forMethod($testClass, $methodName)->fetch('guy');
315-
if (!$overriddenGuy) {
316-
$overriddenGuy = Annotation::forClass($testClass)->fetch('guy');
317-
}
318-
if ($overriddenGuy) {
319-
$guy = $overriddenGuy;
320-
}
321-
322-
$cest = new TestCase\Cest($this->dispatcher, array(
323-
'name' => $methodName,
324-
'instance' => $cestInstance,
325-
'method' => $methodName,
326-
'file' => $file,
327-
'bootstrap' => $this->settings['bootstrap'],
328-
'guy' => $guy
329-
));
280+
$cest = new TestCase\Cest();
281+
$cest->configDispatcher($this->dispatcher)
282+
->configName($methodName)
283+
->configBootstrap($this->settings['bootstrap'])
284+
->configFile($file)
285+
->config('testClassInstance', $cestInstance)
286+
->config('testMethod', $methodName)
287+
->configActor($guy)
288+
->initConfig();
330289

331290
$cest->getScenario()->env(Annotation::forMethod($testClass, $methodName)->fetchAll('env'));
332291
$cest->getScenario()->groups(\PHPUnit_Util_Test::getGroups($testClass, $methodName));

src/Codeception/TestCase.php

Lines changed: 1 addition & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -4,108 +4,10 @@
44

55
use Codeception\Event\StepEvent;
66
use Codeception\Exception\ConditionalAssertionFailed;
7+
use Codeception\TestCase\Shared\Dependencies;
78
use Symfony\Component\EventDispatcher\Event;
89

910
abstract class TestCase extends \PHPUnit_Framework_TestCase implements \PHPUnit_Framework_SelfDescribing
1011
{
11-
/**
12-
* @var \Codeception\Scenario
13-
*/
14-
protected $scenario;
15-
16-
protected $trace = array();
17-
1812
protected $backupGlobalsBlacklist = array('app');
19-
20-
protected $dependencies;
21-
22-
protected $dispatcher;
23-
24-
protected function fire($event, Event $eventType)
25-
{
26-
foreach ($this->scenario->getGroups() as $group) {
27-
$this->dispatcher->dispatch($event . '.' . $group, $eventType);
28-
}
29-
$this->dispatcher->dispatch($event, $eventType);
30-
}
31-
32-
protected function handleDependencies()
33-
{
34-
if (empty($this->dependencies)) {
35-
return true;
36-
}
37-
38-
$passed = $this->getTestResultObject()->passed();
39-
$testNames = array_map(
40-
function ($testname) {
41-
return preg_replace('~with data set (.*?)~', '', $testname);
42-
},
43-
array_keys($passed)
44-
);
45-
$testNames = array_unique($testNames);
46-
47-
foreach ($this->dependencies as $dependency) {
48-
if (in_array($dependency, $testNames)) {
49-
continue;
50-
}
51-
$this->getTestResultObject()->addError(
52-
$this,
53-
new \PHPUnit_Framework_SkippedTestError("This test depends on '$dependency' to pass."),
54-
0
55-
);
56-
return false;
57-
}
58-
59-
return true;
60-
}
61-
62-
public function runStep(Step $step)
63-
{
64-
$this->trace[] = $step;
65-
$this->fire(Events::STEP_BEFORE, new StepEvent($this, $step));
66-
try {
67-
$result = $step->run();
68-
} catch (ConditionalAssertionFailed $f) {
69-
$result = $this->getTestResultObject();
70-
$result->addFailure(clone($this), $f, $result->time());
71-
} catch (\Exception $e) {
72-
$this->fire(Events::STEP_AFTER, new StepEvent($this, $step));
73-
throw $e;
74-
}
75-
$this->fire(Events::STEP_AFTER, new StepEvent($this, $step));
76-
return $result;
77-
}
78-
79-
public function getFeature()
80-
{
81-
return null;
82-
}
83-
84-
public function getFileName()
85-
{
86-
return get_class($this) . '::' . $this->getName(false);
87-
}
88-
89-
/**
90-
* @return \Codeception\Scenario
91-
*/
92-
public function getScenario()
93-
{
94-
return $this->scenario;
95-
}
96-
97-
public function getTrace()
98-
{
99-
return $this->trace;
100-
}
101-
102-
public function toString()
103-
{
104-
return $this->getFeature();
105-
}
106-
107-
public function setDependencies(array $dependencies)
108-
{
109-
$this->dependencies = $dependencies;
110-
}
11113
}

0 commit comments

Comments
 (0)