Skip to content

Commit a0995bc

Browse files
committed
Adding in some better unit tests
1 parent ce6f25e commit a0995bc

File tree

4 files changed

+72
-4
lines changed

4 files changed

+72
-4
lines changed

src/Taggable.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,7 @@ private function addTag($tagName)
252252

253253
static::$taggingUtility->incrementCount($tagName, $tagSlug, 1);
254254

255+
unset($this->relations['tagged']);
255256
event(new TagAdded($this));
256257
}
257258

@@ -273,6 +274,7 @@ private function removeTag($tagName)
273274
static::$taggingUtility->decrementCount($tagName, $tagSlug, $count);
274275
}
275276

277+
unset($this->relations['tagged']);
276278
event(new TagRemoved($this));
277279
}
278280

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
use Conner\Tagging\Taggable;
55
use Illuminate\Database\Capsule\Manager as DB;
66

7-
class TagTraitTest extends TestCase
7+
class CommonUsageTest extends TestCase
88
{
99
public function setUp()
1010
{
@@ -44,6 +44,46 @@ public function test_tag_call()
4444
$stub = Stub::create(['name'=>123]);
4545

4646
$stub->tag('test123');
47+
$stub->tag('456');
48+
$stub->tag('third');
49+
50+
$this->assertSame(['Test123', '456', 'Third'], $stub->tagNames());
51+
}
52+
53+
public function test_untag_call()
54+
{
55+
$stub = Stub::create(['name'=>'Stub']);
56+
57+
$stub->tag('one');
58+
$stub->tag('two');
59+
$stub->tag('three');
60+
61+
$stub->untag('two');
62+
63+
$this->assertArraysEqual(['Three', 'One'], $stub->tagNames());
64+
65+
$stub->untag('ONE');
66+
$this->assertArraysEqual(['Three'], $stub->tagNames());
67+
}
68+
69+
public function test_retag()
70+
{
71+
$stub = Stub::create(['name'=>123]);
72+
73+
$stub->tag('first');
74+
$stub->tag('second');
75+
76+
$stub->retag('foo, bar, another');
77+
$this->assertEquals(['foo', 'bar', 'another'], $stub->tagSlugs());
78+
}
79+
80+
public function test_tag_names_attribute()
81+
{
82+
$stub = Stub::create(['name'=>123, 'tag_names'=>'foo, bar']);
83+
84+
$stub->save();
85+
86+
$this->assertEquals(['Foo', 'Bar'], $stub->tagNames());
4787
}
4888
}
4989

tests/TaggedTest.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,12 @@
22

33
use Conner\Tagging\Model\Tagged;
44

5-
class TaggedTest extends PHPUnit_Framework_TestCase {
6-
5+
class TaggedTest extends TestCase
6+
{
77
public function test_instantiation()
88
{
99
$tagged = new Tagged();
1010

1111
$this->assertInternalType('object', $tagged);
1212
}
13-
1413
}

tests/TestCase.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,31 @@ public function setUp()
1414
parent::setUp();
1515
}
1616

17+
/**
18+
* Assert that two arrays are equal. This helper method will sort the two arrays before comparing them if
19+
* necessary. This only works for one-dimensional arrays, if you need multi-dimension support, you will
20+
* have to iterate through the dimensions yourself.
21+
* @param array $expected the expected array
22+
* @param array $actual the actual array
23+
* @param bool $regard_order whether or not array elements may appear in any order, default is false
24+
* @param bool $check_keys whether or not to check the keys in an associative array
25+
*/
26+
protected function assertArraysEqual(array $expected, array $actual, $regard_order = false, $check_keys = false) {
27+
// check length first
28+
$this->assertEquals(count($expected), count($actual), 'Failed to assert that two arrays have the same length.');
29+
30+
// sort arrays if order is irrelevant
31+
if (!$regard_order) {
32+
if ($check_keys) {
33+
$this->assertTrue(ksort($expected), 'Failed to sort array.');
34+
$this->assertTrue(ksort($actual), 'Failed to sort array.');
35+
} else {
36+
$this->assertTrue(sort($expected), 'Failed to sort array.');
37+
$this->assertTrue(sort($actual), 'Failed to sort array.');
38+
}
39+
}
40+
41+
$this->assertEquals($expected, $actual);
42+
}
43+
1744
}

0 commit comments

Comments
 (0)