function ProxyBuilderTest::buildExpectedClass

Same name in this branch
  1. 11.x core/tests/Drupal/Tests/Core/ProxyBuilder/ProxyBuilderTest.php \Drupal\Tests\Core\ProxyBuilder\ProxyBuilderTest::buildExpectedClass()
Same name in other branches
  1. 9 core/tests/Drupal/Tests/Core/ProxyBuilder/ProxyBuilderTest.php \Drupal\Tests\Core\ProxyBuilder\ProxyBuilderTest::buildExpectedClass()
  2. 9 core/tests/Drupal/Tests/Component/ProxyBuilder/ProxyBuilderTest.php \Drupal\Tests\Component\ProxyBuilder\ProxyBuilderTest::buildExpectedClass()
  3. 8.9.x core/tests/Drupal/Tests/Core/ProxyBuilder/ProxyBuilderTest.php \Drupal\Tests\Core\ProxyBuilder\ProxyBuilderTest::buildExpectedClass()
  4. 8.9.x core/tests/Drupal/Tests/Component/ProxyBuilder/ProxyBuilderTest.php \Drupal\Tests\Component\ProxyBuilder\ProxyBuilderTest::buildExpectedClass()
  5. 10 core/tests/Drupal/Tests/Core/ProxyBuilder/ProxyBuilderTest.php \Drupal\Tests\Core\ProxyBuilder\ProxyBuilderTest::buildExpectedClass()
  6. 10 core/tests/Drupal/Tests/Component/ProxyBuilder/ProxyBuilderTest.php \Drupal\Tests\Component\ProxyBuilder\ProxyBuilderTest::buildExpectedClass()

Constructs the expected class output.

Parameters

string $class: The class name that is being built.

string $expected_methods_body: The expected body of decorated methods.

string $interface_string: (optional) The expected "implements" clause of the class definition.

Return value

string The code of the entire proxy.

11 calls to ProxyBuilderTest::buildExpectedClass()
ProxyBuilderTest::testBuildComplexMethod in core/tests/Drupal/Tests/Component/ProxyBuilder/ProxyBuilderTest.php
@covers ::buildMethod @covers ::buildParameter @covers ::buildMethodBody
ProxyBuilderTest::testBuildMethodWithParameter in core/tests/Drupal/Tests/Component/ProxyBuilder/ProxyBuilderTest.php
@covers ::buildMethod @covers ::buildParameter @covers ::buildMethodBody
ProxyBuilderTest::testBuildNoMethod in core/tests/Drupal/Tests/Component/ProxyBuilder/ProxyBuilderTest.php
Tests the basic methods like the constructor and the lazyLoadItself method.
ProxyBuilderTest::testBuildReturnReference in core/tests/Drupal/Tests/Component/ProxyBuilder/ProxyBuilderTest.php
@covers ::buildMethod @covers ::buildMethodBody
ProxyBuilderTest::testBuildServiceMethodReturnsVoid in core/tests/Drupal/Tests/Component/ProxyBuilder/ProxyBuilderTest.php
@covers ::buildMethodBody

... See full list

File

core/tests/Drupal/Tests/Component/ProxyBuilder/ProxyBuilderTest.php, line 322

Class

ProxyBuilderTest
@coversDefaultClass \Drupal\Component\ProxyBuilder\ProxyBuilder @group proxy_builder

Namespace

Drupal\Tests\Component\ProxyBuilder

Code

protected function buildExpectedClass($class, $expected_methods_body, $interface_string = '') {
    $namespace = ProxyBuilder::buildProxyNamespace($class);
    $reflection = new \ReflectionClass($class);
    $proxy_class = $reflection->getShortName();
    $expected_string = <<<'EOS'

namespace {{ namespace }} {

    /**
     * Provides a proxy class for \{{ class }}.
     *
     * @see \Drupal\Component\ProxyBuilder
     */
    class {{ proxy_class }}{{ interface_string }}
    {

        /**
         * The id of the original proxied service.
         *
         * @var string
         */
        protected $drupalProxyOriginalServiceId;

        /**
         * The real proxied service, after it was lazy loaded.
         *
         * @var \{{ class }}
         */
        protected $service;

        /**
         * The service container.
         *
         * @var \Symfony\Component\DependencyInjection\ContainerInterface
         */
        protected $container;

        /**
         * Constructs a ProxyClass Drupal proxy object.
         *
         * @param \Symfony\Component\DependencyInjection\ContainerInterface $container
         *   The container.
         * @param string $drupal_proxy_original_service_id
         *   The service ID of the original service.
         */
        public function __construct(\Symfony\Component\DependencyInjection\ContainerInterface $container, $drupal_proxy_original_service_id)
        {
            $this->container = $container;
            $this->drupalProxyOriginalServiceId = $drupal_proxy_original_service_id;
        }

        /**
         * Lazy loads the real service from the container.
         *
         * @return object
         *   Returns the constructed real service.
         */
        protected function lazyLoadItself()
        {
            if (!isset($this->service)) {
                $this->service = $this->container->get($this->drupalProxyOriginalServiceId);
            }

            return $this->service;
        }
{{ expected_methods_body }}
    }

}

EOS;
    $expected_methods_body = implode("\n", array_map(function ($value) {
        if ($value === '') {
            return $value;
        }
        return "        {$value}";
    }, explode("\n", $expected_methods_body)));
    $expected_string = str_replace('{{ proxy_class }}', $proxy_class, $expected_string);
    $expected_string = str_replace('{{ namespace }}', $namespace, $expected_string);
    $expected_string = str_replace('{{ class }}', $class, $expected_string);
    $expected_string = str_replace('{{ expected_methods_body }}', $expected_methods_body, $expected_string);
    $expected_string = str_replace('{{ interface_string }}', $interface_string, $expected_string);
    return $expected_string;
}

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