Jyxo\Time\TimeTest::testUnserialize PHP Method

testUnserialize() public method

Tests the serialize() function.
See also: Jyxo\Time\Time::unserialize()
public testUnserialize ( )
    public function testUnserialize()
    {
        $time = time();
        $serialized = 'C:14:"Jyxo\\Time\\Time":33:{' . date('Y-m-d H:i:s', $time) . ' Europe/Prague}';
        $unserialized = @unserialize($serialized);
        $this->assertSame((string) $time, (string) $unserialized);
        $this->assertSame('Europe/Prague', $unserialized->getTimezone()->getName());
        // Invalid serialized data
        $serialized = 'C:14:"Jyxo\\Time\\Time":3:{foo}';
        try {
            $unserialized = @unserialize($serialized);
            $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
        $serialized = 'C:14:"Jyxo\\Time\\Time":27:{2010-12-12 10:00:00 Foo/Bar}';
        try {
            $unserialized = @unserialize($serialized);
            $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 date/time definition
        $serialized = 'C:14:"Jyxo\\Time\\Time":33:{2010-13-12 10:00:00 Europe/Prague}';
        try {
            $unserialized = @unserialize($serialized);
            $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);
        }
        // Offset time zone definition
        $serialized = 'C:14:"Jyxo\\Time\\Time":26:{' . date('Y-m-d H:i:s', $time) . ' +05:30}';
        $unserialized = @unserialize($serialized);
        $this->assertEquals('+05:30', $unserialized->getTimeZone()->getName());
        $serialized = 'C:14:"Jyxo\\Time\\Time":26:{' . date('Y-m-d H:i:s', $time) . ' -12:00}';
        $unserialized = @unserialize($serialized);
        $this->assertEquals('-12:00', $unserialized->getTimeZone()->getName());
        // Invalid time zone offset
        $serialized = 'C:14:"Jyxo\\Time\\Time":26:{' . date('Y-m-d H:i:s', $time) . ' +05:60}';
        try {
            $unserialized = @unserialize($serialized);
            $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 time zone offset
        $serialized = 'C:14:"Jyxo\\Time\\Time":26:{' . date('Y-m-d H:i:s', $time) . ' -13:00}';
        try {
            $unserialized = @unserialize($serialized);
            $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);
        }
    }