public function testFromUncleanUnSerialization() { $reflection = new ReflectionClass('InstantiatorTestAsset\\AbstractClassAsset'); $exception = UnexpectedValueException::fromUncleanUnSerialization($reflection, 'foo', 123, 'bar', 456); $this->assertInstanceOf('Instantiator\\Exception\\UnexpectedValueException', $exception); $this->assertSame('Could not produce an instance of "InstantiatorTestAsset\\AbstractClassAsset" via un-serialization, ' . 'since an error was triggered in file "bar" at line "456"', $exception->getMessage()); $previous = $exception->getPrevious(); $this->assertInstanceOf('Exception', $previous); $this->assertSame('foo', $previous->getMessage()); $this->assertSame(123, $previous->getCode()); }
/** * @param ReflectionClass $reflectionClass * @param string $serializedString * * @throws UnexpectedValueException * * @return void */ private function attemptInstantiationViaUnSerialization(ReflectionClass $reflectionClass, $serializedString) { set_error_handler(function ($code, $message, $file, $line) use($reflectionClass, &$error) { $error = UnexpectedValueException::fromUncleanUnSerialization($reflectionClass, $message, $code, $file, $line); }); try { unserialize($serializedString); } catch (Exception $exception) { restore_error_handler(); throw UnexpectedValueException::fromSerializationTriggeredException($reflectionClass, $exception); } restore_error_handler(); if ($error) { throw $error; } }