Skip to content

Commit f66d041

Browse files
committed
Some cleanups and improvements around tag groups
1 parent 084924c commit f66d041

File tree

4 files changed

+47
-36
lines changed

4 files changed

+47
-36
lines changed

migrations/2014_01_07_073615_create_tags_table.php

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ public function up()
1313
$table->string('name', 125);
1414
$table->boolean('suggest')->default(false);
1515
$table->integer('count')->unsigned()->default(0); // count of how many times this tag was used
16+
$table->integer('tag_group_id')->unsigned()->nullable();
1617
});
1718
}
1819

migrations/2016_06_29_073615_update_tags_table.php

-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ class UpdateTagsTable extends Migration {
88
public function up()
99
{
1010
Schema::table('tagging_tags', function ($table) {
11-
$table->integer('tag_group_id')->unsigned()->nullable()->after('id');
1211
$table->foreign('tag_group_id')->references('id')->on('tagging_tag_groups');
1312
});
1413

@@ -20,7 +19,6 @@ public function down()
2019
Schema::disableForeignKeyConstraints();
2120
Schema::table('tagging_tags', function ($table) {
2221
$table->dropForeign('tagging_tags_tag_group_id_foreign');
23-
$table->dropColumn('tag_group_id');
2422
});
2523
}
2624
}

src/Model/Tag.php

