/** * @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; }
/** * 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; }
/** * 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; }