Issue #3020165 by alexpott: Dependency objects can be optimised for serialisation

8.7.x
Nathaniel Catchpole 2018-12-18 09:39:51 +00:00
parent 56ccba066c
commit e5b70636c9
2 changed files with 26 additions and 0 deletions

View File

@ -190,4 +190,14 @@ class Dependency implements \ArrayAccess {
return new static($name, $project, $version_string);
}
/**
* Prevents unnecessary serialization of constraint objects.
*
* @return array
* The properties to seriailize.
*/
public function __sleep() {
return ['name', 'project', 'constraintString'];
}
}

View File

@ -2,6 +2,7 @@
namespace Drupal\Tests\Core\Extension;
use Drupal\Component\Version\Constraint;
use Drupal\Core\Extension\Dependency;
use Drupal\Tests\UnitTestCase;
@ -100,4 +101,19 @@ class DependencyTest extends UnitTestCase {
$dependency['name'] = 'foo';
}
/**
* Ensures that constraint objects are not serialized.
*
* @covers ::__sleep
*/
public function testSerialization() {
$dependency = new Dependency('paragraphs_demo', 'paragraphs', '>8.x-1.1');
$this->assertTrue($dependency->isCompatible('1.2'));
$this->assertInstanceOf(Constraint::class, $this->getObjectAttribute($dependency, 'constraint'));
$dependency = unserialize(serialize($dependency));
$this->assertNull($this->getObjectAttribute($dependency, 'constraint'));
$this->assertTrue($dependency->isCompatible('1.2'));
$this->assertInstanceOf(Constraint::class, $this->getObjectAttribute($dependency, 'constraint'));
}
}