+10-16
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@
1313
* @property string slug
1414
* @property bool suggest
1515
* @property integer count
16+
* @property integer tag_group_id
1617
* @property TagGroup group
1718
* @method static suggested()
18-
* @method static inGroup(string $groupName)
19+
* @method static inGroup(string $group)
1920
*/
2021
class Tag extends Model
2122
{
@@ -49,13 +50,13 @@ public function save(array $options = [])
4950

5051
/**
5152
* Tag group setter
52-
* @param string $groupName
53+
* @param string $group
5354
* @return Tag
5455
*/
55-
public function setGroup($groupName)
56+
public function setGroup(string $group)
5657
{
5758
$tagGroup = TagGroup::query()
58-
->where('slug', TaggingUtility::normalize($groupName))
59+
->where('slug', TaggingUtility::normalize($group))
5960
->first();
6061

6162
if ($tagGroup) {
@@ -64,27 +65,20 @@ public function setGroup($groupName)
6465

6566
return $this;
6667
} else {
67-
throw new \RuntimeException('No Tag Group found: '. $groupName);
68+
throw new \RuntimeException('No Tag Group found: '. $group);
6869
}
6970
}
7071

7172
/**
7273
* Tag group remove
73-
* @param string $groupName
7474
* @return Tag
7575
*/
76-
public function removeGroup(string $groupName)
76+
public function removeGroup()
7777
{
78-
$tagGroup = TagGroup::query()->where('slug', TaggingUtility::normalize($groupName))->first();
79-
80-
if ($tagGroup) {
81-
$this->group()->dissociate($tagGroup);
82-
$this->save();
78+
$this->group()->dissociate();
79+
$this->save();
8380

84-
return $this;
85-
} else {
86-
throw new \RuntimeException('No Tag Group found: '. $groupName);
87-
}
81+
return $this;
8882
}
8983

9084
/**

tests/TagGroupTest.php

+36-18
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,16 @@
55
use Illuminate\Database\Eloquent\Model as Eloquent;
66
use Conner\Tagging\Model\Tag;
77
use Conner\Tagging\Model\TagGroup;
8+
use Illuminate\Foundation\Testing\WithFaker;
89

910
class TagGroupTest extends TestCase
1011
{
12+
use WithFaker;
13+
1114
public function setUp(): void
1215
{
1316
parent::setUp();
17+
$this->setUpFaker();
1418

1519
Eloquent::unguard();
1620
}
@@ -24,73 +28,87 @@ public function test_create_group()
2428

2529
public function test_tag_group_tags_list()
2630
{
27-
$tag_group = $this->createTagGroup('MyTagGroup');
31+
$tagGroup = $this->createTagGroup('MyTagGroup');
2832

2933
$tag = $this->createTag();
3034

31-
$tag->setGroup($tag_group->name);
35+
$tag->setGroup($tagGroup->name);
3236

33-
$this->assertEquals(1, $tag_group->tags()->count());
37+
$this->assertEquals(1, $tagGroup->tags()->count());
38+
$this->assertEquals($tagGroup->id, $tag->tag_group_id);
3439
}
3540

3641
public function test_add_group_to_tag()
3742
{
38-
$tag_group = $this->createTagGroup('MyTagGroup');
43+
$tagGroup = $this->createTagGroup('MyTagGroup');
3944

4045
$tag = $this->createTag();
4146

42-
$tag->setGroup($tag_group->name);
47+
$tag->setGroup($tagGroup->name);
4348

44-
$this->assertCount(1, Tag::inGroup($tag_group->name)->get());
49+
$this->assertCount(1, Tag::inGroup($tagGroup->name)->get());
4550

4651
$this->assertTrue($tag->isInGroup('MyTagGroup'));
4752
}
4853

4954
public function test_delete_group_from_tag()
5055
{
51-
$tag_group = $this->createTagGroup('MyTagGroup');
56+
$tagGroup = $this->createTagGroup('MyTagGroup');
5257

5358
$tag = $this->createTag();
5459

55-
$tag->setGroup($tag_group->name);
60+
$tag->setGroup($tagGroup->name);
5661

57-
$this->assertCount(1, Tag::inGroup($tag_group->name)->get());
62+
$this->assertCount(1, Tag::inGroup($tagGroup->name)->get());
63+
$this->assertEquals($tagGroup->id, $tag->tag_group_id);
5864

59-
$tag->removeGroup($tag_group->name);
65+
$tag->removeGroup();
6066

61-
$this->assertCount(0, Tag::inGroup($tag_group->name)->get());
67+
$this->assertCount(0, Tag::inGroup($tagGroup->name)->get());
6268

6369
$this->assertFalse($tag->isInGroup('MyTagGroup'));
6470
}
6571

72+
public function test_removeGroup_with_no_group()
73+
{
74+
$tag = $this->createTag();
75+
76+
$tag->removeGroup();
77+
78+
$this->assertTrue(true); // no exceptions thrown
79+
}
80+
6681
public function test_delete_group_tag()
6782
{
68-
$tag_group = $this->createTagGroup('MyTagGroup');
83+
$tagGroup = $this->createTagGroup('MyTagGroup');
6984

7085
$tag = $this->createTag();
7186

72-
$tag->setGroup($tag_group->name);
87+
$tag->setGroup($tagGroup->name);
7388

74-
$tag_group->delete();
89+
$tagGroup->delete();
7590

7691
// unless you refresh the tag, it will still think there is a relationship
7792
$tag = $tag->fresh();
7893

79-
$this->assertFalse($tag_group->exists);
94+
$this->assertFalse($tagGroup->exists);
8095

8196
$this->assertNull($tag->group, 'The group should not exist on the tag after it is deleted');
8297

8398
$this->assertFalse($tag->isInGroup('MyTagGroup'), 'The tag should not belong to a deleted group');
8499
}
85100

86-
private function createTagGroup($group_name = 'MyTagGroup')
101+
private function createTagGroup($name = null): TagGroup
87102
{
103+
if(is_null($name)) {
104+
$name = $this->faker->name;
105+
}
88106
return TagGroup::create([
89-
'name' => $group_name
107+
'name' => $name
90108
]);
91109
}
92110

93-
private function createTag($name = 'Test Tag')
111+
private function createTag($name = 'Test Tag'): Tag
94112
{
95113
$tag = new Tag();
96114
$tag->name = $name;

0 commit comments

Comments
 (0)