Skip to content

Commit 2740b9d

Browse files
committed
Improving tests and removing a few bugs from the updates
1 parent 94f1fc5 commit 2740b9d

8 files changed

+102
-99
lines changed

src/Contracts/TaggableContract.php

-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
<?php namespace Conner\Tagging\Contracts;
22

3-
use Illuminate\Support\Facades\Config;
43
use Illuminate\Database\Eloquent\Builder;
54
use Illuminate\Database\Eloquent\Collection;
65

src/Contracts/TaggingUtility.php

-71
This file was deleted.

src/Model/TagGroup.php

+1-3
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,7 @@ class TagGroup extends Model
1616
{
1717
protected $table = 'tagging_tag_groups';
1818
public $timestamps = false;
19-
20-
/** @var TaggingUtility $taggingUtility */
21-
protected $taggingUtility;
19+
protected $fillable = ['name'];
2220

2321
/**
2422
* @param array $attributes

src/Taggable.php

+6-7
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111

1212
/**
1313
* @package Conner\Tagging
14-
* @method withAllTags(array)
15-
* @method withAnyTags(array)
16-
* @method withoutTags(array)
14+
* @method static withAllTags(array $tags)
15+
* @method static withAnyTag(array $tags)
16+
* @method static withoutTags(array $tags)
1717
* @property Collection|Tagged[] tagged
1818
* @property Collection|Tag[] tags
1919
* @property string[] tag_names
@@ -181,14 +181,13 @@ public function scopeWithAllTags(Builder $query, $tagNames): Builder
181181
array_shift($tagNames);
182182
}
183183

184-
$tagNames = static::$taggingUtility->makeTagArray($tagNames);
184+
$tagNames = TaggingUtility::makeTagArray($tagNames);
185185

186-
$normalizer = [static::$taggingUtility, 'normalizer'];
187186
$className = $query->getModel()->getMorphClass();
188187

189188
foreach($tagNames as $tagSlug) {
190189
$tags = Tagged::query()
191-
->where('tag_slug', call_user_func($normalizer, $tagSlug))
190+
->where('tag_slug', TaggingUtility::normalize($tagSlug))
192191
->where('taggable_type', $className)
193192
->get()
194193
->pluck('taggable_id');
@@ -366,7 +365,7 @@ private function assembleTagsForScoping($query, $tagNames)
366365

367366
$tagNames = TaggingUtility::makeTagArray($tagNames);
368367

369-
$normalizer = [TaggingUtility::class, 'normalizer'];
368+
$normalizer = [TaggingUtility::class, 'normalize'];
370369

371370
$tagNames = array_map($normalizer, $tagNames);
372371
$className = $query->getModel()->getMorphClass();

tests/CommonUsageTest.php

+75-15
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,20 @@
22

33
namespace Tests;
44

5+
use Conner\Tagging\Contracts\TaggableContract;
56
use Illuminate\Database\Eloquent\Model as Eloquent;
67
use Conner\Tagging\Taggable;
8+
use Illuminate\Foundation\Testing\WithFaker;
9+
use Illuminate\Support\Collection;
710

811
class CommonUsageTest extends TestCase
912
{
13+
use WithFaker;
14+
1015
public function setUp(): void
1116
{
1217
parent::setUp();
18+
$this->setUpFaker();
1319

1420
\Schema::create('books', function ($table) {
1521
$table->increments('id');
@@ -29,7 +35,7 @@ public function tearDown(): void
2935

3036
public function test_tag_call()
3137
{
32-
$stub = Stub::create(['name'=>123]);
38+
$stub = $this->book();
3339

3440
$stub->tag('test123');
3541
$stub->tag('456');
@@ -40,7 +46,7 @@ public function test_tag_call()
4046

4147
public function test_untag_call()
4248
{
43-
$stub = Stub::create(['name'=>'Stub']);
49+
$stub = $this->book();
4450

4551
$stub->tag('one');
4652
$stub->tag('two');
@@ -56,8 +62,7 @@ public function test_untag_call()
5662

5763
public function test_retag()
5864
{
59-
/** @var Stub $stub */
60-
$stub = Stub::create(['name'=>123]);
65+
$stub = $this->book();
6166

6267
$stub->tag('first');
6368
$stub->tag('second');
@@ -69,7 +74,7 @@ public function test_retag()
6974

7075
public function test_tag_names_attribute()
7176
{
72-
$stub = Stub::create(['name'=>123, 'tag_names'=>'foo, bar']);
77+
$stub = $this->book(['tag_names'=>'foo, bar']);
7378

7479
$stub->save();
7580

@@ -78,8 +83,7 @@ public function test_tag_names_attribute()
7883

7984
public function test_the_tagged_property()
8085
{
81-
/** @var Stub $stub */
82-
$stub = Stub::create(['name'=>123]);
86+
$stub = $this->book();
8387

8488
$stub->tag('first');
8589
$stub->tag('second');
@@ -93,8 +97,7 @@ public function test_the_tagged_property()
9397

9498
public function test_calling_tagNames_as_a_property()
9599
{
96-
/** @var Stub $stub */
97-
$stub = Stub::create(['name'=>123]);
100+
$stub = $this->book();
98101

99102
$stub->tag('first');
100103
$stub->tag('second');
@@ -105,8 +108,7 @@ public function test_calling_tagNames_as_a_property()
105108

106109
public function test_get_tags()
107110
{
108-
/** @var Stub $stub */
109-
$stub = Stub::create(['name'=>123]);
111+
$stub = $this->book();
110112

111113
$stub->tag('first');
112114
$stub->tag('second');
@@ -116,7 +118,7 @@ public function test_get_tags()
116118

117119
public function test_setting_tag_names_array()
118120
{
119-
$stub = new Stub();
121+
$stub = new Book();
120122
$stub->name = 'test';
121123
$stub->tag_names = ['foo', 'bar'];
122124
$stub->save();
@@ -129,21 +131,79 @@ public function test_setting_tag_names_array()
129131

130132
public function test_tagging_with_empty_tags()
131133
{
132-
/** @var Stub $stub */
133-
$stub = Stub::create(['name'=>123]);
134+
$stub = $this->book();
134135

135136
$tagName = "Japan, Asia, Economy, , , , , ";
136137

137138
$stub->retag($tagName);
138139

139140
$this->assertEquals(['Japan', 'Asia', 'Economy'], $stub->tag_names);
140141
}
142+
143+
function test_withAllTags()
144+
{
145+
$one = $this->book();
146+
$two = $this->book();
147+
$three = $this->book();
148+
149+
$one->tag(['one']);
150+
$two->tag(['b', 'two']);
151+
$three->tag(['one', 'two']);
152+
153+
$list = Book::withAllTags(['one', 'two'])->get();
154+
155+
$this->assertCount(1, $list);
156+
$this->assertEquals($three->id, $list[0]->id);
157+
}
158+
159+
function test_withAnyTags()
160+
{
161+
$one = $this->book();
162+
$two = $this->book();
163+
$three = $this->book();
164+
165+
$one->tag(['one']);
166+
$two->tag(['b', 'two']);
167+
168+
/** @var Collection $list */
169+
$list = Book::withAnyTag(['one', 'two'])->get();
170+
171+
$this->assertCount(2, $list);
172+
$indexed = $list->keyBy('id');
173+
$this->assertNotEmpty($indexed[$one->id]);
174+
$this->assertNotEmpty($indexed[$two->id]);
175+
}
176+
177+
function test_withoutTags()
178+
{
179+
$one = $this->book();
180+
$two = $this->book();
181+
$three = $this->book();
182+
183+
$one->tag(['one']);
184+
$two->tag(['b', 'two']);
185+
186+
/** @var Collection $list */
187+
$list = Book::withoutTags(['one', 'two'])->get();
188+
189+
$this->assertCount(1, $list);
190+
$this->assertEquals($three->id, $list[0]->id);
191+
}
192+
193+
194+
private function book($attributes = []): Book
195+
{
196+
$attributes = array_merge(['name'=>$this->faker->name], $attributes);
197+
198+
return Book::create($attributes);
199+
}
141200
}
142201

202+
143203
/**
144204
* @property string name
145205
*/
146-
class Stub extends Eloquent
206+
class Book extends Eloquent implements TaggableContract
147207
{
148208
use Taggable;
149209

tests/TaggableContractTest.php

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ class TaggableContractTest extends TestCase
1111
public function test_is_implemented_by_taggable_trait()
1212
{
1313
$implementation = new TraitImplementation();
14+
1415
$this->assertNotNull($implementation);
1516
}
1617
}

tests/TaggedTest.php

+7-2
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,16 @@
66

77
class TaggedTest extends TestCase
88
{
9-
public function test_instantiation()
9+
public function test_saving_a_tag_model()
1010
{
1111
Tagged::unguard();
1212

13-
$tagged = new Tagged(['taggable_id'=>'123', 'tag_slug'=> 'foo', 'tag_name'=>'Foo', 'taggable_type'=>'bar']);
13+
$tagged = new Tagged([
14+
'taggable_id'=>'123',
15+
'tag_slug'=> 'foo',
16+
'tag_name'=>'Foo',
17+
'taggable_type'=>'bar',
18+
]);
1419

1520
$tagged->save();
1621

tests/TaggingUtilityTest.php

+12
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Tests;
44

55
use Conner\Tagging\TaggingUtility;
6+
use Illuminate\Support\Str;
67

78
class TaggingUtilityTest extends TestCase
89
{
@@ -68,6 +69,17 @@ public function test_normalizer()
6869
$this->assertEquals(TaggingUtility::slug('Sugar Free'), TaggingUtility::normalize('Sugar Free'));
6970
$this->assertEquals(TaggingUtility::slug('ПЧ�Цщ'), TaggingUtility::normalize('ПЧ�Цщ'));
7071
$this->assertEquals(TaggingUtility::slug('quiénsí'), TaggingUtility::normalize('quiénsí'));
72+
73+
config(['tagging.normalizer' => function($str) { return 'aaa'; }]);
74+
$this->assertEquals('aaa', TaggingUtility::normalize('some string'));
7175
}
7276

77+
public function test_displayize()
78+
{
79+
$this->assertEquals('Sugar Free', TaggingUtility::displayize('sugar free'));
80+
$this->assertEquals(Str::title('ПЧ�Цщ'), TaggingUtility::displayize('ПЧ�Цщ'));
81+
82+
config(['tagging.displayer' => function($str) { return 'bbb'; }]);
83+
$this->assertEquals('bbb', TaggingUtility::displayize('some string'));
84+
}
7385
}

0 commit comments

Comments
 (0)