Ejemplo n.º 1
0
 /**
  * Generate stub code for unit tests with the help of Zend\CodeGenerator
  * @param string $extends a classname this unittest extents
  * @return Cogen\CodeGenerator\PhpClass
  */
 public function generateUnitTest($extendedClass = 'PHPUnit_TestCase')
 {
     $mainProperty = "\$this->" . $this->class->getLcLastName();
     $phpUnitClass = new PhpClass($this->class->getLastName() . 'Test');
     $phpUnitClass->setExtendedClass($extendedClass);
     $phpUnitClass->addProperty($this->class->getLcLastName(), $this->class->getName(), 'Testable object');
     $setupCode = new Code('init,expect,default,assert', "");
     $setupCode->append($this->getClassInitialization($setupCode, $mainProperty));
     $phpUnitClass->addMethod('setUp', $setupCode);
     $tearDownCode = new Code('default');
     $tearDownCode[] = "unset({$mainProperty});";
     $phpUnitClass->addMethod('tearDown', $tearDownCode);
     foreach ($this->methods as $method) {
         /* @var $method ReflectionMethod */
         /**
          * Each method could have if() instructions in it - we'd probably generate tests for each of
          * possible conditions.
          */
         foreach ($method->detectDecisions() as $meta) {
             $methodCode = new Code('init,expect,default,assert', "");
             $methodCode['init'] = "\$this->markTestIncomplete();";
             $this->generateTestMethodCall($method, $mainProperty, $methodCode);
             $methodName = "test{$meta['name']}";
             if (!$phpUnitClass->hasMethod($methodName)) {
                 $phpMethod = $phpUnitClass->addMethod($methodName, $methodCode);
                 $phpMethod->setDocblock($method->getName() . '(): ' . $meta['line']);
             }
         }
     }
     return $phpUnitClass;
 }
Ejemplo n.º 2
0
 public function getAllDepencies(Tokenize $tokenize)
 {
     $code = array();
     foreach ($this->getDependentServices($tokenize) as $id => $methods) {
         $service = $this->container->get($id);
         $refl = new ReflectionClass($service);
         $mockVariable = '$mock' . $refl->getLastName();
         foreach ($methods as $methodName) {
             $code[] = "\${$methodName}Value = '';";
         }
         $code[] = "//{$mockVariable} = new {$refl->getName()}();";
         $code[] = "{$mockVariable} = \$this->getMockBuilder('{$refl->getName()}')->disableOriginalConstructor()->setMethods(array('" . join("', '", $methods) . "'))->getMock();";
         foreach ($methods as $methodName) {
             $code[] = "{$mockVariable}->expects(\$this->any())->method('{$methodName}')->will(\$this->returnValue(\${$methodName}Value));";
         }
         $code[] = "\$container->expects(\$this->any())->method('get')->with(\$this->equalTo('{$id}'))->will(\$this->returnValue({$mockVariable}));";
         //$code[] = "\$container->register('$id', $mockVariable);";
         $code[] = '';
     }
     echo join("\n", $code);
 }