-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
RevisionTest.php
178 lines (131 loc) · 4.72 KB
/
RevisionTest.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
<?php
namespace Stevebauman\Revision\Tests;
use Stevebauman\Revision\Tests\Stubs\Models\Revision;
use Stevebauman\Revision\Tests\Stubs\Models\User;
use Stevebauman\Revision\Tests\Stubs\Models\Post;
class RevisionTest extends FunctionalTestCase
{
public $user;
public function setUp()
{
parent::setUp();
$user = new User();
$user->username = 'User One';
$user->save();
$this->user = $user;
}
public function testCreate()
{
$post = new Post();
$post->title = 'Test';
$post->description = 'Testing';
$post->save();
$revisions = Revision::all();
$this->assertEquals(5, $revisions->count());
$idRevision = $revisions->get(0);
$this->assertEquals(Post::class, $idRevision->revisionable_type);
$this->assertEquals(1, $idRevision->revisionable_id);
$this->assertEquals('id', $idRevision->key);
$this->assertNull($idRevision->old_value);
// Test Revision User
$this->assertEquals(1, $idRevision->user_id);
$this->assertInstanceOf(User::class, $idRevision->getUserResponsible());
}
public function testModify()
{
$post = new Post();
$post->title = 'Test';
$post->description = 'Testing';
$post->save();
$post->title = 'Modified';
$post->save();
$revisions = Revision::all();
$this->assertEquals(6, $revisions->count());
$titleRevision = $revisions->get(5);
$this->assertEquals('title', $titleRevision->key);
$this->assertEquals('Test', $titleRevision->old_value);
$this->assertEquals('Modified', $titleRevision->new_value);
}
public function testOnlyColumns()
{
$post = new Post();
$post->setRevisionColumns(['title']);
$post->title = 'Testing';
$post->description = 'Testing';
$post->save();
$revisions = Revision::all();
$this->assertEquals(1, $revisions->count());
$this->assertEquals('title', $revisions->get(0)->key);
$this->assertEquals('Testing', $revisions->get(0)->new_value);
$this->assertNull($revisions->get(0)->old_value);
}
public function testAvoidColumns()
{
$post = new Post();
$post->setRevisionColumnsToAvoid(['title']);
$post->title = 'Testing';
$post->description = 'Testing';
$post->save();
$revisions = Revision::all();
$this->assertEquals(4, $revisions->count());
$this->assertEquals('id', $revisions->get(0)->key);
$this->assertEquals('description', $revisions->get(1)->key);
$this->assertEquals('created_at', $revisions->get(2)->key);
$this->assertEquals('updated_at', $revisions->get(3)->key);
}
public function testColumnFormatting()
{
$post = new Post();
$post->title = 'Testing';
$post->description = 'Testing';
$post->save();
$revisions = $post->revisions;
$this->assertEquals('ID', $revisions->get(0)->getColumnName());
$this->assertEquals('Post Title', $revisions->get(1)->getColumnName());
$this->assertEquals('Post Description', $revisions->get(2)->getColumnName());
$this->assertEquals('Created', $revisions->get(3)->getColumnName());
$this->assertEquals('Updated', $revisions->get(4)->getColumnName());
}
public function testColumnMeans()
{
$post = new Post();
$post->user_id = $this->user->id;
$post->title = 'Testing';
$post->description = 'Testing';
$post->save();
$revisions = $post->revisions;
$this->assertEquals('User One', $revisions->get(0)->getNewValue());
}
public function testGetRevisionColumnsFormatted()
{
$post = new Post();
$columns = [
'id' => 'ID',
'created_at' => 'Created',
'updated_at' => 'Updated',
'title' => 'Post Title',
'description' => 'Post Description',
];
$this->assertEquals($columns, $post->getRevisionColumnsFormatted());
}
public function testGetRevisionColumnsMean()
{
$post = new Post();
$means = [
'user_id' => 'user.username'
];
$this->assertEquals($means, $post->getRevisionColumnsMean());
}
public function testGetUserResponsible()
{
$post = new Post();
$post->user_id = $this->user->id;
$post->title = 'Testing';
$post->description = 'Testing';
$post->save();
$revisions = $post->revisions;
$user = $revisions->get(0)->getUserResponsible();
$this->assertEquals(1, $user->id);
$this->assertEquals('User One', $user->username);
}
}