Exemplo n.º 1
0
 public function testTransform()
 {
     $camelCase = new Format\CamelCase();
     $snakeCase = new Format\SnakeCase();
     $transformer = new CaseTransformer($camelCase, $snakeCase);
     $this->assertEquals('hello_world', $transformer->transform('helloWorld'));
     $transformer = new CaseTransformer($snakeCase, $camelCase);
     $this->assertEquals('helloWorld', $transformer->transform('hello_world'));
 }
 public function testGetterSetter()
 {
     $Transformer = new CaseTransformer(new SnakeCase(), new StudlyCaps());
     $data = $this->getMockData();
     /** @var AbstractModel $Model */
     $Model = new $this->modelClass();
     $Model->exchangeArray(json_decode($data));
     $ModelReflection = new \ReflectionClass($this->modelClass);
     $ClassFinder = new ClassFinder('DockerCloud');
     $StaticReflectionParser = new StaticReflectionParser($this->modelClass, $ClassFinder);
     $useStatements = $StaticReflectionParser->getUseStatements();
     foreach ($ModelReflection->getProperties(\ReflectionProperty::IS_PROTECTED) as $ReflectionProperty) {
         // Parse @var tag
         $DockBlock = DocBlockFactory::createInstance()->create($ReflectionProperty->getDocComment());
         $this->assertTrue($DockBlock->hasTag('var'));
         /**
          * @var Var_ $VarTag
          */
         $VarTag = $DockBlock->getTagsByName('var')[0];
         $varTypes = explode('|', $VarTag->getType()->__toString());
         //echo $VarTag . PHP_EOL;
         $foundMatchVarTypeCount = 0;
         foreach ($varTypes as $varType) {
             // Get value by using getter method
             $getterMethodName = $Transformer->transform($ReflectionProperty->getName());
             if ('bool' == $varType || 'boolean' == $varType) {
                 $getterMethodName = 'is' . $getterMethodName;
             } else {
                 $getterMethodName = 'get' . $getterMethodName;
             }
             if (!method_exists($Model, $getterMethodName)) {
                 continue;
             }
             $value = $Model->{$getterMethodName}();
             if (strpos($varType, '[]') && is_array($value)) {
                 $value = array_pop($value);
             }
             // If there's no actual value we cannot really validate it...
             $varType = str_replace('[]', '', $varType);
             if (strpos($varType, '\\') === 0) {
                 $varType = substr($varType, 1);
             }
             $foundMatchVarType = $this->validateInternalType($varType, $value);
             if (is_null($foundMatchVarType)) {
                 $foundMatchVarType = $this->validateImportedType($varType, $value, $useStatements);
             }
             if ($foundMatchVarType) {
                 $foundMatchVarTypeCount++;
             }
         }
         self::assertTrue($foundMatchVarTypeCount > 0, sprintf("[%s] haven't getter method.", $ReflectionProperty->getName()));
     }
 }