Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to Mock a class: unserialize(): Error at offset 29 of 30 bytes #1409

Closed
morafabio opened this issue Aug 27, 2014 · 5 comments
Closed

Comments

@morafabio
Copy link

Today I upgraded to PHP 5.5.16 and I have an issue mocking a library.
It's \Symfony\Component\HttpFoundation\File\File.

The following code returns an exception:

public function testMock()
{
    $file =
        $this->getMockBuilder('\Symfony\Component\HttpFoundation\File\File')
        ->disableOriginalConstructor()
        ->getMock();
}

I have created a repo to easily reproduce the bug, just git clone, composer install and launch ./phpunit: https://github.com/morafabio/phpunit-unserialize-mock-bug

This is really similar to this bug: mockery/mockery#339.

Actual

phpunit-unserialize-mock-bug git:(master) php -v
PHP 5.5.16 (cli) (built: Aug 27 2014 21:34:18)
Copyright (c) 1997-2014 The PHP Group
Zend Engine v2.5.0, Copyright (c) 1998-2014 Zend Technologies
    with Xdebug v2.2.5, Copyright (c) 2002-2014, by Derick Rethans
➜  phpunit-unserialize-mock-bug git:(master) ./phpunit
PHPUnit 4.2.0 by Sebastian Bergmann.

E

Time: 20 ms, Memory: 3.25Mb

There was 1 error:

1) MyTest::testMock
Instantiator\Exception\UnexpectedValueException: Could not produce an instance of "Mock_File_eef1b578" via un-serialization, since an error was triggered in file "/Users/fabio/phpunit-unserialize-mock-bug/vendor/ocramius/instantiator/src/Instantiator/Instantiator.php" at line "159"

/Users/fabio/phpunit-unserialize-mock-bug/test.php:16

Caused by
Exception: unserialize(): Error at offset 29 of 30 bytes

/Users/fabio/phpunit-unserialize-mock-bug/test.php:16

FAILURES!
Tests: 1, Assertions: 0, Errors: 1.
➜  phpunit-unserialize-mock-bug git:(master)

Here is my environment:

System: Mac OS X 10.9.4 and Ubuntu 14.04-amd64
PHPUnit: 4.2.0  

Expected

Note that with a PHP older than 5.5.13 it works fine, eg.:

phpunit-unserialize-mock-bug git:(master) ✗ /usr/bin/php -v
PHP 5.4.24 (cli) (built: Jan 19 2014 21:32:15)
Copyright (c) 1997-2013 The PHP Group
Zend Engine v2.4.0, Copyright (c) 1998-2013 Zend Technologies
➜  phpunit-unserialize-mock-bug git:(master) ✗ /usr/bin/php     vendor/bin/phpunit test.php
PHPUnit 4.2.0 by Sebastian Bergmann.

.

Time: 16 ms, Memory: 2.75Mb

OK (1 test, 0 assertions)
@whatthejeff
Copy link
Contributor

I don't know if this is going to be fixable in PHP 5.5.16. Due to security concerns, certain internal classes can no longer be instantiated without calling their constructor. I did some research into this a while ago, and SplFileInfo (which \Symfony\Component\HttpFoundation\File\File extends) was definitely on the list of problematic classes.

@Ocramius provides the library we use for instantiation and has done much more research into this lately, so maybe he can give us a status update on the likelyhood of this working now or in the future.

@Ocramius
Copy link
Sponsor Contributor

@whatthejeff I think instantiator doesn't support SplFileInfo instantiation on 5.5.16 here, though I'd have to add a test for it.

@whatthejeff
Copy link
Contributor

Thanks, @Ocramius :)

@morafabio thanks for bringing this to our attention. Unfortunately, there's not much we can do here. For future reference, the library we use for instantiation is https://github.com/doctrine/instantiator. Any further discussion regarding instantiation should probably happen over there.

@morafabio
Copy link
Author

Thanks guys, but another question.

I did a quick test and as I can see here http://3v4l.org/F4fqb, now in PHP 5.6 an internal class can be instantiated without invoking its constructor. Not the same for 5.4.x and 5.5.x, but some minor version support the "unserialization" trick: http://3v4l.org/jqvE2.

I'm a bit confused at this point: regardless of my PHP minor version, should I write a test case using mock SplFileInfo or not?

@Ocramius in your opinion, what should Instantiator support and what not?

@Ocramius
Copy link
Sponsor Contributor

@morafabio I think that I cannot support SplFileInfo for 5.5.14+, but a test is still welcome, as the limitation may be documented this way (assuming that I cannot find a trick to get it running).

martin-helmich added a commit to martin-helmich/typo3-typoscript-lint that referenced this issue Oct 30, 2015
higidi added a commit to higidi/composer-phpcodesniffer-standards-plugin that referenced this issue Apr 25, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants