phpspec / prophecy

Highly opinionated mocking framework for PHP 5.3+
MIT License
8.53k stars 241 forks source link

Serialize on mocked object throw error in PHP 7.4 #447

Open alexander-schranz opened 5 years ago

alexander-schranz commented 5 years ago

When in PHP 7.4 a mocked object get serialized e.g.:

$metadataMock = $this->prophesize(Test::class);
$metadata = $metadataMock->reveal();
// call some function I want to test:
$myClassToTest->test($metadata);
// which does do the following:
serialize($metadata)

The following exception is thrown:

Exception: Serialization of 'ReflectionParameter' is not allowed

Related PHP migration: https://www.php.net/manual/de/migration74.incompatible.php

ciaranmcnulty commented 4 years ago

Why are you serialising the prophet before revealing? Can you expand on the use case?

alexander-schranz commented 4 years ago

Sorry it is revealed updated the example.

georgecoca commented 4 years ago

Same here, after upgrading to PHP 7.4 the error came up. It worked just fine on PHP 7.2

$site = $this->prophesize(Site::class);
$branch = $this->prophesize(Branch::class);
$url = 'http://fake.url';

$event = new NewUrlAddressFound($site->reveal(), $branch->reveal(), $url);
$serialized = serialize($event);

Exception: Serialization of 'Closure' is not allowed

@alexander-schranz did you managed to solve the issue?

alexander-schranz commented 4 years ago

@geodeveloper I refractored the tests and avoided to mock that objects and created real instances of that objects. So I learned for myself avoid mocks where it is possible.

georgecoca commented 4 years ago

@alexander-schranz I ended doing that too, avoiding serializing mock objects. Thanks for your time!