function test_addDefault_object()
 {
     $rvm = new ReturnValuesManager('classe');
     $rvm->setReturnValue('metodo', 'primo', 1)->setReturnObjectDefault('metodo', 'pdefault');
     FakeObjectGenerator::generate($rvm, new CodeGenerator(null));
     //echo FakeObjectGenerator::returnedCode();die();
     $classe = "class classe{\n//timing\n     private static \$times = array();\n//Costants\n//Magic methods\n    public function __call(\$name, \$arguments) {\n        return null;\n    }\n    public function __callStatic(\$name, \$arguments) {\n        return null;\n    }\n//Methods\npublic function __construct()\n{        \$rvm_pdefault = new ReturnValuesManager('pdefault');\n        FakeObjectGenerator::generate(\$rvm_pdefault, new CodeGenerator(null));\n}\n    function metodo(){\n        if(isset(self::\$times['metodo'])){\n            self::\$times['metodo']++;\n        }\n        else{\n            self::\$times['metodo'] = 1;\n        }\nswitch (self::\$times['metodo']){\n        case 1:\n        return 'primo';\n        break;\n        default:         return new pdefault();\n        }\n    }\n}";
     $this->assertEqual(FakeObjectGenerator::returnedCode(), $classe);
     $test_deault = new classe();
     $this->assertEqual($test_deault->metodo(), 'primo');
     $this->assertIsA($test_deault->metodo(), 'pdefault');
     $this->assertIsA($test_deault->metodo(), 'pdefault');
 }
Exemplo n.º 2
0
 function test_returnJustGeneratedClassWithInputParameter_at_with_object_input()
 {
     $rvm = new ReturnValuesManager('Instance2');
     FakeObjectGenerator::generate($rvm, new CodeGenerator(null));
     $rvm = new ReturnValuesManager('test_333');
     $rvm->setInputParameterForMethod('prova_metodo', array('ciao', 'faccia', 1, 2, 3, 5, 'class:Instance2'), 2)->setInputParameterForMethod('prova_metodo', array(6, 7, 8, 'class:Instance2'), 3);
     FakeObjectGenerator::generate($rvm, new CodeGenerator(null));
     $classe = "class test_333{\n//timing\n     private static \$times = array();\n//Costants\n//Magic methods\n    public function __call(\$name, \$arguments) {\n        return null;\n    }\n    public function __callStatic(\$name, \$arguments) {\n        return null;\n    }\n//Methods\n    function prova_metodo(){\n        if(isset(self::\$times['prova_metodo'])){\n            self::\$times['prova_metodo']++;\n        }\n        else{\n            self::\$times['prova_metodo'] = 1;\n        }\nswitch (self::\$times['prova_metodo']){\n        case 2:\n        \$input = array('0'=>'ciao','1'=>'faccia','2'=>1,'3'=>2,'4'=>3,'5'=>5,'6'=>'class:Instance2');\n        \$args = func_get_args();\n        \$index = 0;\n        foreach (\$args as \$arg)\n        {\n          if(is_object(\$arg))\n          {\n            \$args[\$index] = 'class:'.get_class(\$arg);\n          }\n          \$index++;\n        }\n        \$diff = array_diff(\$input,\$args);\n        if(!empty(\$diff)){ throw new Exception(\"Invalid input parameters. [method prova_metodo] [input \".\$args.\"] [case 2]\");}\n        return null;\n        break;\n        case 3:\n        \$input = array('0'=>6,'1'=>7,'2'=>8,'3'=>'class:Instance2');\n        \$args = func_get_args();\n        \$index = 0;\n        foreach (\$args as \$arg)\n        {\n          if(is_object(\$arg))\n          {\n            \$args[\$index] = 'class:'.get_class(\$arg);\n          }\n          \$index++;\n        }\n        \$diff = array_diff(\$input,\$args);\n        if(!empty(\$diff)){ throw new Exception(\"Invalid input parameters. [method prova_metodo] [input \".\$args.\"] [case 3]\");}\n        return null;\n        break;\n        default: return null;\n        }\n    }\n}";
     $this->assertEqual($classe, FakeObjectGenerator::returnedCode());
     $oggetto = new test_333();
     $this->assertNull($oggetto->prova_metodo('ciao'));
     $this->assertNull($oggetto->prova_metodo('ciao', 'faccia', 1, 2, 3, 5, new Instance2()));
     $this->expectException('Exception');
     $oggetto->prova_metodo(new Instance2());
 }
Exemplo n.º 3
0
 function test_ReturnValuesManger_setConstantsWithoutName()
 {
     $rvm = new ReturnValuesManager('classe_senza_costanti');
     $this->expectException('Exception');
     $rvm->setConstant('', '');
 }
