Skip to content

Instantly share code, notes, and snippets.

@WengerK
Created February 19, 2021 13:40
Show Gist options
  • Save WengerK/b95aedd906ccf91c7c7f509dff90c399 to your computer and use it in GitHub Desktop.
Save WengerK/b95aedd906ccf91c7c7f509dff90c399 to your computer and use it in GitHub Desktop.
Article Ressources - Factory Lollipop - Speeding up Kernel tests on Drupal

Article Ressources - Factory Lollipop - Speeding up Kernel tests on Drupal

This is the Gist repository for my article Factory Lollipop - Speeding up Kernel tests on Drupal.

Be aware that this article has been wrote for the Blog of Antistatique — Web Agency in Lausanne, Switzerland. A place where I work as Full Stack Web Developer.

Feel free to read it the full article on Medium or check it out on Antistatique.

Content of this Gist :

  • lollipop-example-01.php : Factory Lollipop inline usage example.
  • NodeArticleFactory.php : Fully working Node Article Factory.
  • NodeArticleFactory--no-resolve.php : Partial Factory containing the basic structure w/o implemented resolver.
  • NodeArticleFactory--only-resolve.php: Example of Factory resolver to create Article Nodes.
  • my_custom_module.services.yml : Service declaration for a Factory definition.
  • MyKernelTest.php : Fully working Kernel tests using the Node Article Factory.
  • MyKernelTest--no-create.php : Partial Kernel tests containing the basic structure to load a Factory Definition.
  • MyKernelTest--only-create.php : Example of Kernel tests using the create method of Factory Lollipop.
$factoryLollipop->define('node type', 'node_type_article', [
'type' => 'article',
]);
$factoryLollipop->define('node', 'node_article', [
'field_slug' => FixtureFactory::sequence("Slug %d"),
'status' => 1,
'type' => $factoryLollipop->association('node_type_article'),
'field_bar' => function() { return now(); },
]);
$node = $factoryLollipop->create('node_article', [
'title' => 'Tortor posuere ornare quisque mi vehicula nostra',
]);
# $node->getTitle() => 'Tortor posuere ornare quisque mi vehicula nostra'
# $node->get('status')->value => 1
# $node->getBundle() => article
services:
my_custom_module.factories.node.article:
class: Drupal\my_custom_module\Factories\NodeArticleFactory
tags:
- { name: factory_lollipop.factory_resolver, priority: 1 }
<?php
/**
* Example of Factory Lollipop usage for a KernelTest.
*/
class MyKernelTest extends LollipopKernelTestBase {
/**
* {@inheritdoc}
*/
public static $modules = [
'node',
];
public function testCreateNode(): void {
$this->factoryLollipop->loadDefinitions(['my_project.definitions.node_article']);
}
}
<?php
public function testCreateNode(): void {
$this->factoryLollipop->loadDefinitions(['my_project.definitions.node_article']);
$node = $this->factoryLollipop->create('node_article', ['title' => 'Magna cursus tempor']);
}
<?php
/**
* Example of Factory Lollipop usage for a KernelTest.
*/
class MyKernelTest extends LollipopKernelTestBase {
/**
* {@inheritdoc}
*/
public static $modules = [
'node',
];
public function testCreateNode(): void {
$this->factoryLollipop->loadDefinitions(['my_project.definitions.node_article']);
$node = $this->factoryLollipop->create('node_article', ['title' => 'Magna cursus tempor']);
}
}
<?php
namespace Drupal\my_custom_module\Factories;
use Drupal\factory_lollipop\FactoryInterface;
use Drupal\factory_lollipop\FixtureFactory;
/**
* Creates Drupal Article Nodes Factory for use in tests.
*/
class NodeArticleFactory implements FactoryInterface {
/**
* {@inheritdoc}
*/
public function getName():string {
return 'my_project.definitions.node_article';
}
/**
* {@inheritdoc}
*/
public function resolve(FixtureFactory $lollipop): void {
// ...
}
}
<?php
/**
* {@inheritdoc}
*/
public function resolve(FixtureFactory $lollipop): void {
// Define the node type "Article".
$lollipop->define('node type', 'node_type_article', [
'type' => 'article',
]);
// Add the "Scheduled at" field without default value.
$lollipop->define('entity field', 'node_article_field_scheduled_at', [
'entity_type' => 'node',
'name' => 'field_scheduled_at',
'bundle' => $lollipop->association('node_type_article'),
'type' => 'datetime',
]);
$lollipop->create('node_article_field_scheduled_at');
// Add the "Is Paid" field with a default value of False.
$lollipop->define('entity field', 'node_article_field_is_paid', [
'entity_type' => 'node',
'name' => 'field_is_paid',
'bundle' => $lollipop->association('node_type_article'),
'type' => 'boolean',
]);
$lollipop->create('node_article_field_is_paid');
// Define the Node Factory for Article".
$lollipop->define('node', 'node_article', [
'type' => $lollipop->association('node_type_article'),
// Setup the default Status to Published.
'status' => 1,
// Setup the "Is Paid" field default value to FALSE.
'field_is_paid' => FALSE,
]);
}
<?php
namespace Drupal\my_custom_module\Factories;
use Drupal\factory_lollipop\FactoryInterface;
use Drupal\factory_lollipop\FixtureFactory;
/**
* Creates Drupal Article Nodes Factory for use in tests.
*/
class NodeArticleFactory implements FactoryInterface {
/**
* {@inheritdoc}
*/
public function getName():string {
return 'my_project.definitions.node_article';
}
/**
* {@inheritdoc}
*/
public function resolve(FixtureFactory $lollipop): void {
// Define the node type "Article".
$lollipop->define('node type', 'node_type_article', [
'type' => 'article',
]);
// Add the "Scheduled at" field without default value.
$lollipop->define('entity field', 'node_article_field_scheduled_at', [
'entity_type' => 'node',
'name' => 'field_scheduled_at',
'bundle' => $lollipop->association('node_type_article'),
'type' => 'datetime',
]);
$lollipop->create('node_article_field_scheduled_at');
// Add the "Is Paid" field with a default value of False.
$lollipop->define('entity field', 'node_article_field_is_paid', [
'entity_type' => 'node',
'name' => 'field_is_paid',
'bundle' => $lollipop->association('node_type_article'),
'type' => 'boolean',
]);
$lollipop->create('node_article_field_is_paid');
// Define the Node Factory for Article".
$lollipop->define('node', 'node_article', [
'type' => $lollipop->association('node_type_article'),
// Setup the default Status to Published.
'status' => 1,
// Setup the "Is Paid" field default value to FALSE.
'field_is_paid' => FALSE,
]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment