@@ -18,30 +18,46 @@ public function it_generates_a_unique_slug()
1818 $ title = 'Existing Title ' ;
1919
2020 // 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 ]);
2222 $ this ->assertEquals ('existing-title ' , $ model1 ->slug );
2323
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 ]);
2627 $ this ->assertEquals ('existing-title-1 ' , $ model2 ->slug );
2728 // Ensure that the two models have different slugs
2829 $ this ->assertNotEquals ($ model1 ->slug , $ model2 ->slug );
29- }
3030
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+ }
3146 protected function getEnvironmentSetUp ($ app )
3247 {
3348 $ app ['config ' ]->set ('database.default ' , 'sqlite ' );
3449 $ app ['config ' ]->set ('database.connections.sqlite ' , [
3550 'driver ' => 'sqlite ' ,
3651 'database ' => ':memory: ' ,
37- 'prefix ' => '' ,
52+ 'prefix ' => 'cw-slug-generator-test- ' ,
3853 ]);
3954
4055 // Database setup
4156 Schema::create ('test_models ' , function (Blueprint $ table ) {
4257 $ table ->increments ('id ' );
4358 $ table ->string ('title ' );
4459 $ table ->string ('slug ' );
60+ $ table ->string ('groupable_field ' )->nullable ();
4561 });
4662 }
4763}
@@ -51,12 +67,20 @@ class TestModel extends \Illuminate\Database\Eloquent\Model
5167
5268 protected $ table = 'test_models ' ;
5369
54- protected $ guarded = [];
70+ protected $ fillable = [
71+ 'title ' ,
72+ 'slug ' ,
73+ 'groupable_field ' ,
74+ ];
5575
5676 public $ timestamps = false ;
5777
5878 public static function getSluggableField ()
5979 {
6080 return 'title ' ;
6181 }
82+ public static function getGroupableField ()
83+ {
84+ return 'groupable_field ' ;
85+ }
6286}
0 commit comments