protected function writeServiceBaseAction($serviceId, $serviceName, $action, $actionParams, KalturaParamInfo $outputTypeReflector = null, $testReturnedType = 'int') { $actionName = ucfirst($action); $this->writeIni(""); $this->writeIni("[test{$actionName}]"); $this->writeBase("\t/**"); $this->writeBase("\t * Tests {$serviceName}->{$action} action"); $testParams = array(); $testValues = array(); $validateValues = array(); $addId = false; foreach ($actionParams as $actionParam) { $paramType = $actionParam->getType(); $paramName = $actionParam->getName(); if ($paramType == 'int' && $paramName == 'id') { $addId = true; $testValues[] = '$id'; $this->writeIni("test1.{$paramName}.type = dependency"); continue; } if ($actionParam->isSimpleType() || $actionParam->isEnum()) { $this->writeIni("test1.{$paramName} = " . $actionParam->getDefaultValue()); } elseif ($actionParam->isFile()) { $this->writeIni("test1.{$paramName}.type = file"); $this->writeIni("test1.{$paramName}.path = "); } else { $this->writeIni("test1.{$paramName}.type = {$paramType}"); $actionParamProperties = $actionParam->getTypeReflector()->getProperties(); foreach ($actionParamProperties as $actionParamProperty) { if ($actionParamProperty->isReadOnly() || $actionParamProperty->isInsertOnly()) { continue; } $propertyType = $actionParamProperty->getType(); $propertyName = $actionParamProperty->getName(); if ($actionParamProperty->isSimpleType() || $actionParamProperty->isEnum()) { $this->writeIni("test1.{$paramName}.{$propertyName} = " . $actionParamProperty->getDefaultValue()); } elseif ($actionParamProperty->isFile()) { $this->writeIni("test1.{$paramName}.{$propertyName}.type = file"); $this->writeIni("test1.{$paramName}.{$propertyName}.path = "); } else { $this->writeIni("test1.{$paramName}.{$propertyName}.type = {$propertyType}"); } } } $paramDesc = $actionParam->getDescription(); $this->writeBase("\t * @param {$paramType} \${$paramName} {$paramDesc}"); if (!$actionParam->isComplexType()) { $testParam = "\${$paramName}"; } else { $testParam = "{$paramType} \${$paramName}"; } if ($actionParam->isOptional()) { if ($actionParam->getDefaultValue()) { if ($actionParam->getType() == 'string') { $testParam .= " = '" . $actionParam->getDefaultValue() . "'"; } else { $testParam .= " = " . $actionParam->getDefaultValue(); } } else { $testParam .= " = null"; } } $testParams[] = $testParam; $testValues[] = "\${$paramName}"; $validateValues[] = "\${$paramName}"; } if ($outputTypeReflector) { $paramType = $outputTypeReflector->getType(); if ($outputTypeReflector->isSimpleType() || $outputTypeReflector->isEnum()) { $this->writeIni("test1.reference = " . $outputTypeReflector->getDefaultValue()); } elseif ($outputTypeReflector->isFile()) { $this->writeIni("test1.reference.type = file"); $this->writeIni("test1.reference.path = "); } else { $this->writeIni("test1.reference.type = {$paramType}"); $actionParamProperties = $outputTypeReflector->getTypeReflector()->getProperties(); foreach ($actionParamProperties as $actionParamProperty) { if ($actionParamProperty->isReadOnly()) { continue; } $propertyType = $actionParamProperty->getType(); $propertyName = $actionParamProperty->getName(); if ($actionParamProperty->isSimpleType() || $actionParamProperty->isEnum()) { $this->writeIni("test1.reference.{$propertyName} = " . $actionParamProperty->getDefaultValue()); } elseif ($actionParamProperty->isFile()) { $this->writeIni("test1.reference.{$propertyName}.type = file"); $this->writeIni("test1.reference.{$propertyName}.path = "); } else { $this->writeIni("test1.reference.{$propertyName}.type = {$propertyType}"); } } } $paramDesc = $outputTypeReflector->getDescription(); $this->writeBase("\t * @param {$paramType} \$reference {$paramDesc}"); if (!$outputTypeReflector->isComplexType()) { $testParam = "\$reference"; } else { $testParam = "{$paramType} \$reference"; } if ($outputTypeReflector->isOptional()) { if ($outputTypeReflector->getDefaultValue()) { if ($outputTypeReflector->getType() == 'string') { $testParam .= " = '" . $outputTypeReflector->getDefaultValue() . "'"; } else { $testParam .= " = " . $outputTypeReflector->getDefaultValue(); } } else { $testParam .= " = null"; } } $testParams[] = $testParam; $validateValues[] = "\$reference"; } if ($addId) { $this->writeBase("\t * @param int id - returned from testAdd"); $testParams[] = '$id'; } $testParams = implode(', ', $testParams); $testValues = implode(', ', $testValues); $validateValues = implode(', ', $validateValues); $outputType = null; if ($outputTypeReflector) { $outputType = $outputTypeReflector->getType(); } if ($testReturnedType) { $this->lastDependencyTest = "test{$actionName}"; $this->writeBase("\t * @return {$testReturnedType}"); } if ($addId) { if ($testReturnedType) { $this->writeBase("\t * @depends testAdd with data set #0"); } else { $this->writeBase("\t * @depends testFinished"); } } $this->writeBase("\t * @dataProvider provideData"); $this->writeBase("\t */"); $this->writeBase("\tpublic function test{$actionName}({$testParams})"); $this->writeBase("\t{"); $this->writeBase("\t\t\$resultObject = \$this->client->{$serviceName}->{$action}({$testValues});"); if ($outputType) { $this->writeBase("\t\t\$this->assertType('{$outputType}', \$resultObject);"); } if ($testReturnedType) { $this->writeBase("\t\t\$this->assertNotNull(\$resultObject->id);"); } $this->writeBase("\t\t\$this->validate{$actionName}({$validateValues});"); if ($testReturnedType) { $this->writeBase("\t\treturn \$resultObject->id;"); } $this->writeBase("\t}"); $this->writeBase(""); $this->writeBase("\t/**"); $this->writeBase("\t * Validates test{$actionName} results"); $this->writeBase("\t */"); $this->writeBase("\tprotected function validate{$actionName}({$testParams})"); $this->writeBase("\t{"); // TODO - add compare based on object type $this->writeBase("\t}"); $this->writeBase(""); $this->writeTest("\t/**"); $this->writeTest("\t * Validates test{$actionName} results"); $this->writeTest("\t */"); $this->writeTest("\tprotected function validate{$actionName}({$testParams})"); $this->writeTest("\t{"); $this->writeTest("\t\tparent::validate{$actionName}({$validateValues});"); $this->writeTest("\t\t// TODO - add your own validations here"); $this->writeTest("\t}"); $this->writeTest(""); }
/** * Action returns array of the parameters the action method expects * @return array<KalturaParamInfo> */ public function getActionParams() { if (is_null($this->_actionParams)) { // reflect the service $reflectionClass = new ReflectionClass($this->_actionClass); $reflectionMethod = $reflectionClass->getMethod($this->_actionMethodName); $docComment = $reflectionMethod->getDocComment(); $reflectionParams = $reflectionMethod->getParameters(); $this->_actionParams = array(); foreach ($reflectionParams as $reflectionParam) { $name = $reflectionParam->getName(); if (in_array($name, $this->_reservedKeys)) { throw new Exception("Param [{$name}] in action [{$this->_actionMethodName}] is a reserved key"); } $parsedDocComment = new KalturaDocCommentParser($docComment, array(KalturaDocCommentParser::DOCCOMMENT_REPLACENET_PARAM_NAME => $name)); $paramClass = $reflectionParam->getClass(); // type hinting for objects if ($paramClass) { $type = $paramClass->getName(); } else { $result = null; if ($parsedDocComment->param) { $type = $parsedDocComment->param; } else { throw new Exception("Type not found in doc comment for param [" . $name . "] in action [" . $this->_actionMethodName . "] in service [" . $this->_serviceId . "]"); } } $paramInfo = new KalturaParamInfo($type, $name); $paramInfo->setDescription($parsedDocComment->paramDescription); if ($reflectionParam->isOptional()) { $paramInfo->setDefaultValue($reflectionParam->getDefaultValue()); $paramInfo->setOptional(true); } else { if ($reflectionParam->getClass() && $reflectionParam->allowsNull()) { $paramInfo->setOptional(true); } } if (array_key_exists($name, $parsedDocComment->validateConstraints)) { $paramInfo->setConstraints($parsedDocComment->validateConstraints[$name]); } if (in_array($name, $parsedDocComment->disableRelativeTimeParams, true)) { $paramInfo->setDisableRelativeTime(true); } $this->_actionParams[$name] = $paramInfo; } } return $this->_actionParams; }
/** * * Sets the test params and values for the given action param * @param string $actionParam * @param array $testParams - passed by reference * @param array $testValues - passed by reference * @param array $validateValues - passed by reference * @param boolean $isBase */ protected function setTestParamsAndValues(KalturaParamInfo $actionParam, &$testParams, &$testValues, &$validateValues, $isBase) { $paramType = $actionParam->getType(); $paramName = $actionParam->getName(); KalturaLog::debug("paramName [{$paramName}] paramType [{$paramType}]"); $isParamContainsId = substr_count($paramName, "id") > 0 || substr_count($paramName, "Id") > 0; if ($actionParam->isSimpleType() || $actionParam->isEnum() || $actionParam->isStringEnum() || $actionParam->isDynamicEnum()) { $paramDefaultValue = $actionParam->getDefaultValue(); $this->writeIni("test1.{$paramName} = " . $paramDefaultValue); } elseif ($actionParam->isFile()) { $this->writeIni("test1.{$paramName}.objectType = file"); $this->writeIni("test1.{$paramName}.path = "); } else { if ($paramName == 'type') { $this->writeIni("test1.objType.{$paramName} = {$paramType}"); } else { $this->writeIni("test1.{$paramName}.objectType = {$paramType}"); } $actionParamProperties = $actionParam->getTypeReflector()->getProperties(); foreach ($actionParamProperties as $actionParamProperty) { if ($actionParamProperty->isReadOnly() || $actionParamProperty->isInsertOnly()) { continue; } $propertyType = $actionParamProperty->getType(); $propertyName = $actionParamProperty->getName(); if ($actionParamProperty->isSimpleType() || $actionParamProperty->isEnum() || $actionParamProperty->isStringEnum() || $actionParamProperty->isDynamicEnum()) { $defaultValue = $actionParamProperty->getDefaultValue(); $this->writeIni("test1.{$paramName}.{$propertyName} = " . $defaultValue); } elseif ($actionParamProperty->isFile()) { $this->writeIni("test1.{$paramName}.{$propertyName}.objectType = file"); $this->writeIni("test1.{$paramName}.{$propertyName}.path = "); } elseif (!$actionParamProperty->isAbstract()) { if ($propertyName == 'type') { $this->writeIni("test1.{$paramName}.objType.{$propertyName} = {$propertyType}"); } else { $this->writeIni("test1.{$paramName}.{$propertyName}.objectType = {$propertyType}"); } } } } $paramDesc = strlen($actionParam->getDescription()) ? ' ' . $actionParam->getDescription() : ''; $this->write("\t * @param {$paramType} \${$paramName}{$paramDesc}", $isBase); if (!$actionParam->isComplexType() || $actionParam->isEnum() || $actionParam->isStringEnum() || $actionParam->isDynamicEnum()) { $testParam = "\${$paramName}"; } else { $testParam = "{$paramType} \${$paramName}"; } if ($actionParam->isOptional()) { if ($actionParam->isSimpleType()) { $defaultValue = $actionParam->getDefaultValue(); if ($defaultValue === "false") { $testParam .= " = false"; } else { if ($defaultValue === "true") { $testParam .= " = true"; } else { if ($defaultValue === "null") { $testParam .= " = null"; } else { if ($paramType == "string") { $testParam .= " = \"{$defaultValue}\""; } else { if ($paramType == "int" || $paramType == "bigint") { if ($defaultValue == "") { $testParam .= " = \"\""; } else { $testParam .= " = {$defaultValue}"; } } } } } } } else { $testParam .= " = null"; } } //Adds the new param / value to the test params / values $testParams[] = $testParam; $testValues[] = "\${$paramName}"; if ($isBase) { $validateValues[] = "\${$paramName}"; } }
public function getActionParams($actionName) { if (!$this->isActionExists($actionName)) { throw new Exception("Action [{$actionName}] does not exists for service [{$this->_serviceId}]"); } $actionId = strtolower($actionName); $methodName = $this->_actions[$actionId]; // reflect the service $reflectionClass = new ReflectionClass($this->_serviceClass); $reflectionMethod = $reflectionClass->getMethod($methodName); $docComment = $reflectionMethod->getDocComment(); $reflectionParams = $reflectionMethod->getParameters(); $actionParams = array(); foreach ($reflectionParams as $reflectionParam) { $name = $reflectionParam->getName(); if (in_array($name, $this->_reservedKeys)) { throw new Exception("Param [{$name}] in action [{$actionName}] is a reserved key"); } $parsedDocComment = new KalturaDocCommentParser($docComment, array(KalturaDocCommentParser::DOCCOMMENT_REPLACENET_PARAM_NAME => $name)); $paramClass = $reflectionParam->getClass(); // type hinting for objects if ($paramClass) { $type = $paramClass->getName(); } else { $result = null; if ($parsedDocComment->param) { $type = $parsedDocComment->param; } else { throw new Exception("Type not found in doc comment for param [" . $name . "] in action [" . $actionName . "] in service [" . $this->_serviceId . "]"); } } $paramInfo = new KalturaParamInfo($type, $name); $paramInfo->setDescription($parsedDocComment->paramDescription); if ($reflectionParam->isOptional()) { $paramInfo->setDefaultValue($reflectionParam->getDefaultValue()); $paramInfo->setOptional(true); } else { if ($reflectionParam->getClass() && $reflectionParam->allowsNull()) { $paramInfo->setOptional(true); } } $actionParams[] = $paramInfo; } return $actionParams; }