Skip to content

Commit 4e979a6

Browse files
committed
add categories tests
1 parent a0101ec commit 4e979a6

File tree

5 files changed

+80
-0
lines changed

5 files changed

+80
-0
lines changed

src/Concerns/HasVisibility.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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+
}

src/Enums/Visibility.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
namespace Coderflex\LaravelTicket\Enums;
4+
5+
enum Visibility: int
6+
{
7+
case VISIBLE = 1;
8+
case HIDDEN = 0;
9+
}

src/Models/Category.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@
22

33
namespace Coderflex\LaravelTicket\Models;
44

5+
use Coderflex\LaravelTicket\Concerns\HasVisibility;
56
use Illuminate\Database\Eloquent\Factories\HasFactory;
67
use Illuminate\Database\Eloquent\Model;
78
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
89

910
class Category extends Model
1011
{
1112
use HasFactory;
13+
use HasVisibility;
1214

1315
/**
1416
* The attributes that aren't mass assignable.

src/Models/Label.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@
22

33
namespace Coderflex\LaravelTicket\Models;
44

5+
use Coderflex\LaravelTicket\Concerns\HasVisibility;
56
use Illuminate\Database\Eloquent\Factories\HasFactory;
67
use Illuminate\Database\Eloquent\Model;
78
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
89

910
class Label extends Model
1011
{
1112
use HasFactory;
13+
use HasVisibility;
1214

1315
/**
1416
* The attributes that aren't mass assignable.

tests/Feature/CategoryTest.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,39 @@
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+

0 commit comments

Comments
 (0)