protected static function _createStatic(array $data) { $user = null; $userId = null; if (isset($data['user'])) { /** @var CM_Model_User $user */ $user = $data['user']; $userId = $user->getId(); } $key = (string) $data['key']; $start = (int) $data['start']; /** @var CM_Model_StreamChannel_Abstract $streamChannel */ $streamChannel = $data['streamChannel']; if (!$streamChannel->isValid()) { throw new CM_Exception_Invalid('Stream channel not valid', null, array('severity' => CM_Exception::WARN)); } $allowedUntil = $streamChannel->canSubscribe($user, time()); if ($allowedUntil <= time()) { throw new CM_Exception_NotAllowed('Not allowed to subscribe'); } $id = CM_Db_Db::insert('cm_stream_subscribe', array('userId' => $userId, 'start' => $start, 'allowedUntil' => $allowedUntil, 'channelId' => $streamChannel->getId(), 'key' => $key)); $streamSubscribe = new self($id); $streamChannel->onSubscribe($streamSubscribe); return $streamSubscribe; }
/** * @param string $name * @return int */ public function createEntry($name) { $id = CM_Db_Db::insert('index_mock', array('name' => (string) $name)); $this->updateDocuments($id); $this->refreshIndex(); return (int) $id; }
/** * @param int $class * @param array $values Feature=>Value pairs */ public function addTraining($class, array $values) { $class = (int) $class; $values = $this->_parseValues($values); $time = time(); CM_Db_Db::insert('cm_svmtraining', array('svmId' => $this->getId(), 'class' => $class, 'values' => serialize($values), 'createStamp' => $time)); CM_Db_Db::replace('cm_svm', array('id' => $this->getId(), 'updateStamp' => $time)); }
public function testTrailingWhitespaceInLanguageKeyName() { CM_Db_Db::insert('cm_model_languagekey', ['name'], [['foo '], ['foo']]); $language = CM_Model_Language::create('Foo', 'foo', true); $language->getTranslations()->getAssociativeArray(); $this->assertEquals(['foo ', 'foo'], array_keys($language->getTranslations()->getAssociativeArray())); $this->assertCount(2, $language->getTranslations()); }
public function create($type, array $data) { $id = CM_Db_Db::insert($this->_getTableName($type), $data); if (null === $id) { throw new CM_Exception_Invalid('Insert statement did not return an ID'); } return array('id' => (int) $id); }
/** * @param string $phrase * @param string|null $value * @param array|null $variables */ public function set($phrase, $value = null, array $variables = null) { if (null === $value) { $value = $phrase; } $languageKey = CM_Model_LanguageKey::replace($phrase, $variables); CM_Db_Db::insert('cm_languageValue', array('value' => $value, 'languageKeyId' => $languageKey->getId(), 'languageId' => $this->_language->getId()), null, array('value' => $value)); $this->_change(); }
public function setUp() { CM_Db_Db::exec('CREATE TABLE `test` ( `id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT, `num` INT(10) NOT NULL, PRIMARY KEY (`id`) )'); for ($i = 0; $i < 100; $i++) { CM_Db_Db::insert('test', array('num' => $i)); } }
public function testGetInvalidMetaInfo() { $paging = $this->getMockBuilder('CM_Paging_Log_Abstract')->setMethods(array('getType'))->disableOriginalConstructor()->getMockForAbstractClass(); $paging->expects($this->any())->method('getType')->will($this->returnValue(14)); /** @var CM_Paging_Log_Abstract $paging */ $paging->__construct(); CM_Db_Db::insert('cm_log', array('msg' => 'foo', 'metaInfo' => str_ireplace('{', '/', serialize(array('foo' => 'bar'))), 'timeStamp' => time(), 'type' => 14)); $items = $paging->getItems(); $this->assertSame(1, count($items)); $this->assertSame('foo', $items[0]['msg']); $this->assertSame(null, $items[0]['metaInfo']); }
public function testPrepare() { $actor = CMTest_TH::createUser(); $action = new CM_Action_Mock('foo', $actor); $action->prepare(null); CM_Db_Db::insert('cm_actionLimit', array('type' => 1, 'actionType' => 1, 'actionVerb' => 1, 'role' => null, 'limit' => 0, 'period' => 0)); CMTest_TH::clearCache(); try { $action->prepare(null); $this->fail('Limited action did not throw exception'); } catch (CM_Exception_ActionLimit $e) { $this->assertSame('Mock overshoot', $e->getMessage()); } }
/** * @param string $filename * @param string|null $content * @param CM_File_Filesystem|null $filesystem * @throws CM_Exception_Invalid * @return CM_File_UserContent_Temp */ public static function create($filename, $content = null, CM_File_Filesystem $filesystem = null) { if ($filesystem) { throw new CM_Exception_Invalid('Temporary user-content file cannot handle filesystem'); } $filename = (string) $filename; if (strlen($filename) > 100) { $filename = substr($filename, -100, 100); } $uniqid = md5(rand() . uniqid()); CM_Db_Db::insert('cm_tmp_userfile', array('uniqid' => $uniqid, 'filename' => $filename, 'createStamp' => time())); $file = new self($uniqid); $file->ensureParentDirectory(); if (null !== $content) { $file->write($content); } return $file; }
public static function setUpBeforeClass() { CM_Db_Db::exec('CREATE TABLE `test_a` ( `id` INT(10) unsigned NOT NULL AUTO_INCREMENT, `num` INT(10) NOT NULL, PRIMARY KEY (`id`) )'); for ($i = 1; $i <= 10; $i++) { CM_Db_Db::insert('test_a', array('num' => $i % 5)); } CM_Db_Db::exec('CREATE TABLE `test_b` ( `id` INT(10) unsigned NOT NULL AUTO_INCREMENT, `num` INT(10) NOT NULL, PRIMARY KEY (`id`) )'); for ($i = 1; $i <= 5; $i++) { CM_Db_Db::insert('test_b', array('num' => $i % 5)); } }
protected static function _createStatic(array $data) { /** @var CM_Model_User $user */ $user = $data['user']; /** @var CM_Model_StreamChannel_Abstract $streamChannel */ $streamChannel = $data['streamChannel']; $start = (int) $data['start']; if (!$streamChannel->isValid()) { throw new CM_Exception_Invalid('Stream channel not valid', CM_Exception::WARN); } $allowedUntil = $streamChannel->canPublish($user, time()); if ($allowedUntil <= time()) { throw new CM_Exception_NotAllowed('Not allowed to publish'); } $key = (string) $data['key']; $id = CM_Db_Db::insert('cm_stream_publish', array('userId' => $user->getId(), 'start' => $start, 'allowedUntil' => $allowedUntil, 'key' => $key, 'channelId' => $streamChannel->getId())); $streamPublish = new self($id); $streamChannel->onPublish($streamPublish); return $streamPublish; }
/** * @param CM_Cli_Command $command */ protected function _lockCommand(CM_Cli_Command $command) { $commandName = $command->getName(); $process = $this->_getProcess(); $hostId = $process->getHostId(); $processId = $process->getProcessId(); $timeoutStamp = time() + self::TIMEOUT; CM_Db_Db::insert('cm_cli_command_manager_process', array('commandName' => $commandName, 'hostId' => $hostId, 'processId' => $processId, 'timeoutStamp' => $timeoutStamp)); }
// 1) Duplicate entry for the city "Saint John's" // Setting the region code to "Newfoundland and Labrador" for one of them // to achieve consistency with the updated GeoIP data CM_Db_Db::update('cm_model_location_city', array('stateId' => 2524), array('id' => 22256)); // 2) Duplicate entries for the cities "Apo"/"Fpo" (U.S. Army post offices) // Adding region entries for the U.S. Armed Forces Americas & Pacific $idUS = CM_Db_Db::select('cm_model_location_country', 'id', array('abbreviation' => 'US'))->fetchColumn(); if (false === $idUS) { throw new CM_Exception_Invalid('No country with abbreviation `US` found'); } $idUS = (int) $idUS; $armedForcesRegionList = array('AA' => 'Armed Forces Americas', 'AE' => 'Armed Forces Europe, Middle East, & Canada', 'AP' => 'Armed Forces Pacific'); $idArmedForcesList = array(); foreach ($armedForcesRegionList as $regionCode => $regionName) { $idArmedForces = CM_Db_Db::select('cm_model_location_state', 'id', array('countryId' => $idUS, 'abbreviation' => $regionCode))->fetchColumn(); if (false === $idArmedForces) { $idArmedForces = CM_Db_Db::insert('cm_model_location_state', array('countryId' => $idUS, 'abbreviation' => $regionCode, 'name' => $regionName)); } else { CM_Db_Db::update('cm_model_location_state', array('name' => $regionName), array('countryId' => $idUS, 'abbreviation' => $regionCode)); } $idArmedForcesList[$regionCode] = (int) $idArmedForces; } // Moving the duplicate cities to the correct regions $armedForcesCityList = array('AA' => array(173158, 173159), 'AE' => array(173160, 173161), 'AP' => array(173944, 173945)); foreach ($armedForcesCityList as $regionCode => $cityIdList) { $regionId = $idArmedForcesList[$regionCode]; foreach ($cityIdList as $cityId) { CM_Db_Db::update('cm_model_location_city', array('stateId' => $regionId), array('id' => $cityId)); } }
protected static function _createStatic(array $data) { return new self(CM_Db_Db::insert('modelThasIsAnAssetMock', array('modelMockId' => $data['modelMockId'], 'bar' => $data['bar']))); }
protected function _upgradeIpBlocks() { if ($this->_withoutIpBlocks) { return; } $this->_streamOutput->writeln('Updating IP blocks database…'); $ipBlocksReader = $this->_getIpBlocksReader(); CM_Db_Db::exec('DROP TABLE IF EXISTS `cm_model_location_ip_new`'); CM_Db_Db::exec('CREATE TABLE `cm_model_location_ip_new` LIKE `cm_model_location_ip`'); $infoListWarning = array(); $stream = $ipBlocksReader['stream']; $count = $ipBlocksReader['lineCount']; $item = 0; $batch = []; while (false !== ($row = fgetcsv($stream))) { if ($item >= 2 && count($row) >= 3) { // Skip copyright, column names and empty lines list($ipStart, $ipEnd, $maxMind) = $row; $ipStart = (int) $ipStart; $ipEnd = (int) $ipEnd; $maxMind = (int) $maxMind; $level = null; $id = null; if (isset($this->_zipCodeIdListByMaxMind[$maxMind])) { $level = CM_Model_Location::LEVEL_ZIP; $id = $this->_zipCodeIdListByMaxMind[$maxMind]; } elseif (isset($this->_cityIdList[$maxMind])) { $level = CM_Model_Location::LEVEL_CITY; $id = $this->_cityIdList[$maxMind]; } elseif (isset($this->_regionIdListByMaxMind[$maxMind])) { $level = CM_Model_Location::LEVEL_STATE; $id = $this->_regionIdListByMaxMind[$maxMind]; } elseif (isset($this->_countryCodeListByMaxMind[$maxMind])) { $level = CM_Model_Location::LEVEL_COUNTRY; $countryCode = $this->_countryCodeListByMaxMind[$maxMind]; if (isset($this->_countryIdList[$countryCode])) { $id = $this->_countryIdList[$countryCode]; } } if ($level && $id) { $batch[] = [$id, $level, $ipStart, $ipEnd]; if (1000 === count($batch)) { CM_Db_Db::insert('cm_model_location_ip_new', ['id', 'level', 'ipStart', 'ipEnd'], $batch); $batch = []; } } else { $infoListWarning['Ignoring unknown locations'][] = $maxMind; } } $this->_printProgressCounter(++$item, $count); } if (!empty($batch)) { CM_Db_Db::insert('cm_model_location_ip_new', ['id', 'level', 'ipStart', 'ipEnd'], $batch); } unset($batch); unset($this->_countryIdList); unset($this->_countryCodeListByMaxMind); unset($this->_regionIdListByMaxMind); unset($this->_cityIdList); unset($this->_zipCodeIdListByMaxMind); $this->_printInfoList($infoListWarning, '!'); $this->_streamOutput->writeln('Checking overlapping of IP blocks…'); $result = CM_Db_Db::select('cm_model_location_ip_new', ['ipStart', 'ipEnd'], null, 'ipEnd ASC'); $ipStartPrevious = $ipEndPrevious = 0; $count = $result->getAffectedRows(); $item = 0; while (false !== ($row = $result->fetch())) { list($ipStart, $ipEnd) = array_values($row); if ($ipStart <= $ipEndPrevious) { $infoListWarning['Overlapping IP blocks'][] = "{$ipStartPrevious}-{$ipEndPrevious} and {$ipStart}-{$ipEnd}"; } $ipStartPrevious = $ipStart; $ipEndPrevious = $ipEnd; $this->_printProgressCounter(++$item, $count); } CM_Db_Db::replaceTable('cm_model_location_ip', 'cm_model_location_ip_new'); $this->_printInfoList($infoListWarning, '!'); }
public function testReplaceTable() { $this->assertInstanceOf('CM_Db_Exception', $this->catchException(function () { CM_Db_Db::replaceTable('test', 'test_new'); })); CM_Db_Db::exec('CREATE TABLE `test_new` (`id` INT(10) UNSIGNED NOT NULL)'); CM_Db_Db::insert('test_new', ['id' => 123]); CM_Db_Db::replaceTable('test', 'test_new'); $this->assertSame([['id' => '123']], CM_Db_Db::select('test', '*')->fetchAll()); $this->assertSame(false, CM_Db_Db::existsTable('test_new')); }
/** * @param array $data * @return CM_Model_User */ protected static function _createStatic(array $data) { $siteType = null; if (isset($data['site'])) { /** @var CM_Site_Abstract $site */ $site = $data['site']; $siteType = $site->getType(); } $languageId = null; if (isset($data['language'])) { /** @var CM_Model_Language $language */ $language = $data['language']; $languageId = $language->getId(); } $userId = CM_Db_Db::insert('cm_user', array('createStamp' => time(), 'activityStamp' => time(), 'site' => $siteType, 'languageId' => $languageId)); return new static($userId); }
/** * @param string $section * @param string $key * @param boolean $defaultValue * @param boolean $configurable */ public static function setDefault($section, $key, $defaultValue, $configurable) { $where = array('section' => (string) $section, 'key' => (string) $key); $values = array('defaultValue' => (int) (bool) $defaultValue, 'configurable' => (int) (bool) $configurable); CM_Db_Db::insert('cm_user_preferenceDefault', array_merge($values, $where), null, $values); }
public function testOutdatedLocalCache() { $test1 = CM_Model_Splittest_Mock::create('foo', range(1, 10)); $test2 = CM_Model_Splittest_Mock::create('bar', range(1, 10)); $mockBuilder = $this->getMockBuilder('CM_Model_User'); $mockBuilder->setMethods(['getId']); $userMock = $mockBuilder->getMock(); $userMock->expects($this->any())->method('getId')->will($this->returnValue(mt_rand())); /** @var CM_Model_User $userMock */ $fixture = new CM_Splittest_Fixture($userMock); CM_Db_Db::insert('cm_splittestVariation_fixture', array('splittestId' => $test1->getId(), $fixture->getColumnId() => $fixture->getId(), 'variationId' => $test1->getVariations()->findByName(1)->getId(), 'createStamp' => time())); $this->assertTrue($test1->isVariationFixture($fixture, 1)); CM_Db_Db::insert('cm_splittestVariation_fixture', array('splittestId' => $test2->getId(), $fixture->getColumnId() => $fixture->getId(), 'variationId' => $test2->getVariations()->findByName(10)->getId(), 'createStamp' => time())); $this->assertTrue($test2->isVariationFixture($fixture, 10)); }
/** * @param CM_Splittest_Fixture $fixture * @throws CM_Db_Exception * @throws CM_Exception_Invalid * @return string */ protected function _getVariationFixture(CM_Splittest_Fixture $fixture) { $variationName = $this->_findVariationNameFixture($fixture); if (null === $variationName) { $variation = $this->_getVariationRandom(); $variationName = $variation->getName(); try { $columnId = $fixture->getColumnId(); $fixtureId = $fixture->getId(); CM_Db_Db::insert('cm_splittestVariation_fixture', array('splittestId' => $this->getId(), $columnId => $fixtureId, 'variationId' => $variation->getId(), 'createStamp' => time())); $variationListFixture = $this->_getVariationListFixture($fixture); $variationListFixture[$this->getId()] = ['variation' => $variationName, 'flushStamp' => $this->getCreated()]; $cacheKey = self::_getCacheKeyFixture($fixture); $cache = CM_Cache_Local::getInstance(); $cache->set($cacheKey, $variationListFixture); $this->getServiceManager()->getTrackings()->trackSplittest($fixture, $variation); } catch (CM_Db_Exception $exception) { $variationName = $this->_findVariationNameFixture($fixture, true); if (null === $variationName) { throw $exception; } } } return $variationName; }
private function _queue($subject, $text, $html) { CM_Db_Db::insert('cm_mail', array('subject' => $subject, 'text' => $text, 'html' => $html, 'createStamp' => time(), 'sender' => serialize($this->getSender()), 'replyTo' => serialize($this->getReplyTo()), 'to' => serialize($this->getTo()), 'cc' => serialize($this->getCc()), 'bcc' => serialize($this->getBcc()), 'customHeaders' => serialize($this->_getCustomHeaders()))); }
/** * @param CM_Jobdistribution_Job_Abstract $job * @param array $params * @param int $delay */ public function addJob(CM_Jobdistribution_Job_Abstract $job, array $params, $delay) { CM_Db_Db::insert('cm_jobdistribution_delayedqueue', ['className' => get_class($job), 'params' => CM_Params::encode($params, true), 'executeAt' => time() + (int) $delay]); }
<?php if (!CM_Db_Db::existsTable('cm_requestClientCounter')) { CM_Db_Db::exec(' CREATE TABLE `cm_requestClientCounter` ( `counter` int(10) unsigned NOT NULL, PRIMARY KEY (`counter`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8; '); CM_Db_Db::insert('cm_requestClientCounter', array('counter' => 0)); } if (CM_Db_Db::existsTable('cm_requestClient')) { $highestEntry = (int) CM_Db_Db::select('cm_requestClient', 'id', null, array('id' => 'DESC'))->fetchColumn(); CM_Db_Db::update('cm_requestClientCounter', array('counter' => $highestEntry)); CM_Db_Db::exec('DROP TABLE IF EXISTS `cm_requestClient`;'); }
<?php if (CM_Db_Db::existsTable('cm_model_location_city_ip') && !CM_Db_Db::existsTable('cm_model_location_ip')) { CM_Db_Db::exec('RENAME TABLE `cm_model_location_city_ip` TO `cm_model_location_ip`'); } if (CM_Db_Db::existsTable('cm_model_location_ip')) { if (CM_Db_Db::existsIndex('cm_model_location_ip', 'cityId')) { CM_Db_Db::exec('DROP INDEX `cityId` ON `cm_model_location_ip`'); } if (CM_Db_Db::existsColumn('cm_model_location_ip', 'cityId') && !CM_Db_Db::existsColumn('cm_model_location_ip', 'id')) { CM_Db_Db::exec('ALTER TABLE `cm_model_location_ip` CHANGE COLUMN `cityId` `id` int(10) unsigned NOT NULL '); } if (!CM_Db_Db::existsColumn('cm_model_location_ip', 'level')) { CM_Db_Db::exec('ALTER TABLE `cm_model_location_ip` ADD COLUMN `level` int(10) unsigned NOT NULL AFTER `id`'); } if (CM_Db_Db::existsColumn('cm_model_location_ip', 'level')) { CM_Db_Db::update('cm_model_location_ip', array('level' => CM_Model_Location::LEVEL_CITY), array('level' => 0)); } if (CM_Db_Db::existsTable('cm_model_location_country_ip')) { $result = CM_Db_Db::select('cm_model_location_country_ip', array('countryId', 'ipStart', 'ipEnd')); foreach ($result->fetchAll() as $row) { CM_Db_Db::insert('cm_model_location_ip', array('id' => $row['countryId'], 'level' => CM_Model_Location::LEVEL_COUNTRY, 'ipStart' => $row['ipStart'], 'ipEnd' => $row['ipEnd'])); } CM_Db_Db::exec('DROP TABLE `cm_model_location_country_ip`'); } }
protected static function _createStatic(array $data) { $key = (string) $data['key']; $serverId = $data['serverId']; $adapterType = (int) $data['adapterType']; $mediaId = !empty($data['mediaId']) ? (string) $data['mediaId'] : null; $id = CM_Db_Db::insert('cm_streamChannel', ['key' => $key, 'createStamp' => time(), 'type' => static::getTypeStatic(), 'adapterType' => $adapterType], null, ['id' => ['literal' => 'LAST_INSERT_ID(id)']]); CM_Db_Db::insert('cm_streamChannel_media', ['id' => $id, 'serverId' => $serverId, 'mediaId' => $mediaId], null, ['id' => ['literal' => 'LAST_INSERT_ID(id)']]); $cacheKey = CM_CacheConst::StreamChannel_Id . '_key' . $key . '_adapterType:' . $adapterType; CM_Cache_Shared::getInstance()->delete($cacheKey); return new static($id); }
public function testMonitorSynchronizedCommands() { $mockBuilder = $this->getMockBuilder('CM_Process'); $mockBuilder->setMethods(['isRunning']); $mockBuilder->disableOriginalConstructor(); $processMock = $mockBuilder->getMock(); $processMock->expects($this->any())->method('isRunning')->will($this->returnCallback(function ($processId) { return $processId !== 3; })); $mockBuilder = $this->getMockBuilder('CM_Cli_CommandManager'); $mockBuilder->setMethods(['_getProcess', '_getMachineId']); $commandManagerMock = $mockBuilder->getMock(); $commandManagerMock->expects($this->any())->method('_getProcess')->will($this->returnValue($processMock)); $commandManagerMock->expects($this->any())->method('_getMachineId')->will($this->returnValue('my-machine-1')); CM_Db_Db::insert('cm_cli_command_manager_process', array('commandName' => 'command-mock1', 'machineId' => 'my-machine-1', 'processId' => 1, 'timeoutStamp' => time() + 60)); CM_Db_Db::insert('cm_cli_command_manager_process', array('commandName' => 'command-mock2', 'machineId' => 'my-machine-1', 'processId' => 2, 'timeoutStamp' => time() - 60)); CM_Db_Db::insert('cm_cli_command_manager_process', array('commandName' => 'command-mock3', 'machineId' => 'my-machine-1', 'processId' => 3, 'timeoutStamp' => time() + 60)); CM_Db_Db::insert('cm_cli_command_manager_process', array('commandName' => 'command-mock4', 'machineId' => 'my-machine-2', 'processId' => 4, 'timeoutStamp' => time() + 60)); CM_Db_Db::insert('cm_cli_command_manager_process', array('commandName' => 'command-mock5', 'machineId' => 'my-machine-2', 'processId' => 5, 'timeoutStamp' => time() - 60)); /** @var CM_Cli_CommandManager $commandManagerMock */ $commandManagerMock->monitorSynchronizedCommands(); $timeoutStampExpected = time() + CM_Cli_CommandManager::TIMEOUT; $this->assertRow('cm_cli_command_manager_process', array('commandName' => 'command-mock1', 'timeoutStamp' => $timeoutStampExpected)); $this->assertRow('cm_cli_command_manager_process', array('commandName' => 'command-mock2', 'timeoutStamp' => $timeoutStampExpected)); $this->assertNotRow('cm_cli_command_manager_process', array('commandName' => 'command-mock3')); $this->assertRow('cm_cli_command_manager_process', array('commandName' => 'command-mock4', 'timeoutStamp' => time() + 60)); $this->assertNotRow('cm_cli_command_manager_process', array('commandName' => 'command-mock5')); CM_Db_Db::truncate('cm_cli_command_manager_process'); }
protected static function _createStatic(array $data) { $key = (string) $data['key']; $width = (int) $data['width']; $height = (int) $data['height']; $serverId = $data['serverId']; $thumbnailCount = (int) $data['thumbnailCount']; $adapterType = (int) $data['adapterType']; $id = CM_Db_Db::insert('cm_streamChannel', array('key' => $key, 'type' => static::getTypeStatic(), 'adapterType' => $adapterType)); try { CM_Db_Db::insert('cm_streamChannel_video', array('id' => $id, 'width' => $width, 'height' => $height, 'serverId' => $serverId, 'thumbnailCount' => $thumbnailCount)); } catch (CM_Exception $ex) { CM_Db_Db::delete('cm_streamChannel', array('id' => $id)); throw $ex; } return new static($id); }
protected static function _createStatic(array $data) { $name = (string) $data['name']; $percentage = self::_checkPercentage($data['percentage']); CM_Db_Db::insert('cm_splitfeature', array('name' => $name, 'percentage' => $percentage)); return new static($name); }
/** * @param int|null $level * @return CM_Model_Location */ public static function createLocation($level = null) { $country = CM_Db_Db::insert('cm_model_location_country', array('abbreviation' => 'FOO', 'name' => 'countryFoo')); $state = CM_Db_Db::insert('cm_model_location_state', array('countryId' => $country, 'name' => 'stateFoo')); $city = CM_Db_Db::insert('cm_model_location_city', array('stateId' => $state, 'countryId' => $country, 'name' => 'cityFoo', 'lat' => 10, 'lon' => 15)); $zip = CM_Db_Db::insert('cm_model_location_zip', array('cityId' => $city, 'name' => '1000', 'lat' => 10, 'lon' => 15)); CM_Model_Location::createAggregation(); switch ($level) { case CM_Model_Location::LEVEL_COUNTRY: return new CM_Model_Location(CM_Model_Location::LEVEL_COUNTRY, $country); case CM_Model_Location::LEVEL_CITY: return new CM_Model_Location(CM_Model_Location::LEVEL_CITY, $city); case CM_Model_Location::LEVEL_STATE: return new CM_Model_Location(CM_Model_Location::LEVEL_STATE, $state); default: return new CM_Model_Location(CM_Model_Location::LEVEL_ZIP, $zip); } }