/** * Install hstore * /usr/share/postgresql/contrib # cat hstore.sql | psql -U pgsql -d onphp **/ public function testHstore() { foreach (DBTestPool::me()->getPool() as $connector => $db) { DBPool::me()->setDefault($db); $properties = array('age' => '23', 'weight' => 80, 'comment' => null); $user = TestUser::create()->setCity($moscow = TestCity::create()->setName('Moscow'))->setCredentials(Credentials::create()->setNickname('fake')->setPassword(sha1('passwd')))->setLastLogin(Timestamp::create(time()))->setRegistered(Timestamp::create(time())->modify('-1 day'))->setProperties(Hstore::make($properties)); $moscow = TestCity::dao()->add($moscow); $user = TestUser::dao()->add($user); Cache::me()->clean(); TestUser::dao()->dropIdentityMap(); $user = TestUser::dao()->getById('1'); $this->assertInstanceOf('Hstore', $user->getProperties()); $this->assertEquals($properties, $user->getProperties()->getList()); $form = TestUser::proto()->makeForm(); $form->get('properties')->setFormMapping(array(Primitive::string('age'), Primitive::integer('weight'), Primitive::string('comment'))); $form->import(array('id' => $user->getId())); $this->assertNotNull($form->getValue('id')); $object = $user; FormUtils::object2form($object, $form); $this->assertInstanceOf('Hstore', $form->getValue('properties')); $this->assertEquals(array_filter($properties), $form->getValue('properties')->getList()); $subform = $form->get('properties')->getInnerForm(); $this->assertEquals($subform->getValue('age'), '23'); $this->assertEquals($subform->getValue('weight'), 80); $this->assertNull($subform->getValue('comment')); $user = new TestUser(); FormUtils::form2object($form, $user, false); $this->assertEquals($user->getProperties()->getList(), array_filter($properties)); } }
public function testStartHour() { $stamp = Timestamp::create('2010-03-25 14:15:10'); $this->assertNotEquals($stamp->toStamp(), $stamp->getHourStartStamp()); $this->assertTrue(Timestamp::create($stamp->getHourStartStamp()) instanceof Timestamp); $this->assertEquals(Timestamp::create($stamp->getHourStartStamp())->toString(), '2010-03-25 14:00:00'); }
public function testDayDifference() { $today = Date::makeToday(); $this->dayDifferenceTest($today, $today, 0); $timestamp = Timestamp::makeNow(); $this->dayDifferenceTest($timestamp, $timestamp, 0); $left = Date::create('2008-01-12'); $right = Date::create('2008-01-13'); $this->dayDifferenceTest($left, $right, 1); $left = Date::create('2008-01-12'); $right = Date::create('2009-01-13'); $this->dayDifferenceTest($left, $right, 367); $left = Date::create('2008-01-12'); $right = Date::create('2008-01-11'); $this->dayDifferenceTest($left, $right, -1); $left = Timestamp::create('2008-01-12 01:23:00'); $right = Timestamp::create('2008-01-13 13:01:17'); $this->dayDifferenceTest($left, $right, 1); // change time from winter to summer $left = Timestamp::create('2008-03-29 02:00:00'); $right = Timestamp::create('2008-03-30 02:00:00'); $this->dayDifferenceTest($left, $right, 1); $left = Timestamp::create('2008-03-29 03:00:00'); $right = Timestamp::create('2008-03-30 03:00:00'); $this->dayDifferenceTest($left, $right, 1); // unsolved giv's case // $left = Timestamp::create('2008-10-25 03:00:00'); // $right = Timestamp::create('2008-10-26 02:59:00'); // $this->dayDifferenceTest($left, $right, 0); return $this; }
/** * @return Timestamp * * Emulates PostgreSQL's date_trunc() function * **/ public function truncate(Date $time, $ceil = false) { $time = $time->toTimestamp(); $function = $ceil ? 'ceil' : 'floor'; if ($this->seconds) { if ($this->seconds < 1) { return $time->spawn(); } $truncated = (int) ($function($time->toStamp() / $this->seconds) * $this->seconds); return Timestamp::create($truncated); } elseif ($this->days) { $epochStartTruncated = Date::create('1970-01-05'); $truncatedDate = Date::create($time->toDate()); if ($ceil && $truncatedDate->toStamp() < $time->toStamp()) { $truncatedDate->modify('+1 day'); } $difference = Date::dayDifference($epochStartTruncated, $truncatedDate); $truncated = (int) ($function($difference / $this->days) * $this->days); return Timestamp::create($epochStartTruncated->spawn($truncated . ' days')->toStamp()); } elseif ($this->months) { $monthsCount = $time->getYear() * 12 + ($time->getMonth() - 1); if ($ceil && $time->getDay() - 1 + $time->getHour() + $time->getMinute() + $time->getSecond() > 0) { $monthsCount += 0.1; } // delta $truncated = (int) ($function($monthsCount / $this->months) * $this->months); $months = $truncated % 12; $years = ($truncated - $months) / 12; Assert::isEqual($years, (int) $years); $years = (int) $years; $months = $months + 1; return Timestamp::create("{$years}-{$months}-01 00:00:00"); } Assert::isUnreachable(); }
public function testCount() { foreach (DBTestPool::me()->getPool() as $db) { DBPool::me()->setDefault($db); $this->getDBCreator()->fillDB(); $count = TestUser::dao()->getTotalCount(); $this->assertGreaterThan(1, $count); $city = TestCity::create()->setId(1); $newUser = TestUser::create()->setCity($city)->setCredentials(Credentials::create()->setNickname('newuser')->setPassword(sha1('newuser')))->setLastLogin(Timestamp::create(time()))->setRegistered(Timestamp::create(time())); TestUser::dao()->add($newUser); $newCount = TestUser::dao()->getTotalCount(); $this->assertEquals($count + 1, $newCount); } }
public function makeItems(SimpleXMLElement $xmlFeed) { $result = array(); if (isset($xmlFeed->channel->item)) { foreach ($xmlFeed->channel->item as $item) { $feedItem = FeedItem::create((string) $item->title)->setContent(FeedItemContent::create()->setBody((string) $item->description))->setPublished(Timestamp::create(strtotime((string) $item->pubDate)))->setLink((string) $item->link); if (isset($item->guid)) { $feedItem->setId($item->guid); } if (isset($item->category)) { $feedItem->setCategory((string) $item->category); } $result[] = $feedItem; } } return $result; }
public function makeItems(SimpleXMLElement $xmlFeed) { $result = array(); foreach ($xmlFeed->entry as $entry) { $feedItem = FeedItem::create((string) $entry->title); if (isset($entry->content)) { $feedItem->setContent($this->makeFeedItemContent($entry->content)); } if (isset($entry->summary)) { $feedItem->setSummary($this->makeFeedItemContent($entry->summary)); } if (isset($entry->id)) { $feedItem->setId($entry->id); } $result[] = $feedItem->setPublished(Timestamp::create(strtotime((string) $entry->updated)))->setLink((string) $entry->link); } return $result; }
public function makeItems(SimpleXMLElement $xmlFeed) { $xmlFeed->registerXPathNamespace(YandexRssFeedFormat::YANDEX_NAMESPACE_PREFIX, YandexRssFeedFormat::YANDEX_NAMESPACE_URI); $fullTextList = $xmlFeed->xpath('//' . YandexRssFeedFormat::YANDEX_NAMESPACE_PREFIX . ':full-text'); $result = array(); $i = 0; if (isset($xmlFeed->channel->item)) { foreach ($xmlFeed->channel->item as $item) { $feedItem = YandexRssFeedItem::create((string) $item->title)->setContent(FeedItemContent::create()->setBody((string) $item->description))->setPublished(Timestamp::create(strtotime((string) $item->pubDate)))->setFullText((string) $fullTextList[$i++])->setLink((string) $item->link); if (isset($item->guid)) { $feedItem->setId($item->guid); } if (isset($item->category)) { $feedItem->setCategory((string) $item->category); } $result[] = $feedItem; } } return $result; }
/** * @return Message **/ public function receive($uTimeout = null) { if (!$this->queue) { throw new WrongStateException('you must set the queue first'); } if ($uTimeout && $this->getStream()->isEof()) { usleep($uTimeout); } $string = $this->getStream()->readString(); if (!$string && $this->getStream()->isEof()) { return null; } $this->getQueue()->setOffset($this->getStream()->getOffset()); $string = rtrim($string, PHP_EOL); $chunks = preg_split("/\t/", $string, 2); $time = isset($chunks[0]) ? $chunks[0] : null; $text = isset($chunks[1]) ? $chunks[1] : null; Assert::isNotNull($time); $result = TextMessage::create(Timestamp::create($time))->setText($text); return $result; }
public function testCountMonthsNotOverlapped() { $unit = IntervalUnit::create('month'); $this->assertEquals(4, $result = $unit->countInRange(TimestampRange::create($start = Timestamp::create('2008-12-31 23:59:58'), $end = Timestamp::create('2009-05-28 03:00:00')), false)); $this->assertLessThanOrEqual($end->toStamp(), $start->spawn($result . ' ' . $unit->getName())->toStamp()); }
/** * @return TestUser */ private function spawnUser($options = array()) { $options += array('id' => '77', 'credentials' => Credentials::create(), 'lastLogin' => Timestamp::create('2011-12-31'), 'registered' => Timestamp::create('2011-12-30'), 'strangeTime' => Time::create('01:23:45'), 'city' => null, 'firstOptional' => null, 'secondOptional' => null, 'url' => HttpUrl::create()->parse('https://www.github.com'), 'properties' => Hstore::make(array('a' => 'apple', 'b' => 'bananas')), 'ip' => IpAddress::create('127.0.0.1')); return $this->spawnObject(TestUser::create(), $options); }
public static function alignToSecondsDataProvider() { return array(array(Timestamp::create('2009-01-01 10:00:42'), '2009-01-01 10:00:42'), array(Timestamp::create('2009-01-01 10:00:41'), '2009-01-01 10:00:00'), array(Timestamp::create('2009-01-01 10:01:34'), '2009-01-01 10:01:24'), array(Timestamp::create('2009-01-01 10:10:01'), '2009-01-01 10:09:48')); }
/** * @return Timestamp **/ public static function alignToSeconds(Timestamp $stamp, $seconds) { $rawStamp = $stamp->toStamp(); $align = floor($rawStamp / $seconds); return Timestamp::create($align * $seconds); }
public function testSleeping() { $stamp = Timestamp::makeNow(); $serializedStamp = serialize($stamp); $unserializedStamp = unserialize($serializedStamp); $this->assertEquals($stamp->getDay(), $unserializedStamp->getDay()); $this->assertEquals($stamp->getMonth(), $unserializedStamp->getMonth()); $this->assertEquals($stamp->getYear(), $unserializedStamp->getYear()); $this->assertEquals($stamp->getMinute(), $unserializedStamp->getMinute()); $this->assertEquals($stamp->getSecond(), $unserializedStamp->getSecond()); $stamp = Timestamp::create('2039-01-05 12:14:05 Europe/Moscow'); $serializedStamp = serialize($stamp); $unserializedStamp = unserialize($serializedStamp); $this->assertEquals($stamp->getDay(), $unserializedStamp->getDay()); $this->assertEquals($stamp->getMonth(), $unserializedStamp->getMonth()); $this->assertEquals($stamp->getYear(), $unserializedStamp->getYear()); $this->assertEquals($stamp->getMinute(), $unserializedStamp->getMinute()); $this->assertEquals($stamp->getSecond(), $unserializedStamp->getSecond()); $stamp = Timestamp::create('1899-12-31 16:07:45 Europe/London'); $serializedStamp = serialize($stamp); $unserializedStamp = unserialize($serializedStamp); $this->assertEquals($stamp->getDay(), $unserializedStamp->getDay()); $this->assertEquals($stamp->getMonth(), $unserializedStamp->getMonth()); $this->assertEquals($stamp->getYear(), $unserializedStamp->getYear()); $this->assertEquals($stamp->getMinute(), $unserializedStamp->getMinute()); $this->assertEquals($stamp->getSecond(), $unserializedStamp->getSecond()); }
/** * @return Timestamp **/ public function toTimestamp() { return Timestamp::create($this->toStamp()); }
/** * @param TestCase $test * @return DBTestCreator */ public function fillDB(TestCase $test = null) { $moscow = TestCity::create()->setName('Moscow'); $piter = TestCity::create()->setName('Saint-Peterburg'); $mysqler = TestUser::create()->setCity($moscow)->setCredentials(Credentials::create()->setNickname('mysqler')->setPassword(sha1('mysqler')))->setLastLogin(Timestamp::create(time()))->setRegistered(Timestamp::create(time())->modify('-1 day')); $postgreser = clone $mysqler; $postgreser->setCredentials(Credentials::create()->setNickName('postgreser')->setPassword(sha1('postgreser')))->setCity($piter)->setUrl(HttpUrl::create()->parse('http://postgresql.org/')); $piter = TestCity::dao()->add($piter); $moscow = TestCity::dao()->add($moscow); if ($test) { $test->assertEquals($piter->getId(), 1); $test->assertEquals($moscow->getId(), 2); } $postgreser = TestUser::dao()->add($postgreser); for ($i = 0; $i < 10; $i++) { $encapsulant = TestEncapsulant::dao()->add(TestEncapsulant::create()->setName($i)); $encapsulant->getCities()->fetch()->setList(array($piter, $moscow))->save(); } $mysqler = TestUser::dao()->add($mysqler); if ($test) { $test->assertEquals($postgreser->getId(), 1); $test->assertEquals($mysqler->getId(), 2); } if ($test) { // put them in cache now TestUser::dao()->dropIdentityMap(); TestUser::dao()->getById(1); TestUser::dao()->getById(2); if ($test instanceof DBDataTest) { $test->getListByIdsTest(); } Cache::me()->clean(); $test->assertTrue($postgreser == TestUser::dao()->getById(1)); $test->assertTrue($mysqler == TestUser::dao()->getById(2)); } $firstClone = clone $postgreser; $secondClone = clone $mysqler; $firstCount = TestUser::dao()->dropById($postgreser->getId()); $secondCount = TestUser::dao()->dropByIds(array($mysqler->getId())); if ($test) { $test->assertEquals($firstCount, 1); $test->assertEquals($secondCount, 1); try { TestUser::dao()->getById(1); $test->fail(); } catch (ObjectNotFoundException $e) { /* pass */ } $result = Criteria::create(TestUser::dao())->add(Expression::eq(1, 2))->getResult(); $test->assertEquals($result->getCount(), 0); $test->assertEquals($result->getList(), array()); } TestUser::dao()->import($firstClone); TestUser::dao()->import($secondClone); if ($test && $test instanceof DBDataTest) { // cache multi-get $test->getListByIdsTest(); $test->getListByIdsTest(); } return $this; }