Skip to content

Commit

Permalink
Adding in some better unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rtconner committed Jan 6, 2016
1 parent ce6f25e commit a0995bc
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 4 deletions.
2 changes: 2 additions & 0 deletions src/Taggable.php
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@ private function addTag($tagName)

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

unset($this->relations['tagged']);
event(new TagAdded($this));
}

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

unset($this->relations['tagged']);
event(new TagRemoved($this));
}

Expand Down
42 changes: 41 additions & 1 deletion tests/TaggableTest.php → tests/CommonUsageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
use Conner\Tagging\Taggable;
use Illuminate\Database\Capsule\Manager as DB;

class TagTraitTest extends TestCase
class CommonUsageTest extends TestCase
{
public function setUp()
{
Expand Down Expand Up @@ -44,6 +44,46 @@ public function test_tag_call()
$stub = Stub::create(['name'=>123]);

$stub->tag('test123');
$stub->tag('456');
$stub->tag('third');

$this->assertSame(['Test123', '456', 'Third'], $stub->tagNames());
}

public function test_untag_call()
{
$stub = Stub::create(['name'=>'Stub']);

$stub->tag('one');
$stub->tag('two');
$stub->tag('three');

$stub->untag('two');

$this->assertArraysEqual(['Three', 'One'], $stub->tagNames());

$stub->untag('ONE');
$this->assertArraysEqual(['Three'], $stub->tagNames());
}

public function test_retag()
{
$stub = Stub::create(['name'=>123]);

$stub->tag('first');
$stub->tag('second');

$stub->retag('foo, bar, another');
$this->assertEquals(['foo', 'bar', 'another'], $stub->tagSlugs());
}

public function test_tag_names_attribute()
{
$stub = Stub::create(['name'=>123, 'tag_names'=>'foo, bar']);

$stub->save();

$this->assertEquals(['Foo', 'Bar'], $stub->tagNames());
}
}

Expand Down
5 changes: 2 additions & 3 deletions tests/TaggedTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@

use Conner\Tagging\Model\Tagged;

class TaggedTest extends PHPUnit_Framework_TestCase {

class TaggedTest extends TestCase
{
public function test_instantiation()
{
$tagged = new Tagged();

$this->assertInternalType('object', $tagged);
}

}
27 changes: 27 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,31 @@ public function setUp()
parent::setUp();
}

/**
* Assert that two arrays are equal. This helper method will sort the two arrays before comparing them if
* necessary. This only works for one-dimensional arrays, if you need multi-dimension support, you will
* have to iterate through the dimensions yourself.
* @param array $expected the expected array
* @param array $actual the actual array
* @param bool $regard_order whether or not array elements may appear in any order, default is false
* @param bool $check_keys whether or not to check the keys in an associative array
*/
protected function assertArraysEqual(array $expected, array $actual, $regard_order = false, $check_keys = false) {
// check length first
$this->assertEquals(count($expected), count($actual), 'Failed to assert that two arrays have the same length.');

// sort arrays if order is irrelevant
if (!$regard_order) {
if ($check_keys) {
$this->assertTrue(ksort($expected), 'Failed to sort array.');
$this->assertTrue(ksort($actual), 'Failed to sort array.');
} else {
$this->assertTrue(sort($expected), 'Failed to sort array.');
$this->assertTrue(sort($actual), 'Failed to sort array.');
}
}

$this->assertEquals($expected, $actual);
}

}

0 comments on commit a0995bc

Please sign in to comment.