class ProxyBuilderTest

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

@coversDefaultClass \Drupal\Core\ProxyBuilder\ProxyBuilder @group proxy_builder

Hierarchy

Expanded class hierarchy of ProxyBuilderTest

File

core/tests/Drupal/Tests/Core/ProxyBuilder/ProxyBuilderTest.php, line 14

Namespace

Drupal\Tests\Core\ProxyBuilder
View source
class ProxyBuilderTest extends UnitTestCase {
    
    /**
     * The tested proxy builder.
     *
     * @var \Drupal\Core\ProxyBuilder\ProxyBuilder
     */
    protected $proxyBuilder;
    
    /**
     * {@inheritdoc}
     */
    protected function setUp() : void {
        parent::setUp();
        $this->proxyBuilder = new ProxyBuilder();
    }
    
    /**
     * @covers ::buildMethod
     * @covers ::buildParameter
     * @covers ::buildMethodBody
     */
    public function testBuildComplexMethod() : void {
        $class = 'Drupal\\Tests\\Core\\ProxyBuilder\\TestServiceComplexMethod';
        $result = $this->proxyBuilder
            ->build($class);
        // @todo Solve the silly linebreak for array()
        $method_body = <<<'EOS'

/**
 * {@inheritdoc}
 */
public function complexMethod($parameter, callable $function, ?\Drupal\Tests\Core\ProxyBuilder\TestServiceNoMethod $test_service = NULL, array &$elements = array (
))
{
    return $this->lazyLoadItself()->complexMethod($parameter, $function, $test_service, $elements);
}

EOS;
        $this->assertEquals($this->buildExpectedClass($class, $method_body), $result);
    }
    
    /**
     * Constructs the expected class output.
     *
     * @param string $class
     *   The class name that is being built.
     * @param string $expected_methods_body
     *   The expected body of decorated methods.
     * @param string $interface_string
     *   (optional) The expected "implements" clause of the class definition.
     *
     * @return string
     *   The code of the entire proxy.
     */
    protected function buildExpectedClass($class, $expected_methods_body, $interface_string = '') {
        $reflection = new \ReflectionClass($class);
        $namespace = ProxyBuilder::buildProxyNamespace($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 }}
    {

        use \Drupal\Core\DependencyInjection\DependencySerializationTrait;

        /**
         * 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;
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
ExpectDeprecationTrait::expectDeprecation public function Adds an expected deprecation.
ExpectDeprecationTrait::getCallableName private static function Returns a callable as a string suitable for inclusion in a message.
ExpectDeprecationTrait::setUpErrorHandler public function Sets up the test error handler.
ExpectDeprecationTrait::tearDownErrorHandler public function Tears down the test error handler.
ProxyBuilderTest::$proxyBuilder protected property The tested proxy builder.
ProxyBuilderTest::buildExpectedClass protected function Constructs the expected class output.
ProxyBuilderTest::setUp protected function Overrides UnitTestCase::setUp
ProxyBuilderTest::testBuildComplexMethod public function @covers ::buildMethod
@covers ::buildParameter
@covers ::buildMethodBody
RandomGeneratorTrait::getRandomGenerator protected function Gets the random generator for the utility methods.
RandomGeneratorTrait::randomMachineName protected function Generates a unique random string containing letters and numbers.
RandomGeneratorTrait::randomObject public function Generates a random PHP object.
RandomGeneratorTrait::randomString public function Generates a pseudo-random string of ASCII characters of codes 32 to 126.
UnitTestCase::$root protected property The app root.
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::setUpBeforeClass public static function

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