/** * Create skeleton object * * @param array $request_data Request datas * @return int ID of skeleton * * @url POST skeleton/ */ function post($request_data = NULL) { if (!DolibarrApiAccess::$user->rights->skeleton->create) { throw new RestException(401); } // Check mandatory fields $result = $this->_validate($request_data); foreach ($request_data as $field => $value) { $this->skeleton->{$field} = $value; } if (!$this->skeleton->create(DolibarrApiAccess::$user)) { throw new RestException(500); } return $this->skeleton->id; }
/** * Build a fake class dynamically that extends the desired class, * but will act more like a Friend class on a dynamically created mock. * This lets you use the mock methods in order to validate inputs and * return good, simulated values. * * By using the class renaming functionality of Renamer, the mock is * generated whenever you use the "new" keyword on the class we're * changing. Please consider restructuring your code to use some sort * of dependency injection since overriding "new" is an icky hack. * * Arguments for the callback, when used, should be like this: * function ($testObject, $newMockObject, $constructorArguments) * * @param string $type Name of class to mock * @param callback $callback Constructor callback for mock set up * @param array $methodsToKeep array to pass to getMockExceptMethods * @param boolean $callParent should the mock call the parent constructor */ public function stubWithMock($mocker, $className, $methodsToKeep = array(), $callParent = false) { $methodsToKeep = (array) $methodsToKeep; // Build unique information about this mock for building a key $mockInfo = array(); $mockInfo['class'] = strtolower($className); $methodsToKeep = array_map('strtolower', $methodsToKeep); sort($methodsToKeep); $mockInfo['methodsToKeep'] = $methodsToKeep; $mockInfo['callParent'] = $callParent; // Generate a key so we don't make the same sort of mock twice, then // check that key to see if we need to build the object $mockKey = serialize($mockInfo); if (!isset(self::$stubWithMockCache[$mockKey])) { // First, we will need to generate or reuse a mock object as our base class $mockObject = $mocker(); $skeleton = new Skeleton($mockObject); $php = 'PHPToolsTestUtil::stubWithMockConstructor("' . addslashes($mockKey) . '", $this, func_get_args());'; if ($callParent) { $reflection = $skeleton->reflect()->getConstructor(); $php .= $skeleton->chainMethod($reflection); } $skeleton->setConstructor($php); $skeletonClass = $skeleton->create(); self::$stubWithMockCache[$mockKey] = (object) array('className' => $skeletonClass, 'key' => $mockKey); } $mockDef = self::$stubWithMockCache[$mockKey]; $mockDef->callback = null; self::getRenamer()->renameClass($className, $mockDef->className); return $mockDef; }
/** * Create Skeleton * * @param array $authentication Array of authentication information * @param Skeleton $skeleton $skeleton * @return array Array result */ function createSkeleton($authentication, $skeleton) { global $db, $conf, $langs; $now = dol_now(); dol_syslog("Function: createSkeleton login=" . $authentication['login']); if ($authentication['entity']) { $conf->entity = $authentication['entity']; } // Init and check authentication $objectresp = array(); $errorcode = ''; $errorlabel = ''; $error = 0; $fuser = check_authentication($authentication, $error, $errorcode, $errorlabel); // Check parameters if (!$error) { $newobject = new Skeleton($db); $newobject->prop1 = $skeleton->prop1; $newobject->prop2 = $skeleton->prop2; //... $db->begin(); $result = $newobject->create($fuser); if ($result <= 0) { $error++; } if (!$error) { $db->commit(); $objectresp = array('result' => array('result_code' => 'OK', 'result_label' => ''), 'id' => $newobject->id, 'ref' => $newobject->ref); } else { $db->rollback(); $error++; $errorcode = 'KO'; $errorlabel = $newobject->error; } } if ($error) { $objectresp = array('result' => array('result_code' => $errorcode, 'result_label' => $errorlabel)); } return $objectresp; }
/** * Gets a static class name for a method to work around a PHP bug with * reflection and late static binding calling down to descendents. * * @return string */ protected function getStaticClassName() { $originalName = get_class($this->object); if (!empty(static::$staticClassNames[$originalName])) { return static::$staticClassNames[$originalName]; } $skeleton = new Skeleton($originalName); $newClassName = $skeleton->create(); // Returns new class name static::$staticClassNames[$originalName] = $newClassName; return $newClassName; }