Exemplo n.º 4
0
 function test_returnCodeForReturnValueManager_one_two_level_with_parameter_and_redeclaring_class()
 {
     $rm1 = new ReturnValuesManager('test_1');
     $rm2 = new ReturnValuesMAnager('test_2');
     $rm3 = new ReturnValuesManager('test_3');
     $rm5 = new ReturnValuesMAnager('test_5');
     $rvm = new ReturnValuesManager('test_4');
     $rvm->setInputParameterForMethod('metodo_1', array('ciao', 1, 2, 3))->setInputParameterForMethod('metodo_2', array('a', 'b', 'd'), 1)->setInputParameterForMethod('metodo_2', array(1, 2, 3), 2)->setReturnException('metodo_3', 'Exception', 'Eccezione ritornata')->setReturnException('metodo_4', 'Exception', 'Eccezione at', 2)->setReturnObject('metodo_5', 'oggetto_nuovo')->setReturnValue('metodo_7', 'ritorno')->setReturnValue('metodo_8', false, 1)->setReturnValuesArray('metodo_9', array('a', 1, false, true, 33))->setReturnValuesArray('metodo_10', array('b', 1, false, false, 4), 3)->setReturnObjectsArray('metodo_6', array($rm1, $rm2))->setReturnObjectsArray('metodo_11', array($rm3, $rm5), 3);
     $rvm5 = $rvm->getReturnValue('metodo_5');
     $rvm5->setReturnObject('metodo_23', 'oggetto_nuovo_2');
     //echo $rvm->returnCodeForReturnValueManager($rvm);
     $classe = "        \$rvm_test_4 = new ReturnValuesManager('test_4');\n        \$rvm_test_4->setInputParameterForMethod('metodo_1', array('0'=>'ciao','1'=>1,'2'=>2,'3'=>3));\n        \$rvm_test_4->setInputParameterForMethod('metodo_2', array('0'=>'a','1'=>'b','2'=>'d'), 1);\n        \$rvm_test_4->setInputParameterForMethod('metodo_2', array('0'=>1,'1'=>2,'2'=>3), 2);\n        \$rvm_test_4->setReturnException('metodo_3', 'Exception', 'Eccezione ritornata');\n        \$rvm_test_4->setReturnException('metodo_4', 'Exception', 'Eccezione at', 2);\n        \$rvm_test_4->setReturnObject('metodo_5','oggetto_nuovo');\n        \$rvm_oggetto_nuovo = \$rvm_test_4->getReturnValue('metodo_5');\n        \$rvm_oggetto_nuovo->setReturnObject('metodo_23','oggetto_nuovo_2');\n        \$rvm_test_4->setReturnValue('metodo_7', 'ritorno');\n        \$rvm_test_4->setReturnValue('metodo_8', false, 1);\n        \$rvm_test_4->setReturnValuesArray('metodo_9', array('0'=>'a','1'=>1,'2'=>false,'3'=>true,'4'=>33));\n        \$rvm_test_4->setReturnValuesArray('metodo_10', array('0'=>'b','1'=>1,'2'=>false,'3'=>false,'4'=>4), 3);\n        \$rvm_test_1 = new ReturnValuesManager('test_1');\n        \$rvm_test_2 = new ReturnValuesManager('test_2');\n        \$rvm_test_4->setReturnObjectsArray('metodo_6',array(\$rvm_test_1,\$rvm_test_2));\n        \$rvm_test_3 = new ReturnValuesManager('test_3');\n        \$rvm_test_5 = new ReturnValuesManager('test_5');\n        \$rvm_test_4->setReturnObjectsArray('metodo_11',array(\$rvm_test_3,\$rvm_test_5), 3);\n";
     $this->assertEqual($classe, $rvm->returnCodeForReturnValueManager($rvm));
 }
 function test_timingConfigurationOnlyOncePerMethod_without_methods_list()
 {
     $rvm = new ReturnValuesManager('test_99');
     $rvm_array = new ReturnValuesManager('test_101');
     $rvm->setReturnObject('prova_metodo', 'test_101', 1)->setReturnObjectsArray('prova_metodo', array($rvm_array, $rvm_array), 2);
     FakeObjectGenerator::generate($rvm, new CodeGenerator(null));
     $classe = "class test_99{\n//timing\n     private static \$times = array();\n//Costants\n//Magic methods\n    public function __call(\$name, \$arguments) {\n        return null;\n    }\n    public function __callStatic(\$name, \$arguments) {\n        return null;\n    }\n//Methods\n    function prova_metodo(){\n        if(isset(self::\$times['prova_metodo'])){\n            self::\$times['prova_metodo']++;\n        }\n        else{\n            self::\$times['prova_metodo'] = 1;\n        }\nswitch (self::\$times['prova_metodo']){\n        case 1:\n        \$rvm_test_101 = new ReturnValuesManager('test_101');\n        if(self::\$times['prova_metodo'] == 1){\n        FakeObjectGenerator::generate(\$rvm_test_101, new CodeGenerator(null));\n        }\n        \$mocktest_101 = new test_101();\n        return new test_101();\n        break;\n        case 2:\n        \$rvm_test_101 = new ReturnValuesManager('test_101');\n        if(self::\$times['prova_metodo'] == 1){\n        FakeObjectGenerator::generate(\$rvm_test_101, new CodeGenerator(null));\n        }\n        \$mocktest_101 = new test_101();\n        return array(\$mocktest_101,\$mocktest_101);\n        break;\n        default: return null;\n        }\n    }\n}";
     $this->assertEqual($classe, FakeObjectGenerator::returnedCode());
     $oggetto = new test_99();
     $this->assertIsA($oggetto->prova_metodo(), test_101);
     $array = $oggetto->prova_metodo();
     $this->assertIsA($array[0], test_101);
     $this->assertIsA($array[1], test_101);
 }
Exemplo n.º 6
0
 /**
  * Run throw all the methods looking for a method with a default value.
  * If it is found, this method acts as a proxy for the private method that generate the code.
  *
  * @param ReturnValuesManager $returnValuesManager
  * @return string
  */
 public function returnGenerateClassForDefault(ReturnValuesManager $returnValuesManager)
 {
     $code = "";
     foreach ($returnValuesManager->getMethodsSet() as $method) {
         $return_values = $returnValuesManager->getReturnValueArray();
         if (isset($return_values[$method]['default']) && $return_values[$method]['default'] instanceof ReturnValuesManager) {
             $code .= $this->getCodeForCreatingClassForDefault($return_values[$method]['default']);
         }
     }
     return $code;
 }