You are here

VariableGetTest.php in Drupal 7 to 8/9 Module Upgrader 8

File

tests/src/Unit/Plugin/DMU/Converter/Functions/VariableGetTest.php
View source
<?php

namespace Drupal\Tests\drupalmoduleupgrader\Unit\Plugin\DMU\Converter\Functions;

use Pharborist\Filter;
use Pharborist\Parser;

/**
 * @group DMU.Converter.Functions
 * @covers \Drupal\drupalmoduleupgrader\Plugin\DMU\Converter\Functions\VariableGet
 */
class VariableGetTest extends FunctionCallModifierTestBase {
  public function testNonStringKey() {
    $original = <<<'END'
<?php
variable_get($my_var, TRUE);
END;
    $expected = <<<'END'
<?php
// @FIXME
// The correct configuration object could not be determined. You'll need to
// rewrite this call manually.
variable_get($my_var, TRUE);
END;
    $snippet = Parser::parseSource($original);
    $function_call = $snippet
      ->find(Filter::isFunctionCall('variable_get'))
      ->get(0);
    $rewritten = $this->plugin
      ->rewrite($function_call, $this->target);
    $this
      ->assertNull($rewritten);
    $this
      ->assertSame($expected, $snippet
      ->getText());
  }
  public function testForeignStringKey() {
    $original = <<<'END'
<?php
variable_get('bar_wambooli', TRUE);
END;
    $expected = <<<'END'
<?php
// @FIXME
// This looks like another module's variable. You'll need to rewrite this call
// to ensure that it uses the correct configuration object.
variable_get('bar_wambooli', TRUE);
END;
    $snippet = Parser::parseSource($original);
    $function_call = $snippet
      ->find(Filter::isFunctionCall('variable_get'))
      ->get(0);
    $rewritten = $this->plugin
      ->rewrite($function_call, $this->target);
    $this
      ->assertNull($rewritten);
    $this
      ->assertSame($expected, $snippet
      ->getText());
  }
  public function testStringKeyAndUnextractableDefaultValue() {
    $original = <<<'END'
<?php
variable_get('foo_wambooli', array());
END;
    $expected = <<<'END'
<?php
// @FIXME
// Could not extract the default value because it is either indeterminate, or
// not scalar. You'll need to provide a default value in
// config/install/foo.settings.yml and config/schema/foo.schema.yml.
variable_get('foo_wambooli', array());
END;
    $snippet = Parser::parseSource($original);
    $function_call = $snippet
      ->find(Filter::isFunctionCall('variable_get'))
      ->get(0);
    $rewritten = $this->plugin
      ->rewrite($function_call, $this->target);
    $this
      ->assertInstanceOf('\\Pharborist\\Objects\\ObjectMethodCallNode', $rewritten);
    $this
      ->assertEquals('\\Drupal::config(\'foo.settings\')->get(\'foo_wambooli\')', $rewritten
      ->getText());
    $this
      ->assertSame($expected, $snippet
      ->getText());
  }
  public function testStringKeyAndExtractableDefaultValue() {
    $function_call = Parser::parseExpression('variable_get("foo_wambooli", 30)');
    $rewritten = $this->plugin
      ->rewrite($function_call, $this->target);
    $this
      ->assertInstanceOf('\\Pharborist\\Objects\\ObjectMethodCallNode', $rewritten);
    $this
      ->assertEquals('\\Drupal::config(\'foo.settings\')->get("foo_wambooli")', $rewritten
      ->getText());
  }

}

Classes