Example #1
0
 public function test__toString()
 {
     $argument = new php\method\argument($name = uniqid());
     $this->assert->castToString($argument)->isEqualTo('$' . $name)->castToString($argument->isArray())->isEqualTo('array $' . $name)->castToString($argument->isObject($type = uniqid()))->isEqualTo($type . ' $' . $name)->castToString($argument->isUntyped()->setDefaultValue(__FUNCTION__))->isEqualTo('$' . $name . '=' . var_export(__FUNCTION__, true))->castToString($argument->setDefaultValue($defaultValue = uniqid()))->isEqualTo('$' . $name . '=' . var_export($defaultValue, true))->castToString($argument->setDefaultValue(array()))->isEqualTo('$' . $name . '=' . var_export(array(), true))->castToString($argument->setDefaultValue(null))->isEqualTo('$' . $name . '=' . var_export(null, true));
     $argument = new php\method\argument($name = uniqid());
     $argument->isReference();
     $this->assert->castToString($argument)->isEqualTo('& $' . $name)->castToString($argument->setDefaultValue(__FUNCTION__))->isEqualTo('& $' . $name . '=' . var_export(__FUNCTION__, true))->castToString($argument->setDefaultValue($defaultValue = uniqid()))->isEqualTo('& $' . $name . '=' . var_export($defaultValue, true))->castToString($argument->setDefaultValue(array()))->isEqualTo('& $' . $name . '=' . var_export(array(), true))->castToString($argument->setDefaultValue(null))->isEqualTo('& $' . $name . '=' . var_export(null, true));
 }
