Refactor to use a factory class

This commit is contained in:
Oliver Davies 2020-09-04 13:16:49 +01:00
parent 10e4616e01
commit 2617e29dec
6 changed files with 150 additions and 33 deletions

View file

@ -8,6 +8,7 @@
<sourceFolder url="file://$MODULE_DIR$/web/modules/custom/opd_talks/src" isTestSource="false" packagePrefix="Drupal\opd_talks" /> <sourceFolder url="file://$MODULE_DIR$/web/modules/custom/opd_talks/src" isTestSource="false" packagePrefix="Drupal\opd_talks" />
<sourceFolder url="file://$MODULE_DIR$/web/modules/custom/opdavies_blog/tests/src" isTestSource="true" packagePrefix="Drupal\Tests\opdavies_blog" /> <sourceFolder url="file://$MODULE_DIR$/web/modules/custom/opdavies_blog/tests/src" isTestSource="true" packagePrefix="Drupal\Tests\opdavies_blog" />
<excludeFolder url="file://$MODULE_DIR$/vendor/asm89/stack-cors" /> <excludeFolder url="file://$MODULE_DIR$/vendor/asm89/stack-cors" />
<excludeFolder url="file://$MODULE_DIR$/vendor/beberlei/assert" />
<excludeFolder url="file://$MODULE_DIR$/vendor/behat/mink" /> <excludeFolder url="file://$MODULE_DIR$/vendor/behat/mink" />
<excludeFolder url="file://$MODULE_DIR$/vendor/behat/mink-browserkit-driver" /> <excludeFolder url="file://$MODULE_DIR$/vendor/behat/mink-browserkit-driver" />
<excludeFolder url="file://$MODULE_DIR$/vendor/behat/mink-goutte-driver" /> <excludeFolder url="file://$MODULE_DIR$/vendor/behat/mink-goutte-driver" />

View file

@ -141,6 +141,12 @@
<path value="$PROJECT_DIR$/vendor/cweagans/composer-patches" /> <path value="$PROJECT_DIR$/vendor/cweagans/composer-patches" />
<path value="$PROJECT_DIR$/vendor/laminas/laminas-zendframework-bridge" /> <path value="$PROJECT_DIR$/vendor/laminas/laminas-zendframework-bridge" />
<path value="$PROJECT_DIR$/vendor/laminas/laminas-stdlib" /> <path value="$PROJECT_DIR$/vendor/laminas/laminas-stdlib" />
<path value="$PROJECT_DIR$/vendor/symfony/polyfill-intl-idn" />
<path value="$PROJECT_DIR$/vendor/symfony/polyfill-php80" />
<path value="$PROJECT_DIR$/vendor/laminas/laminas-feed" />
<path value="$PROJECT_DIR$/vendor/laminas/laminas-diactoros" />
<path value="$PROJECT_DIR$/vendor/laminas/laminas-escaper" />
<path value="$PROJECT_DIR$/vendor/beberlei/assert" />
</include_path> </include_path>
</component> </component>
<component name="PhpProjectSharedConfiguration" php_language_level="7.4" /> <component name="PhpProjectSharedConfiguration" php_language_level="7.4" />

View file

