Jyxo\Time\TimeTest::testFormat PHP Method

testFormat() public method

Tests the format() method.
See also: Jyxo\Time\Time::format()
public testFormat ( )
    public function testFormat()
    {
        // Time with no translation necessary
        $this->assertEquals('121212', Time::get('2012-12-12T12:12:12+02:00')->format('ymd'));
        // Weekdays translation
        $days = [_('Monday'), _('Tuesday'), _('Wednesday'), _('Thursday'), _('Friday'), _('Saturday'), _('Sunday')];
        $daysShort = [_('Mon'), _('Tue'), _('Wed'), _('Thu'), _('Fri'), _('Sat'), _('Sun')];
        foreach ($days as $day => $name) {
            $time = new Time('2009-10-' . ($day + 12));
            $this->assertEquals($days[date('N', strtotime('2009-10-' . ($day + 12))) - 1], $time->format('l'));
            $this->assertEquals($daysShort[date('N', strtotime('2009-10-' . ($day + 12))) - 1], $time->format('D'));
        }
        // Months translation
        $months = [_('January'), _('February'), _('March'), _('April'), _('May'), _('June'), _('July'), _('August'), _('September'), _('October'), _('November'), _('December')];
        $monthsGen = [_('January#~Genitive'), _('February#~Genitive'), _('March#~Genitive'), _('April#~Genitive'), _('May#~Genitive'), _('June#~Genitive'), _('July#~Genitive'), _('August#~Genitive'), _('September#~Genitive'), _('October#~Genitive'), _('November#~Genitive'), _('December#~Genitive')];
        $monthsShort = [_('Jan'), _('Feb'), _('Mar'), _('Apr'), _('May#~Shortcut'), _('Jun'), _('Jul'), _('Aug'), _('Sep'), _('Oct'), _('Nov'), _('Dec')];
        foreach ($months as $month => $name) {
            $time = new Time('2009-' . str_pad((string) ($month + 1), 2, '0', STR_PAD_LEFT) . '-01');
            $this->assertEquals($name, $time->format('F'));
            $this->assertEquals('1. ' . mb_strtolower($monthsGen[$month], 'utf-8'), $time->format('j. F'));
            $this->assertEquals($monthsShort[$month], $time->format('M'));
        }
        // Full date/time
        $this->assertEquals(sprintf('%s 10. %s 2012 10:11:12', $days[5], mb_strtolower($monthsGen[10]), 'utf-8'), Time::get('2012-11-10 10:11:12')->format('l j. F Y H:i:s'));
        $this->assertEquals(sprintf('%s 2012', $months[9]), Time::get('2012-10-10')->format('F Y'));
        $this->assertEquals(sprintf('%s 2012', $monthsShort[8]), Time::get('2012-09-09')->format('M Y'));
        // Time zone handling
        $time1 = Time::now();
        $timeZone = new \DateTimeZone($time1->getTimeZone()->getName() == 'Europe/Prague' ? 'UTC' : 'Europe/Prague');
        $time2 = Time::now()->setTimeZone($timeZone);
        // Date/times must not be the same (Prague is UTC+1/+2)
        $this->assertNotSame($time1->format('Y-m-d H:i:s'), $time2->format('Y-m-d H:i:s'));
        // Get the results in the same time zone (of the first one)
        $this->assertSame($time1->format('Y-m-d H:i:s'), $time2->format('Y-m-d H:i:s', $time1->getTimeZone()));
        // Get the results in the same time zone (different for both instances)
        $commonTimeZone = new \DateTimeZone($timeZone->getName() == 'Europe/Prague' ? $time1->getTimeZone()->getName() == 'Pacific/Honolulu' ? 'America/Havana' : 'Pacific/Honolulu' : ($time1->getTimeZone()->getName() == 'Europe/Prague' ? 'Pacific/Honolulu' : 'Europe/Prague'));
        // The "common" time zone differs from both instances' time zones
        $this->assertNotSame($time1->getTimeZone()->getName(), $commonTimeZone->getName());
        $this->assertNotSame($time2->getTimeZone()->getName(), $commonTimeZone->getName());
        // And therefore local times differ (the common time zone is selected so that they do really differ :)
        $this->assertNotSame($time1->format('Y-m-d H:i:s'), $time1->format('Y-m-d H:i:s', $commonTimeZone));
        $this->assertNotSame($time2->format('Y-m-d H:i:s'), $time2->format('Y-m-d H:i:s', $commonTimeZone));
        // The result in the common time zone
        $this->assertSame($time1->format('Y-m-d H:i:s', $commonTimeZone), $time2->format('Y-m-d H:i:s', $commonTimeZone));
        // Invalid timezone - name
        try {
            $this->assertSame(null, $time->format('Y-m-d', 'Foo/Bar'));
            $this->fail(sprintf('Expected exception %s.', \InvalidArgumentException::class));
        } catch (\PHPUnit_Framework_AssertionFailedError $e) {
            throw $e;
        } catch (\Exception $e) {
            // Correctly thrown exception
            $this->assertInstanceOf(\InvalidArgumentException::class, $e);
        }
        // Invalid timezone - object
        try {
            $this->assertSame(null, $time->format('Y-m-d', new \stdClass()));
            $this->fail(sprintf('Expected exception %s.', \InvalidArgumentException::class));
        } catch (\PHPUnit_Framework_AssertionFailedError $e) {
            throw $e;
        } catch (\Exception $e) {
            // Correctly thrown exception
            $this->assertInstanceOf(\InvalidArgumentException::class, $e);
        }
    }