Skip to content

Commit 6eb64be

Browse files
committed
Improve events, add tests/documentation
1 parent 66f1204 commit 6eb64be

File tree

6 files changed

+110
-17
lines changed

6 files changed

+110
-17
lines changed

docs/events.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,13 @@ Conner\Tagging\Events\TagRemoved;
1212
You can add listeners and track these events.
1313

1414
```php
15-
\Event::listen(Conner\Tagging\Events\TagAdded::class, function($article){
16-
\Log::debug($article->title . ' was tagged');
15+
\Event::listen(TagAdded::class, function(TagAdded $event){
16+
\Log::debug($event->model->title . ' was tagged');
17+
});
18+
```
19+
20+
```php
21+
\Event::listen(TagRemoved::class, function(TagRemoved $event){
22+
\Log::debug($event->tagSlug . ' was removed');
1723
});
1824
```

src/Events/TagAdded.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?php namespace Conner\Tagging\Events;
22

3+
use Conner\Tagging\Model\Tagged;
34
use Conner\Tagging\Taggable;
45
use Illuminate\Queue\SerializesModels;
56
use Illuminate\Database\Eloquent\Model;
@@ -8,17 +9,26 @@ class TagAdded
89
{
910
use SerializesModels;
1011

11-
/** @var \Illuminate\Database\Eloquent\Model **/
12+
/** @var Model **/
1213
public $model;
1314

15+
/** @var string */
16+
public $tagSlug;
17+
18+
/** @var Tagged */
19+
public $tagged;
20+
1421
/**
1522
* Create a new event instance.
1623
*
1724
* @param Taggable|Model $model
18-
* @return void
25+
* @param string $tagSlug
26+
* @param Tagged $tagged
1927
*/
20-
public function __construct($model)
28+
public function __construct($model, string $tagSlug, Tagged $tagged)
2129
{
2230
$this->model = $model;
31+
$this->tagSlug = $tagSlug;
32+
$this->tagged = $tagged;
2333
}
2434
}

src/Events/TagRemoved.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,21 @@ class TagRemoved
1010
{
1111
use SerializesModels;
1212

13-
/** @var \Conner\Tagging\Taggable **/
13+
/** @var Taggable|Model **/
1414
public $model;
1515

16+
/*** @var string */
17+
public $tagSlug;
18+
1619
/**
1720
* Create a new event instance.
1821
*
1922
* @param Taggable|Model $model
20-
* @return void
23+
* @param string $tagSlug
2124
*/
22-
public function __construct($model)
25+
public function __construct($model, string $tagSlug)
2326
{
2427
$this->model = $model;
28+
$this->tagSlug = $tagSlug;
2529
}
2630
}

src/Taggable.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,8 @@ private function addTag($tagName)
255255
TaggingUtility::incrementCount($tagName, $tagSlug, 1);
256256

257257
unset($this->relations['tagged']);
258-
event(new TagAdded($this));
258+
259+
event(new TagAdded($this, $tagSlug, $tagged));
259260
}
260261

261262
/**
@@ -273,8 +274,9 @@ private function removeTag($tagName)
273274
TaggingUtility::decrementCount($tagName, $tagSlug, $count);
274275
}
275276

276-
unset($this->relations['tagged']);
277-
event(new TagRemoved($this));
277+
unset($this->relations['tagged']); // clear the "cache"
278+
279+
event(new TagRemoved($this, $tagSlug));
278280
}
279281

280282
/**

tests/CommonUsageTest.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
namespace Tests;
44

55
use Conner\Tagging\Contracts\TaggableContract;
6-
use Illuminate\Database\Eloquent\Model as Eloquent;
76
use Conner\Tagging\Taggable;
7+
use Illuminate\Database\Eloquent\Model as Eloquent;
88
use Illuminate\Foundation\Testing\WithFaker;
99
use Illuminate\Support\Collection;
1010

@@ -74,7 +74,7 @@ public function test_retag()
7474

7575
public function test_tag_names_attribute()
7676
{
77-
$stub = $this->book(['tag_names'=>'foo, bar']);
77+
$stub = $this->book(['tag_names' => 'foo, bar']);
7878

7979
$stub->save();
8080

@@ -118,7 +118,7 @@ public function test_get_tags()
118118

119119
public function test_setting_tag_names_array()
120120
{
121-
$stub = new Book();
121+
$stub = $this->book();
122122
$stub->name = 'test';
123123
$stub->tag_names = ['foo', 'bar'];
124124
$stub->save();
@@ -190,8 +190,7 @@ function test_withoutTags()
190190
$this->assertEquals($three->id, $list[0]->id);
191191
}
192192

193-
194-
private function book($attributes = []): Book
193+
function book($attributes = []): Book
195194
{
196195
$attributes = array_merge(['name'=>$this->faker->name], $attributes);
197196

@@ -208,6 +207,6 @@ class Book extends Eloquent implements TaggableContract
208207
use Taggable;
209208

210209
protected $connection = 'testing';
211-
210+
protected static $unguarded = true;
212211
public $table = 'books';
213212
}

tests/EventTests.php

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
3+
namespace Tests;
4+
5+
use Conner\Tagging\Contracts\TaggableContract;
6+
use Conner\Tagging\Events\TagAdded;
7+
use Conner\Tagging\Events\TagRemoved;
8+
use Conner\Tagging\Taggable;
9+
use Illuminate\Database\Eloquent\Model as Eloquent;
10+
use Illuminate\Foundation\Testing\WithFaker;
11+
12+
class EventTests extends TestCase
13+
{
14+
use WithFaker;
15+
16+
public function setUp(): void
17+
{
18+
parent::setUp();
19+
20+
$this->setUpFaker();
21+
22+
\Schema::create('books', function ($table) {
23+
$table->increments('id');
24+
$table->string('name');
25+
$table->timestamps();
26+
});
27+
}
28+
29+
function test_tag_added()
30+
{
31+
\Event::listen(TagAdded::class, function(TagAdded $event){
32+
$this->assertNotEmpty($event->model);
33+
$this->assertEquals('test', $event->tagSlug);
34+
$this->assertEquals('Test', $event->tagged->tag_name);
35+
});
36+
37+
$book = $this->book();
38+
$book->tag('Test');
39+
}
40+
41+
function test_tag_removed()
42+
{
43+
\Event::listen(TagRemoved::class, function(TagRemoved $event){
44+
$this->assertNotEmpty($event->model);
45+
$this->assertEquals('test', $event->tagSlug);
46+
});
47+
48+
$book = $this->book();
49+
$book->tag('Test');
50+
51+
$book->untag('Test');
52+
}
53+
54+
function book($attributes = []): Book
55+
{
56+
$attributes = array_merge(['name'=>$this->faker->name], $attributes);
57+
58+
return Book::create($attributes);
59+
}
60+
}
61+
62+
/**
63+
* @property string name
64+
*/
65+
class Book extends Eloquent implements TaggableContract
66+
{
67+
use Taggable;
68+
69+
protected $connection = 'testing';
70+
protected static $unguarded = true;
71+
public $table = 'books';
72+
}

0 commit comments

Comments
 (0)