function DependencyTest::testSerialization

Same name in other branches
  1. 8.9.x core/tests/Drupal/Tests/Core/Extension/DependencyTest.php \Drupal\Tests\Core\Extension\DependencyTest::testSerialization()
  2. 10 core/tests/Drupal/Tests/Core/Extension/DependencyTest.php \Drupal\Tests\Core\Extension\DependencyTest::testSerialization()
  3. 11.x core/tests/Drupal/Tests/Core/Extension/DependencyTest.php \Drupal\Tests\Core\Extension\DependencyTest::testSerialization()

Ensures that constraint objects are not serialized.

@covers ::__sleep

File

core/tests/Drupal/Tests/Core/Extension/DependencyTest.php, line 52

Class

DependencyTest
@coversDefaultClass \Drupal\Core\Extension\Dependency @group Extension

Namespace

Drupal\Tests\Core\Extension

Code

public function testSerialization() {
    $dependency = new Dependency('paragraphs_demo', 'paragraphs', '>8.x-1.1');
    $this->assertTrue($dependency->isCompatible('1.2'));
    $reflected_constraint = (new \ReflectionObject($dependency))->getProperty('constraint');
    $reflected_constraint->setAccessible(TRUE);
    $constraint = $reflected_constraint->getValue($dependency);
    $this->assertInstanceOf(Constraint::class, $constraint);
    $dependency = unserialize(serialize($dependency));
    $reflected_constraint = (new \ReflectionObject($dependency))->getProperty('constraint');
    $reflected_constraint->setAccessible(TRUE);
    $constraint = $reflected_constraint->getValue($dependency);
    $this->assertNull($constraint);
    $this->assertTrue($dependency->isCompatible('1.2'));
    $constraint = $reflected_constraint->getValue($dependency);
    $this->assertInstanceOf(Constraint::class, $constraint);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.