Eloquent\Phony\Mock\MockFactory::createPartialMock PHP Метод

createPartialMock() публичный Метод

Create a new partial mock instance for the supplied definition.
public createPartialMock ( ReflectionClass $class, Arguments | array | null $arguments = [] ) : Mock
$class ReflectionClass The class.
$arguments Eloquent\Phony\Call\Arguments | array | null The constructor arguments, or null to bypass the constructor.
Результат Mock The newly created mock.
    public function createPartialMock(ReflectionClass $class, $arguments = array())
    {
        $constructor = $class->getConstructor();
        $isDone = false;
        $isConstructorCalled = false;
        if ($constructor && $constructor->isFinal()) {
            $mock = false;
            if ($this->isConstructorBypassSupportedForExtendedInternals) {
                $mock = $class->newInstanceWithoutConstructor();
                $isDone = true;
                // @codeCoverageIgnoreStart
            } elseif ($this->isConstructorBypassSupported) {
                $isInternal = false;
                $ancestor = $class->getParentClass();
                while (!$isInternal && $ancestor) {
                    if ($isInternal = $ancestor->isInternal()) {
                        break;
                    }
                    $ancestor = $ancestor->getParentClass();
                }
                if (!$isInternal) {
                    $mock = $class->newInstanceWithoutConstructor();
                    $isDone = true;
                }
            }
            if (!$isDone) {
                $className = $class->getName();
                $serialized = sprintf('O:%d:"%s":0:{}', strlen($className), $className);
                try {
                    $mock = @unserialize($serialized);
                    $isDone = $mock instanceof $className;
                } catch (Throwable $error) {
                    // re-thrown after cleanup
                } catch (Exception $error) {
                    // re-thrown after cleanup
                }
            }
            if (!$isDone) {
                if (null === $arguments) {
                    throw new RuntimeException(sprintf('Unable to bypass final constructor for %s.', $class->getName()));
                }
                $mock = $class->newInstanceArgs($arguments);
                $isDone = true;
                $isConstructorCalled = true;
            }
        }
        // @codeCoverageIgnoreEnd
        if (!$isDone) {
            $mock = $class->newInstance();
        }
        $handle = $this->handleFactory->instanceHandle($mock, strval($this->labelSequencer->next()));
        $handle->partial();
        if (!$isConstructorCalled && null !== $arguments) {
            $handle->constructWith($arguments);
        }
        return $mock;
    }