File tree Expand file tree Collapse file tree 5 files changed +80
-0
lines changed
Expand file tree Collapse file tree 5 files changed +80
-0
lines changed Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace Coderflex \LaravelTicket \Concerns ;
4+
5+ use Coderflex \LaravelTicket \Enums \Visibility ;
6+ use Illuminate \Database \Eloquent \Builder ;
7+
8+ trait HasVisibility
9+ {
10+ /**
11+ * Determine wheather if the model is visible
12+ *
13+ * @return \Illuminate\Database\Eloquent\Builder
14+ */
15+ public function scopeVisible (Builder $ builder )
16+ {
17+ return $ builder ->where ('is_visible ' , Visibility::VISIBLE ->value );
18+ }
19+
20+ /**
21+ * Determine wheather if the model is hidden
22+ *
23+ * @return \Illuminate\Database\Eloquent\Builder
24+ */
25+ public function scopeHidden (Builder $ builder )
26+ {
27+ return $ builder ->where ('is_visible ' , Visibility::HIDDEN ->value );
28+ }
29+ }
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace Coderflex \LaravelTicket \Enums ;
4+
5+ enum Visibility: int
6+ {
7+ case VISIBLE = 1 ;
8+ case HIDDEN = 0 ;
9+ }
Original file line number Diff line number Diff line change 22
33namespace Coderflex \LaravelTicket \Models ;
44
5+ use Coderflex \LaravelTicket \Concerns \HasVisibility ;
56use Illuminate \Database \Eloquent \Factories \HasFactory ;
67use Illuminate \Database \Eloquent \Model ;
78use Illuminate \Database \Eloquent \Relations \BelongsToMany ;
89
910class Category extends Model
1011{
1112 use HasFactory;
13+ use HasVisibility;
1214
1315 /**
1416 * The attributes that aren't mass assignable.
Original file line number Diff line number Diff line change 22
33namespace Coderflex \LaravelTicket \Models ;
44
5+ use Coderflex \LaravelTicket \Concerns \HasVisibility ;
56use Illuminate \Database \Eloquent \Factories \HasFactory ;
67use Illuminate \Database \Eloquent \Model ;
78use Illuminate \Database \Eloquent \Relations \BelongsToMany ;
89
910class Label extends Model
1011{
1112 use HasFactory;
13+ use HasVisibility;
1214
1315 /**
1416 * The attributes that aren't mass assignable.
Original file line number Diff line number Diff line change 11<?php
2+
3+ use Coderflex \LaravelTicket \Models \Category ;
4+ use Coderflex \LaravelTicket \Models \Ticket ;
5+
6+ it ('can attach category to a ticket ' , function () {
7+ $ category = Category::factory ()->create ();
8+ $ ticket = Ticket::factory ()->create ();
9+
10+ $ category ->tickets ()->attach ($ ticket );
11+
12+ $ this ->assertEquals ($ category ->tickets ->count (), 1 );
13+ });
14+
15+ it ('can deattach category to a ticket ' , function () {
16+ $ category = Category::factory ()->create ();
17+ $ ticket = Ticket::factory ()->create ();
18+
19+ $ ticket ->attachCategories ($ category );
20+
21+ $ category ->tickets ()->detach ($ ticket );
22+
23+ $ this ->assertEquals ($ category ->tickets ->count (), 0 );
24+ });
25+
26+ it ('gets categories by visibility status ' , function () {
27+ $ categories = Category::factory ()->times (10 )->create ([
28+ 'is_visible ' => true ,
29+ ]);
30+
31+ Category::factory ()->times (9 )->create ([
32+ 'is_visible ' => false
33+ ]);
34+
35+ $ this ->assertEquals (Category::count (), 19 );
36+ $ this ->assertEquals (Category::visible ()->count (), 10 );
37+ $ this ->assertEquals (Category::hidden ()->count (), 9 );
38+ });
39+
You can’t perform that action at this time.
0 commit comments