Skip to content

Commit 6d9bf2a

Browse files
committed
feat: add AuthorId model and integrate it into Article entity
1 parent 9582c8f commit 6d9bf2a

File tree

3 files changed

+58
-26
lines changed

3 files changed

+58
-26
lines changed

contexts/ArticlePublishing/Domain/Models/Article.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ private function __construct(
1616
private string $body,
1717
private ArticleStatus $status,
1818
private ArticleCategoryCollection $categories,
19+
private AuthorId $authorId,
1920
private ?CarbonImmutable $created_at = null,
2021
private ?CarbonImmutable $updated_at = null
2122
) {
@@ -27,6 +28,7 @@ public function revise(
2728
?string $newBody,
2829
?ArticleStatus $newStatus,
2930
?ArticleCategoryCollection $categories,
31+
?AuthorId $authorId,
3032
?CarbonImmutable $newCreatedAt = null,
3133
) {
3234
$this->title = $newTitle ?? $this->title;
@@ -35,6 +37,7 @@ public function revise(
3537
$this->transitionStatus($newStatus);
3638
}
3739
$this->categories = $categories ?? $this->categories;
40+
$this->authorId = $authorId ?? $this->authorId;
3841
$this->created_at = $newCreatedAt ?? $this->created_at;
3942
}
4043

@@ -44,11 +47,12 @@ public static function reconstitute(
4447
string $body,
4548
ArticleStatus $status,
4649
ArticleCategoryCollection $categories,
50+
AuthorId $authorId,
4751
?CarbonImmutable $created_at = null,
4852
?CarbonImmutable $updated_at = null,
4953
array $events = []
5054
): self {
51-
$article = new self($id, $title, $body, $status, $categories, $created_at, $updated_at);
55+
$article = new self($id, $title, $body, $status, $categories, $authorId, $created_at, $updated_at);
5256
foreach ($events as $event) {
5357
$article->recordEvent($event);
5458
}
@@ -61,21 +65,23 @@ public static function createDraft(
6165
string $title,
6266
string $body,
6367
ArticleCategoryCollection $categories,
68+
AuthorId $authorId,
6469
?CarbonImmutable $created_at = null,
6570
?CarbonImmutable $updated_at = null
6671
): self {
67-
return new self($id, $title, $body, ArticleStatus::draft(), $categories, $created_at, $updated_at);
72+
return new self($id, $title, $body, ArticleStatus::draft(), $categories, $authorId, $created_at, $updated_at);
6873
}
6974

7075
public static function createPublished(
7176
ArticleId $id,
7277
string $title,
7378
string $body,
7479
ArticleCategoryCollection $categories,
80+
AuthorId $authorId,
7581
?CarbonImmutable $created_at = null,
7682
?CarbonImmutable $updated_at = null
7783
): self {
78-
$article = new self($id, $title, $body, ArticleStatus::published(), $categories, $created_at, $updated_at);
84+
$article = new self($id, $title, $body, ArticleStatus::published(), $categories, $authorId, $created_at, $updated_at);
7985
$article->recordEvent(new ArticlePublishedEvent($article->id));
8086

8187
return $article;
@@ -131,6 +137,11 @@ public function getCategories(): ArticleCategoryCollection
131137
return $this->categories;
132138
}
133139

140+
public function getAuthorId(): AuthorId
141+
{
142+
return $this->authorId;
143+
}
144+
134145
public function getCreatedAt(): CarbonImmutable
135146
{
136147
return $this->created_at;
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Contexts\ArticlePublishing\Domain\Models;
6+
7+
use Contexts\Shared\ValueObjects\IntId;
8+
9+
class AuthorId extends IntId {}

contexts/ArticlePublishing/Tests/Unit/Domain/Models/ArticleTest.php

Lines changed: 35 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Contexts\ArticlePublishing\Domain\Models\ArticleCategoryCollection;
99
use Contexts\ArticlePublishing\Domain\Models\ArticleId;
1010
use Contexts\ArticlePublishing\Domain\Models\ArticleStatus;
11+
use Contexts\ArticlePublishing\Domain\Models\AuthorId;
1112

1213
beforeEach(function () {
1314
$this->categories = new ArticleCategoryCollection(
@@ -19,23 +20,23 @@
1920
});
2021

2122
it('can create draft article with valid data', function () {
22-
$article = Article::createDraft(ArticleId::null(), 'Title', 'body', $this->categories);
23+
$article = Article::createDraft(ArticleId::null(), 'Title', 'body', $this->categories, AuthorId::null());
2324
expect($article->getTitle())->toBe('Title');
2425
expect($article->getbody())->toBe('body');
2526
expect($article->getCategories()->getIdsArray())->toBe([1, 2]);
2627
expect($article->getStatus()->equals(ArticleStatus::draft()))->toBeTrue();
2728
});
2829

2930
it('can create published article with valid data', function () {
30-
$article = Article::createPublished(ArticleId::null(), 'Title', 'body', $this->categories);
31+
$article = Article::createPublished(ArticleId::null(), 'Title', 'body', $this->categories, AuthorId::null());
3132
expect($article->getTitle())->toBe('Title');
3233
expect($article->getbody())->toBe('body');
3334
expect($article->getCategories()->getIdsArray())->toBe([1, 2]);
3435
expect($article->getStatus()->equals(ArticleStatus::published()))->toBeTrue();
3536
});
3637

3738
it('can auto generate created_at date', function () {
38-
$article = Article::createDraft(ArticleId::null(), 'Title', 'body', $this->categories);
39+
$article = Article::createDraft(ArticleId::null(), 'Title', 'body', $this->categories, AuthorId::null());
3940
expect($article->getCreatedAt())->toBeInstanceOf(CarbonImmutable::class);
4041
});
4142

@@ -47,7 +48,7 @@
4748
$createdAt = CarbonImmutable::now()->subDays(5);
4849
$updatedAt = CarbonImmutable::now()->subDays(1);
4950

50-
$article = Article::reconstitute($id, $title, $body, $status, $this->categories, $createdAt, $updatedAt);
51+
$article = Article::reconstitute($id, $title, $body, $status, $this->categories, AuthorId::null(), $createdAt, $updatedAt);
5152

5253
expect($article->id)->toEqual($id);
5354
expect($article->getTitle())->toBe($title);
@@ -59,23 +60,23 @@
5960
});
6061

6162
it('can publish a draft article', function () {
62-
$article = Article::createDraft(ArticleId::fromInt(1), 'Draft Title', 'Draft content', $this->categories);
63+
$article = Article::createDraft(ArticleId::fromInt(1), 'Draft Title', 'Draft content', $this->categories, AuthorId::null());
6364
$article->publish();
6465

6566
expect($article->getStatus()->equals(ArticleStatus::published()))->toBeTrue();
6667
expect($article->releaseEvents())->toHaveCount(1);
6768
});
6869

6970
it('should record domain events when article is published', function () {
70-
$article = Article::createPublished(ArticleId::fromInt(1), 'Title', 'body', $this->categories);
71+
$article = Article::createPublished(ArticleId::fromInt(1), 'Title', 'body', $this->categories, AuthorId::null());
7172
$events = $article->releaseEvents();
7273

7374
expect($events)->toHaveCount(1);
7475
expect($events[0])->toBeInstanceOf(\Contexts\ArticlePublishing\Domain\Events\ArticlePublishedEvent::class);
7576
});
7677

7778
it('can release events and clear them from the article', function () {
78-
$article = Article::createPublished(ArticleId::fromInt(1), 'Title', 'body', $this->categories);
79+
$article = Article::createPublished(ArticleId::fromInt(1), 'Title', 'body', $this->categories, AuthorId::null());
7980

8081
// First release should return events
8182
$events = $article->releaseEvents();
@@ -87,26 +88,26 @@
8788
});
8889

8990
it('can revise an article title', function () {
90-
$article = Article::createDraft(ArticleId::fromInt(1), 'Original Title', 'Original Body', $this->categories);
91-
$article->revise('New Title', null, null, null);
91+
$article = Article::createDraft(ArticleId::fromInt(1), 'Original Title', 'Original Body', $this->categories, AuthorId::null());
92+
$article->revise('New Title', null, null, null, null);
9293

9394
expect($article->getTitle())->toBe('New Title');
9495
expect($article->getBody())->toBe('Original Body');
9596
expect($article->getStatus()->equals(ArticleStatus::draft()))->toBeTrue();
9697
});
9798

9899
it('can revise an article body', function () {
99-
$article = Article::createDraft(ArticleId::fromInt(1), 'Original Title', 'Original Body', $this->categories);
100-
$article->revise(null, 'New Body Content', null, null);
100+
$article = Article::createDraft(ArticleId::fromInt(1), 'Original Title', 'Original Body', $this->categories, AuthorId::null());
101+
$article->revise(null, 'New Body Content', null, null, null);
101102

102103
expect($article->getTitle())->toBe('Original Title');
103104
expect($article->getBody())->toBe('New Body Content');
104105
expect($article->getStatus()->equals(ArticleStatus::draft()))->toBeTrue();
105106
});
106107

107108
it('can revise an article status', function () {
108-
$article = Article::createDraft(ArticleId::fromInt(1), 'Original Title', 'Original Body', $this->categories);
109-
$article->revise(null, null, ArticleStatus::published(), null);
109+
$article = Article::createDraft(ArticleId::fromInt(1), 'Original Title', 'Original Body', $this->categories, AuthorId::null());
110+
$article->revise(null, null, ArticleStatus::published(), null, null);
110111

111112
expect($article->getTitle())->toBe('Original Title');
112113
expect($article->getBody())->toBe('Original Body');
@@ -115,57 +116,68 @@
115116
});
116117

117118
it('can revise an article categories', function () {
118-
$article = Article::createDraft(ArticleId::fromInt(1), 'Original Title', 'Original Body', $this->categories);
119+
$article = Article::createDraft(ArticleId::fromInt(1), 'Original Title', 'Original Body', $this->categories, AuthorId::null());
119120
$newCategories = new ArticleCategoryCollection(
120121
[
121122
new ArticleCategory(3, 'Category 3'),
122123
new ArticleCategory(4, 'Category 4'),
123124
]
124125
);
125-
$article->revise(null, null, null, $newCategories, null);
126+
$article->revise(null, null, null, $newCategories, null, null);
126127

127128
expect($article->getTitle())->toBe('Original Title');
128129
expect($article->getBody())->toBe('Original Body');
129130
expect($article->getCategories()->getIdsArray())->toBe([3, 4]);
130131
expect($article->getStatus()->equals(ArticleStatus::draft()))->toBeTrue();
131132
});
132133

134+
it('can revise an article author', function () {
135+
$article = Article::createDraft(ArticleId::fromInt(1), 'Original Title', 'Original Body', $this->categories, AuthorId::null());
136+
$article->revise(null, null, null, null, AuthorId::fromInt(1));
137+
138+
expect($article->getTitle())->toBe('Original Title');
139+
expect($article->getBody())->toBe('Original Body');
140+
expect($article->getAuthorId())->toEqual(AuthorId::fromInt(1));
141+
});
142+
133143
it('can revise multiple article properties at once', function () {
134-
$article = Article::createDraft(ArticleId::fromInt(1), 'Original Title', 'Original Body', $this->categories);
144+
$article = Article::createDraft(ArticleId::fromInt(1), 'Original Title', 'Original Body', $this->categories, AuthorId::null());
135145
$newCategories = new ArticleCategoryCollection(
136146
[
137147
new ArticleCategory(3, 'Category 3'),
138148
new ArticleCategory(4, 'Category 4'),
139149
]
140150
);
141-
$article->revise('New Title', 'New Body', ArticleStatus::published(), $newCategories);
151+
$article->revise('New Title', 'New Body', ArticleStatus::published(), $newCategories, AuthorId::fromInt(1));
142152

143153
expect($article->getTitle())->toBe('New Title');
144154
expect($article->getBody())->toBe('New Body');
145155
expect($article->getStatus()->equals(ArticleStatus::published()))->toBeTrue();
156+
expect($article->getCategories()->getIdsArray())->toBe([3, 4]);
157+
expect($article->getAuthorId())->toEqual(AuthorId::fromInt(1));
146158
expect($article->releaseEvents())->toHaveCount(1);
147159
});
148160

149161
it('can revise article created_at date', function () {
150162
$originalDate = CarbonImmutable::now()->subDays(5);
151-
$article = Article::createDraft(ArticleId::fromInt(1), 'Original Title', 'Original Body', $this->categories, $originalDate);
163+
$article = Article::createDraft(ArticleId::fromInt(1), 'Original Title', 'Original Body', $this->categories, AuthorId::null(), $originalDate);
152164

153165
$newDate = CarbonImmutable::now()->subDays(10);
154-
$article->revise(null, null, null, null, $newDate);
166+
$article->revise(null, null, null, null, null, $newDate);
155167

156168
expect($article->getCreatedAt())->toEqual($newDate);
157169
});
158170

159171
it('does not trigger status transition when same status provided', function () {
160-
$article = Article::createDraft(ArticleId::fromInt(1), 'Title', 'Body', $this->categories);
161-
$article->revise(null, null, ArticleStatus::draft(), null);
172+
$article = Article::createDraft(ArticleId::fromInt(1), 'Title', 'Body', $this->categories, AuthorId::null());
173+
$article->revise(null, null, ArticleStatus::draft(), null, null);
162174

163175
expect($article->getStatus()->equals(ArticleStatus::draft()))->toBeTrue();
164176
expect($article->releaseEvents())->toBeEmpty();
165177
});
166178

167179
it('can archive an article', function () {
168-
$article = Article::createPublished(ArticleId::fromInt(1), 'Title', 'Body', $this->categories);
180+
$article = Article::createPublished(ArticleId::fromInt(1), 'Title', 'Body', $this->categories, AuthorId::null());
169181
$article->releaseEvents(); // Clear initial events
170182

171183
$article->archive();
@@ -175,7 +187,7 @@
175187
});
176188

177189
it('can delete an article', function () {
178-
$article = Article::createPublished(ArticleId::fromInt(1), 'Title', 'Body', $this->categories);
190+
$article = Article::createPublished(ArticleId::fromInt(1), 'Title', 'Body', $this->categories, AuthorId::null());
179191
$article->archive();
180192
$article->releaseEvents(); // Clear initial events
181193

0 commit comments

Comments
 (0)