-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathEloquentPostRepositoryTest.php
119 lines (97 loc) · 3.42 KB
/
EloquentPostRepositoryTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
<?php
namespace Modules\Blog\Tests;
use Illuminate\Support\Facades\Event;
use Modules\Blog\Entities\Status;
use Modules\Blog\Events\PostIsCreating;
use Modules\Blog\Events\PostIsUpdating;
use Modules\Blog\Events\PostWasCreated;
use Modules\Blog\Events\PostWasUpdated;
class EloquentPostRepositoryTest extends BaseBlogTestCase
{
/** @test */
public function it_should_only_returns_translated_posts()
{
// Prepare
$this->createBlogPost();
$this->createBlogPost();
$data = [
'en' => [
'title' => 'One language title',
'slug' => 'slug',
'content' => 'lorem ipsum',
],
'category_id' => 1,
'status' => Status::PUBLISHED,
'tags' => [],
];
$this->post->create($data);
// Assert
$englishPosts = $this->post->allTranslatedIn('en');
$this->assertEquals(3, $englishPosts->count());
$frenchPosts = $this->post->allTranslatedIn('fr');
$this->assertEquals(2, $frenchPosts->count());
}
/** @test */
public function it_triggers_event_when_post_was_created()
{
Event::fake();
$post = $this->createBlogPost();
Event::assertDispatched(PostWasCreated::class, function ($e) use ($post) {
return $e->post->translate('en')->title === $post->translate('en')->title;
});
}
/** @test */
public function it_triggers_event_when_post_is_creating()
{
Event::fake();
$post = $this->createBlogPost();
Event::assertDispatched(PostIsCreating::class, function ($e) use ($post) {
return $e->getAttribute('en.title') === $post->translate('en')->title;
});
}
/** @test */
public function it_can_change_data_when_it_is_creating_event()
{
Event::listen(PostIsCreating::class, function (PostIsCreating $event) {
$event->setAttributes(['en' => ['title' => 'awesome title']]);
});
$post = $this->createBlogPost();
$this->assertEquals('awesome title', $post->translate('en')->title);
}
/** @test */
public function it_triggers_event_when_post_was_updated()
{
Event::fake();
$post = $this->createBlogPost();
$this->post->update($post, ['tags' => []]);
Event::assertDispatched(PostWasUpdated::class, function ($e) use ($post) {
return $e->post->translate('en')->title === $post->translate('en')->title;
});
}
/** @test */
public function it_triggers_event_when_post_is_updating()
{
Event::fake();
$post = $this->createBlogPost();
$this->post->update($post, ['tags' => []]);
Event::assertDispatched(PostIsUpdating::class, function ($e) use ($post) {
return $e->getPost()->translate('en')->title === $post->translate('en')->title;
});
}
/** @test */
public function it_can_change_data_when_it_is_updating_event()
{
Event::listen(PostIsUpdating::class, function (PostIsUpdating $event) {
$event->setAttributes(['en' => ['title' => 'awesome title']]);
});
$post = $this->createBlogPost();
$this->post->update($post, [
'tags' => [],
'en' => [
'title' =>
'not awesome title',
],
]);
$this->assertEquals('awesome title', $post->translate('en')->title);
}
}