Skip to content

Commit 907cba2

Browse files
torenwarepaul-m
torenware
authored andcommitted
Issue #2751893 by Torenware: Use Twig for the phpunit example controller
1 parent 671e630 commit 907cba2

File tree

2 files changed

+66
-25
lines changed

2 files changed

+66
-25
lines changed

phpunit_example/src/Controller/PHPUnitExampleController.php

+11-25
Original file line numberDiff line numberDiff line change
@@ -2,38 +2,24 @@
22

33
namespace Drupal\phpunit_example\Controller;
44

5+
use Drupal\examples\Utility\DescriptionTemplateTrait;
6+
57
/**
68
* Controller for PHPUnit description page.
9+
*
10+
* This class uses the DescriptionTemplateTrait to display text we put in the
11+
* templates/description.html.twig file. We render out the text via its
12+
* description() method, and set up our routing to point to
13+
* PHPUnitExampleController::description().
714
*/
815
class PHPUnitExampleController {
16+
use DescriptionTemplateTrait;
917

1018
/**
11-
* Displays a page with a descriptive page.
12-
*
13-
* Our router maps this method to the path 'examples/phpunit_example'.
19+
* {@inheritdoc}
1420
*/
15-
public function description() {
16-
$build = array(
17-
'#markup' => t('<h2>PHPUnit for Drupal: A very basic how-to.</h2>
18-
<h3>How to use this example module</h3>
19-
<p>You really should be reading the various docblocks in the test files.</p>
20-
<h3>How To:</h3>
21-
<ul>
22-
<li><p>PHPUnit tests belong in their own directory, so they won&#39;t be loaded by the autoloader during normal bootstrap. This means you should have a <code>/tests/src</code> directory in the root of your module directory.</p></li>
23-
<li><p>Your tests should be in the <code>Drupal\Tests\[your_module]\Unit</code> namespace. Under Drupal&#39;s PSR-4 system, this means your PHPUnit-based tests should go in a <code>[your_module]/tests/src/Unit</code> directory.</p></li>
24-
<li><p>Your test case should subclass <code>Drupal\Tests\UnitTestCase</code>.</p></li>
25-
<li><p>You can run PHPUnit-based tests from within Drupal 8 by enabling the Testing module and then selecting the PHPUnit group from the testing page. As of this writing, this method doesn&#39;t provide any useful output.</p></li>
26-
</ul>
27-
<h3>Standard PHPUnit Practices</h3>
28-
<p>You can (and really, should) run PHPUnit from the command line.</p>
29-
<p>On unix-based systems this means you need to <code>cd core</code> and then <code>./vendor/bin/phpunit</code>.</p>
30-
<p>On Windows-based systems, assuming you have php in your path, <code>php ./vendor/phpunit/phpunit/composer/bin/phpunit --group phpunit_example</code>.</p>
31-
<p>Also, you should mark your tests as belonging to a group, so they can be run independently. You do this by annotating your test classes with <code>@group group_name</code>. You should have a <code>@group</code> for your module name, and you should also have a <code>@group</code> for integrations, such as <code>views</code>.</p>
32-
<p>So, for instance, to run all of the PHPUnit example tests, you would type <code>./vendor/bin/phpunit --group phpunit_example</code>.</p>
33-
<p>As you can see, including a <code>@group</code> annotation is a good idea.</p>'),
34-
);
35-
36-
return $build;
21+
protected function getModuleName() {
22+
return 'phpunit_example';
3723
}
3824

3925
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
{#
2+
3+
Description text for the PHPUnit Example.
4+
5+
#}
6+
7+
{% trans %}
8+
<h2>PHPUnit for Drupal: A very basic how-to.</h2>
9+
10+
<h3>How to use this example module</h3>
11+
<p>You really should be reading the various docblocks in the test files.</p>
12+
13+
<h3>How To:</h3>
14+
<ul>
15+
<li><p>PHPUnit tests belong in their own directory, so they won&#39;t be loaded
16+
by the autoloader during normal bootstrap. This means you should have a
17+
<code>/tests/src</code> directory in the root of your module directory.</p>
18+
</li>
19+
20+
<li><p>Your tests should be in the <code>Drupal\Tests\[your_module]\Unit</code>
21+
namespace. Under Drupal&#39;s PSR-4 system, this means your PHPUnit-based
22+
tests should go in a <code>[your_module]/tests/src/Unit</code>
23+
directory.</p>
24+
</li>
25+
<li><p>Your test case should subclass <code>Drupal\Tests\UnitTestCase</code>.
26+
</p>
27+
</li>
28+
<li>
29+
<p>You can run PHPUnit-based tests from within Drupal 8 by enabling the
30+
Testing module and then selecting the PHPUnit group from the testing page.
31+
As of this writing, this method doesn&#39;t provide any useful output.
32+
</p>
33+
</li>
34+
</ul>
35+
36+
<h3>Standard PHPUnit Practices</h3>
37+
<p>You can (and really, should) run PHPUnit from the command line.</p>
38+
<p>On unix-based systems this means you need to <code>cd core</code> and then
39+
<pre><code>./vendor/bin/phpunit</code></pre>
40+
</p>
41+
<p>On Windows-based systems, assuming you have php in your path,
42+
<pre><code>php ./vendor/phpunit/phpunit/composer/bin/phpunit --group phpunit_example</code></pre>
43+
</p>
44+
<p>Also, you should mark your tests as belonging to a group, so they can be run
45+
independently. You do this by annotating your test classes with
46+
<code>@group group_name</code>. You should have a <code>@group</code> for
47+
your module name, and you should also have a <code>@group</code> for
48+
integrations, such as <code>views</code>.
49+
</p>
50+
<p>So, for instance, to run all of the PHPUnit example tests, you would type
51+
<pre><code>./vendor/bin/phpunit --group phpunit_example</code></pre>
52+
</p>
53+
<p>As you can see, including a <code>@group</code> annotation is a good idea.</p>
54+
55+
{% endtrans %}

0 commit comments

Comments
 (0)