/** * @param MockClass $mock * * @return MockSpec */ public function createMockSpecFromMockClass(MockClass $mock) { $objectName = $mock->getMockObjectName(); $mockedClassName = $mock->getClassNameBeingMocked(); $methodInvocations = $mock->getMethodInvocations(); $invocationSpecs = $this->convertToInvocationSpecs($methodInvocations); $mockSpec = new MockSpec($objectName, $mockedClassName, $invocationSpecs); return $mockSpec; }
/** * @param \Box\TestScribe\Mock\MockClass $mockClass * @param string $injectMockedObjectMethodName * * @return string */ private function genInjectionStatement(MockClass $mockClass, $injectMockedObjectMethodName) { $className = $mockClass->getClassNameBeingMocked(); $classNameInCode = var_export($className, true); $mockObjectName = $mockClass->getMockObjectName(); // The $mockVariableName value doesn't include '$' // add '$' in the front to make it a valid statement. $statement = sprintf("%s(%s, \$%s);", $injectMockedObjectMethodName, $classNameInCode, $mockObjectName); return $statement; }
/** * 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; }
/** * Handle intercepted calls made to the mock class instance. * * @param \Box\TestScribe\Mock\MockClass $mockClass * @param string $methodName * @param array $arguments * * @return mixed|void * @throws \Box\TestScribe\Exception\TestScribeException * @throw \RuntimeException */ public function invokeInterceptedCall(MockClass $mockClass, $methodName, $arguments) { $this->output->writeStartSeparator(); $phpClassObj = $mockClass->getPhpClass(); $methodObj = $this->methodHelper->createFromMethodName($phpClassObj, $methodName); $mockObjName = $mockClass->getMockObjectName(); $this->mockClassServiceCallInfo->showCallInfo($mockObjName, $methodObj, $arguments); $returnValue = $this->mockClassServiceInfoSaver->gatherAndSaveCallInfo($mockClass, $methodName, $arguments); $this->output->writeEndSeparator(); return $returnValue; }
/** * Handle intercepted calls made to the mock class instance. * * @param \Box\TestScribe\Mock\MockClass $mockClass * @param string $methodName * @param array $arguments * * @return mixed|void * @throws \Box\TestScribe\Exception\TestScribeException * @throw \RuntimeException */ public function gatherAndSaveCallInfo(MockClass $mockClass, $methodName, $arguments) { $phpClass = $mockClass->getPhpClass(); $className = $mockClass->getClassNameBeingMocked(); $methodObj = $this->methodHelper->createFromMethodName($phpClass, $methodName); $retPHPDocType = $methodObj->getReturnType(); $value = $this->inputValueGetter->get($retPHPDocType, 'return value', $className, $methodName, ''); $mockClass->saveInvocationInformation($methodObj, $arguments, $value); $returnValue = $value->getValue(); return $returnValue; }
/** * Return true if partial mocking of the class under test is selected. * * @param \Box\TestScribe\Mock\MockClass|null $mockClassUnderTest * * @return bool */ public function isClassUnderTestPartiallyMocked($mockClassUnderTest) { if ($mockClassUnderTest === null) { // Testing a static method will not create a mock class under test // since partial mocking for static methods is not supported. return false; } $nameOfClassBeingMocked = $mockClassUnderTest->getClassNameBeingMocked(); $isAbstract = $this->reflectionUtil->isAbstractClass($nameOfClassBeingMocked); if ($isAbstract) { // Always use partial mocking when testing an abstract class. // The mock class is a concrete class that can be instantiated. // The abstract class can't be instantiated. return true; } // Only use partial mocking when the other public/protected methods of the // class under test are invoked during execution of the method under test. $mockMethodInvocations = $mockClassUnderTest->getMethodInvocations(); $count = count($mockMethodInvocations); return $count !== 0; }
/** * @param \Box\TestScribe\Mock\MockClass $mockClassUnderTest * * @return \Box\TestScribe\Execution\ClassUnderTestMockCreationResultValue */ public function createMockObjectForTheClassUnderTest(MockClass $mockClassUnderTest) { $constructorMethodObj = $mockClassUnderTest->getConstructorOfTheMockedClass(); if ($constructorMethodObj) { $constructorArgs = $this->argumentsCollector->collect($constructorMethodObj); $constructorArgValues = $constructorArgs->getValues(); $this->output->writeln("\nStart executing the constructor.\n"); } else { // When the class under test doesn't have a constructor defined // don't display the constructor execution message. $constructorArgs = new Arguments([]); $constructorArgValues = []; } $result = $this->expectedExceptionCatcher->execute([$this->mockObjectFactory, 'createMockObjectFromMockClass'], [$mockClassUnderTest, $constructorArgValues]); $mockObj = $result->getResult(); $exception = $result->getException(); if ($constructorMethodObj) { $this->output->writeln("\nFinish executing the constructor.\n"); } $result = new ClassUnderTestMockCreationResultValue($constructorArgs, $mockObj, $exception); return $result; }
/** * @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; }
/** * @param string $methodName * @param array $arguments * * @return mixed */ public static function __callStatic($methodName, $arguments) { $rc = self::$generatorMock->invokeInterceptedCall($methodName, $arguments); return $rc; }
private function __routeAllCallsToTestGeneratorMockObjects($methodName, $arguments) { $rc = $this->unitTestGeneratorMockObj->invokeInterceptedCall($methodName, $arguments); return $rc; }