vendredi 13 novembre 2015

PHPUnit - Serialization of 'Closure' is not allowed

I have a very weird issue when writing tests for our Laravel 4.2 application with PHP 5.6.15 and PHPUnit 5.0.3.

Below you can find the exception it throws in

  • some cases when we use assertEquals, but it works with assertSame
  • some cases when assertSame's expected value is different than the value
  • when using helper functions (like assertResponseOK(), which I think are using assertEquals)

Note the weird thing that in one test it works with the same structure, but in another test where only the api's response is a bit different, it throws the exception. Especially, throwing exception except of managing the assertion as false is a nice thing (it sometimes works, sometimes doesn't too).

Example Stacktrace

~ $ phpunit --filter FooController
PHPUnit 5.0.3 by Sebastian Bergmann and contributors.

Runtime:       PHP 5.6.15
Configuration: /path/to/project/phpunit.xml

.........E...                                                     13 / 13 (100%)

Time: 11.66 seconds, Memory: 15.25Mb

There was 1 error:

1) Namespace\To\Tests\Integration\V2\FooControllerTest::test_updating_a_foo
PHPUnit_Framework_Exception: {"error":{"type":"Exception","message":"Serialization of 'Closure' is not allowed","file":"-","line":71}}

/path/to/tests/TestCase.php:46

Caused by
exception 'ErrorException' with message 'unserialize(): Error at offset 0 of 105 bytes' in phar:///usr/share/webapps/bin/phpunit.phar/phar/phpunit/Util/PHP.php:107
Stack trace:
#0 [internal function]: PHPUnit_Util_PHP->{closure}(8, 'unserialize(): ...', 'phar:///usr/sha...', 107, Array)
#1 phar:///usr/share/webapps/bin/phpunit.phar/phar/phpunit/Util/PHP.php(107): unserialize('{"error":{"type...')
#2 phar:///usr/share/webapps/bin/phpunit.phar/phar/phpunit/Util/PHP.php(50): PHPUnit_Util_PHP->processChildResult(Object(Namespace\To\Tests\Integration\V2\FooControllerTest), Object(PHPUnit_Framework_TestResult), '{"error":{"type...', '')
#3 phar:///usr/share/webapps/bin/phpunit.phar/phar/phpunit/Framework/TestCase.php(706): PHPUnit_Util_PHP->runTestJob('<?php\nif (!defi...', Object(Namespace\To\Tests\Integration\V2\FooControllerTest), Object(PHPUnit_Framework_TestResult))
#4 /path/to/tests/TestCase.php(46): PHPUnit_Framework_TestCase->run(Object(PHPUnit_Framework_TestResult))
#5 phar:///usr/share/webapps/bin/phpunit.phar/phar/phpunit/Framework/TestSuite.php(735): Namespace\To\Tests\TestCase->run(Object(PHPUnit_Framework_TestResult))
#6 phar:///usr/share/webapps/bin/phpunit.phar/phar/phpunit/Framework/TestSuite.php(735): PHPUnit_Framework_TestSuite->run(Object(PHPUnit_Framework_TestResult))
#7 phar:///usr/share/webapps/bin/phpunit.phar/phar/phpunit/Framework/TestSuite.php(735): PHPUnit_Framework_TestSuite->run(Object(PHPUnit_Framework_TestResult))
#8 phar:///usr/share/webapps/bin/phpunit.phar/phar/phpunit/TextUI/TestRunner.php(423): PHPUnit_Framework_TestSuite->run(Object(PHPUnit_Framework_TestResult))
#9 phar:///usr/share/webapps/bin/phpunit.phar/phar/phpunit/TextUI/Command.php(153): PHPUnit_TextUI_TestRunner->doRun(Object(PHPUnit_Framework_TestSuite), Array)
#10 phar:///usr/share/webapps/bin/phpunit.phar/phar/phpunit/TextUI/Command.php(105): PHPUnit_TextUI_Command->run(Array, true)
#11 /usr/share/webapps/bin/phpunit.phar(510): PHPUnit_TextUI_Command::main()
#12 {main}

Aucun commentaire:

Enregistrer un commentaire