Example #2
0
 protected function generateClassMethodCode(\reflectionClass $class)
 {
     $mockedMethods = '';
     $mockedMethodNames = array();
     $className = $class->getName();
     $constructor = $class->getConstructor();
     if ($constructor === null) {
         $mockedMethods .= self::generateDefaultConstructor();
         $mockedMethodNames[] = '__construct';
     } else {
         if ($constructor->isFinal() === false) {
             $constructorName = $constructor->getName();
             $overload = $this->getOverload($constructorName);
             if ($constructor->isPublic() === false) {
                 $this->shuntParentClassCalls();
                 if ($overload === null) {
                     $this->overload(new php\method('__construct'));
                     $overload = $this->getOverload('__construct');
                 }
             }
             $parameters = $this->getParameters($constructor);
             if ($overload === null) {
                 $mockedMethods .= "\t" . 'public function __construct(' . $this->getParametersSignature($constructor) . ')';
             } else {
                 $overload->addArgument(php\method\argument::get('mockController')->isObject('\\' . __NAMESPACE__ . '\\controller')->setDefaultValue(null));
                 $mockedMethods .= "\t" . $overload;
             }
             $mockedMethods .= PHP_EOL;
             $mockedMethods .= "\t" . '{' . PHP_EOL;
             $mockedMethods .= "\t\t" . '$arguments = array_merge(array(' . join(', ', $parameters) . '), array_slice(func_get_args(), ' . sizeof($parameters) . ', -1));' . PHP_EOL;
             $mockedMethods .= "\t\t" . 'if ($mockController === null)' . PHP_EOL;
             $mockedMethods .= "\t\t" . '{' . PHP_EOL;
             $mockedMethods .= "\t\t\t" . '$mockController = \\mageekguy\\atoum\\mock\\controller::get();' . PHP_EOL;
             $mockedMethods .= "\t\t" . '}' . PHP_EOL;
             $mockedMethods .= "\t\t" . 'if ($mockController !== null)' . PHP_EOL;
             $mockedMethods .= "\t\t" . '{' . PHP_EOL;
             $mockedMethods .= "\t\t\t" . '$this->setMockController($mockController);' . PHP_EOL;
             $mockedMethods .= "\t\t" . '}' . PHP_EOL;
             if ($constructor->isAbstract() === true || $this->isShunted('__construct') === true || $this->isShunted($className) === true) {
                 $methodName = $this->isShunted($className) === true ? $className : '__construct';
                 $mockedMethods .= "\t\t" . 'if (isset($this->getMockController()->' . $methodName . ') === false)' . PHP_EOL;
                 $mockedMethods .= "\t\t" . '{' . PHP_EOL;
                 $mockedMethods .= "\t\t\t" . '$this->getMockController()->' . $methodName . ' = function() {};' . PHP_EOL;
                 $mockedMethods .= "\t\t" . '}' . PHP_EOL;
                 $mockedMethods .= "\t\t" . '$this->getMockController()->invoke(\'' . $methodName . '\', $arguments);' . PHP_EOL;
             } else {
                 $mockedMethods .= "\t\t" . 'if (isset($this->getMockController()->' . $constructorName . ') === true)' . PHP_EOL;
                 $mockedMethods .= "\t\t" . '{' . PHP_EOL;
                 $mockedMethods .= "\t\t\t" . '$this->getMockController()->invoke(\'' . $constructorName . '\', $arguments);' . PHP_EOL;
                 $mockedMethods .= "\t\t" . '}' . PHP_EOL;
                 $mockedMethods .= "\t\t" . 'else' . PHP_EOL;
                 $mockedMethods .= "\t\t" . '{' . PHP_EOL;
                 $mockedMethods .= "\t\t\t" . '$this->getMockController()->addCall(\'' . $constructorName . '\', $arguments);' . PHP_EOL;
                 if ($this->shuntParentClassCalls === false) {
                     $mockedMethods .= "\t\t\t" . 'call_user_func_array(\'parent::' . $constructorName . '\', $arguments);' . PHP_EOL;
                 }
                 $mockedMethods .= "\t\t" . '}' . PHP_EOL;
             }
             $mockedMethods .= "\t" . '}' . PHP_EOL;
             $mockedMethodNames[] = $constructorName;
         }
     }
     foreach ($class->getMethods() as $method) {
         if ($method->isConstructor() === false && $this->methodIsMockable($method) === true) {
             $methodName = $method->getName();
             $mockedMethodNames[] = strtolower($methodName);
             $overload = $this->getOverload($methodName);
             $parameters = $this->getParameters($method);
             if ($overload !== null) {
                 $mockedMethods .= "\t" . $overload;
             } else {
                 $mockedMethods .= "\t" . ($method->isPublic() === true ? 'public' : 'protected') . ' function' . ($method->returnsReference() === false ? '' : ' &') . ' ' . $methodName . '(' . $this->getParametersSignature($method) . ')';
             }
             $mockedMethods .= PHP_EOL . "\t" . '{' . PHP_EOL;
             $mockedMethods .= "\t\t" . '$arguments = array_merge(array(' . join(', ', $parameters) . '), array_slice(func_get_args(), ' . sizeof($parameters) . '));' . PHP_EOL;
             if ($this->isShunted($methodName) === true || $method->isAbstract() === true) {
                 $mockedMethods .= "\t\t" . 'if (isset($this->getMockController()->' . $methodName . ') === false)' . PHP_EOL;
                 $mockedMethods .= "\t\t" . '{' . PHP_EOL;
                 $mockedMethods .= "\t\t\t" . '$this->getMockController()->' . $methodName . ' = function() {};' . PHP_EOL;
                 $mockedMethods .= "\t\t" . '}' . PHP_EOL;
                 $mockedMethods .= "\t\t" . 'return $this->getMockController()->invoke(\'' . $methodName . '\', $arguments);' . PHP_EOL;
             } else {
                 $mockedMethods .= "\t\t" . 'if (isset($this->getMockController()->' . $methodName . ') === true)' . PHP_EOL;
                 $mockedMethods .= "\t\t" . '{' . PHP_EOL;
                 $mockedMethods .= "\t\t\t" . 'return $this->getMockController()->invoke(\'' . $methodName . '\', $arguments);' . PHP_EOL;
                 $mockedMethods .= "\t\t" . '}' . PHP_EOL;
                 $mockedMethods .= "\t\t" . 'else' . PHP_EOL;
                 $mockedMethods .= "\t\t" . '{' . PHP_EOL;
                 if ($methodName === '__call') {
                     $mockedMethods .= "\t\t\t" . '$this->getMockController()->addCall(current(array_slice($arguments, 0, 1)), current(array_slice($arguments, 1)));' . PHP_EOL;
                 }
                 $mockedMethods .= "\t\t\t" . '$this->getMockController()->addCall(\'' . $methodName . '\', $arguments);' . PHP_EOL;
                 if ($this->shuntParentClassCalls === false) {
                     $mockedMethods .= "\t\t\t" . 'return call_user_func_array(\'parent::' . $methodName . '\', $arguments);' . PHP_EOL;
                 }
                 $mockedMethods .= "\t\t" . '}' . PHP_EOL;
             }
             $mockedMethods .= "\t" . '}' . PHP_EOL;
         }
     }
     return $mockedMethods . self::generateGetMockedMethod($mockedMethodNames);
 }
