22namespace UniSharp \Category \Test ;
33
44use UniSharp \Category \Test \TestCase ;
5- use UniSharp \Category \Models \Category ;
65use UniSharp \Category \Test \TestModel ;
6+ use UniSharp \Category \Models \Category ;
7+ use UniSharp \Category \Services \TagService as UnisharpTagService ;
8+ use Cviebrock \EloquentTaggable \Services \TagService ;
79
810class CategoriesTest extends TestCase
911{
1012 public function setUp ()
1113 {
1214 parent ::setUp ();
1315 $ this ->testModel = $ this ->newModel ();
16+ $ this ->app ->singleton (TagService::class, UnisharpTagService::class);
1417 }
1518
1619 // tag所需用id(Integer屬性)
1720 public function testCategoryAddIntegerId ()
1821 {
19- $ this ->testModel ->tagId (1 );
22+ $ this ->testModel ->categorize (1 );
2023 $ this ->assertCount (1 , $ this ->testModel ->tags );
2124 $ arr = [2 ,3 ,4 ,1 ];
22- $ this ->testModel ->tagId ($ arr );
25+ $ this ->testModel ->categorize ($ arr );
2326 $ this ->assertCount (4 , $ this ->testModel ->tags );
2427 }
28+
29+ public function testCategorize ()
30+ {
31+ $ model = TestModel::create (['title ' => 'test ' ]);
32+ $ model ->categorize ("News " );
33+ $ this ->assertEquals ("News " , $ model ->categories ->pluck ('name ' )->first ());
34+ }
35+
36+ public function testCategorizeDelimeter ()
37+ {
38+ $ model = TestModel::create (['title ' => 'test ' ]);
39+ $ model ->categorize ("News,Products " );
40+ $ this ->assertArraySubset (["News " , "Products " ], $ model ->categories ->pluck ('name ' )->toArray ());
41+ }
42+
43+ public function testCategorizeArray ()
44+ {
45+ $ model = TestModel::create (['title ' => 'test ' ]);
46+ $ model ->categorize (["News " , "Products " ]);
47+ $ this ->assertArraySubset (["News " , "Products " ], $ model ->categories ->pluck ('name ' )->toArray ());
48+ }
49+
50+ public function testCategorizeById ()
51+ {
52+ $ model = TestModel::create (['title ' => 'test ' ]);
53+ $ foo = Category::create (["name " => "foo " ]);
54+ $ bar = Category::create (["name " => "bar " ]);
55+ $ model ->categorize ("{$ bar ->tag_id }" );
56+ $ this ->assertCount (1 , $ model ->categories );
57+ $ this ->assertEquals ($ bar ->id , $ model ->categories ->pluck ('id ' )->first ());
58+ }
59+
60+ public function testCategorizeByManyId ()
61+ {
62+ $ model = TestModel::create (['title ' => 'test ' ]);
63+ $ foo = Category::create (["name " => "foo " ]);
64+ $ bar = Category::create (["name " => "bar " ]);
65+ $ model ->categorize (["{$ foo ->tag_id }" , "{$ bar ->tag_id }" ]);
66+ $ this ->assertCount (2 , $ model ->categories );
67+ $ this ->assertEquals ([$ foo ->tag_id , $ bar ->tag_id ], $ model ->categories ->pluck ('tag_id ' )->toArray ());
68+ }
2569
26- //
2770 public function testCategoryUnTagId ()
2871 {
29- $ this ->testModel ->tagId ([1 ,2 ,3 ]);
30- $ this ->testModel ->untagId (1 );
72+ $ this ->testModel ->categorize ([1 ,2 ,3 ]);
73+ $ this ->testModel ->decategorize (1 );
3174 $ this ->assertEquals (
3275 "2,3 " ,
3376 $ this ->testModel ->tagList
@@ -36,8 +79,8 @@ public function testCategoryUnTagId()
3679
3780 public function testCategoryReTagId ()
3881 {
39- $ this ->testModel ->tagId (1 );
40- $ this ->testModel ->retagId (2 );
82+ $ this ->testModel ->categorize (1 );
83+ $ this ->testModel ->recategorize (2 );
4184 $ this ->assertEquals (
4285 2 ,
4386 $ this ->testModel ->tagList
@@ -46,41 +89,11 @@ public function testCategoryReTagId()
4689
4790 public function testCategoryDeTagId ()
4891 {
49- $ this ->testModel ->tagId (1 );
50- $ this ->testModel ->detagId (1 );
92+ $ this ->testModel ->categorize (1 );
93+ $ this ->testModel ->decategorize (1 );
5194 $ this ->assertEquals (
5295 "" ,
5396 $ this ->testModel ->tagList
5497 );
5598 }
56-
57- public function testIntegerOneIdGetModel ()
58- {
59- $ arr = [2 ,3 ,4 ,1 ];
60- $ this ->testModel ->tagId ($ arr );
61- $ model = $ this ->newModel (["title " => "apple " ]);
62- $ model ->tagId (2 );
63- $ test = $ this ->newModel (["title " => "banana " ]);
64- $ this ->assertCount (2 , TestModel::withAllTagsId (2 )->get ());
65- $ title = TestModel::withAllTagsId ([2 ])->get ();
66- $ this ->assertEquals (
67- "apple " ,
68- $ title [1 ]->title
69- );
70- }
71-
72- public function testIntegerManyIdGetModel ()
73- {
74- $ arr = [2 ,3 ,4 ,1 ];
75- $ this ->testModel ->tagId ($ arr );
76- $ model = $ this ->newModel (["title " => "apple " ]);
77- $ model ->tagId (2 );
78- $ test = $ this ->newModel (["title " => "banana " ]);
79- $ this ->assertCount (2 , TestModel::withAllTagsId (2 )->get ());
80- $ title = TestModel::withAllTagsId ([2 ,4 ])->get ();
81- $ this ->assertEquals (
82- "test " ,
83- $ title [0 ]->title
84- );
85- }
8699}
0 commit comments