|
8 | 8 | use Contexts\ArticlePublishing\Domain\Models\ArticleCategoryCollection; |
9 | 9 | use Contexts\ArticlePublishing\Domain\Models\ArticleId; |
10 | 10 | use Contexts\ArticlePublishing\Domain\Models\ArticleStatus; |
| 11 | +use Contexts\ArticlePublishing\Domain\Models\AuthorId; |
11 | 12 |
|
12 | 13 | beforeEach(function () { |
13 | 14 | $this->categories = new ArticleCategoryCollection( |
|
19 | 20 | }); |
20 | 21 |
|
21 | 22 | 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()); |
23 | 24 | expect($article->getTitle())->toBe('Title'); |
24 | 25 | expect($article->getbody())->toBe('body'); |
25 | 26 | expect($article->getCategories()->getIdsArray())->toBe([1, 2]); |
26 | 27 | expect($article->getStatus()->equals(ArticleStatus::draft()))->toBeTrue(); |
27 | 28 | }); |
28 | 29 |
|
29 | 30 | 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()); |
31 | 32 | expect($article->getTitle())->toBe('Title'); |
32 | 33 | expect($article->getbody())->toBe('body'); |
33 | 34 | expect($article->getCategories()->getIdsArray())->toBe([1, 2]); |
34 | 35 | expect($article->getStatus()->equals(ArticleStatus::published()))->toBeTrue(); |
35 | 36 | }); |
36 | 37 |
|
37 | 38 | 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()); |
39 | 40 | expect($article->getCreatedAt())->toBeInstanceOf(CarbonImmutable::class); |
40 | 41 | }); |
41 | 42 |
|
|
47 | 48 | $createdAt = CarbonImmutable::now()->subDays(5); |
48 | 49 | $updatedAt = CarbonImmutable::now()->subDays(1); |
49 | 50 |
|
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); |
51 | 52 |
|
52 | 53 | expect($article->id)->toEqual($id); |
53 | 54 | expect($article->getTitle())->toBe($title); |
|
59 | 60 | }); |
60 | 61 |
|
61 | 62 | 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()); |
63 | 64 | $article->publish(); |
64 | 65 |
|
65 | 66 | expect($article->getStatus()->equals(ArticleStatus::published()))->toBeTrue(); |
66 | 67 | expect($article->releaseEvents())->toHaveCount(1); |
67 | 68 | }); |
68 | 69 |
|
69 | 70 | 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()); |
71 | 72 | $events = $article->releaseEvents(); |
72 | 73 |
|
73 | 74 | expect($events)->toHaveCount(1); |
74 | 75 | expect($events[0])->toBeInstanceOf(\Contexts\ArticlePublishing\Domain\Events\ArticlePublishedEvent::class); |
75 | 76 | }); |
76 | 77 |
|
77 | 78 | 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()); |
79 | 80 |
|
80 | 81 | // First release should return events |
81 | 82 | $events = $article->releaseEvents(); |
|
87 | 88 | }); |
88 | 89 |
|
89 | 90 | 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); |
92 | 93 |
|
93 | 94 | expect($article->getTitle())->toBe('New Title'); |
94 | 95 | expect($article->getBody())->toBe('Original Body'); |
95 | 96 | expect($article->getStatus()->equals(ArticleStatus::draft()))->toBeTrue(); |
96 | 97 | }); |
97 | 98 |
|
98 | 99 | 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); |
101 | 102 |
|
102 | 103 | expect($article->getTitle())->toBe('Original Title'); |
103 | 104 | expect($article->getBody())->toBe('New Body Content'); |
104 | 105 | expect($article->getStatus()->equals(ArticleStatus::draft()))->toBeTrue(); |
105 | 106 | }); |
106 | 107 |
|
107 | 108 | 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); |
110 | 111 |
|
111 | 112 | expect($article->getTitle())->toBe('Original Title'); |
112 | 113 | expect($article->getBody())->toBe('Original Body'); |
|
115 | 116 | }); |
116 | 117 |
|
117 | 118 | 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()); |
119 | 120 | $newCategories = new ArticleCategoryCollection( |
120 | 121 | [ |
121 | 122 | new ArticleCategory(3, 'Category 3'), |
122 | 123 | new ArticleCategory(4, 'Category 4'), |
123 | 124 | ] |
124 | 125 | ); |
125 | | - $article->revise(null, null, null, $newCategories, null); |
| 126 | + $article->revise(null, null, null, $newCategories, null, null); |
126 | 127 |
|
127 | 128 | expect($article->getTitle())->toBe('Original Title'); |
128 | 129 | expect($article->getBody())->toBe('Original Body'); |
129 | 130 | expect($article->getCategories()->getIdsArray())->toBe([3, 4]); |
130 | 131 | expect($article->getStatus()->equals(ArticleStatus::draft()))->toBeTrue(); |
131 | 132 | }); |
132 | 133 |
|
| 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 | + |
133 | 143 | 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()); |
135 | 145 | $newCategories = new ArticleCategoryCollection( |
136 | 146 | [ |
137 | 147 | new ArticleCategory(3, 'Category 3'), |
138 | 148 | new ArticleCategory(4, 'Category 4'), |
139 | 149 | ] |
140 | 150 | ); |
141 | | - $article->revise('New Title', 'New Body', ArticleStatus::published(), $newCategories); |
| 151 | + $article->revise('New Title', 'New Body', ArticleStatus::published(), $newCategories, AuthorId::fromInt(1)); |
142 | 152 |
|
143 | 153 | expect($article->getTitle())->toBe('New Title'); |
144 | 154 | expect($article->getBody())->toBe('New Body'); |
145 | 155 | expect($article->getStatus()->equals(ArticleStatus::published()))->toBeTrue(); |
| 156 | + expect($article->getCategories()->getIdsArray())->toBe([3, 4]); |
| 157 | + expect($article->getAuthorId())->toEqual(AuthorId::fromInt(1)); |
146 | 158 | expect($article->releaseEvents())->toHaveCount(1); |
147 | 159 | }); |
148 | 160 |
|
149 | 161 | it('can revise article created_at date', function () { |
150 | 162 | $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); |
152 | 164 |
|
153 | 165 | $newDate = CarbonImmutable::now()->subDays(10); |
154 | | - $article->revise(null, null, null, null, $newDate); |
| 166 | + $article->revise(null, null, null, null, null, $newDate); |
155 | 167 |
|
156 | 168 | expect($article->getCreatedAt())->toEqual($newDate); |
157 | 169 | }); |
158 | 170 |
|
159 | 171 | 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); |
162 | 174 |
|
163 | 175 | expect($article->getStatus()->equals(ArticleStatus::draft()))->toBeTrue(); |
164 | 176 | expect($article->releaseEvents())->toBeEmpty(); |
165 | 177 | }); |
166 | 178 |
|
167 | 179 | 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()); |
169 | 181 | $article->releaseEvents(); // Clear initial events |
170 | 182 |
|
171 | 183 | $article->archive(); |
|
175 | 187 | }); |
176 | 188 |
|
177 | 189 | 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()); |
179 | 191 | $article->archive(); |
180 | 192 | $article->releaseEvents(); // Clear initial events |
181 | 193 |
|
|
0 commit comments