class ContrivedController in Examples for Developers 3.x
Same name and namespace in other branches
- 8 testing_example/src/Controller/ContrivedController.php \Drupal\testing_example\Controller\ContrivedController
A highly-contrived controller class used to demonstrate unit testing.
Hierarchy
- class \Drupal\testing_example\Controller\ContrivedController implements ContainerInjectionInterface uses StringTranslationTrait
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\ControllerView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ContrivedController:: |
protected | function | Add two numbers. | |
ContrivedController:: |
public static | function |
Instantiates a new instance of this class. Overrides ContainerInjectionInterface:: |
|
ContrivedController:: |
public | function | A controller method which displays a sum in terms of hands. | |
ContrivedController:: |
protected | function | Generate a message based on how many hands are needed to count the sum. | |
ContrivedController:: |
public | function | Construct a new controller. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 4 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |