Exemplo n.º 1
0
 /**
  * @covers \Box\TestScribe\Utils\ArrayUtil::joinNonEmptyStringsWithNewLine
  * @covers \Box\TestScribe\Utils\ArrayUtil
  */
 public function testJoinNonEmptyStringsWithNewLine_empty_strings_only()
 {
     // Execute the method under test.
     $executionResult = \Box\TestScribe\Utils\ArrayUtil::joinNonEmptyStringsWithNewLine([''], 2);
     // Validate the execution result.
     $expected = '';
     $this->assertSame($expected, $executionResult, 'Variable ( executionResult ) doesn\'t have the expected value.');
 }
 /**
  * @param \Box\TestScribe\Mock\MockClass $mockClass
  * @param string                              $injectMethodName
  *
  * @return string
  */
 public function genInjectedMockStatement(MockClass $mockClass, $injectMethodName)
 {
     $createMockObjectStatement = $this->mockRenderer->renderAMock($mockClass);
     $injectStatement = $this->genInjectionStatement($mockClass, $injectMethodName);
     // @TODO (ryang 8/6/14) : better handling of indentations.
     $combinedStatements = ArrayUtil::joinNonEmptyStringsWithNewLine([$createMockObjectStatement, $injectStatement], 1);
     return $combinedStatements;
 }
 /**
  * Generate the test method as a string.
  *
  * @param \Box\TestScribe\Execution\ExecutionResult   $executionResult
  *
  * @return string
  */
 public function renderMethodBody(ExecutionResult $executionResult)
 {
     $objectInjectionStatements = $this->injectedMocksRenderer->renderObjectInjectionStatements();
     $exceptionExpectationStatements = $this->exceptionRenderer->genExceptionExpectation($executionResult);
     $methodInvocationStatements = $this->invocationRenderer->renderMethodInvocation($executionResult);
     // @TODO (ryang 6/3/15) : move exception expectation statement after all the mocks statements.
     $methodBody = ArrayUtil::joinNonEmptyStringsWithNewLine([$objectInjectionStatements, $exceptionExpectationStatements, $methodInvocationStatements], 2);
     return $methodBody;
 }
 /**
  * Generate statements for setting up the mocks of the objects
  * injected by the dependency management system.
  *
  * @return string
  */
 public function renderObjectInjectionStatements()
 {
     $objectInjectionStatements = $this->injectedMockObjectsRenderer->genMockedObjectStatements();
     $mockClassInjectionStatements = $this->injectedMockClassesRenderer->genMockedClassesStatements();
     $combinedStatements = ArrayUtil::joinNonEmptyStringsWithNewLine([$objectInjectionStatements, $mockClassInjectionStatements], 2);
     $comment = "// Setup mocks injected by the dependency management system.\n\n";
     $result = Util::appendStringIfNotEmpty($comment, $combinedStatements);
     return $result;
 }
 /**
  * Return the statements that sets the expectations and
  * return values of the method invocation of the mocked
  * object.
  *
  * @param MockClass $mock
  *
  * @return string
  */
 public function renderMethodExpectations(MockClass $mock)
 {
     $mockedExpectationsArray = [];
     foreach ($mock->getMethodInvocations() as $invocation) {
         list($methodObj, $arguments, $value) = $invocation;
         $mockedOneExpectation = $this->mockMethodExpectationRenderer->renderOneMethodExpectation($methodObj, $arguments, $value);
         $mockedExpectationsArray[] = $mockedOneExpectation;
     }
     $mockedExpectations = ArrayUtil::joinNonEmptyStringsWithNewLine($mockedExpectationsArray, 2);
     return $mockedExpectations;
 }
 /**
  * Generate all injection statements for either mock classes or mock objects.
  *
  * @param array  $mocks
  *   class name string => MockClass
  * @param string $injectMethodName
  *
  * @return string
  */
 public function genInjectionStatements($mocks, $injectMethodName)
 {
     $statementArray = [];
     /* @var $mock \Box\TestScribe\Mock\MockClass */
     foreach ($mocks as $mock) {
         $statement = $this->oneInjectedMockRenderer->genInjectedMockStatement($mock, $injectMethodName);
         $statementArray[] = $statement;
     }
     $combinedStatements = ArrayUtil::joinNonEmptyStringsWithNewLine($statementArray, 2);
     return $combinedStatements;
 }
 /**
  * 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;
 }
 /**
  * Generate the argument list as a string and
  * its referenced mock statements.
  *
  * @param \Box\TestScribe\ArgumentInfo\Arguments $argsObj
  *
  * @return \Box\TestScribe\Renderers\ArgumentsRenderResult
  */
 public function renderArguments(Arguments $argsObj)
 {
     $expressions = $argsObj->getExpressions();
     $argumentsString = implode(', ', $expressions);
     $mocks = $argsObj->getMocks();
     $mockObjectStatementArray = [];
     foreach ($mocks as $mock) {
         $mockObjectStatement = $this->mockRenderer->renderAMock($mock);
         $mockObjectStatementArray[] = $mockObjectStatement;
     }
     $mockObjectStatementsString = ArrayUtil::joinNonEmptyStringsWithNewLine($mockObjectStatementArray, 2);
     $result = new ArgumentsRenderResult($argumentsString, $mockObjectStatementsString);
     return $result;
 }
 /**
  * Generate assertions of an object.
  *
  * @param string  $variableName name without '$' prefix
  * @param \object $value
  *
  * @return string
  */
 public function generateForAnObject($variableName, $value)
 {
     $objectTypeString = get_class($value);
     $isMockObject = $this->mockClassUtil->isMockClass($objectTypeString);
     $typeCheckStatement = $this->renderObjectTypeAssertion($isMockObject, $variableName, $value);
     if ($isMockObject) {
         // Calling methods on mocked object at the rendering phase can
         // confuse users where the call comes from.
         // Don't call json_encode() method here.
         // @TODO (ryang 12/19/14) : re-evaluate when we start supporting non shmock based mocking
         // frameworks
         $valueCheckStatement = '';
     } else {
         $valueCheckStatement = $this->renderObjectValueAssertion($variableName, $value);
     }
     $statements = ArrayUtil::joinNonEmptyStringsWithNewLine([$typeCheckStatement, $valueCheckStatement], 2);
     return $statements;
 }
Exemplo n.º 10
0
 /**
  * @param \Box\TestScribe\Mock\MockClass $mock
  *
  * @return string
  *
  * Define this method in a separate class causes circular dependencies.
  * i.e. these two methods depend on each other.
  */
 public function renderMockedReturnValue(MockClass $mock)
 {
     $statementsArray = [];
     $mocks = $mock->getMockedReturnValues();
     if ($mocks) {
         $statementsArray[] = "// Set up mocks of return values.";
         foreach ($mocks as $mockedReturnValueObj) {
             $oneMockStatement = $this->renderAMock($mockedReturnValueObj);
             $statementsArray[] = $oneMockStatement;
         }
     }
     $statementsString = ArrayUtil::joinNonEmptyStringsWithNewLine($statementsArray, 2);
     return $statementsString;
 }
Exemplo n.º 11
0
 /**
  * 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;
 }