-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathHasHashidTest.php
More file actions
167 lines (120 loc) · 3.54 KB
/
HasHashidTest.php
File metadata and controls
167 lines (120 loc) · 3.54 KB
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
<?php
namespace Mtvs\EloquentHashids\Tests;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Mtvs\EloquentHashids\Tests\Models\Item;
use Vinkla\Hashids\Facades\Hashids;
class HasHashidTest extends TestCase
{
use RefreshDatabase;
/**
* @test
*/
public function it_can_encode_the_model_id_to_its_hashid()
{
$item = factory(Item::class)->create();
$hashid = Hashids::encode($item->getKey());
$this->assertEquals($hashid, $item->hashid());
$this->assertEquals($hashid, $item->hashid);
}
/**
* @test
*/
public function it_can_append_its_hashid_to_its_serialized_version()
{
$item = factory(Item::class)->create();
$hashid = Hashids::encode($item->getKey());
$serialized = json_decode($item->append('hashid')->toJson());
$this->assertEquals($hashid, $serialized->hashid);
}
/**
* @test
*/
public function it_can_enceode_an_arbitrary_id_to_its_hashid()
{
$item = new Item();
$hashid = Hashids::encode(123);
$this->assertEquals($hashid, $item->idToHashid(123));
}
/**
* @test
*/
public function it_can_find_models_by_hashid()
{
$item = factory(Item::class)->create();
$hashid = Hashids::encode($item->getKey());
$found = Item::findByHashid($hashid);
$this->assertNotNull($found);
$this->assertEquals($item->id, $found->id);
}
/**
* @test
*/
public function it_returns_null_when_cannot_find_a_model_with_a_hashid()
{
$hashid = Hashids::encode(1);
$found = Item::findByHashid($hashid);
$this->assertNull($found);
}
/**
* @test
*/
public function it_can_find_a_model_by_its_hashid_or_fail()
{
$item = factory(Item::class)->create();
$hashid = Hashids::encode($item->getKey());
$found = Item::findByHashidOrFail($hashid);
$this->assertEquals($item->id, $found->id);
$item->delete();
$this->expectException(ModelNotFoundException::class);
Item::findByHashidOrFail($hashid);
}
/**
* @test
*/
public function it_can_decode_a_hashid_to_the_id()
{
$item = factory(Item::class)->create();
$hashid = Hashids::encode($item->getKey());
$id = (new Item)->hashidToId($hashid);
$this->assertEquals($item->id, $id);
}
/**
* @test
*/
public function it_can_handle_invalid_hashids_properly()
{
$this->assertNull((new Item)->hashidToId('not a hashid'));
$this->assertNull(Item::findByHashid('not a hashid'));
$this->expectException(ModelNotFoundException::class);
Item::findByHashidOrFail('not a hashid');
}
/**
* @test
*/
public function it_can_find_a_model_by_its_hashid_with_specific_columns()
{
$item = factory(Item::class)->create();
$hashid = Hashids::encode($item->getKey());
$selectedColumns = ['id'];
$found = Item::findByHashid($hashid, $selectedColumns);
$this->assertNotNull($found);
$this->assertEquals($item->id, $found->id);
$this->assertEquals($selectedColumns, array_keys($found->getAttributes()));
}
/**
* @test
*/
public function it_can_find_a_model_by_its_hashid_with_specific_columns_or_fail()
{
$item = factory(Item::class)->create();
$hashid = Hashids::encode($item->getKey());
$selectedColumns = ['id'];
$found = Item::findByHashidOrFail($hashid, $selectedColumns);
$this->assertEquals($item->id, $found->id);
$this->assertEquals($selectedColumns, array_keys($found->getAttributes()));
$item->delete();
$this->expectException(ModelNotFoundException::class);
Item::findByHashidOrFail($hashid);
}
}