PHPackages                             mpndev/d8tdd - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Testing &amp; Quality](/categories/testing)
4. /
5. mpndev/d8tdd

ActiveLibrary[Testing &amp; Quality](/categories/testing)

mpndev/d8tdd
============

Help with Drupal 8 TDD...

v1.0.8(6y ago)0521MITPHPPHP &gt;=7.2.0

Since Nov 11Pushed 6y ago1 watchersCompare

[ Source](https://github.com/mpndev/d8tdd)[ Packagist](https://packagist.org/packages/mpndev/d8tdd)[ RSS](/packages/mpndev-d8tdd/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (1)Versions (10)Used By (0)

You want to test Drupal-8.\*
============================

[](#you-want-to-test-drupal-8)

Now that's a very challenging task! For every test you need to write tons and tons of preparation code.

Here are some helpful tools!
----------------------------

[](#here-are-some-helpful-tools)

Install via composer:

```
composer require mpndev/t8tdd

```

---

Lets start with the Kernel tests for your module.

First you need to create an abstract class that extends KernelTestBase class. KernelTestBase lives in this library.

- From terminal, go to the root directory of your Drupal installation;
- Let's say you have module "my\_module". Execute the following command:

    use the name of your module in "snake\_case":

    ```
    php ./vendor/mpndev/d8tdd/src/generate.php make:kerneltest my_module

    ```

    or the name of your module in "PascalCase":

    ```
    php ./vendor/mpndev/d8tdd/src/generate.php make:kerneltest MyModule

    ```

    This will scaffold the abstract class for you.
- Every test class, that will make kernel-tests, must extends "MyModuleKernelTestBase";
- This will give you the powers of "factory" and "jsonRequest" functionalities:

    ```
    $this->factory(Node::class)->make();

    $this->jsonRequest('http://localhost/some/endpoint')
      ->using('POST')
      ->send();
    ```

    (below we have examples on how to use these tools.)

---

Factories:
----------

[](#factories)

Let's assume we need Node instance from bundle "project". The very first thing is to define factory for default "project" implementation.

Open MyModuleKernelTestBase.php and add the following in the setUp method

```
$this->factory(Node::class)->define('project', [
  'type' => 'project',
  'title' => 'Some Project Title'
]);
```

Here you have access to the "Faker" library. [Click here](https://github.com/fzaninotto/Faker) to see the full list of a available methods.

```
$this->factory(Node::class)->define('project', [
  'type' => 'project',
  'title' => $this->faker->name,
]);
```

---

If you need to make an instance of a "project" in your tests you can do so by using:

```
$project = $this->factory(Node::class)->make('project');

$project->bundle();  // will return 'project'

$project->get('title')->getString();  // will return 'Some Project Title'
```

$project will be fresh content type of bundle "project"

---

You can override and add fields on the fly:

```
$project = $this->factory(Node::class)->make('project', [
  'title' => 'Some Another Title'
  'field_something' => 'Something'
]);

$project->bundle();  // will return 'project'

$project->get('title')->getString();  // will return 'Some Another Title'

$project->get('field_something')->getString();  // will return 'Something'
```

This will override the default behavior of the "factory" for bundle "project"

---

You can also make multiple instances of "project" like this:

```
$projects = $this->factory(Node::class, 15)->make('project');
```

That will give you an array of 15 "projects".

---

If you use "create" instead of "make", factory will save the "project" in the database:

```
$this->factory(Node::class)->create('project');
```

---

You can "make" "project" and save it later (normal Drupal stuff):

```
$project = $this->factory(Node::class)->make('project');

//Do something...

$project->save()
```

---

You can access the “project” instance by using closure as a third argument in the “make” or “create” methods. By doing so you can modify the “project” instance i.e. by adding something via a reference field:

```
/**
 * make
 */
$project = $this->factory(Node::class)->make('project', [], function($project_instance){
  $project_instance->get('field_environments')->appendItem($this->factory(Paragraph::class)->create('environment'));
  return $project_instance;
});

/**
 * create
 */
$project = $this->factory(Node::class)->create('project', [], function($project_instance){
  $project_instance->get('field_environments')->appendItem($this->factory(Paragraph::class)->create('environment'));
  return $project_instance;
});
```

### IMPORTANT!!!

[](#important)

\- In closure, always return the passed instance variable! (In our example `$project_instance`);

\- `appendItem()` method will expect the saved instance to be in the database for the attached item! (In our example "environment", that was created on the fly, again with factory);

---

HttpRequest:
------------

[](#httprequest)

Helper, that makes available requests for testing endpoints.

The request will not go outside. Instead, it will be handled from the Drupal kernel, and the response can be inspected. Nice isn't it?

```
$response = $this->httpRequest('http://localhost/some/endpoint')
  ->using('GET')
  ->send();
```

```
$response = $this->httpRequest('http://localhost/some/endpoint')
  ->using('POST')
  ->send();
```

```
$response = $this->httpRequest('http://localhost/some/endpoint')
  ->using('PUT')
  ->send();
```

```
$response = $this->httpRequest('http://localhost/some/endpoint')
  ->using('PATCH')
  ->send();
```

```
$response = $this->httpRequest('http://localhost/some/endpoint')
  ->using('DELETE')
  ->send();
```

---

Attach cookie:

```
$response = $this->httpRequest('http://localhost/some/endpoint')
  ->using('POST')
  ->withCookie($some_cookie)
  ->send();
```

---

Attach content:

```
$response = $this->httpRequest('http://localhost/some/endpoint')
  ->using('POST')
  ->withContent($some_content)
  ->send();
```

---

Attach server:

```
$response = $this->httpRequest('http://localhost/some/endpoint')
  ->using('POST')
  ->withServer($some_server)
  ->send();
```

---

Attach files:

```
$response = $this->httpRequest('http://localhost/some/endpoint')
  ->using('POST')
  ->withFiles($some_files)
  ->send();
```

---

JsonRequest:
------------

[](#jsonrequest)

Helper, that makes available json requests (with application/json header) for testing endpoints.

The request will not go outside. Instead, it will be handled from the Drupal kernel, and the response can be inspected. The same like HttpRequest Helper.

```
$response = $this->jsonRequest('http://localhost/some/endpoint')
  ->using('GET')
  ->send();
```

```
$response = $this->jsonRequest('http://localhost/some/endpoint')
  ->using('POST')
  ->send();
```

```
$response = $this->jsonRequest('http://localhost/some/endpoint')
  ->using('PUT')
  ->send();
```

```
$response = $this->jsonRequest('http://localhost/some/endpoint')
  ->using('PATCH')
  ->send();
```

```
$response = $this->jsonRequest('http://localhost/some/endpoint')
  ->using('DELETE')
  ->send();
```

---

Attach cookie:

```
$response = $this->jsonRequest('http://localhost/some/endpoint')
  ->using('POST')
  ->withCookie($some_cookie)
  ->send();
```

---

Attach content:

```
$response = $this->jsonRequest('http://localhost/some/endpoint')
  ->using('POST')
  ->withContent($some_content)
  ->send();
```

---

Attach server:

```
$response = $this->jsonRequest('http://localhost/some/endpoint')
  ->using('POST')
  ->withServer($some_server)
  ->send();
```

---

Attach files:

```
$response = $this->jsonRequest('http://localhost/some/endpoint')
  ->using('POST')
  ->withFiles($some_files)
  ->send();
```

---

Happy testing :)
----------------

[](#happy-testing-)

###  Health Score

26

—

LowBetter than 43% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity9

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity58

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~1 days

Total

9

Last Release

2370d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/6635e7653d4e810a9b8ef3d9cc39ca6cf4261b227cdaec9c0cf5288adbe01373?d=identicon)[mpndev](/maintainers/mpndev)

---

Top Contributors

[![mpndev](https://avatars.githubusercontent.com/u/55798038?v=4)](https://github.com/mpndev "mpndev (33 commits)")

### Embed Badge

![Health badge](/badges/mpndev-d8tdd/health.svg)

```
[![Health](https://phpackages.com/badges/mpndev-d8tdd/health.svg)](https://phpackages.com/packages/mpndev-d8tdd)
```

###  Alternatives

[sizuhiko/fabricate

PHP data generator for Testing

64626.3k2](/packages/sizuhiko-fabricate)[silverstripe/frameworktest

Aids core and module developers in testing their code against a set of sample data and behaviour.

17304.6k23](/packages/silverstripe-frameworktest)[refinery29/test-util

Provides a test helper, generic data providers, and assertions.

1554.0k3](/packages/refinery29-test-util)[liorchamla/symfony-test-helpers

Provides cool tools to make testing over symfony smoothie and great !

222.2k](/packages/liorchamla-symfony-test-helpers)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
