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

Serialization of mock issues #266

Open
garethellis36 opened this issue Jul 3, 2018 · 0 comments
Open

Serialization of mock issues #266

garethellis36 opened this issue Jul 3, 2018 · 0 comments

Comments

@garethellis36
Copy link

I came across a bit of an edge-case in a test which touched the database. I was using a Phake mock as follows:

$mock = Phake::mock(MyClass::class);
Phake::when($mock)->myMethod->thenThrow(new MyException);

The issue was that later in the test, I was passing the mock as a constructor argument to the Class Under Test and then trying to serialize the CUT. I was getting the error:
You cannot serialize or unserialize PDO instances

The thing is that MyClass did not have anything to do with the database, and neither did the CUT or any of its dependencies.

After a lot of debugging I found that the mock object had the instance of the exception stored as a data property a long way down the nested structure, and the exception itself had a trace property which included all of the global context from the test class, including the CakePHP fixture manager, which of course had a PDO instance inside it!

So this was really unexpected behaviour and after losing a few hours, in the end I could not use Phake to mock this class because of it.

I'm not sure if there is a solution to this. Perhaps Phake mocks could implement __sleep() in such a way as to exclude expected exceptions?

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

1 participant