public function runTestMethod($testMethod, array $tags = array())
{
if ($this->methodIsIgnored($testMethod, $tags) === false) {
$this->mockAutoloader->setMockGenerator($this->mockGenerator)->register();
set_error_handler(array($this, 'errorHandler'));
ini_set('display_errors', 'stderr');
ini_set('log_errors', 'Off');
ini_set('log_errors_max_len', '0');
$this->currentMethod = $testMethod;
$this->executeOnFailure = array();
$this->phpFunctionMocker->setDefaultNamespace($this->getTestedClassNamespace());
$this->phpConstantMocker->setDefaultNamespace($this->getTestedClassNamespace());
try {
foreach ($this->getMethodPhpVersions($testMethod) as $phpVersion => $operator) {
if (version_compare(phpversion(), $phpVersion, $operator) === false) {
throw new test\exceptions\skip('PHP version ' . PHP_VERSION . ' is not ' . $operator . ' to ' . $phpVersion);
}
}
foreach ($this->getMandatoryMethodExtensions($testMethod) as $mandatoryExtension) {
try {
call_user_func($this->phpExtensionFactory, $mandatoryExtension)->requireExtension();
} catch (php\exception $exception) {
throw new test\exceptions\skip($exception->getMessage());
}
}
try {
ob_start();
test\adapter::setStorage($this->testAdapterStorage);
mock\controller::setLinker($this->mockControllerLinker);
$this->testAdapterStorage->add(php\mocker::getAdapter());
$this->beforeTestMethod($this->currentMethod);
$this->mockGenerator->testedClassIs($this->getTestedClassName());
try {
$testedClass = new \reflectionClass($testedClassName = $this->getTestedClassName());
} catch (\exception $exception) {
throw new exceptions\runtime('Tested class \'' . $testedClassName . '\' does not exist for test class \'' . $this->getClass() . '\'');
}
if ($testedClass->isAbstract() === true) {
$testedClass = new \reflectionClass($testedClassName = $this->mockGenerator->getDefaultNamespace() . '\\' . $testedClassName);
}
$this->factoryBuilder->build($testedClass, $instance)->addToAssertionManager($this->assertionManager, 'newTestedInstance', function () use($testedClass) {
throw new exceptions\runtime('Tested class ' . $testedClass->getName() . ' has no constructor or its constructor has at least one mandatory argument');
});
$this->factoryBuilder->build($testedClass)->addToAssertionManager($this->assertionManager, 'newInstance', function () use($testedClass) {
throw new exceptions\runtime('Tested class ' . $testedClass->getName() . ' has no constructor or its constructor has at least one mandatory argument');
});
$this->assertionManager->setPropertyHandler('testedInstance', function () use(&$instance) {
if ($instance === null) {
throw new exceptions\runtime('Use $this->newTestedInstance before using $this->testedInstance');
}
return $instance;
});
if ($this->codeCoverageIsEnabled() === true) {
$options = XDEBUG_CC_UNUSED | XDEBUG_CC_DEAD_CODE;
if ($this->branchesAndPathsCoverageIsEnabled() === true) {
$options |= XDEBUG_CC_BRANCH_CHECK;
}
xdebug_start_code_coverage($options);
}
$assertionNumber = $this->score->getAssertionNumber();
$time = microtime(true);
$memory = memory_get_usage(true);
if (isset($this->dataProviders[$testMethod]) === false) {
$this->{$testMethod}();
$this->asserterCallManager->check();
} else {
$dataProvider = $this->dataProviders[$testMethod];
if ($dataProvider instanceof test\data\provider) {
$data = $this->dataProviders[$testMethod]();
} else {
$data = $this->{$this->dataProviders[$testMethod]}();
}
if (is_array($data) === false && $data instanceof \traversable === false) {
throw new test\exceptions\runtime('Data provider ' . $this->getClass() . '::' . $this->dataProviders[$testMethod] . '() must return an array or an iterator');
}
$reflectedTestMethod = call_user_func($this->reflectionMethodFactory, $this, $testMethod);
$numberOfArguments = $reflectedTestMethod->getNumberOfRequiredParameters();
foreach ($data as $key => $arguments) {
if (is_array($arguments) === false) {
$arguments = array($arguments);
}
if (sizeof($arguments) < $numberOfArguments) {
throw new test\exceptions\runtime('Data provider ' . ($dataProvider instanceof test\data\provider ? '' : $this->getClass() . '::' . $this->dataProviders[$testMethod] . '() ') . 'does not provide enough arguments at key ' . $key . ' for test method ' . $this->getClass() . '::' . $testMethod . '()');
}
$this->score->setDataSet($key, $this->dataProviders[$testMethod]);
$reflectedTestMethod->invokeArgs($this, $arguments);
$this->asserterCallManager->check();
$this->score->unsetDataSet();
}
}
$this->mockControllerLinker->reset();
$this->testAdapterStorage->reset();
$memoryUsage = memory_get_usage(true) - $memory;
$duration = microtime(true) - $time;
$this->score->addMemoryUsage($this->path, $this->class, $this->currentMethod, $memoryUsage)->addDuration($this->path, $this->class, $this->currentMethod, $duration)->addOutput($this->path, $this->class, $this->currentMethod, ob_get_clean());
if ($this->codeCoverageIsEnabled() === true) {
$this->score->getCoverage()->addXdebugDataForTest($this, xdebug_get_code_coverage());
xdebug_stop_code_coverage();
}
if ($assertionNumber == $this->score->getAssertionNumber() && $this->methodIsNotVoid($this->currentMethod) === false) {
$this->score->addVoidMethod($this->path, $this->class, $this->currentMethod);
}
} catch (\exception $exception) {
$this->score->addOutput($this->path, $this->class, $this->currentMethod, ob_get_clean());
throw $exception;
}
} catch (asserter\exception $exception) {
foreach ($this->executeOnFailure as $closure) {
ob_start();
$closure();
$this->score->addOutput($this->path, $this->class, $this->currentMethod, ob_get_clean());
}
if ($this->score->failExists($exception) === false) {
$this->addExceptionToScore($exception);
}
} catch (test\exceptions\runtime $exception) {
$this->score->addRuntimeException($this->path, $this->class, $this->currentMethod, $exception);
} catch (test\exceptions\skip $exception) {
list($file, $line) = $this->getBacktrace($exception->getTrace());
$this->score->addSkippedMethod($file, $this->class, $this->currentMethod, $line, $exception->getMessage());
} catch (test\exceptions\stop $exception) {
} catch (exception $exception) {
list($file, $line) = $this->getBacktrace($exception->getTrace());
$this->errorHandler(E_USER_ERROR, $exception->getMessage(), $file, $line);
} catch (\exception $exception) {
$this->addExceptionToScore($exception);
}
$this->afterTestMethod($this->currentMethod);
$this->currentMethod = null;
restore_error_handler();
ini_restore('display_errors');
ini_restore('log_errors');
ini_restore('log_errors_max_len');
$this->mockAutoloader->unregister();
}
return $this;
}