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

Fix serialization of ComparisionFailure with PDO in stacktrace. #47

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion src/ComparisonFailure.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
/**
* Thrown when an assertion for string equality failed.
*/
class ComparisonFailure extends \RuntimeException
class ComparisonFailure extends \RuntimeException implements \Serializable
{
/**
* Expected value of the retrieval which does not match $actual.
Expand Down Expand Up @@ -125,4 +125,26 @@ public function toString()
{
return $this->message . $this->getDiff();
}

/**
* Serialises all parts of the comparison failure, apart from the stacktrace which might contain references to
* objects which cannot be serialised.
*
* @return string
*/
public function serialize()
{
return serialize(array(
$this->expected, $this->actual, $this->expectedAsString, $this->actualAsString,
$this->identical, $this->message, $this->file, $this->line
));
}

public function unserialize($serialized)
{
list(
$this->expected, $this->actual, $this->expectedAsString, $this->actualAsString,
$this->identical, $this->message, $this->file, $this->line
) = unserialize($serialized);
}
}
20 changes: 20 additions & 0 deletions tests/ComparisonFailureTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,24 @@ public function testDiffNotPossible(): void
$this->assertSame('', $failure->getDiff());
$this->assertSame('test', $failure->toString());
}

public function testSerializesWithoutStackTrace()
{
$instance = new NonSerializableClass;

$failure = $this->functionWithNonSerializableParam($instance);

$serialised = serialize($failure);
$deserialised = unserialize($serialised);

$this->assertSame($failure->getActual(), $deserialised->getActual());
$this->assertSame($failure->getExpected(), $deserialised->getExpected());
$this->assertSame($failure->getActualAsString(), $deserialised->getActualAsString());
$this->assertSame($failure->getExpectedAsString(), $deserialised->getExpectedAsString());
$this->assertSame($failure->toString(), $deserialised->toString());
}

private function functionWithNonSerializableParam(NonSerializableClass $instance) {
return new ComparisonFailure('a', 'b', 'a', 'b', false, 'test');
}
}
20 changes: 20 additions & 0 deletions tests/_fixture/NonSerializableClass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php
/*
* This file is part of sebastian/comparator.
*
* (c) Sebastian Bergmann <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace SebastianBergmann\Comparator;

class NonSerializableClass implements \Serializable {
public function serialize()
{
throw new \Error('This class cannot be serialized');
}

public function unserialize($serialized) {}
}