/** * Generate the test method as a string. * * @param bool $shouldVerifyResult * * @param \Box\TestScribe\Execution\ExecutionResult $executionResult * * @return string */ public function genResultValidation($shouldVerifyResult, ExecutionResult $executionResult) { if ($shouldVerifyResult) { $resultValue = $executionResult->getResultValue(); $resultValidationStatements = $this->valueAssertionRenderer->generate('executionResult', $resultValue); $result = "// Validate the execution result.\n\n{$resultValidationStatements}"; } else { $result = ''; } return $result; }
/** * Generate an OneSpec instance from the execution result. * * @param \Box\TestScribe\Execution\ExecutionResult $executionResult * * @return \Box\TestScribe\Spec\OneSpec */ public function genSpecDetail(ExecutionResult $executionResult) { $testName = $this->globalComputedConfig->getTestMethodName(); $result = $executionResult->getResultValue(); $methodArguments = $executionResult->getMethodArguments(); $methodParameters = $methodArguments->getValues(); $constructorArguments = $executionResult->getConstructorArguments(); $constructorParameters = $constructorArguments->getValues(); $mockSpecs = $this->allMockSpecs->getAllMockSpecs(); $oneSpec = new OneSpec($testName, $result, $constructorParameters, $methodParameters, $mockSpecs); return $oneSpec; }
/** * Return statements for invoking the test and verifying the result. * * @param \Box\TestScribe\Execution\ExecutionResult $executionResult * * @param string $targetObjectName * * @return string */ public function genExecutionAndVerification(ExecutionResult $executionResult, $targetObjectName) { $exception = $executionResult->getException(); $isReturnTypeVoid = $this->globalComputedConfig->isReturnTypeVoid(); $shouldVerifyResult = $exception === null && !$isReturnTypeVoid; $methodArguments = $executionResult->getMethodArguments(); $argumentsString = $this->argumentsRenderer->renderArgumentsAsStringInCode($methodArguments); $executionStatements = $this->executionRenderer->genExecutionStatements($shouldVerifyResult, $argumentsString, $targetObjectName); $resultValidationStatements = $this->resultValidationRenderer->genResultValidation($shouldVerifyResult, $executionResult); $result = ArrayUtil::joinNonEmptyStringsWithNewLine([$executionStatements, $resultValidationStatements], 2); return $result; }
/** * @covers Box\TestScribe\Execution\ExecutionResult::jsonSerialize * @covers Box\TestScribe\Execution\ExecutionResult */ public function testJsonSerialize_mockclass_exception() { // Execute the method under test. // Setup mocks for parameters to the constructor. $mockClass = MockObjectFactoryForTest::getInputHistoryDataMockClass(); $constructorArgs = new Arguments([]); $inputValue = InputValueFactoryForTest::createValue('2'); $methodArgs = new Arguments([$inputValue]); $objectUnderTest = new ExecutionResult($constructorArgs, $methodArgs, $mockClass, 1, new \Exception('test exception')); $executionResult = $objectUnderTest->jsonSerialize(); // Validate the execution result. $expected = ['constructorArguments' => "[]", 'methodArguments' => '["2"]', 'mockClassUnderTest' => '"mock object ( mockInputHistoryData )"', 'resultValue' => 1, 'exception' => "Exception (Exception) Msg ( test exception )"]; $this->assertSame($expected, $executionResult); }
/** * 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; }
/** * @param \Box\TestScribe\Execution\ExecutionResult $result * * @return void */ public function showExecutionResult(ExecutionResult $result) { $exception = $result->getException(); if ($exception) { $exceptionType = get_class($exception); $exceptionMsg = $exception->getMessage(); $resultMsg = "An exception ( {$exceptionType} ) is thrown.\n" . "Exception message ( {$exceptionMsg} )."; } else { $value = $result->getResultValue(); // @TODO (ryang 6/4/15) : don't show the value if the return value of the method is void. $valueStr = $this->valueFormatter->getReadableFormat($value); $resultMsg = "Result from this method execution is :\n" . "{$valueStr}\n" . "End of the result."; } $msg = "{$resultMsg}\n\n" . "Please verify this result and the interactions with the mocks are what you expect."; $this->output->writeln($msg); }
/** * Return statements for invoking the test and verifying the result. * * @param \Box\TestScribe\Execution\ExecutionResult $executionResult * * @return string */ public function renderMethodInvocation(ExecutionResult $executionResult) { $mockClassUnderTest = $executionResult->getMockClassUnderTest(); $config = $this->globalComputedConfig; $inMethod = $config->getInMethod(); $reflectionMethod = $inMethod->getReflectionMethod(); $isStatic = $reflectionMethod->isStatic(); $fullyQualifiedClassName = $config->getFullClassName(); $constructorArguments = $executionResult->getConstructorArguments(); $constructorArgumentsRenderedResult = $this->argumentsRenderer->renderArguments($constructorArguments); $constructorArgumentsString = $constructorArgumentsRenderedResult->getArgumentString(); $mockObjectForConstructorStatements = $constructorArgumentsRenderedResult->getMockStatements(); if ($mockObjectForConstructorStatements) { $mockObjectForConstructorStatements = "// Setup mocks for parameters to the constructor.\n\n" . $mockObjectForConstructorStatements; } $methodArguments = $executionResult->getMethodArguments(); $argumentsToTheMethodRenderedResult = $this->argumentsRenderer->renderArguments($methodArguments); $mockMethodArgumentsStatements = $argumentsToTheMethodRenderedResult->getMockStatements(); if ($mockMethodArgumentsStatements) { $mockMethodArgumentsStatements = "// Setup mocks for parameters to the method under test.\n\n" . $mockMethodArgumentsStatements; } $mockAndComment = ArrayUtil::joinNonEmptyStringsWithNewLine(["{$mockMethodArgumentsStatements}", "// Execute the method under test."], 2); $isPartialMocking = $this->partialMockUtil->isClassUnderTestPartiallyMocked($mockClassUnderTest); $createObjectWithMocksStatement = ''; $targetObjectName = ''; if (!$isStatic) { if ($isPartialMocking) { // @TODO (ryang 3/4/15) : move the generated mock statements for the constructor into this scope. $createObjectWithMocksStatement = $this->mockRenderer->renderPartialMock($mockClassUnderTest, $constructorArgumentsString, $mockObjectForConstructorStatements); $targetObjectName = $mockClassUnderTest->getMockObjectName(); } else { $targetObjectName = 'objectUnderTest'; $createObjectStatement = "\${$targetObjectName} = new {$fullyQualifiedClassName}({$constructorArgumentsString});"; $createObjectWithMocksStatement = ArrayUtil::joinNonEmptyStringsWithNewLine([$mockObjectForConstructorStatements, $createObjectStatement], 2); } } $invocationStatement = $this->executionAndVerificationRenderer->genExecutionAndVerification($executionResult, $targetObjectName); $result = ArrayUtil::joinNonEmptyStringsWithNewLine([$mockAndComment, $createObjectWithMocksStatement, $invocationStatement], 2); return $result; }