public function testGenerate() { $this->if($php = new testedClass())->then->object($php->generate($functionName = __NAMESPACE__ . '\\version_compare'))->isIdenticalTo($php)->boolean(function_exists($functionName))->isTrue()->boolean(version_compare('5.4.0', '5.3.0'))->isFalse()->integer(\version_compare('5.4.0', '5.3.0'))->isEqualTo(1)->boolean(version_compare('5.3.0', '5.4.0'))->isTrue()->integer(\version_compare('5.3.0', '5.4.0'))->isEqualTo(-1)->exception(function () use($php) { $php->generate(__NAMESPACE__ . '\\doesSomething'); })->isInstanceof('mageekguy\\atoum\\exceptions\\logic\\invalidArgument')->hasMessage('Function \'' . __NAMESPACE__ . '\\doesSomething\' already exists')->if($php->{$functionName} = $returnValue = uniqid())->then->string(version_compare(uniqid(), uniqid()))->isEqualTo($returnValue)->object($php->generate($functionName = __NAMESPACE__ . '\\version_compare'))->isIdenticalTo($php)->boolean(version_compare('5.4.0', '5.3.0'))->isFalse()->boolean(version_compare('5.3.0', '5.4.0'))->isTrue()->object($php->generate($unknownFunctionName = __NAMESPACE__ . '\\foo'))->isIdenticalTo($php)->variable(foo())->isNull()->if($php->{$unknownFunctionName} = $fooReturnValue = uniqid())->then->string(foo())->isEqualTo($fooReturnValue)->if($php->{$functionName} = $returnValue = uniqid())->when(function () use($php, $functionName) { unset($php->{$functionName}); })->then->boolean(version_compare('5.4.0', '5.3.0'))->isFalse()->boolean(version_compare('5.3.0', '5.4.0'))->isTrue()->when(function () use($php, $unknownFunctionName) { unset($php->{$unknownFunctionName}); })->then->variable(foo())->isNull(); }
} return join(', ', $parameters); } protected static function getParameters(\reflectionFunction $function) { $parameters = array(); foreach ($function->getParameters() as $parameter) { $parameters[] = ($parameter->isPassedByReference() === false ? '' : '& ') . '$' . $parameter->getName(); } return 'array(' . join(',', $parameters) . ')'; } protected static function getParameterType(\reflectionParameter $parameter) { switch (true) { case $parameter->isArray(): return 'array '; case method_exists($parameter, 'isCallable') && $parameter->isCallable(): return 'callable '; case $class = $parameter->getClass(): return '\\' . $class->getName() . ' '; default: return ''; } } protected static function defineMockedFunction($namespace, $class, $function, \reflectionFunction $reflectedFunction = null) { eval(sprintf('namespace %s { function %s(%s) { return \\%s::getAdapter()->invoke(__FUNCTION__, %s); } }', $namespace, $function, $reflectedFunction ? static::getParametersSignature($reflectedFunction) : '', $class, $reflectedFunction ? static::getParameters($reflectedFunction) : 'func_get_args()')); } } mocker::setAdapter();
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 testResetCalls() { $this->given($this->newTestedInstance)->if($this->mockGenerator->orphanize('__construct')->orphanize('resetCalls'), php\mocker::setAdapter($adapter = new \mock\atoum\test\adapter()))->then->object($this->testedInstance->resetCalls())->isTestedInstance->mock($adapter)->call('resetCalls')->once->object($this->testedInstance->resetCalls($functionName = uniqid()))->isTestedInstance->mock($adapter)->call('resetCalls')->withArguments($functionName)->once->if($this->testedInstance->setDefaultNamespace($defaultNamespace = uniqid()))->then->object($this->testedInstance->resetCalls($functionName = uniqid()))->isTestedInstance->mock($adapter)->call('resetCalls')->withArguments($defaultNamespace . '\\' . $functionName)->once; }
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 __construct($defaultNamespace = '') { parent::__construct($defaultNamespace); $this->setReflectedFunctionFactory(); }
public function setWith($function) { return parent::setWith(clone php\mocker::getAdapter())->setFunction($function); }
public function testSetAdapter() { $this->variable(php\mocker::setAdapter($adapter = new atoum\php\mocker\adapter()))->isNull()->object(php\mocker::getAdapter())->isIdenticalTo($adapter)->variable(php\mocker::setAdapter())->isNull()->object(php\mocker::getAdapter())->isNotIdenticalTo($adapter)->isEqualTo(new atoum\php\mocker\adapter()); }
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()); }