DependencySerializationTest.php

Same filename in other branches
  1. 9 core/tests/Drupal/Tests/Core/DependencyInjection/DependencySerializationTest.php
  2. 8.9.x core/tests/Drupal/Tests/Core/DependencyInjection/DependencySerializationTest.php
  3. 11.x core/tests/Drupal/Tests/Core/DependencyInjection/DependencySerializationTest.php

Namespace

Drupal\Tests\Core\DependencyInjection

File

core/tests/Drupal/Tests/Core/DependencyInjection/DependencySerializationTest.php

View source
<?php

declare (strict_types=1);
namespace Drupal\Tests\Core\DependencyInjection;

use Drupal\Component\DependencyInjection\ReverseContainer;
use Drupal\Core\DependencyInjection\DependencySerializationTrait;
use Drupal\Core\Test\TestKernel;
use Drupal\Tests\UnitTestCase;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * @coversDefaultClass \Drupal\Core\DependencyInjection\DependencySerializationTrait
 * @group DependencyInjection
 */
class DependencySerializationTest extends UnitTestCase {
    
    /**
     * @covers ::__sleep
     * @covers ::__wakeup
     */
    public function testSerialization() : void {
        // Create a pseudo service and dependency injected object.
        $service = new \stdClass();
        $container = TestKernel::setContainerWithKernel();
        $container->set('test_service', $service);
        $this->assertSame($container, $container->get('service_container'));
        $dependencySerialization = new DependencySerializationTestDummy($service);
        $dependencySerialization->setContainer($container);
        $string = serialize($dependencySerialization);
        
        /** @var \Drupal\Tests\Core\DependencyInjection\DependencySerializationTestDummy $dependencySerialization */
        $dependencySerialization = unserialize($string);
        $this->assertTrue($container->has(ReverseContainer::class));
        $this->assertSame($service, $dependencySerialization->service);
        $this->assertSame($container, $dependencySerialization->container);
        $this->assertEmpty($dependencySerialization->getServiceIds());
    }

}

/**
 * Defines a test class which has a single service as dependency.
 */
class DependencySerializationTestDummy implements ContainerAwareInterface {
    use DependencySerializationTrait;
    
    /**
     * A test service.
     *
     * @var object
     */
    public $service;
    
    /**
     * The container.
     *
     * @var \Symfony\Component\DependencyInjection\ContainerInterface
     */
    public $container;
    
    /**
     * Constructs a new TestClass object.
     *
     * @param object $service
     *   A test service.
     */
    public function __construct(\stdClass $service) {
        $this->service = $service;
    }
    
    /**
     * {@inheritdoc}
     *
     * phpcs:ignore Drupal.Commenting.FunctionComment.VoidReturn
     * @return void
     */
    public function setContainer(?ContainerInterface $container = NULL) {
        $this->container = $container;
    }
    
    /**
     * Gets the stored service IDs.
     */
    public function getServiceIds() {
        return $this->_serviceIds;
    }

}

Classes

Title Deprecated Summary
DependencySerializationTest @coversDefaultClass \Drupal\Core\DependencyInjection\DependencySerializationTrait @group DependencyInjection
DependencySerializationTestDummy Defines a test class which has a single service as dependency.

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