示例#1
0
    /**
     * @param bool                                $isPartialMock
     * @param \Box\TestScribe\Mock\MockClass $mock
     *
     * @param string                              $constructorArgumentAsStringInCode
     *   if the string is not empty it means partial mocking is requested.
     *
     * @param string                              $constructorArgumentMockingStatement
     *   mocking statements for any objects in the constructor.
     *
     * @return string
     */
    private function renderAMockInternal($isPartialMock, MockClass $mock, $constructorArgumentAsStringInCode, $constructorArgumentMockingStatement)
    {
        $isStaticMock = $mock->isStaticMock();
        if ($isStaticMock) {
            $mockMethodName = 'shmock_class';
        } else {
            $mockMethodName = 'shmock';
        }
        $mockObjectName = $mock->getMockObjectName();
        $className = $mock->getClassNameBeingMocked();
        $classNameAsStringInCode = var_export($className, true);
        $bodyStatements = $this->renderMockObjectBody($mock);
        $configureMockingPropertiesStatements = "\$shmock->order_matters();";
        if ($isStaticMock) {
            $mockObjectTypeString = 'ClassBuilderStaticClass';
            // @TODO (ryang 1/29/15) : re-evaluate after
            // https://github.com/box/shmock/issues/7 is fixed.
            // Due to the bug above, dont_preserve_original_methods
            // will interfere will order_matters and cause the generated tests to fail
            // Not calling dont_preserve_original_methods will cause
            // the original methods not overwritten to be preserved.
        } else {
            $mockObjectTypeString = 'PHPUnitMockInstance';
            if ($isPartialMock) {
                $configureMockingPropertiesStatements = ArrayUtil::joinNonEmptyStringsWithNewLine([$configureMockingPropertiesStatements, $constructorArgumentMockingStatement, "\$shmock->set_constructor_arguments({$constructorArgumentAsStringInCode});"], 2);
            } else {
                if ($this->isInterfaceOrAbstract($className)) {
                    // When the class being mocked is an abstract class or an interface
                    // all original methods have to be overwritten.
                    // Otherwise the mocked object generated by shmock will have syntax errors
                    // when running the generated tests.
                    $doNotPreserveOriginalMethodsStatement = "\$shmock->dont_preserve_original_methods();";
                } else {
                    // @TODO (ryang 4/21/15) : re-evaluate after
                    // https://github.com/box/shmock/issues/8 is fixed.
                    $doNotPreserveOriginalMethodsStatement = '';
                }
                $configureMockingPropertiesStatements = ArrayUtil::joinNonEmptyStringsWithNewLine([$configureMockingPropertiesStatements, "\$shmock->disable_original_constructor();", $doNotPreserveOriginalMethodsStatement], 1);
            }
        }
        $indentUtilObj = new IndentationUtil();
        $mockPropertyStatements = $indentUtilObj->indent(2, $configureMockingPropertiesStatements);
        $combinedBody = ArrayUtil::joinNonEmptyStringsWithNewLine([$mockPropertyStatements, $bodyStatements], 2);
        $mockedObjectString = <<<TAG
/** @var {$className} \${$mockObjectName} */
\${$mockObjectName} = \$this->{$mockMethodName}(
    {$classNameAsStringInCode},
    function (
        /** @var {$className}|\\Shmock\\{$mockObjectTypeString} \$shmock */
        \$shmock
    ) {
{$combinedBody}
    }
);
TAG;
        return $mockedObjectString;
    }