You are here

public function DependencyTest::testSerialization in Drupal 10

Same name and namespace in other branches
  1. 8 core/tests/Drupal/Tests/Core/Extension/DependencyTest.php \Drupal\Tests\Core\Extension\DependencyTest::testSerialization()
  2. 9 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);
}