@@ -18,30 +18,46 @@ public function it_generates_a_unique_slug()
18
18
$ title = 'Existing Title ' ;
19
19
20
20
// Create the first entry in test_models with the slug
21
- $ model1 = TestModel::create (['title ' => $ title, ' slug ' => Str:: slug ( $ title ) ]);
21
+ $ model1 = TestModel::create (['title ' => $ title ]);
22
22
$ this ->assertEquals ('existing-title ' , $ model1 ->slug );
23
23
24
- // Create another model with the same title
25
- $ model2 = TestModel::create (['title ' => $ title , 'slug ' => Str::slug ($ title )]);
24
+ // Create another model with the same title;
25
+ // let the SlugGenerator trait handle the slug creation
26
+ $ model2 = TestModel::create (['title ' => $ title ]);
26
27
$ this ->assertEquals ('existing-title-1 ' , $ model2 ->slug );
27
28
// Ensure that the two models have different slugs
28
29
$ this ->assertNotEquals ($ model1 ->slug , $ model2 ->slug );
29
- }
30
30
31
+ $ model3 = TestModel::create (['title ' => 'Groupable Title ' , 'groupable_field ' => 1 ]);
32
+ $ this ->assertEquals ('groupable-title ' , $ model3 ->slug );
33
+
34
+ // Create another model with the same title and groupable_field;
35
+ // let the SlugGenerator trait handle the slug creation
36
+ $ model4 = TestModel::create (['title ' => $ title , 'groupable_field ' => 1 ]);
37
+ $ this ->assertEquals ('existing-title ' , $ model4 ->slug );
38
+
39
+ $ model5 = TestModel::create (['title ' => $ title ]);
40
+ $ this ->assertEquals ('existing-title-1 ' , $ model5 ->slug );
41
+
42
+
43
+ $ model6 = TestModel::create (['title ' => 'Groupable Title ' , 'groupable_field ' => 1 ]);
44
+ $ this ->assertEquals ('groupable-title-1 ' , $ model6 ->slug );
45
+ }
31
46
protected function getEnvironmentSetUp ($ app )
32
47
{
33
48
$ app ['config ' ]->set ('database.default ' , 'sqlite ' );
34
49
$ app ['config ' ]->set ('database.connections.sqlite ' , [
35
50
'driver ' => 'sqlite ' ,
36
51
'database ' => ':memory: ' ,
37
- 'prefix ' => '' ,
52
+ 'prefix ' => 'cw-slug-generator-test- ' ,
38
53
]);
39
54
40
55
// Database setup
41
56
Schema::create ('test_models ' , function (Blueprint $ table ) {
42
57
$ table ->increments ('id ' );
43
58
$ table ->string ('title ' );
44
59
$ table ->string ('slug ' );
60
+ $ table ->string ('groupable_field ' )->nullable ();
45
61
});
46
62
}
47
63
}
@@ -51,12 +67,20 @@ class TestModel extends \Illuminate\Database\Eloquent\Model
51
67
52
68
protected $ table = 'test_models ' ;
53
69
54
- protected $ guarded = [];
70
+ protected $ fillable = [
71
+ 'title ' ,
72
+ 'slug ' ,
73
+ 'groupable_field ' ,
74
+ ];
55
75
56
76
public $ timestamps = false ;
57
77
58
78
public static function getSluggableField ()
59
79
{
60
80
return 'title ' ;
61
81
}
82
+ public static function getGroupableField ()
83
+ {
84
+ return 'groupable_field ' ;
85
+ }
62
86
}
0 commit comments