@ -15,6 +15,7 @@
} }
], ],
"require": { "require": {
"beberlei/assert": "^3.2",
"composer/installers": "^1.2", "composer/installers": "^1.2",
"cweagans/composer-patches": "^1.6", "cweagans/composer-patches": "^1.6",
"drupal/admin_toolbar": "^2.0", "drupal/admin_toolbar": "^2.0",

65
composer.lock generated
View file

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically" "This file is @generated automatically"
], ],
"content-hash": "4026a4ca20f7aa831743f07d0d3cc8d7", "content-hash": "3aa40373d1c7d531970ee2137e660534",
"packages": [ "packages": [
{ {
"name": "asm89/stack-cors", "name": "asm89/stack-cors",
@ -58,6 +58,68 @@
], ],
"time": "2019-12-24T22:41:47+00:00" "time": "2019-12-24T22:41:47+00:00"
}, },
{
"name": "beberlei/assert",
"version": "v3.2.7",
"source": {
"type": "git",
"url": "https://github.com/beberlei/assert.git",
"reference": "d63a6943fc4fd1a2aedb65994e3548715105abcf"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/beberlei/assert/zipball/d63a6943fc4fd1a2aedb65994e3548715105abcf",
"reference": "d63a6943fc4fd1a2aedb65994e3548715105abcf",
"shasum": ""
},
"require": {
"ext-ctype": "*",
"ext-json": "*",
"ext-mbstring": "*",
"ext-simplexml": "*",
"php": "^7"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "*",
"phpstan/phpstan-shim": "*",
"phpunit/phpunit": ">=6.0.0 <8"
},
"suggest": {
"ext-intl": "Needed to allow Assertion::count(), Assertion::isCountable(), Assertion::minCount(), and Assertion::maxCount() to operate on ResourceBundles"
},
"type": "library",
"autoload": {
"psr-4": {
"Assert\\": "lib/Assert"
},
"files": [
"lib/Assert/functions.php"
]
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"BSD-2-Clause"
],
"authors": [
{
"name": "Benjamin Eberlei",
"email": "kontakt@beberlei.de",
"role": "Lead Developer"
},
{
"name": "Richard Quadling",
"email": "rquadling@gmail.com",
"role": "Collaborator"
}
],
"description": "Thin assertion library for input validation in business models.",
"keywords": [
"assert",
"assertion",
"validation"
],
"time": "2019-12-19T17:51:41+00:00"
},
{ {
"name": "chi-teck/drupal-code-generator", "name": "chi-teck/drupal-code-generator",
"version": "1.32.0", "version": "1.32.0",
@ -9881,6 +9943,7 @@
"keywords": [ "keywords": [
"tokenizer" "tokenizer"
], ],
"abandoned": true,
"time": "2019-09-17T06:23:10+00:00" "time": "2019-09-17T06:23:10+00:00"
}, },
{ {

View file

@ -0,0 +1,61 @@
<?php
declare(strict_types=1);
namespace Drupal\opdavies_blog_test\Factory;
use Assert\Assert;
use Drupal\node\Entity\Node;
use Drupal\opdavies_blog\Entity\Node\Post;
use Drupal\taxonomy\Entity\Term;
use Drupal\taxonomy\TermInterface;
use Illuminate\Support\Collection;
final class PostFactory {
private Collection $tags;
private string $title = 'This is a test blog post';
public function __construct() {
$this->tags = new Collection();
}
public function create(array $overrides = []): Post {
$this->tags->each(function (TermInterface $tag): void {
Assert::that($tag->bundle())->same('tags');
});
$values = [
'field_tags' => $this->tags->toArray(),
'title' => $this->title,
'type' => 'post',
];
/** @var Post $post */
$post = Node::create($values + $overrides);
return $post;
}
public function setTitle(string $title): self {
Assert::that($title)->notEmpty();
$this->title = $title;
return $this;
}
public function withTags(array $tags): self {
foreach ($tags as $tag) {
Assert::that($tag)->notEmpty()->string();
$this->tags->push(
Term::create(['vid' => 'tags', 'name' => $tag])
);
}
return $this;
}
}

View file

@ -6,9 +6,8 @@ namespace Drupal\Tests\custom\Kernel\Entity\Node;
use Drupal\KernelTests\Core\Entity\EntityKernelTestBase; use Drupal\KernelTests\Core\Entity\EntityKernelTestBase;
use Drupal\node\Entity\Node; use Drupal\node\Entity\Node;
use Drupal\node\NodeInterface;
use Drupal\opdavies_blog\Entity\Node\Post; use Drupal\opdavies_blog\Entity\Node\Post;
use Drupal\taxonomy\Entity\Term; use Drupal\opdavies_blog_test\Factory\PostFactory;
final class PostTest extends EntityKernelTestBase { final class PostTest extends EntityKernelTestBase {
@ -28,32 +27,24 @@ final class PostTest extends EntityKernelTestBase {
/** @test */ /** @test */
public function it_can_determine_if_a_post_contains_a_tweet(): void { public function it_can_determine_if_a_post_contains_a_tweet(): void {
/** @var Post $post */ $post = (new PostFactory())->create();
$post = Node::create(['type' => 'post']); $post->save();
$this->assertFalse($post->hasTweet()); $this->assertFalse($post->hasTweet());
/** @var Post $post */ $post = (new PostFactory())->create(['field_has_tweet' => TRUE]);
$post = Node::create([ $post->save();
'field_has_tweet' => TRUE,
'type' => 'post',
]);
$this->assertTrue($post->hasTweet()); $this->assertTrue($post->hasTweet());
} }
/** @test */ /** @test */
public function it_converts_a_post_to_a_tweet(): void { public function it_converts_a_post_to_a_tweet(): void {
/** @var Post $post */ $post = (new PostFactory())
$post = Node::create([ ->setTitle('Creating a custom PHPUnit command for DDEV')
'field_tags' => [ ->withTags(['Automated testing', 'DDEV', 'Drupal', 'PHP'])
Term::create(['vid' => 'tags', 'name' => 'Automated testing']), ->create();
Term::create(['vid' => 'tags', 'name' => 'DDEV']),
Term::create(['vid' => 'tags', 'name' => 'Drupal']),
Term::create(['vid' => 'tags', 'name' => 'PHP']),
],
'status' => NodeInterface::PUBLISHED,
'title' => 'Creating a custom PHPUnit command for DDEV',
'type' => 'post',
]);
$post->save(); $post->save();
$expected = <<<EOF $expected = <<<EOF
@ -69,17 +60,11 @@ final class PostTest extends EntityKernelTestBase {
/** @test */ /** @test */
public function certain_terms_are_not_added_as_hashtags(): void { public function certain_terms_are_not_added_as_hashtags(): void {
/** @var Post $post */ $post = (new PostFactory())
$post = Node::create([ ->setTitle('Drupal Planet should not be added as a hashtag')
'field_tags' => [ ->withTags(['Drupal', 'Drupal Planet', 'PHP'])
Term::create(['vid' => 'tags', 'name' => 'Drupal']), ->create();
Term::create(['vid' => 'tags', 'name' => 'Drupal Planet']),
Term::create(['vid' => 'tags', 'name' => 'PHP']),
],
'status' => NodeInterface::PUBLISHED,
'title' => 'Drupal Planet should not be added as a hashtag',
'type' => 'post',
]);
$post->save(); $post->save();
$expected = <<<EOF $expected = <<<EOF