This repository has been archived by the owner on Dec 20, 2024. It is now read-only.
forked from spatie/laravel-tags
-
Notifications
You must be signed in to change notification settings - Fork 2
/
HasTagsScopesTest.php
119 lines (88 loc) · 3.56 KB
/
HasTagsScopesTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
<?php
namespace Spatie\Translatable\Test;
use Spatie\Tags\Tag;
use Spatie\Tags\Test\TestCase;
use Spatie\Tags\Test\TestModel;
class HasTagsScopesTest extends TestCase
{
/** @var \Spatie\Tags\Test\TestModel */
protected $testModel;
public function setUp(): void
{
parent::setUp();
TestModel::create([
'name' => 'model1',
'tags' => ['tagA'],
]);
TestModel::create([
'name' => 'model2',
'tags' => ['tagA', 'tagB'],
]);
TestModel::create([
'name' => 'model3',
'tags' => ['tagA', 'tagB', 'tagC'],
]);
TestModel::create([
'name' => 'model4',
'tags' => ['tagD'],
]);
$typedTag = Tag::findOrCreate('tagE', 'typedTag');
$anotherTypedTag = Tag::findOrCreate('tagF', 'typedTag');
TestModel::create([
'name' => 'model5',
'tags' => [$typedTag, $anotherTypedTag],
]);
TestModel::create([
'name' => 'model6',
'tags' => [$typedTag],
]);
}
/** @test */
public function it_provides_as_scope_to_get_all_models_that_have_any_of_the_given_tags()
{
$testModels = TestModel::withAnyTags(['tagC', 'tagD'])->get();
$this->assertEquals(['model3', 'model4'], $testModels->pluck('name')->toArray());
}
/** @test */
public function the_with_any_tags_scopes_will_still_items_when_passing_a_non_existing_tag()
{
$testModels = TestModel::withAnyTags(['tagB', 'tagC', 'nonExistingTag'])->get();
$this->assertEquals(['model2', 'model3'], $testModels->pluck('name')->toArray());
}
/** @test */
public function it_provides_as_scope_to_get_all_models_that_have_all_of_the_given_tags()
{
$testModels = TestModel::withAllTags(['tagA', 'tagB'])->get();
$this->assertEquals(['model2', 'model3'], $testModels->pluck('name')->toArray());
$testModels = TestModel::withAllTags(['tagB', 'tagC'])->get();
$this->assertEquals(['model3'], $testModels->pluck('name')->toArray());
}
/** @test */
public function it_provides_as_scope_to_get_all_models_that_have_any_of_the_given_tags_with_type()
{
$testModels = TestModel::withAnyTags(['tagE'], 'typedTag')->get();
$this->assertEquals(['model5', 'model6'], $testModels->pluck('name')->toArray());
$testModels = TestModel::withAnyTags(['tagF'], 'typedTag')->get();
$this->assertEquals(['model5'], $testModels->pluck('name')->toArray());
$testModels = TestModel::withAnyTags(['tagF'])->get();
$this->assertEquals([], $testModels->pluck('name')->toArray());
}
/** @test */
public function it_provides_as_scope_to_get_all_models_that_have_all_of_the_given_tags_with_type()
{
$testModels = TestModel::withAllTags(['tagE', 'tagF'], 'typedTag')->get();
$this->assertEquals(['model5'], $testModels->pluck('name')->toArray());
}
/** @test */
public function it_provides_as_scope_to_get_all_models_that_have_any_of_the_given_tags_with_any_type()
{
$testModels = TestModel::withAnyTagsOfAnyType(['tagE', 'tagF'])->get();
$this->assertEquals(['model5', 'model6'], $testModels->pluck('name')->toArray());
}
/** @test */
public function it_provides_as_scope_to_get_all_models_that_have_all_of_the_given_tags_with_any_type()
{
$testModels = TestModel::withAllTagsOfAnyType(['tagE', 'tagF'])->get();
$this->assertEquals(['model5'], $testModels->pluck('name')->toArray());
}
}