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->phpMocker->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 (atoum\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 { $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 ' . $this->getClass() . '::' . $this->dataProviders[$testMethod] . '() 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; }
public function runTestMethod($testMethod, array $tags = array()) { if ($this->methodIsIgnored($testMethod, $tags) === false) { $mockGenerator = $this->getMockGenerator(); $mockNamespacePattern = '/^' . preg_quote($mockGenerator->getDefaultNamespace()) . '\\\\/i'; $mockAutoloader = function ($class) use($mockGenerator, $mockNamespacePattern) { $mockedClass = preg_replace($mockNamespacePattern, '', $class); if ($mockedClass !== $class) { $mockGenerator->generate($mockedClass); } }; if (spl_autoload_register($mockAutoloader, true, true) === false) { throw new \runtimeException('Unable to register mock autoloader'); } 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->phpMocker->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) { $this->extension($mandatoryExtension)->isLoaded(); } try { ob_start(); if ($this->adapter->class_exists($testedClassName = $this->getTestedClassName()) === false) { throw new exceptions\runtime('Tested class \'' . $testedClassName . '\' does not exist for test class \'' . $this->getClass() . '\''); } test\adapter::setStorage($this->testAdapterStorage); mock\controller::setLinker($this->mockControllerLinker); $this->testAdapterStorage->add(php\mocker::getAdapter()); $this->beforeTestMethod($this->currentMethod); if ($this->codeCoverageIsEnabled() === true) { xdebug_start_code_coverage(XDEBUG_CC_UNUSED | XDEBUG_CC_DEAD_CODE); } $assertionNumber = $this->score->getAssertionNumber(); $time = microtime(true); $memory = memory_get_usage(true); if (isset($this->dataProviders[$testMethod]) === false) { $this->{$testMethod}(); asserters\adapter\call::areEvaluated(); } 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 ' . $this->getClass() . '::' . $this->dataProviders[$testMethod] . '() not provide enough arguments at key ' . $key . ' for test method ' . $this->getClass() . '::' . $testMethod . '()'); } $this->score->setDataSet($key, $this->dataProviders[$testMethod]); $reflectedTestMethod->invokeArgs($this, $arguments); asserters\adapter\call::areEvaluated(); $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'); if (spl_autoload_unregister($mockAutoloader) === false) { throw new \runtimeException('Unable to unregister mock autoloader'); } } return $this; }
public function testSetAdapter() { $this->variable(testedClass::setAdapter($adapter = new atoum\php\mocker\adapter()))->isNull()->object(testedClass::getAdapter())->isIdenticalTo($adapter)->variable(testedClass::setAdapter())->isNull()->object(testedClass::getAdapter())->isNotIdenticalTo($adapter)->isEqualTo(new atoum\php\mocker\adapter()); }
public function setWith($function) { return parent::setWith(clone php\mocker::getAdapter())->setFunction($function); }
public function testSetWith() { $this->mockGenerator->orphanize('asserterFail')->if($asserter = new sut(new \mock\mageekguy\atoum\asserter\generator()))->then->object($asserter->disableEvaluationChecking()->setWith($function = uniqid()))->isIdenticalTo($asserter)->object($asserter->getAdapter())->isCloneOf(php\mocker::getAdapter())->string($asserter->getCall()->getFunction())->isEqualTo($function); }
public function testSetWith() { $this->given($this->newTestedInstance)->then->object($this->testedInstance->setWith($function = uniqid()))->isTestedInstance->string($this->testedInstance->getFunction())->isEqualTo($function)->object($this->testedInstance->getAdapter())->isCloneOf(php\mocker::getAdapter()); }