Skip to content

Commit

Permalink
Add more robust unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rtconner committed Jan 18, 2016
1 parent 74a1cf4 commit 8dbf026
Show file tree
Hide file tree
Showing 5 changed files with 170 additions and 7 deletions.
8 changes: 7 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,18 @@
"illuminate/support": ">=5.0"
},
"require-dev": {
"phpunit/phpunit": ">=4.0",
"orchestra/testbench": "~3.0",
"phpunit/phpunit": "~4.0",
"mockery/mockery": "~0.9"
},
"autoload": {
"psr-4": {
"Conner\\Likeable\\": "src/"
}
},
"autoload-dev": {
"classmap": [
"tests/TestCase.php"
]
}
}
30 changes: 25 additions & 5 deletions src/LikeableTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,18 @@
trait LikeableTrait
{
/**
* Fetch only records that currently logged in user has liked/followed
* DEPRECATED - Use whereLikedBy()
*/
public function scopeWhereLiked($query, $userId=null)
{
return $this->scopeWhereLikedBy($query, $userId);
}

/**
* Fetch records that are liked by a given user.
* Ex: Book::whereLikedBy(123)->get();
*/
public function scopeWhereLikedBy($query, $userId=null)
{
if(is_null($userId)) {
$userId = $this->loggedInUserId();
Expand All @@ -21,6 +30,7 @@ public function scopeWhereLiked($query, $userId=null)
});
}


/**
* Populate the $model->likes attribute
*/
Expand All @@ -34,7 +44,7 @@ public function getLikeCountAttribute()
*/
public function likes()
{
return $this->morphMany('\Conner\Likeable\Like', 'likable');
return $this->morphMany(\Conner\Likeable\Like::class, 'likable');
}

/**
Expand All @@ -43,7 +53,7 @@ public function likes()
*/
public function likeCounter()
{
return $this->morphOne('\Conner\Likeable\LikeCounter', 'likable');
return $this->morphOne(\Conner\Likeable\LikeCounter::class, 'likable');
}

/**
Expand Down Expand Up @@ -86,7 +96,7 @@ public function unlike($userId=null)
->where('user_id', '=', $userId)
->first();

if(!$like) return;
if(!$like) { return; }

$like->delete();
}
Expand Down Expand Up @@ -151,7 +161,17 @@ private function decrementLikeCount()
*/
public function loggedInUserId()
{
return \Auth::id();
return auth()->id();
}

/**
* Did the currently logged in user like this model
* Example : if($book->liked) { }
* @return boolean
*/
public function getLikedAttribute()
{
return $this->liked();
}

}
93 changes: 93 additions & 0 deletions tests/CommonUseTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php

use Illuminate\Database\Eloquent\Model as Eloquent;
use Illuminate\Database\Capsule\Manager as DB;
use Conner\Likeable\LikeableTrait;

class CommonUseTest extends TestCase
{
public function setUp()
{
parent::setUp();

Eloquent::unguard();

$this->artisan('migrate', [
'--database' => 'testbench',
'--realpath' => realpath(__DIR__.'/../migrations'),
]);
}

protected function getEnvironmentSetUp($app)
{
$app['config']->set('database.default', 'testbench');
$app['config']->set('database.connections.testbench', [
'driver' => 'sqlite',
'database' => ':memory:',
'prefix' => '',
]);

\Schema::create('books', function ($table) {
$table->increments('id');
$table->string('name');
$table->timestamps();
});
}

public function tearDown()
{
\Schema::drop('books');
}

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

$stub->like();

$this->assertEquals(1, $stub->likeCount);
}

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

$stub->like(1);
$stub->like(2);
$stub->like(3);
$stub->like(4);

$this->assertEquals(4, $stub->likeCount);
}

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

$stub->unlike(1);

$this->assertEquals(0, $stub->likeCount);
}

public function test_where_liked_by()
{
Stub::create(['name'=>'A'])->like(1);
Stub::create(['name'=>'B'])->like(1);
Stub::create(['name'=>'C'])->like(1);

$stubs = Stub::whereLikedBy(1)->get();
$shouldBeEmpty = Stub::whereLikedBy(2)->get();

$this->assertEquals(3, $stubs->count());
$this->assertEmpty($shouldBeEmpty);
}
}

class Stub extends Eloquent
{
use LikeableTrait;

protected $connection = 'testbench';

public $table = 'books';
}
2 changes: 1 addition & 1 deletion tests/LikeableTest.php → tests/CounterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use Mockery as m;
use Conner\Likeable\LikeableTrait;

class TaggingTest extends PHPUnit_Framework_TestCase {
class CounterTest extends PHPUnit_Framework_TestCase {

public function tearDown()
{
Expand Down
44 changes: 44 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

use Conner\Likeable\LikeableServiceProvider;

abstract class TestCase extends Orchestra\Testbench\TestCase
{
protected function getPackageProviders($app)
{
return [LikeableServiceProvider::class];
}

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 8dbf026

Please sign in to comment.