Run prettier on all *.md files

```
prettier '{app,source}/**/**.md' --write
```
This commit is contained in:
Oliver Davies 2020-03-08 17:52:59 +00:00
parent a3ceeaf0f3
commit 85a10c545b
170 changed files with 5127 additions and 2282 deletions

View file

@ -1,19 +1,25 @@
---
title: Using PSR-4 Autoloading for your Drupal 7 Test Cases
excerpt: How to use the PSR-4 autoloading standard for Drupal 7 Simpletest test cases.
excerpt:
How to use the PSR-4 autoloading standard for Drupal 7 Simpletest test cases.
tags: [drupal, drupal-planet, drupal-7, testing, simpletest, php, psr]
date: 2020-02-04
---
<p>{{ page.excerpt }}</p>
## The Traditional Way
The typical way of including test cases in Drupal 7 is to add one or more classes within a `.test` file - e.g. `opdavies.test`.
This would typically include all of the different test cases for that module, and would be placed in the root of the modules directory alongside the `.info` and `.module` files.
The typical way of including test cases in Drupal 7 is to add one or more
classes within a `.test` file - e.g. `opdavies.test`. This would typically
include all of the different test cases for that module, and would be placed in
the root of the modules directory alongside the `.info` and `.module` files.
In order to load the files, each file would need to be declared within the `.info` file for the module.
In order to load the files, each file would need to be declared within the
`.info` file for the module.
There is a convention that if you have multiple tests for your project, these can be split into different files and grouped within a `tests` directory.
There is a convention that if you have multiple tests for your project, these
can be split into different files and grouped within a `tests` directory.
```ini
; Load a test file at the root of the module
@ -26,10 +32,11 @@ files[] = tests/bar.test
## Using the xautoload Module
Whilst splitting tests into separate files makes things more organised, each file needs to be loaded separately.
This can be made simpler by using the [Xautoload module][], which supports wildcards when declaring files.
Whilst splitting tests into separate files makes things more organised, each
file needs to be loaded separately. This can be made simpler by using the
[Xautoload module][], which supports wildcards when declaring files.
[Xautoload module]: https://www.drupal.org/project/xautoload
[xautoload module]: https://www.drupal.org/project/xautoload
```ini
files[] = tests/**/*.test
@ -41,10 +48,15 @@ This would load all of the `.test` files within the tests directory.
Another option is to use PSR-4 (or PSR-0) autoloading.
This should be a lot more familiar to those who have worked with Drupal 8, Symfony etc, and means that each test case is in its own file which is cleaner, files have the `.php` extension which is more standard, and the name of the file matches the name of the test class for consistency.
This should be a lot more familiar to those who have worked with Drupal 8,
Symfony etc, and means that each test case is in its own file which is cleaner,
files have the `.php` extension which is more standard, and the name of the file
matches the name of the test class for consistency.
To do this, create a `src/Tests` (PSR-4) or `lib/Drupal/{module_name}/Tests` (PSR-0) directory within your module, and then add or move your test cases there.
Add the appropriate namespace for your module, and ensure that `DrupalWebTestCase` or `DrupalUnitTestCase` is also namespaced.
To do this, create a `src/Tests` (PSR-4) or `lib/Drupal/{module_name}/Tests`
(PSR-0) directory within your module, and then add or move your test cases
there. Add the appropriate namespace for your module, and ensure that
`DrupalWebTestCase` or `DrupalUnitTestCase` is also namespaced.
```php
// src/Tests/Functional/OliverDaviesTest.php
@ -56,11 +68,14 @@ class OliverDaviesTest extends \DrupalWebTestCase {
}
```
This also supports subdirectories, so you can group classes within `Functional` and `Unit` directories if you like.
This also supports subdirectories, so you can group classes within `Functional`
and `Unit` directories if you like.
If you want to see an real-world example, see the Drupal 7 branch of the [Override Node Options module][override_node_options].
If you want to see an real-world example, see the Drupal 7 branch of the
[Override Node Options module][override_node_options].
[override_node_options]: https://git.drupalcode.org/project/override_node_options/tree/7.x-1.x
[override_node_options]:
https://git.drupalcode.org/project/override_node_options/tree/7.x-1.x
### Digging into the simpletest_test_get_all function
@ -104,13 +119,18 @@ foreach (array(
}
```
It looks for a the tests directory (`src/Tests` or `lib/Drupal/{module_name}/Tests`) within the module, and then finds any `.php` files within it. It then converts the file name into the fully qualified (namespaced) class name and loads it automatically.
It looks for a the tests directory (`src/Tests` or
`lib/Drupal/{module_name}/Tests`) within the module, and then finds any `.php`
files within it. It then converts the file name into the fully qualified
(namespaced) class name and loads it automatically.
### Running the Tests
You can still run the tests from within the Simpletest UI, or from the command line using `run-tests.sh`.
You can still run the tests from within the Simpletest UI, or from the command
line using `run-tests.sh`.
If you want to run a specific test case using the `--class` option, you will now need to include the fully qualified name.
If you want to run a specific test case using the `--class` option, you will now
need to include the fully qualified name.
```
php scripts/run-tests.sh --class Drupal\\opdavies\\Tests\\Functional\\OliverDaviesTest