You are here

class ContrivedController in Examples for Developers 3.x

Same name and namespace in other branches
  1. 8 testing_example/src/Controller/ContrivedController.php \Drupal\testing_example\Controller\ContrivedController

A highly-contrived controller class used to demonstrate unit testing.

Hierarchy

Expanded class hierarchy of ContrivedController

1 file declares its use of ContrivedController
ContrivedControllerTest.php in modules/testing_example/tests/src/Unit/Controller/ContrivedControllerTest.php

File

modules/testing_example/src/Controller/ContrivedController.php, line 13

Namespace

Drupal\testing_example\Controller
View source
class ContrivedController implements ContainerInjectionInterface {
  use StringTranslationTrait;

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('string_translation'));
  }

  /**
   * Construct a new controller.
   *
   * @param Drupal\Core\StringTranslation\TranslationInterface $translation
   *   The translation service.
   */
  public function __construct(TranslationInterface $translation) {
    $this
      ->setStringTranslation($translation);
  }

  /**
   * A controller method which displays a sum in terms of hands.
   *
   * @param int $first
   *   A parameter to the controller path.
   * @param int $second
   *   A parameter to the controller path.
   *
   * @return string[]
   *   A markup array.
   */
  public function displayAddedNumbers($first, $second) {
    return [
      '#markup' => '<p>' . $this
        ->handCount($first, $second) . '</p>',
    ];
  }

  /**
   * Generate a message based on how many hands are needed to count the sum.
   *
   * @param int $first
   *   First parameter.
   * @param int $second
   *   Second parameter.
   *
   * @return \Drupal\Core\StringTranslation\TranslatableMarkup
   *   The translated message.
   */
  protected function handCount($first, $second) {
    $sum = abs($this
      ->add((int) $first, (int) $second));
    if ($sum <= 5) {
      $message = $this
        ->t('I can count these on one hand.');
    }
    elseif ($sum <= 10) {
      $message = $this
        ->t('I need two hands to count these.');
    }
    else {
      $message = $this
        ->t("That's just too many numbers to count.");
    }
    return $message;
  }

  /**
   * Add two numbers.
   *
   * @param int $first
   *   The first parameter.
   * @param int $second
   *   The second parameter.
   *
   * @return int
   *   The sum of the two parameters.
   */
  protected function add($first, $second) {
    return $first + $second;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ContrivedController::add protected function Add two numbers.
ContrivedController::create public static function Instantiates a new instance of this class. Overrides ContainerInjectionInterface::create
ContrivedController::displayAddedNumbers public function A controller method which displays a sum in terms of hands.
ContrivedController::handCount protected function Generate a message based on how many hands are needed to count the sum.
ContrivedController::__construct public function Construct a new controller.
StringTranslationTrait::$stringTranslation protected property The string translation service. 4
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.