Example #3
0
 protected function generateClassMethodCode(\reflectionClass $class)
 {
     $mockedMethods = '';
     $hasConstructor = false;
     $className = $class->getName();
     foreach ($class->getMethods() as $method) {
         $isConstructor = $method->isConstructor() || $method->getName() === '__construct';
         if ($isConstructor === true) {
             $hasConstructor = true;
         }
         if ($method->isFinal() === false && $method->isStatic() === false) {
             $methodCode = '';
             $methodName = $method->getName();
             switch (true) {
                 case $method->isProtected() && $method->isAbstract():
                     $methodCode = "\t" . 'protected function' . ($method->returnsReference() === false ? '' : ' &') . ' ' . $methodName . '(' . self::getParameters($method) . ') {}' . PHP_EOL;
                     break;
                 case $method->isPublic():
                     $parameters = array();
                     $overload = $this->getOverload($methodName);
                     if ($overload === null) {
                         $methodCode = "\t" . 'public function' . ($method->returnsReference() === false ? '' : ' &') . ' ' . $methodName . '(' . self::getParameters($method, $isConstructor) . ')' . PHP_EOL;
                         $methodCode .= "\t" . '{' . PHP_EOL;
                         $parameters = array();
                         foreach ($method->getParameters() as $parameter) {
                             $parameters[] = ($parameter->isPassedByReference() === false ? '' : '& ') . '$' . $parameter->getName();
                         }
                     } else {
                         foreach ($overload->getArguments() as $argument) {
                             $parameters[] = $argument->getVariable();
                         }
                         if ($isConstructor === true) {
                             $overload->addArgument(php\method\argument::get('mockController')->isObject('\\' . __NAMESPACE__ . '\\controller')->setDefaultValue(null));
                         }
                         $methodCode = "\t" . $overload . PHP_EOL . "\t" . '{' . PHP_EOL;
                     }
                     $methodCode .= "\t\t" . '$arguments = array_merge(array(' . join(', ', $parameters) . '), array_slice(func_get_args(), ' . sizeof($parameters) . ($isConstructor === false ? '' : ', -1') . '));' . PHP_EOL;
                     if ($isConstructor === true) {
                         $methodCode .= "\t\t" . 'if ($mockController === null)' . PHP_EOL;
                         $methodCode .= "\t\t" . '{' . PHP_EOL;
                         $methodCode .= "\t\t\t" . '$mockController = \\mageekguy\\atoum\\mock\\controller::get();' . PHP_EOL;
                         $methodCode .= "\t\t" . '}' . PHP_EOL;
                         $methodCode .= "\t\t" . 'if ($mockController !== null)' . PHP_EOL;
                         $methodCode .= "\t\t" . '{' . PHP_EOL;
                         $methodCode .= "\t\t\t" . '$this->setMockController($mockController);' . PHP_EOL;
                         $methodCode .= "\t\t" . '}' . PHP_EOL;
                     }
                     if ($this->isShunted($methodName) === true || $method->isAbstract() === true) {
                         $methodCode .= "\t\t" . 'if (isset($this->getMockController()->' . $methodName . ') === false)' . PHP_EOL;
                         $methodCode .= "\t\t" . '{' . PHP_EOL;
                         $methodCode .= "\t\t\t" . '$this->mockController->' . $methodName . ' = function() {};' . PHP_EOL;
                         $methodCode .= "\t\t" . '}' . PHP_EOL;
                         $methodCode .= "\t\t" . ($isConstructor === true ? '' : 'return ') . '$this->mockController->invoke(\'' . $methodName . '\', $arguments);' . PHP_EOL;
                     } else {
                         $methodCode .= "\t\t" . 'if (isset($this->getMockController()->' . $methodName . ') === true)' . PHP_EOL;
                         $methodCode .= "\t\t" . '{' . PHP_EOL;
                         $methodCode .= "\t\t\t" . ($isConstructor === true ? '' : 'return ') . '$this->mockController->invoke(\'' . $methodName . '\', $arguments);' . PHP_EOL;
                         $methodCode .= "\t\t" . '}' . PHP_EOL;
                         $methodCode .= "\t\t" . 'else' . PHP_EOL;
                         $methodCode .= "\t\t" . '{' . PHP_EOL;
                         if ($methodName === '__call') {
                             $methodCode .= "\t\t\t" . '$this->getMockController()->addCall(current(array_slice($arguments, 0, 1)), current(array_slice($arguments, 1)));' . PHP_EOL;
                         }
                         $methodCode .= "\t\t\t" . '$this->getMockController()->addCall(\'' . $methodName . '\', $arguments);' . PHP_EOL;
                         if ($this->shuntParentClassCalls === false) {
                             $methodCode .= "\t\t\t" . ($isConstructor === true ? '' : 'return ') . 'call_user_func_array(\'parent::' . $methodName . '\', $arguments);' . PHP_EOL;
                         }
                         $methodCode .= "\t\t" . '}' . PHP_EOL;
                     }
                     $methodCode .= "\t" . '}' . PHP_EOL;
                     break;
             }
             $mockedMethods .= $methodCode;
         }
     }
     if ($hasConstructor === false) {
         $mockedMethods .= self::generateDefaultConstructor();
     }
     return $mockedMethods;
 }