public function testRun() { $this->if($php = new testedClass($phpPath = uniqid(), $adapter = new atoum\test\adapter()))->and($adapter->proc_open = false)->then->exception(function () use($php, &$code) { $php->run($code = uniqid()); })->isInstanceOf('mageekguy\\atoum\\cli\\command\\exception')->hasMessage('Unable to run \'' . $php . '\'')->adapter($adapter)->call('proc_open')->withArguments((string) $php, array(0 => array('pipe', 'r'), 1 => array('pipe', 'w'), 2 => array('pipe', 'w')), array())->once()->if($php = new testedClass($phpPath = uniqid(), $adapter))->and($code = uniqid())->and($adapter->proc_open = function ($command, $descriptors, &$streams) use(&$phpResource, &$stdin, &$stdout, &$stderr) { $streams = array($stdin = uniqid(), $stdout = uniqid(), $stderr = uniqid); return $phpResource = uniqid(); })->and($adapter->fwrite = strlen($code))->and($adapter->fclose = null)->and($adapter->stream_set_blocking = null)->then->object($php->run($code))->isIdenticalTo($php)->adapter($adapter)->call('proc_open')->withArguments((string) $php, array(0 => array('pipe', 'r'), 1 => array('pipe', 'w'), 2 => array('pipe', 'w')), array())->once()->call('fwrite')->withArguments($stdin, $code, strlen($code))->once()->call('fclose')->withArguments($stdin)->once()->call('stream_set_blocking')->withArguments($stdout)->once()->call('stream_set_blocking')->withArguments($stderr)->once()->if($php = new testedClass($phpPath = uniqid(), $adapter))->and($adapter->resetCalls())->and($adapter->fwrite[1] = 4)->and($adapter->fwrite[2] = strlen($code) - 4)->then->object($php->run($code))->isIdenticalTo($php)->adapter($adapter)->call('proc_open')->withArguments((string) $php, array(0 => array('pipe', 'r'), 1 => array('pipe', 'w'), 2 => array('pipe', 'w')), array())->once()->call('fwrite')->withArguments($stdin, $code, strlen($code))->once()->call('fwrite')->withArguments($stdin, substr($code, 4), strlen($code) - 4)->once()->call('fclose')->withArguments($stdin)->once()->call('stream_set_blocking')->withArguments($stdout)->once()->call('stream_set_blocking')->withArguments($stderr)->once()->if($php = new testedClass($phpPath = uniqid(), $adapter))->and($php->addOption('firstOption'))->then->object($php->run($code))->isIdenticalTo($php)->adapter($adapter)->call('proc_open')->withArguments((string) $php, array(0 => array('pipe', 'r'), 1 => array('pipe', 'w'), 2 => array('pipe', 'w')), array())->once()->if($php = new testedClass($phpPath = uniqid(), $adapter))->and($php->addOption('firstOption'))->and($php->addOption('secondOption', 'secondOptionValue'))->then->object($php->run($code))->isIdenticalTo($php)->adapter($adapter)->call('proc_open')->withArguments((string) $php, array(0 => array('pipe', 'r'), 1 => array('pipe', 'w'), 2 => array('pipe', 'w')), array())->once()->if($php = new testedClass($phpPath = uniqid(), $adapter))->and($php->addArgument($argument1 = uniqid()))->and($php->addArgument($argument2 = uniqid()))->then->object($php->run($code))->isIdenticalTo($php)->adapter($adapter)->call('proc_open')->withArguments((string) $php, array(0 => array('pipe', 'r'), 1 => array('pipe', 'w'), 2 => array('pipe', 'w')), array())->once(); }
public function testClear() { $this->if($adapter = new atoum\test\adapter())->if($adapter->fopen = false)->and($file = new testedClass(null, $adapter))->and($adapter->resetCalls())->then->exception(function () use($file) { $file->clear(); })->isInstanceOf('mageekguy\\atoum\\exceptions\\runtime')->hasMessage('Unable to open file \'' . $file->getFilename() . '\'')->if($adapter->fopen = $resource = uniqid())->and($adapter->flock = true)->and($adapter->ftruncate = false)->and($adapter->fclose = function () { })->and($adapter->resetCalls())->then->exception(function () use($file) { $file->clear(); })->isInstanceOf('mageekguy\\atoum\\exceptions\\runtime')->hasMessage('Unable to truncate file \'' . $file->getFilename() . '\'')->adapter($adapter)->call('fopen')->withArguments($file->getFilename(), 'c')->once()->call('flock')->withArguments($resource, LOCK_EX)->once()->if($adapter->ftruncate = true)->then->object($file->clear())->isIdenticalTo($file)->adapter($adapter)->call('fopen')->withArguments($file->getFilename(), 'c')->once()->call('ftruncate')->withArguments($resource, 0)->after($this->adapter($adapter)->call('flock')->withArguments($resource, LOCK_EX))->twice(); }
public function testGetLastCall() { $this->if($call = new call\adapter(new asserters\mock(new asserter\generator()), $adapter = new test\adapter(), 'md5'))->then->variable($call->getLastCall())->isNull()->when(function () { $otherAdapter = new test\adapter(); $otherAdapter->md5(uniqid()); })->variable($call->getLastCall())->isNull()->when(function () use($adapter) { $adapter->md5(uniqid()); })->integer($call->getLastCall())->isEqualTo(2)->when(function () use($adapter) { $adapter->md5(uniqid()); })->integer($call->getLastCall())->isEqualTo(3); }
public function testWrite() { $this->if($handle = uniqid())->and($adapter = new atoum\test\adapter())->and($adapter->fopen = function () use($handle) { return $handle; })->and($adapter->fclose = function () { })->and($adapter->fwrite = function () { })->and($adapter->is_writable = function () { return true; })->and($file = new writers\file(null, $adapter))->and($adapter->resetCalls())->then->object($file->write($string = uniqid()))->isIdenticalTo($file)->adapter($adapter)->call('dirname')->withArguments('atoum.log')->once()->call('is_writable')->withArguments('.')->once()->call('fopen')->withArguments('atoum.log', 'w')->once()->call('fwrite')->withArguments($handle, $string)->once()->object($file->write($string = uniqid() . "\n"))->isIdenticalTo($file)->adapter($adapter)->call('fwrite')->withArguments($handle, $string)->once()->if($adapter->is_null = function () { return false; })->then->object($file->write($string = uniqid()))->isIdenticalTo($file)->adapter($adapter)->call('fwrite')->withArguments($handle, $string)->once(); }
public function testExportRepository() { $this->if($adapter = new atoum\test\adapter(), $adapter->extension_loaded = true, $svn = new \mock\mageekguy\atoum\scripts\builder\vcs\svn($adapter), $svn->getMockController()->cleanWorkingDirectory = $svn)->then->exception(function () use($svn) { $svn->exportRepository(uniqid()); })->isInstanceOf('mageekguy\\atoum\\exceptions\\runtime')->hasMessage('Unable to export repository, repository url is undefined')->adapter($adapter)->call('svn_auth_set_parameter')->withArguments(PHP_SVN_AUTH_PARAM_IGNORE_SSL_VERIFY_ERRORS, true)->never()->call('svn_auth_set_parameter')->withArguments(SVN_AUTH_PARAM_DEFAULT_USERNAME, $svn->getUsername())->never()->call('svn_auth_set_parameter')->withArguments(SVN_AUTH_PARAM_DEFAULT_PASSWORD, $svn->getPassword())->never()->call('svn_checkout')->never()->if($svn->setRepositoryUrl($repositoryUrl = uniqid())->setWorkingDirectory($workingDirectory = __DIR__), $adapter->resetCalls(), $adapter->svn_checkout = false, $adapter->svn_auth_set_parameter = function () { })->then->exception(function () use($svn) { $svn->exportRepository(); })->isInstanceOf('mageekguy\\atoum\\exceptions\\runtime')->hasMessage('Unable to checkout repository \'' . $repositoryUrl . '\' in directory \'' . $workingDirectory . '\'')->adapter($adapter)->call('svn_auth_set_parameter')->withArguments(PHP_SVN_AUTH_PARAM_IGNORE_SSL_VERIFY_ERRORS, true)->once()->call('svn_auth_set_parameter')->withArguments(SVN_AUTH_PARAM_DEFAULT_USERNAME, $svn->getUsername())->never()->call('svn_auth_set_parameter')->withArguments(SVN_AUTH_PARAM_DEFAULT_PASSWORD, $svn->getPassword())->never()->call('svn_checkout')->withArguments($svn->getRepositoryUrl(), $workingDirectory, $svn->getRevision())->once()->mock($svn)->call('cleanWorkingDirectory')->once()->if($svn->setUsername(uniqid())->getMockController()->resetCalls(), $adapter->resetCalls())->then->exception(function () use($svn) { $svn->exportRepository(); })->isInstanceOf('mageekguy\\atoum\\exceptions\\runtime')->hasMessage('Unable to checkout repository \'' . $repositoryUrl . '\' in directory \'' . $workingDirectory . '\'')->adapter($adapter)->call('svn_auth_set_parameter')->withArguments(PHP_SVN_AUTH_PARAM_IGNORE_SSL_VERIFY_ERRORS, true)->once()->call('svn_auth_set_parameter')->withArguments(SVN_AUTH_PARAM_DEFAULT_USERNAME, $svn->getUsername())->once()->call('svn_auth_set_parameter')->withArguments(SVN_AUTH_PARAM_DEFAULT_PASSWORD, $svn->getPassword())->never()->call('svn_checkout')->withArguments($svn->getRepositoryUrl(), $workingDirectory, $svn->getRevision())->once()->mock($svn)->call('cleanWorkingDirectory')->once()->if($svn->setPassword(uniqid())->getMockController()->resetCalls(), $adapter->resetCalls())->then->exception(function () use($svn) { $svn->exportRepository(); })->isInstanceOf('mageekguy\\atoum\\exceptions\\runtime')->hasMessage('Unable to checkout repository \'' . $repositoryUrl . '\' in directory \'' . $workingDirectory . '\'')->adapter($adapter)->call('svn_auth_set_parameter')->withArguments(PHP_SVN_AUTH_PARAM_IGNORE_SSL_VERIFY_ERRORS, true)->once()->call('svn_auth_set_parameter')->withArguments(SVN_AUTH_PARAM_DEFAULT_USERNAME, $svn->getUsername())->once()->call('svn_auth_set_parameter')->withArguments(SVN_AUTH_PARAM_DEFAULT_PASSWORD, $svn->getPassword())->once()->call('svn_checkout')->withArguments($svn->getRepositoryUrl(), $workingDirectory, $svn->getRevision())->once()->mock($svn)->call('cleanWorkingDirectory')->once()->if($svn->getMockController()->resetCalls(), $adapter->svn_checkout = true, $adapter->resetCalls())->then->object($svn->exportRepository())->isIdenticalTo($svn)->adapter($adapter)->call('svn_auth_set_parameter')->withArguments(PHP_SVN_AUTH_PARAM_IGNORE_SSL_VERIFY_ERRORS, true)->once()->call('svn_auth_set_parameter')->withArguments(SVN_AUTH_PARAM_DEFAULT_USERNAME, $svn->getUsername())->once()->call('svn_auth_set_parameter')->withArguments(SVN_AUTH_PARAM_DEFAULT_PASSWORD, $svn->getPassword())->once()->call('svn_checkout')->withArguments($svn->getRepositoryUrl(), $workingDirectory, $svn->getRevision())->once()->mock($svn)->call('cleanWorkingDirectory')->once(); }
public function invoke($method, array $arguments = array()) { $method = self::mapMethod($method); if ($method === 'dir_rewinddir' && isset($this->{$method}) === true) { $this->resetCalls('dir_readdir'); } return isset($this->{$method}) === false ? null : parent::invoke($method, $arguments); }
protected function setInvoker($functionName, \closure $factory = null) { if ($factory === null) { $factory = function ($functionName) { return new mocker\adapter\invoker($functionName); }; } return parent::setInvoker($functionName, $factory); }
public function testTagVersion() { $this->if($tagger = $this->newTestedInstance($adapter = new atoum\test\adapter()), $adapter->is_dir = true, $adapter->mkdir = function () { })->then->exception(function () use($tagger) { $tagger->tagVersion(uniqid()); })->isInstanceOf('mageekguy\\atoum\\exceptions\\logic')->hasMessage('Unable to tag, src directory is undefined')->if($this->testedInstance->setSrcDirectory($srcDirectory = uniqid()))->then->exception(function () use($tagger) { $tagger->tagVersion(uniqid()); })->isInstanceOf('mageekguy\\atoum\\exceptions\\logic')->hasMessage('Unable to tag, version is undefined')->if($tagger->setVersion($version = uniqid())->setSrcIteratorInjector(function ($directory) { }))->then->exception(function () use($tagger) { $tagger->tagVersion(uniqid()); })->isInstanceOf('mageekguy\\atoum\\exceptions\\logic')->hasMessage('Unable to tag, src iterator injector does not return an iterator')->if($srcIterator = new \arrayIterator(array($file1 = $srcDirectory . \DIRECTORY_SEPARATOR . ($basename1 = uniqid()), $file2 = $srcDirectory . \DIRECTORY_SEPARATOR . ($basename2 = uniqid()), $file3 = $srcDirectory . \DIRECTORY_SEPARATOR . ($basename3 = uniqid()))), $this->testedInstance->setSrcIteratorInjector(function ($directory) use($srcIterator) { return $srcIterator; }), $adapter->file_get_contents[1] = ($file1Part1 = uniqid()) . '\'$Rev: ' . rand(1, PHP_INT_MAX) . ' $\'' . ($file1Part2 = uniqid()), $adapter->file_get_contents[2] = $contentOfFile2 = uniqid(), $adapter->file_get_contents[3] = ($file3Part1 = uniqid()) . '"$Rev: ' . rand(1, PHP_INT_MAX) . ' $"' . ($file3Part2 = uniqid()), $adapter->file_put_contents = function () { })->then->object($this->testedInstance->tagVersion())->isTestedInstance->adapter($adapter)->call('file_get_contents')->withArguments($file1)->once()->call('file_put_contents')->withArguments($file1, $file1Part1 . '\'' . $version . '\'' . $file1Part2, \LOCK_EX)->once()->call('file_get_contents')->withArguments($file2)->once()->call('file_put_contents')->withArguments($file2, $contentOfFile2, \LOCK_EX)->once()->call('file_get_contents')->withArguments($file3)->once()->call('file_put_contents')->withArguments($file3, $file3Part1 . '"' . $version . '"' . $file3Part2, \LOCK_EX)->once()->if($adapter->resetCalls()->file_get_contents[2] = false)->then->exception(function () use($tagger) { $tagger->tagVersion(uniqid()); })->isInstanceOf('mageekguy\\atoum\\exceptions\\runtime')->hasMessage('Unable to tag, path \'' . $file2 . '\' is unreadable')->if($adapter->resetCalls(), $adapter->file_get_contents[2] = $contentOfFile2, $adapter->file_put_contents[2] = false)->then->exception(function () use($tagger) { $tagger->tagVersion(uniqid()); })->isInstanceOf('mageekguy\\atoum\\exceptions\\runtime')->hasMessage('Unable to tag, path \'' . $file2 . '\' is unwritable')->if($adapter->resetCalls(), $this->testedInstance->setDestinationDirectory($destinationDirectory = uniqid()))->when(function () use($adapter) { unset($adapter->file_put_contents[2]); })->then->object($this->testedInstance->tagVersion())->isTestedInstance->adapter($adapter)->call('is_dir')->withArguments($destinationDirectory)->exactly(3)->call('mkdir')->never()->call('file_get_contents')->withArguments($file1)->once()->call('file_put_contents')->withArguments($destinationDirectory . \DIRECTORY_SEPARATOR . $basename1, $file1Part1 . '\'' . $version . '\'' . $file1Part2, \LOCK_EX)->once()->call('file_get_contents')->withArguments($file2)->once()->call('file_put_contents')->withArguments($destinationDirectory . \DIRECTORY_SEPARATOR . $basename2, $contentOfFile2, \LOCK_EX)->once()->call('file_get_contents')->withArguments($file3)->once()->call('file_put_contents')->withArguments($destinationDirectory . \DIRECTORY_SEPARATOR . $basename3, $file3Part1 . '"' . $version . '"' . $file3Part2, \LOCK_EX)->once()->if($adapter->resetCalls()->is_dir = false)->then->object($this->testedInstance->tagVersion())->isTestedInstance->adapter($adapter)->call('is_dir')->withArguments($destinationDirectory)->exactly(3)->call('mkdir')->withArguments($destinationDirectory, 0777, true)->exactly(3)->call('file_get_contents')->withArguments($file1)->once()->call('file_put_contents')->withArguments($destinationDirectory . \DIRECTORY_SEPARATOR . $basename1, $file1Part1 . '\'' . $version . '\'' . $file1Part2, \LOCK_EX)->once()->call('file_get_contents')->withArguments($file2)->once()->call('file_put_contents')->withArguments($destinationDirectory . \DIRECTORY_SEPARATOR . $basename2, $contentOfFile2, \LOCK_EX)->once()->call('file_get_contents')->withArguments($file3)->once()->call('file_put_contents')->withArguments($destinationDirectory . \DIRECTORY_SEPARATOR . $basename3, $file3Part1 . '"' . $version . '"' . $file3Part2, \LOCK_EX)->once(); }
public function testWriteAsynchronousReport() { $this->if($mailer = new atoum\mailers\mail())->and($writer = new \mock\mageekguy\atoum\writers\mail($mailer, $locale = new \mock\mageekguy\atoum\locale(), $adapter = new atoum\test\adapter()))->and($writer->getMockController()->write = $writer)->and($adapter->date = function ($arg) { return $arg; })->then->object($writer->writeAsynchronousReport($report = new \mock\mageekguy\atoum\reports\asynchronous()))->isIdenticalTo($writer)->mock($writer)->call('write')->withArguments((string) $report)->once()->string($mailer->getSubject())->isEqualTo('Unit tests report, the Y-m-d at H:i:s')->mock($locale)->call('_')->withArguments('Unit tests report, the %1$s at %2$s')->once()->call('_')->withArguments('Y-m-d')->once()->call('_')->withArguments('H:i:s')->once()->if($mailer = new atoum\mailers\mail())->and($writer = new \mock\mageekguy\atoum\writers\mail($mailer, $locale = new \mock\mageekguy\atoum\locale(), $adapter = new atoum\test\adapter()))->and($writer->getMockController()->write = $writer)->then->object($writer->writeAsynchronousReport($report->setTitle($title = uniqid())))->isIdenticalTo($writer)->mock($writer)->call('write')->withArguments((string) $report)->once()->string($mailer->getSubject())->isEqualTo($title)->mock($locale)->call('_')->never()->if($mailer->setSubject($mailerSubject = uniqid()))->and(atoum\test\adapter::resetCallsForAllInstances())->then->object($writer->writeAsynchronousReport($report))->isIdenticalTo($writer)->mock($writer)->call('write')->withArguments((string) $report)->once()->string($mailer->getSubject())->isEqualTo($mailerSubject); }
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; }
protected function buildCall($function, array $arguments) { $call = parent::buildCall($function, $arguments); if ($this->mockClass !== null) { $call->setDecorator(new decorators\addClass($this->mockClass)); } return $call; }
public function testReset() { $this->if($adapter = new testedClass())->then->array($adapter->getInvokers())->isEmpty()->array($adapter->getCalls())->isEmpty()->object($adapter->reset())->isIdenticalTo($adapter)->array($adapter->getInvokers())->isEmpty()->array($adapter->getCalls())->isEmpty()->if($adapter->md5(uniqid()))->then->array($adapter->getInvokers())->isEmpty()->array($adapter->getCalls())->isNotEmpty()->object($adapter->reset())->isIdenticalTo($adapter)->array($adapter->getInvokers())->isEmpty()->array($adapter->getCalls())->isEmpty()->if($adapter->md5 = uniqid())->then->array($adapter->getInvokers())->isNotEmpty()->array($adapter->getCalls())->isEmpty()->object($adapter->reset())->isIdenticalTo($adapter)->array($adapter->getInvokers())->isEmpty()->array($adapter->getCalls())->isEmpty()->if($adapter->md5 = uniqid())->and($adapter->md5(uniqid()))->then->array($adapter->getInvokers())->isNotEmpty()->array($adapter->getCalls())->isNotEmpty()->object($adapter->reset())->isIdenticalTo($adapter)->array($adapter->getInvokers())->isEmpty()->array($adapter->getCalls())->isEmpty(); }
private function set__call() { $controller = $this; parent::__set('__call', function ($method, $arguments) use($controller) { return $controller->invoke($method, $arguments); }); return $this; }
public function stopCase() { test\adapter::resetCallsForAllInstances(); $this->score->unsetCase(); return $this; }
public function testReset() { $this->if($this->newTestedInstance(new asserter\generator()))->then->variable($this->testedInstance->getAdapter())->isNull()->object($this->testedInstance->reset())->isTestedInstance->variable($this->testedInstance->getAdapter())->isNull()->if($this->testedInstance->setWith($adapter = new atoum\test\adapter()))->then->object($this->testedInstance->getAdapter())->isIdenticalTo($adapter)->sizeOf($adapter->getCalls())->isZero()->object($this->testedInstance->reset())->isTestedInstance->object($this->testedInstance->getAdapter())->isIdenticalTo($adapter)->sizeOf($adapter->getCalls())->isZero()->if($adapter->md5(uniqid()))->then->object($this->testedInstance->getAdapter())->isIdenticalTo($adapter)->sizeOf($adapter->getCalls())->isEqualTo(1)->object($this->testedInstance->reset())->isTestedInstance->object($this->testedInstance->getAdapter())->isIdenticalTo($adapter)->sizeOf($adapter->getCalls())->isZero(); }
public function testWriteInErrorDirectory() { $this->if($adapter = new atoum\test\adapter())->and($adapter->file_put_contents = function () { })->and($builder = new testedClass(uniqid(), $adapter))->then->variable($builder->getErrorsDirectory())->isNull()->object($builder->writeErrorInErrorsDirectory(uniqid()))->isIdenticalTo($builder)->adapter($adapter)->call('file_put_contents')->never()->if($builder->setErrorsDirectory($errorDirectory = uniqid()))->then->string($builder->getErrorsDirectory())->isEqualTo($errorDirectory)->exception(function () use($builder) { $builder->writeErrorInErrorsDirectory(uniqid()); })->isInstanceOf('mageekguy\\atoum\\exceptions\\logic')->hasMessage('Revision is undefined')->adapter($adapter)->call('file_put_contents')->never()->if->mockGenerator->shunt('__construct')->and($builder->setVcs($vcs = new \mock\mageekguy\atoum\scripts\builder\vcs()))->and($vcs->setRevision($revision = rand(1, PHP_INT_MAX)))->then->string($builder->getErrorsDirectory())->isEqualTo($errorDirectory)->object($builder->writeErrorInErrorsDirectory($message = uniqid()))->isIdenticalTo($builder)->adapter($adapter)->call('file_put_contents')->withArguments($errorDirectory . \DIRECTORY_SEPARATOR . $revision, $message, \LOCK_EX | \FILE_APPEND)->once()->if($adapter->resetCalls())->and($adapter->file_put_contents = false)->then->string($builder->getErrorsDirectory())->isEqualTo($errorDirectory)->exception(function () use($builder, &$message) { $builder->writeErrorInErrorsDirectory($message = uniqid()); })->isInstanceOf('mageekguy\\atoum\\exceptions\\runtime')->hasMessage('Unable to save error in file \'' . $errorDirectory . \DIRECTORY_SEPARATOR . $revision . '\'')->adapter($adapter)->call('file_put_contents')->withArguments($errorDirectory . \DIRECTORY_SEPARATOR . $revision, $message, \LOCK_EX | \FILE_APPEND)->once(); }
public function testNever() { $this->if($asserter = new asserters\adapter($generator = new asserter\generator()))->then->exception(function () use($asserter) { $asserter->never(); })->isInstanceOf('mageekguy\\atoum\\exceptions\\logic')->hasMessage('Adapter is undefined')->if($asserter->setWith($adapter = new test\adapter()))->then->exception(function () use($asserter) { $asserter->never(); })->isInstanceOf('mageekguy\\atoum\\exceptions\\logic')->hasMessage('Called function is undefined')->if($call = new php\call('md5'))->and($asserter->call('md5'))->then->object($asserter->never())->isIdenticalTo($asserter)->if($adapter->md5($usedArg = uniqid()))->then->exception(function () use(&$line, $asserter) { $line = __LINE__; $asserter->never(); })->isInstanceOf('mageekguy\\atoum\\asserter\\exception')->hasMessage(sprintf($generator->getLocale()->_('function %s is called 1 time instead of 0'), $asserter->getCall()) . PHP_EOL . '[1] ' . $call->setArguments(array($usedArg)))->if($adapter->resetCalls())->and($asserter->withArguments($arg = uniqid()))->then->object($asserter->never())->isIdenticalTo($asserter)->if($adapter->md5($arg))->then->exception(function () use(&$line, $asserter) { $line = __LINE__; $asserter->never(); })->isInstanceOf('mageekguy\\atoum\\asserter\\exception')->hasMessage(sprintf($generator->getLocale()->_('function %s is called 1 time instead of 0'), $asserter->getCall()) . PHP_EOL . '[1] ' . $call->setArguments(array($arg)))->if($adapter->md5($arg))->then->exception(function () use(&$otherLine, $asserter) { $otherLine = __LINE__; $asserter->never(); })->isInstanceOf('mageekguy\\atoum\\asserter\\exception')->hasMessage(sprintf($generator->getLocale()->_('function %s is called 2 times instead of 0'), $asserter->getCall()) . PHP_EOL . '[1] ' . $call->setArguments(array($arg)) . PHP_EOL . '[2] ' . $call->setArguments(array($arg)))->if($asserter->withArguments(uniqid()))->then->object($asserter->never())->isIdenticalTo($asserter); }
public function testWrite() { $this->if($adapter = new atoum\test\adapter())->and($adapter->file_get_contents = '')->and($adapter->stream_context_create = $context = uniqid())->and($writer = new testedClass($adapter))->then->exception(function () use($writer) { $writer->write(uniqid()); })->isInstanceOf('\\mageekguy\\atoum\\exceptions\\runtime')->hasMessage('No URL set for HTTP writer')->if($writer->setUrl($url = uniqid()))->then->object($writer->write($string = uniqid()))->isIdenticalTo($writer)->adapter($adapter)->call('stream_context_create')->withArguments(array('http' => array('method' => 'GET', 'header' => '', 'content' => $string)))->once()->call('file_get_contents')->withArguments($url, false, $context)->once()->if($writer->setMethod($method = uniqid()))->and($adapter->resetCalls())->then->object($writer->write($string = uniqid()))->isIdenticalTo($writer)->adapter($adapter)->call('stream_context_create')->withArguments(array('http' => array('method' => $method, 'header' => '', 'content' => $string)))->once()->call('file_get_contents')->withArguments($url, false, $context)->once()->if($writer->setParameter($param = uniqid()))->and($adapter->resetCalls())->then->object($writer->write($string = uniqid()))->isIdenticalTo($writer)->adapter($adapter)->call('stream_context_create')->withArguments(array('http' => array('method' => $method, 'header' => '', 'content' => http_build_query(array($param => $string)))))->once()->call('file_get_contents')->withArguments($url, false, $context)->once()->if($writer->addHeader($header = uniqid(), $value = uniqid()))->and($adapter->resetCalls())->then->object($writer->write($string = uniqid()))->isIdenticalTo($writer)->adapter($adapter)->call('stream_context_create')->withArguments(array('http' => array('method' => $method, 'header' => $header . ': ' . $value, 'content' => http_build_query(array($param => $string)))))->once()->call('file_get_contents')->withArguments($url, false, $context)->once()->if($writer->addHeader($otherHeader = uniqid(), $otherValue = uniqid()))->and($adapter->resetCalls())->then->object($writer->write($string = uniqid()))->isIdenticalTo($writer)->adapter($adapter)->call('stream_context_create')->withArguments(array('http' => array('method' => $method, 'header' => $header . ': ' . $value . "\r\n" . $otherHeader . ': ' . $otherValue, 'content' => http_build_query(array($param => $string)))))->once()->call('file_get_contents')->withArguments($url, false, $context)->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; }