Skip to content

Commit 174d48e

Browse files
committed
Improving/cleaning up tests
1 parent 5c11544 commit 174d48e

8 files changed

+83
-101
lines changed

src/Taggable.php

+5-3
Original file line numberDiff line numberDiff line change
@@ -130,16 +130,18 @@ public function tagSlugs(): array
130130
*
131131
* @param string|array|null $tagNames (or null to remove all tags)
132132
*/
133-
public function untag($tagNames=null)
133+
public function untag($tagNames = null)
134134
{
135135
if(is_null($tagNames)) {
136136
$tagNames = $this->tagNames();
137137
}
138138

139139
$tagNames = TaggingUtility::makeTagArray($tagNames);
140140

141+
dump($tagNames);
142+
141143
foreach($tagNames as $tagName) {
142-
$this->removeTag($tagName);
144+
$this->removeSingleTag($tagName);
143145
}
144146

145147
if(static::shouldDeleteUnused()) {
@@ -264,7 +266,7 @@ private function addTag($tagName)
264266
*
265267
* @param $tagName string
266268
*/
267-
private function removeTag($tagName)
269+
private function removeSingleTag($tagName)
268270
{
269271
$tagName = trim($tagName);
270272

src/TaggingUtility.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ public static function decrementCount($tagString, $tagSlug, $count)
242242
}
243243

244244
/**
245-
* Look at the tags table and delete any tags that are no londer in use by any taggable database rows.
245+
* Look at the tags table and delete any tags that are no longer in use by any taggable database rows.
246246
* Does not delete tags where 'suggest' is true
247247
*
248248
* @return int

tests/CommonUsageTest.php

-47
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,10 @@
22

33
namespace Tests;
44

5-
use Conner\Tagging\Contracts\TaggableContract;
6-
use Conner\Tagging\Taggable;
7-
use Illuminate\Database\Eloquent\Model as Eloquent;
8-
use Illuminate\Foundation\Testing\WithFaker;
95
use Illuminate\Support\Collection;
106

117
class CommonUsageTest extends TestCase
128
{
13-
use WithFaker;
14-
15-
public function setUp(): void
16-
{
17-
parent::setUp();
18-
$this->setUpFaker();
19-
20-
\Schema::create('books', function ($table) {
21-
$table->increments('id');
22-
$table->string('name');
23-
$table->timestamps();
24-
});
25-
26-
Eloquent::unguard();
27-
}
28-
29-
public function tearDown(): void
30-
{
31-
\Schema::drop('books');
32-
33-
parent::tearDown();
34-
}
35-
369
public function test_tag_call()
3710
{
3811
$stub = $this->book();
@@ -189,24 +162,4 @@ function test_withoutTags()
189162
$this->assertCount(1, $list);
190163
$this->assertEquals($three->id, $list[0]->id);
191164
}
192-
193-
function book($attributes = []): Book
194-
{
195-
$attributes = array_merge(['name'=>$this->faker->name], $attributes);
196-
197-
return Book::create($attributes);
198-
}
199-
}
200-
201-
202-
/**
203-
* @property string name
204-
*/
205-
class Book extends Eloquent implements TaggableContract
206-
{
207-
use Taggable;
208-
209-
protected $connection = 'testing';
210-
protected static $unguarded = true;
211-
public $table = 'books';
212165
}

tests/ConfigTest.php

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace Tests;
4+
5+
use Conner\Tagging\Model\Tag;
6+
7+
class ConfigTest extends TestCase
8+
{
9+
public function test_delete_unused_tags_true()
10+
{
11+
config(['tagging.delete_unused_tags'=>true]);
12+
$book = $this->book();
13+
14+
$book->tag(['tag1', 'tag2', 'tag3']);
15+
$book->untag();
16+
17+
$this->assertEmpty(Tag::all());
18+
}
19+
20+
public function test_delete_unused_tags_false()
21+
{
22+
config(['tagging.delete_unused_tags'=>false]);
23+
$book = $this->book();
24+
25+
$book->tag(['tag1', 'tag2', 'tag3']);
26+
$book->untag();
27+
28+
$this->assertCount(3, Tag::all());
29+
}
30+
}

tests/EventTests.php

-38
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,11 @@
22

33
namespace Tests;
44

5-
use Conner\Tagging\Contracts\TaggableContract;
65
use Conner\Tagging\Events\TagAdded;
76
use Conner\Tagging\Events\TagRemoved;
8-
use Conner\Tagging\Taggable;
9-
use Illuminate\Database\Eloquent\Model as Eloquent;
10-
use Illuminate\Foundation\Testing\WithFaker;
117

128
class EventTests extends TestCase
139
{
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-
2910
function test_tag_added()
3011
{
3112
\Event::listen(TagAdded::class, function(TagAdded $event){
@@ -50,23 +31,4 @@ function test_tag_removed()
5031

5132
$book->untag('Test');
5233
}
53-
54-
function book($attributes = []): Book
55-
{
56-
$attributes = array_merge(['name'=>$this->faker->name], $attributes);
57-
58-
return Book::create($attributes);
59-
}
6034
}
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-
}

tests/TagGroupTest.php

-12
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,11 @@
22

33
namespace Tests;
44

5-
use Illuminate\Database\Eloquent\Model as Eloquent;
65
use Conner\Tagging\Model\Tag;
76
use Conner\Tagging\Model\TagGroup;
8-
use Illuminate\Foundation\Testing\WithFaker;
97

108
class TagGroupTest extends TestCase
119
{
12-
use WithFaker;
13-
14-
public function setUp(): void
15-
{
16-
parent::setUp();
17-
$this->setUpFaker();
18-
19-
Eloquent::unguard();
20-
}
21-
2210
public function test_create_group()
2311
{
2412
$tagGroup = $this->createTagGroup('MyTagGroup');

tests/TaggingUtilityTest.php

+14
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Tests;
44

5+
use Conner\Tagging\Model\Tag;
56
use Conner\Tagging\TaggingUtility;
67
use Illuminate\Support\Str;
78

@@ -102,4 +103,17 @@ public function test_displayize()
102103
config(['tagging.displayer' => function($str) { return 'bbb'; }]);
103104
$this->assertEquals('bbb', TaggingUtility::displayize('some string'));
104105
}
106+
107+
public function test_deleteUnusedTags()
108+
{
109+
config(['tagging.delete_unused_tags'=>false]);
110+
$book = $this->book();
111+
112+
$book->tag(['tag1', 'tag2', 'tag3']);
113+
$book->untag();
114+
115+
TaggingUtility::deleteUnusedTags();
116+
117+
$this->assertEmpty(Tag::all());
118+
}
105119
}

tests/TestCase.php

+33
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,16 @@
22

33
namespace Tests;
44

5+
use Conner\Tagging\Contracts\TaggableContract;
56
use Conner\Tagging\Providers\TaggingServiceProvider;
7+
use Conner\Tagging\Taggable;
8+
use Illuminate\Database\Eloquent\Model as Eloquent;
9+
use Illuminate\Foundation\Testing\WithFaker;
610

711
abstract class TestCase extends \Orchestra\Testbench\TestCase
812
{
13+
use WithFaker;
14+
915
protected function getPackageProviders($app)
1016
{
1117
return [TaggingServiceProvider::class];
@@ -15,11 +21,19 @@ public function setUp(): void
1521
{
1622
parent::setUp();
1723

24+
$this->setUpFaker();
25+
1826
$this->artisan('migrate', [
1927
'--database' => 'testing',
2028
'--path' => realpath(__DIR__.'/../migrations'),
2129
'--realpath' => true,
2230
]);
31+
32+
\Schema::create('books', function ($table) {
33+
$table->increments('id');
34+
$table->string('name');
35+
$table->timestamps();
36+
});
2337
}
2438

2539
/**
@@ -65,4 +79,23 @@ protected function assertArraysEqual(array $expected, array $actual, $regard_ord
6579

6680
$this->assertEquals($expected, $actual);
6781
}
82+
83+
function book($attributes = []): Book
84+
{
85+
$attributes = array_merge(['name'=>$this->faker->name], $attributes);
86+
87+
return Book::create($attributes);
88+
}
89+
}
90+
91+
/**
92+
* @property string name
93+
*/
94+
class Book extends Eloquent implements TaggableContract
95+
{
96+
use Taggable;
97+
98+
protected $connection = 'testing';
99+
protected static $unguarded = true;
100+
public $table = 'books';
68101
}

0 commit comments

Comments
 (0)