ZF-10665: Zend_Http_UserAgent may die on serialization


When serializing a user agent, for example, during a unit test for which there is no device, a fatal error is thrown.

Debug Error: D:\Program Files (x86)\Zend\ZendServer\share\ZendFramework-1.11.0\library\Zend\Http\UserAgent.php line 178 - Call to a member function serialize() on a non-object

public function serialize()

{ $spec = array( 'browser_type' => $this->_browserType, 'config' => $this->_config, 'device_class' => get_class($this->_device), 'device' => $this->_device->serialize(), <--- This is the offending call 'user_agent' => $this->getServerValue('http_user_agent'), 'http_accept' => $this->getServerValue('http_accept'), ); return serialize($spec); }

This is presumably due to the fact that I have a user agent resource loading up using Zend_Application during a unit test, for which no device can be found.


This problem is very simple:

_device is *lazy loaded* and initialized in getDevice(). This means that any direct reference to _device will break, even within the class.

The fix can be as easy as using this:

    public function serialize()
        $spec = array(
            'browser_type' => $this->_browserType,
            'config'       => $this->_config,
            'device_class' => get_class($this->getDevice()),
            'device'       => $this->getDevice()->serialize(),
            'user_agent'   => $this->getServerValue('http_user_agent'),
            'http_accept'  => $this->getServerValue('http_accept'),
        return serialize($spec);

Would be happy if someone could check this in.

Made a fix patch for René Treffer's fix, and enclosed a unit test in the file: ZF-10665-rtuin.patch

Fixed in trunk r23661 merged to branch release 1.11 r23662.