/** * getDeclaringClass() test * * Call as method call * * Returns: \Zend\Server\Reflection\ReflectionClass */ public function testGetDeclaringClass() { $r = new Reflection\ReflectionMethod($this->_class, $this->_method); $class = $r->getDeclaringClass(); $this->assertTrue($class instanceof Reflection\ReflectionClass); $this->assertTrue($this->_class === $class); }
/** * Init mocks for tests * @return void */ protected function setUp() { $this->helperMock = $this->getMock('\\Magento\\Captcha\\Helper\\Data', [], [], '', false); $this->actionFlagMock = $this->getMock('\\Magento\\Framework\\App\\ActionFlag', [], [], '', false); $this->messageManagerMock = $this->getMock('\\Magento\\Framework\\Message\\ManagerInterface', [], [], '', false); $this->customerSessionMock = $this->getMock('\\Magento\\Customer\\Model\\Session', ['setUsername', 'getBeforeAuthUrl'], [], '', false); $this->captchaStringResolverMock = $this->getMock('\\Magento\\Captcha\\Observer\\CaptchaStringResolver', [], [], '', false); $this->customerUrlMock = $this->getMock('\\Magento\\Customer\\Model\\Url', [], [], '', false); $this->customerRepositoryMock = $this->getMock('\\Magento\\Customer\\Api\\CustomerRepositoryInterface', [], [], '', false); $this->accountManagementHelperMock = $this->getMock('\\Magento\\Customer\\Helper\\AccountManagement', [], [], '', false); $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); $this->observer = $objectManager->getObject('Magento\\Captcha\\Observer\\CheckUserLoginObserver', ['helper' => $this->helperMock, 'actionFlag' => $this->actionFlagMock, 'messageManager' => $this->messageManagerMock, 'customerSession' => $this->customerSessionMock, 'captchaStringResolver' => $this->captchaStringResolverMock, 'customerUrl' => $this->customerUrlMock]); $methodSetAccountManagementHelper = new \ReflectionMethod('Magento\\Captcha\\Observer\\CheckUserLoginObserver', 'setAccountManagementHelper'); $methodSetAccountManagementHelper->setAccessible(true); $methodSetAccountManagementHelper->invoke($this->observer, $this->accountManagementHelperMock); $methodSetCustomerRepository = new \ReflectionMethod('Magento\\Captcha\\Observer\\CheckUserLoginObserver', 'setCustomerRepository'); $methodSetCustomerRepository->setAccessible(true); $methodSetCustomerRepository->invoke($this->observer, $this->customerRepositoryMock); }
/** * Retrieve all optional parameters names. * * @param ReflectionMethod $methodReflection * @return array */ protected function _getOptionalParamNames(ReflectionMethod $methodReflection) { $optionalParamNames = array(); $methodInterfaces = $methodReflection->getPrototypes(); /** Take the fullest interface that includes optional parameters also. */ /** @var \Zend\Server\Reflection\Prototype $methodInterface */ $methodInterface = end($methodInterfaces); $methodParams = $methodInterface->getParameters(); /** @var ReflectionParameter $paramReflection */ foreach ($methodParams as $paramReflection) { if ($paramReflection->isOptional()) { $optionalParamNames[] = $paramReflection->getName(); } } return $optionalParamNames; }
/** * Identify if API resource is top level resource or subresource. * * @param ReflectionMethod $methodReflection * @return bool * @throws InvalidArgumentException In case when class name is not valid API resource class. */ public function isSubresource(ReflectionMethod $methodReflection) { $className = $methodReflection->getDeclaringClass()->getName(); if (preg_match(Mage_Webapi_Model_Config_ReaderAbstract::RESOURCE_CLASS_PATTERN, $className, $matches)) { return count(explode('_', trim($matches[3], '_'))) > 1; } throw new InvalidArgumentException(sprintf('"%s" is not a valid resource class.', $className)); }
/** * Retrieve method interface and documentation description. * * @param ReflectionMethod $method * @return array * @throws \InvalidArgumentException */ public function extractMethodData(ReflectionMethod $method) { $methodData = array('documentation' => $method->getDescription(), 'interface' => array()); $prototypes = $method->getPrototypes(); /** Take the fullest interface that also includes optional parameters. */ /** @var \Zend\Server\Reflection\Prototype $prototype */ $prototype = end($prototypes); /** @var \Zend\Server\Reflection\ReflectionParameter $parameter */ foreach ($prototype->getParameters() as $parameter) { $parameterData = array('type' => $this->_typeProcessor->process($parameter->getType()), 'required' => !$parameter->isOptional(), 'documentation' => $parameter->getDescription()); if ($parameter->isOptional()) { $parameterData['default'] = $parameter->getDefaultValue(); } $methodData['interface']['in']['parameters'][$parameter->getName()] = $parameterData; } if ($prototype->getReturnType() != 'void' && $prototype->getReturnType() != 'null') { $methodData['interface']['out']['parameters']['result'] = array('type' => $this->_typeProcessor->process($prototype->getReturnType()), 'documentation' => $prototype->getReturnValue()->getDescription(), 'required' => true); } return $methodData; }
/** * Extract method deprecation policy "use method" data. * * @param ReflectionMethod $methodReflection * @param string $useMethod * @param array $deprecationPolicy * @throws LogicException */ protected function _extractDeprecationPolicyUseMethod(ReflectionMethod $methodReflection, $useMethod, &$deprecationPolicy) { $invalidFormatMessage = sprintf('The "%s" method has invalid format of Deprecation policy. ' . 'Accepted formats are createV1, catalogProduct::createV1 ' . 'and Mage_Catalog_Webapi_ProductController::createV1.', $methodReflection->getDeclaringClass()->getName() . '::' . $methodReflection->getName()); /** Add information about what method should be used instead of deprecated/removed one. */ /** * Description is expected in one of the following formats: * - Mage_Catalog_Webapi_ProductController::createV1 * - catalogProduct::createV1 * - createV1 */ $useMethodParts = explode('::', $useMethod); switch (count($useMethodParts)) { case 2: try { /** Support of: Mage_Catalog_Webapi_ProductController::createV1 */ $resourceName = $this->_helper->translateResourceName($useMethodParts[0]); } catch (InvalidArgumentException $e) { /** Support of: catalogProduct::createV1 */ $resourceName = $useMethodParts[0]; } $deprecationPolicy['use_resource'] = $resourceName; $methodName = $useMethodParts[1]; break; case 1: $methodName = $useMethodParts[0]; /** If resource was not specified, current one should be used. */ $deprecationPolicy['use_resource'] = $this->_helper->translateResourceName($methodReflection->getDeclaringClass()->getName()); break; default: throw new LogicException($invalidFormatMessage); break; } try { $methodWithoutVersion = $this->getMethodNameWithoutVersionSuffix($methodName); } catch (Exception $e) { throw new LogicException($invalidFormatMessage); } $deprecationPolicy['use_method'] = $methodWithoutVersion; $methodVersion = str_replace($methodWithoutVersion, '', $methodName); $deprecationPolicy['use_version'] = ucfirst($methodVersion); }
/** * Retrieve method full documentation description. * * @param ReflectionMethod $method * @return string */ protected function extractMethodDescription(ReflectionMethod $method) { $methodReflection = new MethodReflection($method->getDeclaringClass()->getName(), $method->getName()); $docBlock = $methodReflection->getDocBlock(); return $this->_typeProcessor->getDescription($docBlock); }
/** * Identify API method version by its reflection. * * @param ReflectionMethod $methodReflection * @return string|bool Method version with prefix on success. * false is returned in case when method should not be exposed via API. */ protected function _getMethodVersion(ReflectionMethod $methodReflection) { $methodVersion = false; $methodNameWithSuffix = $methodReflection->getName(); $regularExpression = $this->_helper->getMethodNameRegularExpression(); if (preg_match($regularExpression, $methodNameWithSuffix, $methodMatches)) { $resourceNamePosition = 2; $methodVersion = ucfirst($methodMatches[$resourceNamePosition]); } return $methodVersion; }
/** * Retrieve method full documentation description. * * @param ReflectionMethod $method * @return string */ protected function extractMethodDescription(ReflectionMethod $method) { $methodReflection = new MethodReflection($method->getDeclaringClass()->getName(), $method->getName()); $docBlock = $methodReflection->getDocBlock(); if (!$docBlock) { throw new \LogicException('The docBlock of the method ' . $method->getDeclaringClass()->getName() . '::' . $method->getName() . ' is empty.'); } return $this->_typeProcessor->getDescription($docBlock); }