Skip to content

Commit 8ea1627

Browse files
committed
Add some more tests for scheduling
1 parent a53268a commit 8ea1627

File tree

4 files changed

+47
-18
lines changed

4 files changed

+47
-18
lines changed

database/factories/PostFactory.php

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,31 +11,27 @@ class PostFactory extends \Illuminate\Database\Eloquent\Factories\Factory
1111
/**
1212
* @inheritDoc
1313
*/
14-
public function definition()
14+
public function definition(): array
1515
{
1616
return [
1717
'title' => $this->faker->sentence,
1818
'is_current' => true,
1919
];
2020
}
2121

22-
public function draft()
22+
public function draft(): PostFactory
2323
{
24-
return $this->state(function () {
25-
return [
26-
'published_at' => null,
27-
'is_published' => false,
28-
];
29-
});
24+
return $this->state(fn (): array => [
25+
'published_at' => null,
26+
'is_published' => false,
27+
]);
3028
}
3129

32-
public function published()
30+
public function published(): PostFactory
3331
{
34-
return $this->state(function () {
35-
return [
36-
'published_at' => now()->toDateTimeString(),
37-
'is_published' => true,
38-
];
39-
});
32+
return $this->state(fn (): array => [
33+
'published_at' => now()->toDateTimeString(),
34+
'is_published' => true,
35+
]);
4036
}
4137
}

src/Commands/PublishScheduledDrafts.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ public function handle(): int
1717
{
1818
$class = $this->argument('model');
1919

20-
if (! class_exists($class) || ! in_array(HasDrafts::class, class_uses_recursive($class), strict: true)) {
21-
throw new InvalidArgumentException("The model `{$class}` either doesn't exist or doesn't use the `HasDrafts` trait.");
20+
if (! class_exists($class) || ! in_array(Draftable::class, class_implements($class), strict: true)) {
21+
throw new InvalidArgumentException("The model `{$class}` either doesn't exist or doesn't implement `Draftable`.");
2222
}
2323

2424
$model = new $class();

tests/ConfigTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
it('can override columns via class constants', function () {
2626
$post = new class () extends Post {
2727
public const PUBLISHED_AT = 'published_at_override';
28+
public const WILL_PUBLISH_AT = 'will_publish_at_overridee';
2829
public const IS_PUBLISHED = 'is_published_override';
2930
public const IS_CURRENT = 'is_current_override';
3031
public const UUID = 'uuid_override';
@@ -34,6 +35,7 @@
3435

3536
expect($post->getPublishedAtColumn())->toBe($post::PUBLISHED_AT)
3637
->and($post->getQualifiedPublishedAtColumn())->toBe($post->qualifyColumn($post::PUBLISHED_AT))
38+
->and($post->getWillPublishAtColumn())->toBe($post::WILL_PUBLISH_AT)
3739
->and($post->getIsPublishedColumn())->toBe($post::IS_PUBLISHED)
3840
->and($post->getIsCurrentColumn())->toBe($post::IS_CURRENT)
3941
->and($post->getUuidColumn())->toBe($post::UUID)

tests/SchedulingTest.php

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
use Illuminate\Support\Facades\Artisan;
4+
use Oddvalue\LaravelDrafts\Tests\Post;
35
use Oddvalue\LaravelDrafts\Tests\SchedulingPost;
46

57
use function Spatie\PestPluginTestTime\testTime;
@@ -24,11 +26,40 @@
2426

2527
testTime()->addMonth()->freeze();
2628

27-
\Illuminate\Support\Facades\Artisan::call('drafts:publish', ['model' => SchedulingPost::class]);
29+
Artisan::call('drafts:publish', ['model' => SchedulingPost::class]);
2830

2931
$this->assertDatabaseHas('posts', [
3032
'title' => 'Hello World',
3133
'published_at' => now()->toDateTimeString(),
3234
'will_publish_at' => null,
3335
]);
3436
});
37+
38+
it('fails when the model doesnt implement the contract', function (): void {
39+
expect(static fn() => Artisan::call('drafts:publish', ['model' => Post::class]))
40+
->toThrow(InvalidArgumentException::class);
41+
});
42+
43+
it('can clear the schedule date on a revision', function (): void {
44+
$willPublishAt = now()->addWeek();
45+
$post = SchedulingPost::factory()
46+
->published()
47+
->has(
48+
SchedulingPost::factory(),
49+
'revisions'
50+
)
51+
->create();
52+
$draft = $post->createDraft(['title' => 'Hello World']);
53+
$draft->schedulePublishing($willPublishAt);
54+
$draft->clearScheduledPublishing()->save();
55+
56+
testTime()->addMonth()->freeze();
57+
58+
Artisan::call('drafts:publish', ['model' => SchedulingPost::class]);
59+
60+
$this->assertDatabaseHas('posts', [
61+
'title' => 'Hello World',
62+
'published_at' => null,
63+
'will_publish_at' => null,
64+
]);
65+
});

0 commit comments

Comments
 (0)