Example #1
0
 public function testMethod()
 {
     $class = new ReflectionClass('Psc\\Code\\Generate\\TestClass2');
     $factory = GMethod::reflectorFactory($class->getMethod('factory'));
     $method2 = GMethod::reflectorFactory($class->getMethod('method2'));
     $banane = GMethod::reflectorFactory($class->getMethod('banane'));
     $parameters = $method2->getParameters();
     foreach ($parameters as $param) {
         $this->assertInstanceOf('Psc\\Code\\Generate\\GParameter', $param);
     }
     $cr = "\n";
     $factoryCode = 'public static function factory(TestHintMethod $dunno) {' . $cr;
     $factoryCode .= '}';
     $this->assertEquals(0, count($factory->getBodyCode()));
     $this->assertEquals('', $factory->getBody(0));
     $method2Code = 'public function method2($num, Array $p1, stdClass $std = NULL, $bun = array()) { // does matter' . $cr;
     $body = NULL;
     $body .= '$bimbam = \'pling\';' . $cr;
     $body .= $cr;
     $body .= '// anotherinline comment' . $cr;
     $body .= 'return \'schnurpsel\';' . $cr;
     $method2Code .= S::indent($body, 2, $cr);
     $method2Code .= '}';
     $method2->getBodyCode();
     // 2 mal holen darf die anzahl nicht verdoppeln
     $this->assertEquals(4, count($method2->getBodyCode()));
     /* method2 method */
     $this->assertEquals($body, $method2->getBody(0), \Webforge\Common\String::debugEquals($body, $method2->getBody(0)));
     $this->assertEquals($method2Code, $method2->php(), \Webforge\Common\String::debugEquals($method2Code, $method2->php()));
     /* Factory method */
     $this->assertEquals(TRUE, $factory->isStatic());
     $this->assertEquals($factoryCode, $factory->php(), \Webforge\Common\String::debugEquals($factoryCode, $factory->php()));
     $this->assertEquals('abstract public function banane();', $banane->php(), sprintf("output: '%s'", $banane->php()));
 }
Example #2
0
 /**
  * @param array $parameters wenn ein String wird nur der Name des Parameters gecheckt
  */
 public function assertMethodParameters(GMethod $m, array $parameters)
 {
     $this->lastGet = $methodParameters = $m->getParameters();
     $debugParameters = array_map(function ($p) {
         return $p->getName();
     }, $m->getParameters());
     foreach ($parameters as $key => $parameter) {
         if (is_string($parameter)) {
             $this->test->assertArrayHasKey($key, $methodParameters, $this->msg("MethodParameter %d existiert nicht in '%s'. Parameter sind: [%s]", $key, $m->getName(), implode(", ", $debugParameters)));
             $this->test->assertEquals($methodParameters[$key]->getName(), $parameter, $this->msg("MethodParameter %d hat nicht den Namen %s in '%s'. Parameter sind: [%s]", $key, $parameter, $m->getName(), implode(", ", $debugParameters)));
         } else {
             throw new \Psc\Code\NotImplementedException('Parameter können bis jetzt nur strings sein');
         }
     }
     return $this;
 }
Example #3
0
    public function testNewClass()
    {
        $class = new GClass();
        $class->setName('tiptoi\\Entities\\Page');
        // da dieser Test in keinem NS ist
        $this->assertEquals('\\tiptoi\\Entities', $class->getNamespace());
        $this->assertEquals('Page', $class->getClassName());
        $class->addMethod(GMethod::factory('addOID', array(GParameter::factory('$oid', GClass::factory('OID'))), 'if (!$this->oids->contains($oid)) {
  $this->oids->add($oid);
}

return $this;
'));
        $class->setMethod(GMethod::factory('removeOID', array(GParameter::factory('$oid', GClass::factory('OID'))), 'if ($this->oids->contains($oid)) {
  $this->oids->removeElement($oid);
}

return $this;
'));
        $class->addMethod(GMethod::factory('getOIDs', array(), 'return $this->oids;'));
        $classCode = <<<'CLASS_CODE'
class Page {
  
  public function addOID(OID $oid) {
    if (!$this->oids->contains($oid)) {
      $this->oids->add($oid);
    }
    
    return $this;
  }
  
  public function removeOID(OID $oid) {
    if ($this->oids->contains($oid)) {
      $this->oids->removeElement($oid);
    }
    
    return $this;
  }
  
  public function getOIDs() {
    return $this->oids;
  }
}
CLASS_CODE;
        //    file_put_contents('D:\fixture.txt', $classCode);
        //    file_put_contents('D:\compiled.txt',$class->php());
        $this->assertEquals($classCode, $class->php());
    }
Example #4
0
 /**
  * Erstellt einen Stub für eine gegebene abstrakte Methode
  */
 public function createMethodStub(GMethod $method)
 {
     // method is not abstract (thats strange)
     if (!$method->isAbstract()) {
         return $this;
     }
     // no need to implement
     if ($this->hasMethod($method->getName()) && !$method->isAbstract()) {
         return $this;
     }
     $cMethod = clone $method;
     $cMethod->setAbstract(FALSE);
     $cMethod->setDeclaringClass($this);
     return $this->addMethod($cMethod);
 }