1
+ <?php
2
+
3
+ namespace Drupal \testing_example \Controller ;
4
+
5
+ use Drupal \Core \DependencyInjection \ContainerInjectionInterface ;
6
+ use Drupal \Core \StringTranslation \StringTranslationTrait ;
7
+ use Drupal \Core \StringTranslation \TranslationInterface ;
8
+ use Symfony \Component \DependencyInjection \ContainerInterface ;
9
+
10
+ /**
11
+ * A highly-contrived controller class used to demonstrate unit testing.
12
+ */
13
+ class ContrivedController implements ContainerInjectionInterface {
14
+
15
+ use StringTranslationTrait;
16
+
17
+ /**
18
+ * {@inheritdoc}
19
+ */
20
+ public static function create (ContainerInterface $ container ) {
21
+ return new static (
22
+ $ container ->get ('string_translation ' )
23
+ );
24
+ }
25
+
26
+ /**
27
+ * Construct a new controller.
28
+ *
29
+ * @param Drupal\Core\StringTranslation\TranslationInterface $translation
30
+ * The translation service.
31
+ */
32
+ public function __construct (TranslationInterface $ translation ) {
33
+ $ this ->setStringTranslation ($ translation );
34
+ }
35
+
36
+ /**
37
+ * A controller method which displays a sum in terms of hands.
38
+ *
39
+ * @param int $first
40
+ * A parameter to the controller path.
41
+ * @param int $second
42
+ * A parameter to the controller path.
43
+ * @return string[]
44
+ * A markup array.
45
+ */
46
+ public function displayAddedNumbers ($ first , $ second ) {
47
+ return [
48
+ '#markup ' => '<p> ' . $ this ->handCount ($ first , $ second ) . '</p> ' ,
49
+ ];
50
+ }
51
+
52
+ /**
53
+ * Generate a message based on how many hands are needed to count the sum.
54
+ *
55
+ * @param int $first
56
+ * First parameter.
57
+ * @param int $second
58
+ * Second parameter.
59
+ *
60
+ * @return \Drupal\Core\StringTranslation\TranslatableMarkup
61
+ * The translated message.
62
+ */
63
+ protected function handCount ($ first , $ second ) {
64
+ $ sum = abs ($ this ->add ((int )$ first , (int )$ second ));
65
+ if ($ sum <= 5 ) {
66
+ $ message = $ this ->t ('I can count these on one hand. ' );
67
+ }
68
+ else if ($ sum <= 10 ) {
69
+ $ message = $ this ->t ('I need two hands to count these. ' );
70
+ }
71
+ else {
72
+ $ message = $ this ->t ("That's just too many numbers to count. " );
73
+ }
74
+ return $ message ;
75
+ }
76
+
77
+ /**
78
+ * Add two numbers.
79
+ *
80
+ * @param int $first
81
+ * The first parameter.
82
+ * @param int $second
83
+ * The second parameter.
84
+ *
85
+ * @return int
86
+ * The sum of the two parameters.
87
+ */
88
+ protected function add ($ first , $ second ) {
89
+ return $ first + $ second ;
90
+ }
91
+
92
+ }
0 commit comments