oliverdavies.uk/content/node.4c1497cc-4638-40fc-b045-f342c4d3e5b3.json

91 lines
19 KiB
JSON
Raw Normal View History

2025-05-11 07:15:45 +01:00
{
"uuid": [
{
2025-05-11 09:40:11 +01:00
"value": "4c1497cc-4638-40fc-b045-f342c4d3e5b3"
2025-05-11 07:15:45 +01:00
}
],
"langcode": [
{
"value": "en"
}
],
"type": [
{
"target_id": "daily_email",
"target_type": "node_type",
"target_uuid": "8bde1f2f-eef9-4f2d-ae9c-96921f8193d7"
}
],
"revision_timestamp": [
{
2025-05-11 09:40:11 +01:00
"value": "2025-05-11T09:00:24+00:00"
2025-05-11 07:15:45 +01:00
}
],
"revision_uid": [
{
"target_type": "user",
"target_uuid": "b8966985-d4b2-42a7-a319-2e94ccfbb849"
}
],
"revision_log": [],
"status": [
{
"value": true
}
],
"uid": [
{
"target_type": "user",
"target_uuid": "b8966985-d4b2-42a7-a319-2e94ccfbb849"
}
],
"title": [
{
"value": "A sneak peek of my Drupal automated testing course\n"
}
],
"created": [
{
"value": "2023-12-25T00:00:00+00:00"
}
],
"changed": [
{
2025-05-11 09:40:11 +01:00
"value": "2025-05-11T09:00:24+00:00"
2025-05-11 07:15:45 +01:00
}
],
"promote": [
{
"value": false
}
],
"sticky": [
{
"value": false
}
],
"default_langcode": [
{
"value": true
}
],
"revision_translation_affected": [
{
"value": true
}
],
"path": [
{
"alias": "\/daily\/2023\/12\/25\/zero-to-test",
"langcode": "en"
}
],
"body": [
{
2025-05-30 02:14:32 +01:00
"value": "\n <p>Happy Christmas!<\/p>\n\n<p>Here's my present - a sneak peek at the first lesson in my free upcoming Automated Testing for Drupal email course.<\/p>\n\n<p>In this lesson, we start from scratch and end with a working test suite.<\/p>\n\n<p>If you like it, <a href=\"\/atdc\">register for free and get the full course<\/a> when it launches.<\/p>\n\n<h2 id=\"creating-a-drupal-project\">Creating a Drupal project<\/h2>\n\n<p>If you don't have one, you'll need to create a new Drupal project. I'd suggest using Drupal 10.2 and the instructions at <a href=\"https:\/\/www.drupal.org\/download\">https:\/\/www.drupal.org\/download<\/a>.<\/p>\n\n<p>You'll need <a href=\"https:\/\/www.php.net\/manual\/en\/install.php\">PHP<\/a> and <a href=\"https:\/\/getcomposer.org\/doc\/00-intro.md\">Composer<\/a>.<\/p>\n\n<p>First, run <code>composer create-project drupal\/recommended-project drupal<\/code> followed by <code>cd drupal &amp;&amp; composer require --dev drupal\/core-dev<\/code> to add the development dependencies, including PHPUnit.<\/p>\n\n<p>At this point, you should have a <code>web<\/code> directory and a <code>phpunit<\/code> file within <code>vendor\/bin<\/code>.<\/p>\n\n<p>Finally, run <code>php -S 0.0.0.0:8000 -t web<\/code> to start a local web server.<\/p>\n\n<p>You don't need to install Drupal - as long as you see the installation page, that's fine.<\/p>\n\n<h2 id=\"creating-a-custom-module\">Creating a custom module<\/h2>\n\n<p>Before adding tests, you must create a module to place them in.<\/p>\n\n<p>Run <code>mkdir -p web\/modules\/custom\/atdc<\/code> to create an empty module directory, and create an <code>atdc.info.yml<\/code> file within it with this content:<\/p>\n\n<pre><code class=\"language-yaml\">name: Example\ntype: module\ncore_version_requirement: ^10\npackage: Custom\n<\/code><\/pre>\n\n<p>This is the minimum content needed for a module to be installable.<\/p>\n\n<h3 id=\"writing-your-first-test-class\">Writing your first test class<\/h3>\n\n<p>Test classes are placed within each module's <code>tests\/src<\/code> directory.<\/p>\n\n<p>Run <code>mkdir -p web\/modules\/custom\/atdc\/tests\/src\/Functional &amp;&amp; touch web\/modules\/custom\/atdc\/tests\/src\/Functional\/ExampleTest.php<\/code> to create the directory structure and a blank test class.<\/p>\n\n<p>Then, add this content.<\/p>\n\n<pre><code class=\"language-php\">&lt;?php\n\nnamespace Drupal\\Tests\\atdc\\Functional;\n\nuse Drupal\\Tests\\BrowserTestBase;\nuse Symfony\\Component\\HttpFoundation\\Response;\n\nclass ExampleTest extends BrowserTestBase {\n\n\u00a0 public $defaultTheme = 'stark';\n\n}\n<\/code><\/pre>\n\n<p>Note: within a test class, the namespace is <code>Drupal\\Tests\\{module_name}<\/code> instead of <code>Drupal\\{module_name}<\/code>.<\/p>\n\n<p>With the boilerplate class added, create a test method within it:<\/p>\n\n<pre><code class=\"language-php\">public function testBasic(): void {\n\u00a0 self::assertTrue(FALSE);\n}\n<\/code><\/pre>\n\n<p>Note: the class name must be suffixed with <code>Test<\/code>, and the test method must be prefixed with <code>test<\/code> for them to be run.<\/p>\n\n<p>Now we have a test with an assertion, we need to run it and see if it passes.<\/p>\n\n<h2 id=\"running-the-test\">Running the test<\/h2>\n\n<p>On the command line, run <code>vendor\/bin\/phpunit web\/modules\/custom<\/code>, and you'll get an error like:<\/p>\n\n<blockquote>\n <p>PHPUnit\\TextUI\\RuntimeException: Class \"Drupal\\Tests\\BrowserTestBase\" not found.<\/p>\n<\/blockquote>\n\n<p>This isn't an assertion failure, but that PHPUnit can't find the files it needs to run.<\/p>\n\n<p>To fix this, let's configure PHPUnit.<\/p>\n\n<h2 id=\"configuring-phpunit\">Configuring PHPUnit<\/h2>\n\n<p>Create a new <code>phpunit.xml.dist<\/code> file at the root of your project, with this content:<\/p>\n\n<pre><code class=\"xml\">&lt;?xml version=\"1.0\" encoding=\"UTF-8\"?&gt;\n&lt;phpunit bootstrap=\"web\/core\/tests\/bootstrap.php\" colors=\"true\"&gt;\n &lt;php&gt;\n &lt;env name=\"SIMPLETEST_BA
2025-05-11 07:15:45 +01:00
"format": "full_html",
2025-05-30 02:14:32 +01:00
"processed": "\n <p>Happy Christmas!<\/p>\n\n<p>Here's my present - a sneak peek at the first lesson in my free upcoming Automated Testing for Drupal email course.<\/p>\n\n<p>In this lesson, we start from scratch and end with a working test suite.<\/p>\n\n<p>If you like it, <a href=\"http:\/\/default\/atdc\">register for free and get the full course<\/a> when it launches.<\/p>\n\n<h2 id=\"creating-a-drupal-project\">Creating a Drupal project<\/h2>\n\n<p>If you don't have one, you'll need to create a new Drupal project. I'd suggest using Drupal 10.2 and the instructions at <a href=\"https:\/\/www.drupal.org\/download\">https:\/\/www.drupal.org\/download<\/a>.<\/p>\n\n<p>You'll need <a href=\"https:\/\/www.php.net\/manual\/en\/install.php\">PHP<\/a> and <a href=\"https:\/\/getcomposer.org\/doc\/00-intro.md\">Composer<\/a>.<\/p>\n\n<p>First, run <code>composer create-project drupal\/recommended-project drupal<\/code> followed by <code>cd drupal &amp;&amp; composer require --dev drupal\/core-dev<\/code> to add the development dependencies, including PHPUnit.<\/p>\n\n<p>At this point, you should have a <code>web<\/code> directory and a <code>phpunit<\/code> file within <code>vendor\/bin<\/code>.<\/p>\n\n<p>Finally, run <code>php -S 0.0.0.0:8000 -t web<\/code> to start a local web server.<\/p>\n\n<p>You don't need to install Drupal - as long as you see the installation page, that's fine.<\/p>\n\n<h2 id=\"creating-a-custom-module\">Creating a custom module<\/h2>\n\n<p>Before adding tests, you must create a module to place them in.<\/p>\n\n<p>Run <code>mkdir -p web\/modules\/custom\/atdc<\/code> to create an empty module directory, and create an <code>atdc.info.yml<\/code> file within it with this content:<\/p>\n\n<pre><code class=\"language-yaml\">name: Example\ntype: module\ncore_version_requirement: ^10\npackage: Custom\n<\/code><\/pre>\n\n<p>This is the minimum content needed for a module to be installable.<\/p>\n\n<h3 id=\"writing-your-first-test-class\">Writing your first test class<\/h3>\n\n<p>Test classes are placed within each module's <code>tests\/src<\/code> directory.<\/p>\n\n<p>Run <code>mkdir -p web\/modules\/custom\/atdc\/tests\/src\/Functional &amp;&amp; touch web\/modules\/custom\/atdc\/tests\/src\/Functional\/ExampleTest.php<\/code> to create the directory structure and a blank test class.<\/p>\n\n<p>Then, add this content.<\/p>\n\n<pre><code class=\"language-php\">&lt;?php\n\nnamespace Drupal\\Tests\\atdc\\Functional;\n\nuse Drupal\\Tests\\BrowserTestBase;\nuse Symfony\\Component\\HttpFoundation\\Response;\n\nclass ExampleTest extends BrowserTestBase {\n\n&nbsp; public $defaultTheme = 'stark';\n\n}\n<\/code><\/pre>\n\n<p>Note: within a test class, the namespace is <code>Drupal\\Tests\\{module_name}<\/code> instead of <code>Drupal\\{module_name}<\/code>.<\/p>\n\n<p>With the boilerplate class added, create a test method within it:<\/p>\n\n<pre><code class=\"language-php\">public function testBasic(): void {\n&nbsp; self::assertTrue(FALSE);\n}\n<\/code><\/pre>\n\n<p>Note: the class name must be suffixed with <code>Test<\/code>, and the test method must be prefixed with <code>test<\/code> for them to be run.<\/p>\n\n<p>Now we have a test with an assertion, we need to run it and see if it passes.<\/p>\n\n<h2 id=\"running-the-test\">Running the test<\/h2>\n\n<p>On the command line, run <code>vendor\/bin\/phpunit web\/modules\/custom<\/code>, and you'll get an error like:<\/p>\n\n<blockquote>\n <p>PHPUnit\\TextUI\\RuntimeException: Class \"Drupal\\Tests\\BrowserTestBase\" not found.<\/p>\n<\/blockquote>\n\n<p>This isn't an assertion failure, but that PHPUnit can't find the files it needs to run.<\/p>\n\n<p>To fix this, let's configure PHPUnit.<\/p>\n\n<h2 id=\"configuring-phpunit\">Configuring PHPUnit<\/h2>\n\n<p>Create a new <code>phpunit.xml.dist<\/code> file at the root of your project, with this content:<\/p>\n\n<pre><code class=\"xml\">&lt;?xml version=\"1.0\" encoding=\"UTF-8\"?&gt;\n&lt;phpunit bootstrap=\"web\/core\/tests\/bootstrap.php\" colors=\"true\"&gt;\n &lt;php&gt;\n &lt;env
2025-05-11 07:15:45 +01:00
"summary": null
}
]
}