Skip to content

Commit 8dbf026

Browse files
committed
Add more robust unit tests
1 parent 74a1cf4 commit 8dbf026

File tree

5 files changed

+170
-7
lines changed

5 files changed

+170
-7
lines changed

composer.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,18 @@
1616
"illuminate/support": ">=5.0"
1717
},
1818
"require-dev": {
19-
"phpunit/phpunit": ">=4.0",
19+
"orchestra/testbench": "~3.0",
20+
"phpunit/phpunit": "~4.0",
2021
"mockery/mockery": "~0.9"
2122
},
2223
"autoload": {
2324
"psr-4": {
2425
"Conner\\Likeable\\": "src/"
2526
}
27+
},
28+
"autoload-dev": {
29+
"classmap": [
30+
"tests/TestCase.php"
31+
]
2632
}
2733
}

src/LikeableTrait.php

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,18 @@
88
trait LikeableTrait
99
{
1010
/**
11-
* Fetch only records that currently logged in user has liked/followed
11+
* DEPRECATED - Use whereLikedBy()
1212
*/
1313
public function scopeWhereLiked($query, $userId=null)
14+
{
15+
return $this->scopeWhereLikedBy($query, $userId);
16+
}
17+
18+
/**
19+
* Fetch records that are liked by a given user.
20+
* Ex: Book::whereLikedBy(123)->get();
21+
*/
22+
public function scopeWhereLikedBy($query, $userId=null)
1423
{
1524
if(is_null($userId)) {
1625
$userId = $this->loggedInUserId();
@@ -21,6 +30,7 @@ public function scopeWhereLiked($query, $userId=null)
2130
});
2231
}
2332

33+
2434
/**
2535
* Populate the $model->likes attribute
2636
*/
@@ -34,7 +44,7 @@ public function getLikeCountAttribute()
3444
*/
3545
public function likes()
3646
{
37-
return $this->morphMany('\Conner\Likeable\Like', 'likable');
47+
return $this->morphMany(\Conner\Likeable\Like::class, 'likable');
3848
}
3949

4050
/**
@@ -43,7 +53,7 @@ public function likes()
4353
*/
4454
public function likeCounter()
4555
{
46-
return $this->morphOne('\Conner\Likeable\LikeCounter', 'likable');
56+
return $this->morphOne(\Conner\Likeable\LikeCounter::class, 'likable');
4757
}
4858

4959
/**
@@ -86,7 +96,7 @@ public function unlike($userId=null)
8696
->where('user_id', '=', $userId)
8797
->first();
8898

89-
if(!$like) return;
99+
if(!$like) { return; }
90100

91101
$like->delete();
92102
}
@@ -151,7 +161,17 @@ private function decrementLikeCount()
151161
*/
152162
public function loggedInUserId()
153163
{
154-
return \Auth::id();
164+
return auth()->id();
165+
}
166+
167+
/**
168+
* Did the currently logged in user like this model
169+
* Example : if($book->liked) { }
170+
* @return boolean
171+
*/
172+
public function getLikedAttribute()
173+
{
174+
return $this->liked();
155175
}
156176

157177
}

tests/CommonUseTest.php

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
<?php
2+
3+
use Illuminate\Database\Eloquent\Model as Eloquent;
4+
use Illuminate\Database\Capsule\Manager as DB;
5+
use Conner\Likeable\LikeableTrait;
6+
7+
class CommonUseTest extends TestCase
8+
{
9+
public function setUp()
10+
{
11+
parent::setUp();
12+
13+
Eloquent::unguard();
14+
15+
$this->artisan('migrate', [
16+
'--database' => 'testbench',
17+
'--realpath' => realpath(__DIR__.'/../migrations'),
18+
]);
19+
}
20+
21+
protected function getEnvironmentSetUp($app)
22+
{
23+
$app['config']->set('database.default', 'testbench');
24+
$app['config']->set('database.connections.testbench', [
25+
'driver' => 'sqlite',
26+
'database' => ':memory:',
27+
'prefix' => '',
28+
]);
29+
30+
\Schema::create('books', function ($table) {
31+
$table->increments('id');
32+
$table->string('name');
33+
$table->timestamps();
34+
});
35+
}
36+
37+
public function tearDown()
38+
{
39+
\Schema::drop('books');
40+
}
41+
42+
public function test_basic_like()
43+
{
44+
$stub = Stub::create(['name'=>123]);
45+
46+
$stub->like();
47+
48+
$this->assertEquals(1, $stub->likeCount);
49+
}
50+
51+
public function test_multiple_likes()
52+
{
53+
$stub = Stub::create(['name'=>123]);
54+
55+
$stub->like(1);
56+
$stub->like(2);
57+
$stub->like(3);
58+
$stub->like(4);
59+
60+
$this->assertEquals(4, $stub->likeCount);
61+
}
62+
63+
public function test_unike()
64+
{
65+
$stub = Stub::create(['name'=>123]);
66+
67+
$stub->unlike(1);
68+
69+
$this->assertEquals(0, $stub->likeCount);
70+
}
71+
72+
public function test_where_liked_by()
73+
{
74+
Stub::create(['name'=>'A'])->like(1);
75+
Stub::create(['name'=>'B'])->like(1);
76+
Stub::create(['name'=>'C'])->like(1);
77+
78+
$stubs = Stub::whereLikedBy(1)->get();
79+
$shouldBeEmpty = Stub::whereLikedBy(2)->get();
80+
81+
$this->assertEquals(3, $stubs->count());
82+
$this->assertEmpty($shouldBeEmpty);
83+
}
84+
}
85+
86+
class Stub extends Eloquent
87+
{
88+
use LikeableTrait;
89+
90+
protected $connection = 'testbench';
91+
92+
public $table = 'books';
93+
}

tests/LikeableTest.php renamed to tests/CounterTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use Mockery as m;
44
use Conner\Likeable\LikeableTrait;
55

6-
class TaggingTest extends PHPUnit_Framework_TestCase {
6+
class CounterTest extends PHPUnit_Framework_TestCase {
77

88
public function tearDown()
99
{

tests/TestCase.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
use Conner\Likeable\LikeableServiceProvider;
4+
5+
abstract class TestCase extends Orchestra\Testbench\TestCase
6+
{
7+
protected function getPackageProviders($app)
8+
{
9+
return [LikeableServiceProvider::class];
10+
}
11+
12+
public function setUp()
13+
{
14+
parent::setUp();
15+
}
16+
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+
44+
}

0 commit comments

Comments
 (0)