/** * Generate expected exception statement if an exception is thrown. * Otherwise return ''. * * @param \Box\TestScribe\Execution\ExecutionResult $executionResult * * @return string */ public function genExceptionExpectation(ExecutionResult $executionResult) { $exception = $executionResult->getException(); if ($exception !== null) { $exceptionType = get_class($exception); $exceptionTypeAsStringInCode = $this->varExporter->exportVariable($exceptionType); $exceptionMsg = $exception->getMessage(); $exceptionMsgInCode = $this->varExporter->exportVariable($exceptionMsg); $exceptionStatement = "\$this->setExpectedException({$exceptionTypeAsStringInCode}, {$exceptionMsgInCode});"; } else { $exceptionStatement = ''; } return $exceptionStatement; }
/** * Given the argument value array, return the string * representation. This is used to generate argument list * for method expectations. * If one of the argument is an object, return ''. * * @param array $arguments * * @return string */ public function renderMockedMethodArguments($arguments) { $argArray = []; if ($this->util->isObjectIncluded($arguments)) { // We don't support objects in method expectation arguments yet. // @TODO (ryang 9/16/14) : support objects in method expectation // arguments. return ''; } foreach ($arguments as $arg) { // convert the scalar value to its string representation. $argArray[] = $this->varExporter->exportVariable($arg); } $argumentsString = implode(', ', $argArray); return $argumentsString; }
/** * Generate assertions of a scalar or null value. * * @param string $variableName name without '$' prefix * @param mixed $value * * @return string */ public function generateForAScalarOrNullValue($variableName, $value) { // @TODO (ryang 12/18/14) : handle float type differently. // due to the imprecision of using var_export output to represent a float value // the generated statement may fail under different machines. $valueRepresentationAsCode = $this->varExporter->exportVariable($value); $failureMsg = "Variable ( {$variableName} ) doesn't have the expected value."; // Use var_export directly since the message is a string // and it doesn't contain a return character. // This is done to make it easier to generate unit tests. $failureMsgAsCode = var_export($failureMsg, true); $statement = <<<STRINGEND \$expected = {$valueRepresentationAsCode}; \$this->assertSame( \$expected, \${$variableName}, {$failureMsgAsCode} ); STRINGEND; return $statement; }