public function handleEvent($event, atoum\observable $observable) { $eventHandled = parent::handleEvent($event, $observable); if ($eventHandled === true) { switch ($this->event) { case runner::runStart: $this->testPoint = 0; $this->testLine = ''; break; case test::success: $this->testLine = 'ok ' . ++$this->testPoint . PHP_EOL; $this->testLine .= '# ' . $observable->getClass() . '::' . $observable->getCurrentMethod() . '()' . PHP_EOL; break; case test::error: $lastError = $observable->getScore()->getLastErroredMethod(); $lastErrorType = atoum\asserters\error::getAsString($lastError['type']); $this->testLine = 'not ok ' . ++$this->testPoint . ' - ' . trim($lastError['class']) . '::' . trim($lastError['method']) . '()' . PHP_EOL . '# ' . $lastErrorType . ' : ' . str_replace(PHP_EOL, PHP_EOL . '# ', trim($lastError['message'])) . PHP_EOL; $this->testLine .= '# ' . (isset($lastError['errorFile']) ? $lastError['errorFile'] : $lastError['file']) . ':' . (isset($lastError['errorLine']) ? $lastError['errorLine'] : $lastError['line']) . PHP_EOL; break; case test::fail: $lastFailAssertion = $observable->getScore()->getLastFailAssertion(); $this->testLine = 'not ok ' . ++$this->testPoint . ' - ' . trim($lastFailAssertion['class']) . '::' . trim($lastFailAssertion['method']) . '()' . PHP_EOL . '# ' . str_replace(PHP_EOL, PHP_EOL . '# ', trim($lastFailAssertion['fail'])) . PHP_EOL; $this->testLine .= '# ' . $lastFailAssertion['file'] . ':' . $lastFailAssertion['line'] . PHP_EOL; break; case test::void: $lastVoidMethod = $observable->getScore()->getLastVoidMethod(); $this->testLine = 'not ok ' . ++$this->testPoint . ' # TODO ' . trim($lastVoidMethod['class']) . '::' . trim($lastVoidMethod['method']) . '()' . PHP_EOL; $this->testLine .= '# ' . $lastVoidMethod['file'] . PHP_EOL; break; case test::uncompleted: $lastUncompleteMethod = $observable->getScore()->getLastUncompleteMethod(); $lastError = $observable->getScore()->getLastErroredMethod(); $this->testLine = 'not ok ' . ++$this->testPoint . ' - ' . trim($lastUncompleteMethod['class']) . '::' . trim($lastUncompleteMethod['method']) . '()' . PHP_EOL; if ($lastError['class'] === $lastUncompleteMethod['class'] && $lastError['method'] === $lastUncompleteMethod['method']) { $this->testLine .= '# ' . $lastError['type'] . ' : ' . str_replace(PHP_EOL, PHP_EOL . '# ', trim($lastError['message'])) . PHP_EOL; } else { $this->testLine .= '# ' . str_replace(PHP_EOL, PHP_EOL . '# ', trim($lastUncompleteMethod['output']) ?: 'uncomplete method') . PHP_EOL; } $this->testLine .= '# ' . $lastUncompleteMethod['file'] . PHP_EOL; break; case test::skipped: $lastSkippedMethod = $observable->getScore()->getLastSkippedMethod(); $this->testLine = 'ok ' . ++$this->testPoint . ' # SKIP ' . trim($lastSkippedMethod['class']) . '::' . trim($lastSkippedMethod['method']) . '()' . PHP_EOL . '# ' . str_replace(PHP_EOL, PHP_EOL . '# ', trim($lastSkippedMethod['message'])) . PHP_EOL; $this->testLine .= '# ' . $lastSkippedMethod['file'] . ':' . $lastSkippedMethod['line'] . PHP_EOL; break; case test::exception: $lastException = $observable->getScore()->getLastException(); $this->testLine = 'not ok ' . ++$this->testPoint . ' - ' . trim($lastException['class']) . '::' . trim($lastException['method']) . '()' . PHP_EOL . '# ' . str_replace(PHP_EOL, PHP_EOL . '# ', trim($lastException['value'])) . PHP_EOL; $this->testLine .= '# ' . $lastException['file'] . ':' . $lastException['line'] . PHP_EOL; break; case test::runtimeException: $lastRuntimeException = $observable->getScore()->getLastRuntimeException(); $this->testLine = 'Bail out!' . ($lastRuntimeException->getMessage() ? ' ' . trim($lastRuntimeException->getMessage()) : '') . PHP_EOL; break; } } return $eventHandled; }
private function renderErrors(atoum\observable $observable, $class, $method) { $errors = ''; foreach ($observable->getScore()->getErrors() as $error) { if ($error['class'] === $class && $error['method'] === $method) { $errorType = atoum\asserters\error::getAsString($error['type']); $errors .= '# ' . $errorType . ' : ' . str_replace(PHP_EOL, PHP_EOL . '# ', trim($error['message'])) . PHP_EOL; $errors .= '# ' . (isset($error['errorFile']) ? $error['errorFile'] : $error['file']) . ':' . (isset($error['errorLine']) ? $error['errorLine'] : $error['line']) . PHP_EOL; } } return $errors; }
public function testNotExists() { $this->given($asserter = $this->newTestedInstance)->if($asserter->setLocale($locale = new \mock\atoum\locale()), $this->calling($locale)->_ = $errorExists = uniqid())->then->object($asserter->notExists())->isTestedInstance->object($asserter->notExists)->isTestedInstance->array($asserter->getScore()->getErrors())->isEmpty()->if($asserter->getScore()->addError(uniqid(), uniqid(), uniqid(), rand(1, PHP_INT_MAX), rand(0, PHP_INT_MAX), uniqid(), uniqid(), rand(1, PHP_INT_MAX)))->then->exception(function () use(&$line, $asserter) { $asserter->notExists(); })->isInstanceOf('mageekguy\\atoum\\asserter\\exception')->hasMessage($errorExists)->mock($locale)->call('_')->withArguments('error exists')->once->exception(function () use(&$line, $asserter) { $asserter->notExists; })->isInstanceOf('mageekguy\\atoum\\asserter\\exception')->hasMessage($errorExists)->mock($locale)->call('_')->withArguments('error exists')->twice->if($asserter->setWith($message = uniqid(), null))->then->exception(function () use(&$line, $asserter) { $asserter->exists(); })->isInstanceOf('mageekguy\\atoum\\asserter\\exception')->hasMessage($errorExists)->mock($locale)->call('_')->withArguments('error with message \'%s\' does not exist', $message)->once->if($asserter->getScore()->addError(uniqid(), uniqid(), uniqid(), rand(1, PHP_INT_MAX), rand(0, PHP_INT_MAX), $message, uniqid(), rand(1, PHP_INT_MAX)))->then->exception(function () use(&$line, $asserter) { $asserter->notExists(); })->isInstanceOf('mageekguy\\atoum\\asserter\\exception')->hasMessage($errorExists)->mock($locale)->call('_')->withArguments('error with message \'%s\' exists')->once->exception(function () use(&$line, $asserter) { $asserter->notExists; })->isInstanceOf('mageekguy\\atoum\\asserter\\exception')->hasMessage($errorExists)->mock($locale)->call('_')->withArguments('error with message \'%s\' exists')->twice->array($asserter->getScore()->getErrors())->isNotEmpty()->if($asserter->setWith($message = uniqid(), $type = E_USER_ERROR))->then->object($asserter->notExists())->isTestedInstance->object($asserter->notExists)->isTestedInstance->array($asserter->getScore()->getErrors())->isNotEmpty()->if($asserter->getScore()->addError(uniqid(), uniqid(), uniqid(), rand(1, PHP_INT_MAX), $type, $message, uniqid(), rand(1, PHP_INT_MAX)))->then->exception(function () use(&$line, $asserter) { $line = __LINE__; $asserter->notExists(); })->isInstanceOf('mageekguy\\atoum\\asserter\\exception')->hasMessage($errorExists)->mock($locale)->call('_')->withArguments('error of type %s with message \'%s\' exists', atoum\asserters\error::getAsString($type), $message)->once->if($asserter->setWith(null, $type = E_USER_ERROR))->then->exception(function () use(&$line, $asserter) { $line = __LINE__; $asserter->notExists(); })->isInstanceOf('mageekguy\\atoum\\asserter\\exception')->hasMessage($errorExists)->mock($locale)->call('_')->withArguments('error of type %s exists', atoum\asserters\error::getAsString($type))->once->if($asserter->getScore()->addError(uniqid(), uniqid(), uniqid(), rand(1, PHP_INT_MAX), $type, uniqid(), uniqid(), rand(1, PHP_INT_MAX)))->then->exception(function () use(&$line, $asserter) { $asserter->notExists(); })->isInstanceOf('mageekguy\\atoum\\asserter\\exception')->hasMessage($errorExists)->mock($locale)->call('_')->withArguments('error of type %s with message \'%s\' exists')->once->array($asserter->getScore()->getErrors())->isNotEmpty()->if($asserter->getScore()->addError(uniqid(), uniqid(), uniqid(), rand(1, PHP_INT_MAX), $type, uniqid(), uniqid(), rand(1, PHP_INT_MAX)))->then->exception(function () use(&$line, $asserter) { $asserter->notExists; })->isInstanceOf('mageekguy\\atoum\\asserter\\exception')->hasMessage($errorExists)->mock($locale)->call('_')->withArguments('error of type %s with message \'%s\' exists')->once->array($asserter->getScore()->getErrors())->isNotEmpty()->if($asserter->getScore()->addError(uniqid(), uniqid(), uniqid(), rand(1, PHP_INT_MAX), rand(1, PHP_INT_MAX), $message = uniqid() . 'FOO' . uniqid(), uniqid(), rand(1, PHP_INT_MAX)))->and($asserter->withPattern('/FOO/')->withType(null))->then->object($asserter->exists())->isTestedInstance->array($asserter->getScore()->getErrors())->isNotEmpty(); }
public function testWithAnyMessage() { $this->if($asserter = new asserters\error(new asserter\generator()))->and($asserter->withMessage(uniqid()))->then->variable($asserter->getMessage())->isNotNull()->boolean($asserter->messageIsPattern())->isFalse()->object($asserter->withAnyMessage())->isIdenticalTo($asserter)->variable($asserter->getMessage())->isNull()->boolean($asserter->messageIsPattern())->isFalse()->if($asserter->withPattern(uniqid()))->then->variable($asserter->getMessage())->isNotNull()->boolean($asserter->messageIsPattern())->isTrue()->object($asserter->withAnyMessage())->isIdenticalTo($asserter)->variable($asserter->getMessage())->isNull()->boolean($asserter->messageIsPattern())->isFalse(); }
public function test__toStringWithErrors() { $this->mockGenerator->shunt('__construct')->if($score = new \mock\atoum\test\score())->and($test = new \mock\mageekguy\atoum\test())->and($this->calling($test)->getScore = $score)->and($this->calling($test)->getClass = $class = uniqid())->and($this->calling($test)->getCurrentMethod[1] = $method = uniqid())->and($this->calling($test)->getCurrentMethod[2] = $otherMethod = uniqid())->and($this->calling($score)->getLastErroredMethod[1] = array('case' => $case = uniqid(), 'dataSetKey' => $dataSetKey = uniqid(), 'dataSetProvider' => $dataSetProvider = uniqid(), 'class' => $class, 'method' => $method, 'file' => $file = uniqid(), 'line' => $line = rand(1, PHP_INT_MAX), 'type' => $type = rand(1, E_ALL), 'message' => $message = uniqid(), 'errorFile' => $errorFile = uniqid(), 'errorLine' => $errorLine = rand(1, PHP_INT_MAX)))->and($this->calling($score)->getLastErroredMethod[2] = array('case' => null, 'dataSetKey' => null, 'dataSetProvider' => null, 'class' => $class, 'method' => $otherMethod, 'file' => $otherFile = uniqid(), 'line' => $otherLine = rand(1, PHP_INT_MAX), 'type' => $otherType = rand(1, E_ALL), 'message' => $otherMessage = uniqid(), 'errorFile' => null, 'errorLine' => null))->if($field = new testedClass())->then->castToString($field)->isEmpty()->if($field->handleEvent(atoum\test::error, $test))->then->castToString($field)->isEqualTo('not ok 1 - ' . $class . '::' . $method . '()' . PHP_EOL . '# ' . atoum\asserters\error::getAsString($type) . ' : ' . $message . PHP_EOL . '# ' . $errorFile . ':' . $errorLine . PHP_EOL)->if($field->handleEvent(atoum\test::error, $test))->then->castToString($field)->isEqualTo('not ok 2 - ' . $class . '::' . $otherMethod . '()' . PHP_EOL . '# ' . atoum\asserters\error::getAsString($otherType) . ' : ' . $otherMessage . PHP_EOL . '# ' . $otherFile . ':' . $otherLine . PHP_EOL); }
public function useConfigurationCallable(\closure $callback) { $script = call_user_func($this->configuratorFactory, $this); $runner = $this->runner; $errors = array(); $this->adapter->set_error_handler(function ($error, $message, $file, $line, $context) use(&$errors) { foreach (array_reverse(debug_backtrace()) as $trace) { if (isset($trace['file']) === true && $trace['file'] === __DIR__) { $file = __DIR__; $line = $trace['line']; break; } } $errors[] = array($error, $message, $file, $line, $context); }); $callback($script, $runner); $this->adapter->restore_error_handler(); if (sizeof($errors) > 0) { list($error, $message, $file, $line, $context) = $errors[0]; throw new exceptions\runtime('Unable to configure runner. ' . atoum\asserters\error::getAsString($error) . ': ' . $message . ' in ' . $file . ' on line ' . $line); } return $this; }
public function testSetScore() { $this->if($asserter = new sut())->then->object($asserter->setScore($score = new atoum\test\score()))->isIdenticalTo($asserter)->object($asserter->getScore())->isIdenticalTo($score)->object($asserter->setScore())->isIdenticalTo($asserter)->object($asserter->getScore())->isNotIdenticalTo($score)->isInstanceOf('mageekguy\\atoum\\score'); }