From e98aa98ffc0738e1ede98951ed31fc0afc082aa8 Mon Sep 17 00:00:00 2001 From: rtconner Date: Sun, 15 Feb 2015 10:46:55 -0500 Subject: [PATCH 1/2] Add likeCount --- README.md | 6 ++++-- src/Conner/Likeable/LikeableTrait.php | 2 +- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 00a29a2..cea4cb1 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ Trait for Laravel Eloquent models to allow easy implementation of a "like" or "f #### Composer Install "require": { - "rtconner/laravel-likeable": "0.1.*" + "rtconner/laravel-likeable": "0.2.*" } #### Run the migrations @@ -33,7 +33,9 @@ Trait for Laravel Eloquent models to allow easy implementation of a "like" or "f $article->unlike($myUserId); // pass in your own user id $article->unlike(0); // remove likes from the count -- does not check for user - $article->likes; // get count of likes + $article->likeCount; // get count of likes + + $article->likes; // Iterable collection of likes $article->liked(); // check if currently logged in user liked the article $article->liked($myUserId); diff --git a/src/Conner/Likeable/LikeableTrait.php b/src/Conner/Likeable/LikeableTrait.php index effc676..bfd4cdf 100644 --- a/src/Conner/Likeable/LikeableTrait.php +++ b/src/Conner/Likeable/LikeableTrait.php @@ -22,7 +22,7 @@ public function scopeWhereLiked($query, $userId=null) { /** * Populate the $model->likes attribute */ - public function getLikesAttribute() { + public function getLikeCountAttribute() { return $this->likeCounter ? $this->likeCounter->count : 0; } From 9a0d7bcb18b78e87f019bd86fbabe1211590a888 Mon Sep 17 00:00:00 2001 From: rtconner Date: Sun, 15 Feb 2015 10:52:29 -0500 Subject: [PATCH 2/2] Fixes on tests --- composer.json | 2 +- tests/LikeableTest.php | 40 ++++++++++++++++++++-------------------- 2 files changed, 21 insertions(+), 21 deletions(-) diff --git a/composer.json b/composer.json index baaf31e..4892aa5 100644 --- a/composer.json +++ b/composer.json @@ -17,7 +17,7 @@ }, "require-dev": { "phpunit/phpunit": "4.*", - "doctrine/dbal": "2.5.x-dev", + "doctrine/dbal": "2.5.*", "orchestra/testbench": "2.2.*" }, "autoload": { diff --git a/tests/LikeableTest.php b/tests/LikeableTest.php index 4d459a4..dc247aa 100644 --- a/tests/LikeableTest.php +++ b/tests/LikeableTest.php @@ -53,7 +53,7 @@ public function testLike() { $stub->like(2); $stub->like(3); - $this->assertEquals(3, $stub->likes); + $this->assertEquals(3, $stub->likeCount); } public function testUnlike() { @@ -63,24 +63,24 @@ public function testUnlike() { $stub->unlike(2); $stub->unlike(3); - $this->assertEquals(0, $stub->likes); + $this->assertEquals(0, $stub->likeCount); $stub = $this->randomStub(100); $stub->like(2); $stub->like(3); - $this->assertEquals(2, $stub->likes); + $this->assertEquals(2, $stub->likeCount); $stub = $this->randomStub(100); $stub->like(4); $stub->like(4); - $this->assertEquals(3, $stub->likes); + $this->assertEquals(3, $stub->likeCount); $stub = $this->randomStub(100); $stub->unlike(4); - $this->assertEquals(2, $stub->likes); + $this->assertEquals(2, $stub->likeCount); } public function testMultiple() { @@ -96,8 +96,8 @@ public function testMultiple() { $stub2->like(8); $stub2->like(5); - $this->assertEquals(4, $stub1->likes); - $this->assertEquals(3, $stub2->likes); + $this->assertEquals(4, $stub1->likeCount); + $this->assertEquals(3, $stub2->likeCount); } public function test_loggedInUserId() { @@ -112,7 +112,7 @@ public function testNoUser() { for($i=0;$i<$j;$i++) $stub1->like(0); - $this->assertEquals($j, $stub1->likes); + $this->assertEquals($j, $stub1->likeCount); $stub1 = $this->randomStub(6); @@ -120,21 +120,11 @@ public function testNoUser() { for($i=0;$i<$k;$i++) $stub1->unlike(0); - $this->assertEquals($j-$k, $stub1->likes); + $this->assertEquals($j-$k, $stub1->likeCount); $stub2 = $this->randomStub(4); $stub2->like(); - $this->assertEquals(1, $stub2->likes); - } - - private function randomStub($id=null) { - LikeableStub::migrate(); - - if(is_null($id)) { $id = rand(1,100); } - - $stub = LikeableStub::firstOrCreate(['id'=>$id]); - - return $stub; + $this->assertEquals(1, $stub2->likeCount); } public function testLiked() { @@ -159,5 +149,15 @@ public function testWithLiked() { $found = LikeableStub::whereLiked()->count(); $this->assertEquals(2, $found); } + + private function randomStub($id=null) { + LikeableStub::migrate(); + + if(is_null($id)) { $id = rand(1,100); } + + $stub = LikeableStub::firstOrCreate(['id'=>$id]); + + return $stub; + } } \ No newline at end of file