├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── composer.json ├── phpunit.xml ├── src ├── Fenos │ └── Rally │ │ ├── Exceptions │ │ ├── AlreadyFollowerException.php │ │ └── FollowerNotFoundException.php │ │ ├── Facades │ │ └── Rally.php │ │ ├── Models │ │ ├── Follower.php │ │ └── Relations.php │ │ ├── Rally.php │ │ ├── RallyServiceProvider.php │ │ └── Repositories │ │ ├── RallyPolymorphicRepository.php │ │ ├── RallyRepository.php │ │ └── RallyRepositoryInterface.php ├── config │ └── config.php └── migrations │ └── 2014_05_14_093849_create_followers_table.php └── tests ├── RallyPolymorphicRepositoryTest.php ├── RallyRepositoryTest.php └── RallyTest.php /.gitignore: -------------------------------------------------------------------------------- 1 | /vendor 2 | composer.phar 3 | composer.lock 4 | .DS_Store 5 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: php 2 | 3 | php: 4 | - 5.4 5 | - 5.5 6 | - 5.6 7 | - hhvm 8 | 9 | before_script: 10 | - composer self-update 11 | - composer install --prefer-source --no-interaction --dev 12 | 13 | script: phpunit 14 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014 Fabrizio 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Rally (Deprecated) 2 | ===== 3 | 4 | [![Build Status](https://travis-ci.org/fenos/Rally.svg?branch=master)](https://travis-ci.org/fenos/Rally) 5 | [![ProjectStatus](http://stillmaintained.com/fenos/Rally.png)](http://stillmaintained.com/fenos/Rally) 6 | [![Latest Stable Version](https://poser.pugx.org/fenos/rally/v/stable.png)](https://packagist.org/packages/fenos/rally) 7 | [![License](https://poser.pugx.org/fenos/rally/license.png)](https://packagist.org/packages/fenos/rally) 8 | 9 | Follow, Let Follow you, Follow with Rally. Rally is a plugin that implement in your application the follow system. It is quick to implement on your laravel project. 10 | It give you the freedom to create your own followers system. It is can be polymorphic, so you can follow anybody or anything you want. The package has been released for laravel 4.* 11 | 12 | * [Installation](#installation) 13 | * [Documentation](#documentation) 14 | * [Follow](#follow) 15 | * [unFollow](#unfollow) 16 | * [Check if Is follow of](#check-if-is-follow-of) 17 | * [Get Lists of followers](#get-lists-of-followers) 18 | * [Count Followers](#count-followers) 19 | * [Note](#note) 20 | * [Tests](#tests) 21 | * [Credits](#credits) 22 | 23 | 24 | ## Installation ## 25 | 26 | ### Step 1 ### 27 | 28 | Add it on your composer.json 29 | 30 | ~~~ 31 | "fenos/rally": "1.0.*" 32 | ~~~ 33 | 34 | and run **composer update** 35 | 36 | 37 | ### Step 2 ### 38 | 39 | Add the following string to **app/config/app.php** 40 | 41 | **Providers array:** 42 | 43 | ~~~ 44 | 'Fenos\Rally\RallyServiceProvider' 45 | ~~~ 46 | 47 | **Aliases array:** 48 | 49 | ~~~ 50 | 'Rally' => 'Fenos\Rally\Facades\Rally' 51 | ~~~ 52 | 53 | ### Step 3 ### 54 | 55 | #### Migration #### 56 | 57 | Make sure that your settings on **app/config/database.php** are correct, then make the migration typing: 58 | 59 | ~~~ 60 | php artisan migrate --package="fenos/rally" 61 | ~~~ 62 | 63 | ### Step 4 ### 64 | 65 | #### Include relations ### 66 | 67 | Rally comes with some relations already setted for you, you just need to insert the `trait` that I made for you in all your models you wish to have relations with Rally. 68 | 69 | ~~~ 70 | 71 | class User extends Eloquent 72 | { 73 | use \Fenos\Rally\Models\Relations; 74 | } 75 | 76 | ~~~ 77 | 78 | That's it your have done. 79 | 80 | ## Documentation ## 81 | 82 | How i said on the installation, Rally can be **Polymorphic**, it means that if you have `Users` and `Teams` as entity of your application they can follow between them. But it is just up to you. If you realize that 83 | you don't need of it, You can keep it as a single model binding. 84 | 85 | The key to enable or disable the polymorphic relation is in the configuration files. You just need to push them and change the key polymorphic to `true`. 86 | if instead you want to keep the plugin as 1 model but the `User` model is not your main model change it ;) 87 | 88 | ~~~ 89 | php artisan config:publish fenos/rally 90 | ~~~ 91 | 92 | 93 | ### Follow ### 94 | 95 | For start to be followers of a entity when it comes polymorphically you will use the following method let me show you. 96 | 97 | ~~~ 98 | try 99 | { 100 | Rally::follower('User',$user_id)->follow('Team',$team_id); 101 | } 102 | catch(\Fenos\Rally\Exceptions\AlreadyFollowerException $e) 103 | { 104 | // is already fan 105 | } 106 | ~~~ 107 | With only fews line of code the user has started to follow the team. 108 | 109 | If instead you use **Rally** as normal 110 | ~~~ 111 | try 112 | { 113 | Rally::follower($user_id)->follow($user_id); 114 | } 115 | catch(\Fenos\Rally\Exceptions\AlreadyFollowerException $e) 116 | { 117 | // is already follower 118 | } 119 | ~~~ 120 | 121 | Let me explain it. The method `follower()` specify the user that want to be follower, so if Rally comes polymorphically you have to specify as 122 | `first paramter` The model of it, as `second parameter` the id if instead is not polymorphically just the ID. 123 | Almost same the method `follow()` in this method you specify who will be followed parameters are same. 124 | 125 | ### UnFollow ### 126 | 127 | If you don't want follow someone anymore you will use this method: 128 | 129 | **Polymorphically** 130 | ~~~ 131 | try 132 | { 133 | Rally::follower('User',$user_id)->unFollow('Team',$team_id); 134 | } 135 | catch(\Fenos\Rally\Exceptions\FollowerNotFoundException $e) 136 | { 137 | // the user already doesn't follow him 138 | } 139 | ~~~ 140 | 141 | **Normal** 142 | ~~~ 143 | try 144 | { 145 | Rally::follower($user_id)->unFollow($user_id); 146 | } 147 | catch(\Fenos\Rally\Exceptions\FollowerNotFoundException $e) 148 | { 149 | // the user already doesn't follow him 150 | } 151 | ~~~ 152 | 153 | ### Check if Is follow of ### 154 | 155 | If you want to know a given User if has following someone use: 156 | 157 | **Polymorphically** 158 | ~~~ 159 | Rally::follower('User',$user_id)->isFollowerOf('Team',$team_id); 160 | ~~~ 161 | 162 | **Normal** 163 | ~~~ 164 | Rally::follower($user_id)->isFollowerOf($user_id); // return Boolean 165 | ~~~ 166 | 167 | ### Get lists of followers ### 168 | 169 | Well Rally give to you a easy way to get the lists of your followers but remeber that you implemented the `trait` with the relations 170 | in your model, So you can even access to them directly from that, I suggest that. But let me show you if you want use Rally. 171 | 172 | **Polymorphically** 173 | ~~~ 174 | Rally::follower('User',$user_id)->getLists(); 175 | 176 | Rally::follower('User',$user_id)->getLists(['orderBy' => 'DESC', 'limit' => 10]); 177 | 178 | Rally::follower('User',$user_id)->getLists(['orderBy' => 'DESC', 'paginate' => 5 ]); 179 | ~~~ 180 | 181 | **Normal** 182 | ~~~ 183 | Rally::follower($user_id)->getLists(); 184 | 185 | Rally::follower($user_id)->getLists(['orderBy' => 'DESC', 'limit' => 10]); 186 | 187 | Rally::follower($user_id)->getLists(['orderBy' => 'DESC', 'paginate' => 5 ]); 188 | ~~~ 189 | 190 | You can even chain `count()` it return a Collection so you can use all the methods of it. 191 | 192 | #### Count Followers #### 193 | 194 | You Need just the numbers of followers and nothing else? 195 | 196 | **Polymorphically** 197 | ~~~ 198 | Rally::follower('User',$user_id)->count(); 199 | ~~~ 200 | 201 | **Normal** 202 | ~~~ 203 | Rally::follower($user_id)->count(); 204 | ~~~ 205 | 206 | I hope you'll enjoy it. 207 | 208 | ### Note ### 209 | 210 | I made it with <3 211 | 212 | ### Tests ### 213 | 214 | For run the tests make sure to have phpUnit and Mockery installed 215 | 216 | ### Credits ### 217 | 218 | © Copyright Fabrizio Fenoglio 219 | 220 | Released package under MIT Licence. 221 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "fenos/rally", 3 | "description": "Followers system for laravel 4", 4 | "keywords": ["Follow", "Followers", "laravel"], 5 | "license": "MIT", 6 | "authors": [ 7 | { 8 | "name": "Fabrizio Fenoglio", 9 | "email": "fabri_feno@yahoo.it" 10 | } 11 | ], 12 | "require": { 13 | "php": ">=5.4.0", 14 | "illuminate/support": "~4.1" 15 | }, 16 | "require-dev": { 17 | "phpunit/phpunit": "~4.0", 18 | "mockery/mockery": "~0.9.1" 19 | }, 20 | "autoload": { 21 | "classmap": [ 22 | "src/migrations" 23 | ], 24 | "psr-0": { 25 | "Fenos\\Rally\\": "src/" 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /phpunit.xml: -------------------------------------------------------------------------------- 1 | 2 | 13 | 14 | 15 | ./tests/ 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /src/Fenos/Rally/Exceptions/AlreadyFollowerException.php: -------------------------------------------------------------------------------- 1 | morphTo(); 25 | } 26 | 27 | return $this->belongsTo(\Config::get('rally::model'),'follower_id'); 28 | } 29 | 30 | public function followed() 31 | { 32 | if (\Config::get('rally::polymorphic') !== false) 33 | { 34 | return $this->morphTo(); 35 | } 36 | 37 | return $this->hasOne(\Config::get('rally::model'),'follower_id'); 38 | } 39 | 40 | } 41 | -------------------------------------------------------------------------------- /src/Fenos/Rally/Models/Relations.php: -------------------------------------------------------------------------------- 1 | hasMany('Fenos\Rally\Models\Follower','followed_id'); 20 | } 21 | 22 | return $this->morphMany('Fenos\Rally\Models\Follower','follower'); 23 | } 24 | 25 | public function followed() 26 | { 27 | if (\Config::get('rally::polymorphic') === false) 28 | { 29 | return $this->hasMany('Fenos\Rally\Models\Follower','follower_id'); 30 | } 31 | 32 | return $this->morphMany('Fenos\Rally\Models\Follower','follower'); 33 | } 34 | 35 | } 36 | -------------------------------------------------------------------------------- /src/Fenos/Rally/Rally.php: -------------------------------------------------------------------------------- 1 | rallyRepository = $rallyRepository; 45 | $this->config = $config; 46 | } 47 | 48 | /** 49 | * Initialize the follower 50 | * 51 | * @param $follower_type 52 | * @param $follower_id 53 | * @return $this 54 | */ 55 | public function follower($follower_type,$follower_id = false) 56 | { 57 | $this->lightValidation($follower_type, $follower_id); 58 | 59 | // store the informations on the property 60 | // if polymorphic is enabled it will store 2 paramets 61 | // others ways just 1 the ID 62 | if ($this->config->get('rally::polymorphic')) 63 | { 64 | $this->follower['follower_type'] = $follower_type; 65 | $this->follower['follower_id'] = $follower_id; 66 | } 67 | else 68 | { 69 | // I get the first parameter to be the id 70 | $this->follower['follower_id'] = $follower_type; 71 | } 72 | 73 | return $this; 74 | } 75 | 76 | /** 77 | * Follow a entity 78 | * 79 | * @param $followed_type 80 | * @param $followed_id 81 | * @return \Illuminate\Database\Eloquent\Model|static 82 | * @throws Exceptions\AlreadyFollowerException 83 | * @throws \InvalidArgumentException 84 | */ 85 | public function follow($followed_type,$followed_id = false) 86 | { 87 | $this->lightValidation($followed_type, $followed_id); 88 | 89 | $isFollower = $this->isFollowerOf($followed_type,$followed_id); 90 | 91 | // before insert the relations between followers I need to check 92 | // if the follower is already fan of him. 93 | if ($isFollower !== false) 94 | { 95 | throw new AlreadyFollowerException('The follower is already followers of this Entity'); 96 | } 97 | 98 | // check that there are the informations of the follower 99 | $this->checkFollowerInformation(); 100 | 101 | if ($this->config->get('rally::polymorphic') !== false) 102 | { 103 | $this->follower['followed_type'] = $followed_type; 104 | $this->follower['followed_id'] = $followed_id; 105 | } 106 | else 107 | { 108 | $this->follower['followed_id'] = $followed_type; 109 | } 110 | 111 | 112 | $follow = $this->rallyRepository->follow($this->follower); 113 | 114 | return $follow; 115 | } 116 | 117 | /** 118 | * Check if is follower 119 | * 120 | * @param $followed_type 121 | * @param $followed_id 122 | * @return bool 123 | * @throws \InvalidArgumentException 124 | */ 125 | public function isFollowerOf($followed_type,$followed_id = false) 126 | { 127 | // check that there are the informations of the follower 128 | $this->checkFollowerInformation(); 129 | 130 | if ($this->config->get('rally::polymorphic')) 131 | { 132 | $this->follower['followed_type'] = $followed_type; 133 | $this->follower['followed_id'] = $followed_id; 134 | } 135 | else 136 | { 137 | $this->follower['followed_id'] = $followed_type; 138 | } 139 | 140 | // check if the current entity is follower of the current entity 141 | $check = $this->rallyRepository->isFollower($this->follower); 142 | 143 | if (is_null($check)) 144 | { 145 | return false; 146 | } 147 | 148 | return $check; 149 | } 150 | 151 | /** 152 | * Get list of following 153 | * 154 | * @param array $filters 155 | * @return \Illuminate\Database\Eloquent\Collection|mixed|static[] 156 | */ 157 | public function getListsFollowing(array $filters = []) 158 | { 159 | $this->checkFollowerInformation(); 160 | 161 | return $this->rallyRepository->listsFollowing($this->follower,$filters); 162 | } 163 | 164 | /** 165 | * Unfollow Someone 166 | * 167 | * @param $followed_type 168 | * @param $followed_id 169 | * @return mixed 170 | * @throws Exceptions\FollowerNotFoundException 171 | */ 172 | public function unFollow($followed_type,$followed_id = false) 173 | { 174 | $isFollower = $this->isFollowerOf($followed_type,$followed_id); 175 | 176 | if ($isFollower !== false) 177 | { 178 | return $this->rallyRepository->unFollow($isFollower); 179 | } 180 | 181 | throw new FollowerNotFoundException('Relation with thoose followers not found'); 182 | } 183 | 184 | /** 185 | * Get lits of the followers 186 | * 187 | * @param array $filters 188 | * @return \Illuminate\Database\Eloquent\Collection|static[] 189 | * @throws \InvalidArgumentException 190 | */ 191 | public function getLists(array $filters = []) 192 | { 193 | $this->checkFollowerInformation(); 194 | 195 | return $this->rallyRepository->listsFollowers($this->follower,$filters); 196 | } 197 | 198 | /** 199 | * Get empty Query 200 | * 201 | * @param array $filters 202 | * @return mixed 203 | */ 204 | public function emptyQuery($filters = []) 205 | { 206 | $this->checkFollowerInformation(); 207 | 208 | return $this->rallyRepository->emptyQuery($this->follower,$filters); 209 | } 210 | 211 | /** 212 | * Get list of followers knowing 213 | * if you are follower or no 214 | * 215 | * @param $entity_id 216 | * @param $entity_type 217 | * @param array $filters 218 | * @return mixed 219 | */ 220 | public function listsWithImFollow($entity_id,$entity_type,array $filters = []) 221 | { 222 | $this->checkFollowerInformation(); 223 | 224 | return $this->rallyRepository->listsWithImFollow($this->follower,$entity_id,$entity_type,$filters); 225 | } 226 | 227 | /** 228 | * Get only the number of followers 229 | * 230 | * @return mixed 231 | * @throws \InvalidArgumentException 232 | */ 233 | public function count() 234 | { 235 | // check that there are the informations of the follower 236 | $this->checkFollowerInformation(); 237 | 238 | return $this->rallyRepository->countFollowers($this->follower); 239 | } 240 | 241 | /** 242 | * Count following 243 | * 244 | * @return mixed 245 | */ 246 | public function countFollowing() 247 | { 248 | // check that there are the informations of the follower 249 | $this->checkFollowerInformation(); 250 | 251 | return $this->rallyRepository->countFollowing($this->follower); 252 | } 253 | 254 | /** 255 | * Light validation for make sure that any kind of 256 | * value different to string or number will go on the databse 257 | * This not replace a normal validation 258 | * 259 | * @param $follower_type 260 | * @param $follower_id 261 | * @throws \InvalidArgumentException 262 | */ 263 | public function lightValidation($follower_type, $follower_id = false) 264 | { 265 | // Check if the follower type is a string 266 | // and nothing else 267 | if ($this->config->get('rally::polymorphic')) 268 | { 269 | if (!is_string($follower_type)) { 270 | throw new \InvalidArgumentException('The follower type must be a string'); 271 | } 272 | 273 | // Check if the id is a number and nothing else 274 | if (!is_numeric($follower_id)) { 275 | throw new \InvalidArgumentException('The follower Id must be a integer'); 276 | } 277 | } 278 | else 279 | { 280 | // Check if the id is a number and nothing else 281 | if (!is_numeric($follower_type)) { 282 | throw new \InvalidArgumentException('The follower Id must be a integer'); 283 | } 284 | } 285 | } 286 | 287 | /** 288 | * Check follower Information 289 | * 290 | * @throws \InvalidArgumentException 291 | */ 292 | public function checkFollowerInformation() 293 | { 294 | // if the polymorphic option is active we must to check about 295 | // 2 partameters ID and TYPE if no Just 1 the ID, 296 | if ($this->config->get('rally::polymorphic') !== false) 297 | { 298 | // check that there are the informations of the follower 299 | if (count($this->follower) < 2) { 300 | throw new \InvalidArgumentException('You must specify the ID and type of the follower'); 301 | } 302 | } 303 | else 304 | { 305 | // check that there are the informations of the follower 306 | if (count($this->follower) < 1) { 307 | throw new \InvalidArgumentException('You must specify the ID of the follower'); 308 | } 309 | } 310 | 311 | } 312 | 313 | /** 314 | * @param $follower_type 315 | * @param $follower_id 316 | */ 317 | public function setFollowerPolymorpich($follower_type,$follower_id) 318 | { 319 | $this->follower['follower_type'] = $follower_type; 320 | $this->follower['follower_id'] = $follower_id; 321 | } 322 | 323 | /** 324 | * @param $follower_id 325 | */ 326 | public function setFollower($follower_id) 327 | { 328 | $this->follower['follower_id'] = $follower_id; 329 | } 330 | } 331 | 332 | // Rally::follower('Team',$team_id)->follow('User',$user_id); 333 | // Rally::follower('Team',$team_id)->isFollowerOf('User',$user_id); 334 | // Rally::follower('Team',$team_id)->unFollow('User',$user_id); 335 | -------------------------------------------------------------------------------- /src/Fenos/Rally/RallyServiceProvider.php: -------------------------------------------------------------------------------- 1 | package('fenos/rally'); 23 | } 24 | 25 | /** 26 | * Register the service provider. 27 | * 28 | * @return void 29 | */ 30 | public function register() 31 | { 32 | $this->rally(); 33 | $this->repositories(); 34 | } 35 | 36 | public function rally() 37 | { 38 | $this->app['rally'] = $this->app->share(function($app){ 39 | 40 | return new Rally( 41 | 42 | $app->make('rally.repository'), 43 | $app['config'] 44 | ); 45 | }); 46 | } 47 | 48 | public function repositories() 49 | { 50 | $this->app->bind('rally.repository', function($app){ 51 | 52 | if ($this->app['config']->get('rally::polymorphic') !== false) 53 | { 54 | if (($bindClass = $this->app['config']->get('rally::polymorphic.repository')) == null) { 55 | $bindClass = "\Fenos\Rally\Repositories\RallyPolymorphicRepository"; 56 | } 57 | } 58 | else 59 | { 60 | if (($bindClass = $this->app['config']->get('rally::repository')) == null) { 61 | $bindClass = "\Fenos\Rally\Repositories\RallyRepository"; 62 | } 63 | } 64 | 65 | return new $bindClass( 66 | new Follower(), 67 | $app['db'] 68 | ); 69 | }); 70 | 71 | $this->app->bind('Fenos\Rally\Repositories\RallyRepositoryInterface','rally.repository'); 72 | 73 | // $this->app->bind('rally.polymorphic.repository', function($app){ 74 | // return new Repositories\RallyPolymorphicRepository( 75 | // new Follower(), 76 | // $app['db'] 77 | // ); 78 | // }); 79 | } 80 | 81 | /** 82 | * Get the services provided by the provider. 83 | * 84 | * @return array 85 | */ 86 | public function provides() 87 | { 88 | return array(); 89 | } 90 | 91 | } 92 | -------------------------------------------------------------------------------- /src/Fenos/Rally/Repositories/RallyPolymorphicRepository.php: -------------------------------------------------------------------------------- 1 | follow = $follow; 39 | $this->db = $db; 40 | 41 | parent::__construct($this->follow,$this->db); 42 | } 43 | 44 | /** 45 | * @param array $follower 46 | * @return mixed 47 | */ 48 | public function isFollower(array $follower) 49 | { 50 | return $this->follow->where('follower_type',$follower['follower_type']) 51 | ->where('follower_id', $follower['follower_id']) 52 | ->where(function($where) use ($follower){ 53 | $where->where('followed_type',$follower['followed_type']) 54 | ->where('followed_id',$follower['followed_id']); 55 | })->first(); 56 | 57 | } 58 | 59 | /** 60 | * Get followed lists 61 | * 62 | * @param array $followed 63 | * @param $filters 64 | * @return mixed 65 | */ 66 | public function listsFollowed(array $followed,$filters) 67 | { 68 | $lists = $this->follow->with('follower') 69 | ->where('followers.follower_id',$followed['follower_id']) 70 | ->where('followers.follower_type', $followed['follower_type']) 71 | ->leftJoin('followers as fol',function($join) use ($followed) 72 | { 73 | $join->on('fol.follower_id','=','followers.followed_id') 74 | ->on('fol.followed_id','=','followers.follower_id'); 75 | }) 76 | ->groupBy('followers.followed_id') 77 | ->groupBy('fol.followed_id') 78 | ->select('followers.*','fol.follower_id as is_fan'); 79 | 80 | $this->addFilters($lists,$filters); 81 | 82 | if (array_key_exists('paginate',$filters)) 83 | { 84 | return $lists->paginate($filters['paginate']); 85 | } 86 | 87 | return $lists->get(); 88 | } 89 | 90 | /** 91 | * Get followed lists 92 | * 93 | * @param array $followed 94 | * @param $only 95 | * @param $filters 96 | * @return mixed 97 | */ 98 | public function listsFollowedOnly(array $followed,$only,$filters) 99 | { 100 | $lists = $this->follow->with('follower') 101 | ->where('followers.follower_id',$followed['follower_id']) 102 | ->where('followers.follower_type', $followed['follower_type']) 103 | ->leftJoin('followers as fol',function($join) use ($followed) 104 | { 105 | $join->on('fol.follower_id','=','followers.followed_id') 106 | ->on('fol.followed_id','=','followers.follower_id'); 107 | }) 108 | ->where('followers.followed_type',$only) 109 | ->groupBy('followers.follower_id') 110 | ->groupBy('fol.follower_id') 111 | ->select('followers.*','fol.follower_id as is_fan'); 112 | 113 | $this->addFilters($lists,$filters); 114 | 115 | if (array_key_exists('paginate',$filters)) 116 | { 117 | return $lists->paginate($filters['paginate']); 118 | } 119 | 120 | return $lists->get(); 121 | } 122 | 123 | /** 124 | * @param array $followed 125 | * @param $filters 126 | * @return \Illuminate\Database\Eloquent\Collection|static[] 127 | */ 128 | public function listsFollowers(array $followed,$filters) 129 | { 130 | $lists = $this->follow->with('follower') 131 | ->where('followers.followed_type', $followed['follower_type']) 132 | ->where('followers.followed_id',$followed['follower_id']) 133 | ->leftJoin('followers as fol',function($join) use ($followed) 134 | { 135 | $join->on('fol.follower_id','=','followers.followed_id') 136 | ->on('fol.followed_id','=','followers.follower_id'); 137 | }) 138 | ->groupBy('followers.follower_id') 139 | ->groupBy('fol.follower_id') 140 | ->select('followers.*',"fol.followed_id as is_fan"); 141 | 142 | $this->addFilters($lists,$filters); 143 | 144 | if (array_key_exists('paginate',$filters)) 145 | { 146 | return $lists->paginate($filters['paginate']); 147 | } 148 | 149 | return $lists->get(); 150 | } 151 | 152 | /** 153 | * @param $entity_id 154 | * @param $entity_type 155 | * @param array $followed 156 | * @param $filters 157 | * @return \Illuminate\Database\Eloquent\Collection|\Illuminate\Pagination\Paginator|static[] 158 | */ 159 | public function listsWithImFollow($entity_id,$entity_type,array $followed,$filters) 160 | { 161 | $lists = $this->follow->with('follower') 162 | ->where('followed_type', $followed['follower_type']) 163 | ->where('followed_id',$followed['follower_id']); 164 | 165 | $this->addFilters($lists,$filters); 166 | 167 | if (array_key_exists('paginate',$filters)) 168 | { 169 | return $lists->paginate($filters['paginate']); 170 | } 171 | 172 | return $lists->get(); 173 | } 174 | 175 | /** 176 | * @param array $followed 177 | * @param $filters 178 | * @return \Illuminate\Database\Eloquent\Collection|static[] 179 | */ 180 | public function listsFollowing(array $followed,$filters) 181 | { 182 | $lists = $this->follow->with('follower') 183 | ->where('follower_type', $followed['follower_type']) 184 | ->where('follower_id',$followed['follower_id']); 185 | 186 | $this->addFilters($lists,$filters); 187 | 188 | if (array_key_exists('paginate',$filters)) 189 | { 190 | return $lists->paginate($filters['paginate']); 191 | } 192 | 193 | return $lists->get(); 194 | } 195 | 196 | /** 197 | * @param array $followed 198 | * @return mixed 199 | */ 200 | public function countFollowers(array $followed) 201 | { 202 | return $this->follow->select($this->db->raw('Count(*) as numbers_followers')) 203 | ->where('followed_type', $followed['follower_type']) 204 | ->where('followed_id',$followed['follower_id'])->first(); 205 | } 206 | 207 | /** 208 | * @param array $followed 209 | * @return mixed 210 | */ 211 | public function countFollowing(array $followed) 212 | { 213 | return $this->follow->select($this->db->raw('Count(*) as numbers_followers')) 214 | ->where('follower_type', $followed['follower_type']) 215 | ->where('follower_id',$followed['follower_id'])->first(); 216 | } 217 | 218 | /** 219 | * @param $followed 220 | * @param array $filters 221 | * @return mixed 222 | */ 223 | public function emptyQuery($followed,array $filters) 224 | { 225 | $lists = $this->follow->with('follower') 226 | ->where('followers.followed_type', $followed['follower_type']) 227 | ->where('followers.followed_id',$followed['follower_id']); 228 | 229 | $this->addFilters($lists,$filters); 230 | 231 | return $lists; 232 | } 233 | } 234 | -------------------------------------------------------------------------------- /src/Fenos/Rally/Repositories/RallyRepository.php: -------------------------------------------------------------------------------- 1 | follow = $follow; 38 | $this->db = $db; 39 | } 40 | 41 | /** 42 | * @param array $follow_information 43 | * @return \Illuminate\Database\Eloquent\Model|static 44 | */ 45 | public function follow(array $follow_information) 46 | { 47 | return $this->follow->create($follow_information); 48 | } 49 | 50 | /** 51 | * @param $objectFollow 52 | * @return mixed 53 | */ 54 | public function unFollow($objectFollow) 55 | { 56 | return $objectFollow->delete(); 57 | } 58 | 59 | /** 60 | * @param array $follower 61 | * @return mixed 62 | */ 63 | public function isFollower(array $follower) 64 | { 65 | return $this->follow->where('follower_id', $follower['follower_id']) 66 | ->where('followed_id',$follower['followed_id']) 67 | ->first(); 68 | 69 | } 70 | 71 | /** 72 | * Get followed lists 73 | * 74 | * @param array $followed 75 | * @param $filters 76 | * @return mixed 77 | */ 78 | public function listsFollowed(array $followed,$filters) 79 | { 80 | $lists = $this->follow->with('follower') 81 | ->where('followers.follower_id',$followed['follower_id']) 82 | ->leftJoin('followers as fol',function($join) use ($followed) 83 | { 84 | $join->on('fol.follower_id','=','followers.followed_id') 85 | ->on('fol.followed_id','=','followers.follower_id'); 86 | }) 87 | ->groupBy('followers.followed_id') 88 | ->groupBy('fol.followed_id') 89 | ->select('followers.*','fol.follower_id as is_fan'); 90 | 91 | $this->addFilters($lists,$filters); 92 | 93 | if (array_key_exists('paginate',$filters)) 94 | { 95 | return $lists->paginate($filters['paginate']); 96 | } 97 | 98 | return $lists->get(); 99 | } 100 | 101 | /** 102 | * Get followed lists only 1 103 | * type 104 | * 105 | * @param array $followed 106 | * @param $only 107 | * @param $filters 108 | * @return mixed 109 | */ 110 | public function listsFollowedOnly(array $followed,$only,$filters) 111 | { 112 | $lists = $this->follow->with('follower') 113 | ->where('followers.follower_id',$followed['follower_id']) 114 | ->leftJoin('followers as fol',function($join) use ($followed) 115 | { 116 | $join->on('fol.follower_id','=','followers.followed_id') 117 | ->on('fol.followed_id','=','followers.follower_id'); 118 | }) 119 | ->where('followers.followed_type',$only) 120 | ->groupBy('followers.follower_id') 121 | ->groupBy('fol.follower_id') 122 | ->select('followers.*','fol.follower_id as is_fan'); 123 | 124 | $this->addFilters($lists,$filters); 125 | 126 | if (array_key_exists('paginate',$filters)) 127 | { 128 | return $lists->paginate($filters['paginate']); 129 | } 130 | 131 | return $lists->get(); 132 | } 133 | 134 | /** 135 | * @param array $followed 136 | * @param $filters 137 | * @return \Illuminate\Database\Eloquent\Collection|static[] 138 | */ 139 | public function listsFollowers(array $followed,$filters) 140 | { 141 | $lists = $this->follow->with('follower') 142 | ->where('followers.followed_id',$followed['follower_id']) 143 | ->leftJoin('followers as fol',function($join) use ($followed) 144 | { 145 | $join->on('fol.follower_id','=','followers.followed_id') 146 | ->on('fol.followed_id','=','followers.follower_id'); 147 | }) 148 | ->groupBy('followers.follower_id') 149 | ->groupBy('fol.follower_id') 150 | ->select('followers.*','fol.follower_id as is_fan'); 151 | 152 | $this->addFilters($lists,$filters); 153 | 154 | if (array_key_exists('paginate',$filters)) 155 | { 156 | return $lists->paginate($filters['paginate']); 157 | } 158 | 159 | return $lists->get(); 160 | } 161 | 162 | /** 163 | * @param array $followed 164 | * @param $filters 165 | * @return \Illuminate\Database\Eloquent\Collection|static[] 166 | */ 167 | public function listsFollowing(array $followed,$filters) 168 | { 169 | $lists = $this->follow->with('follower') 170 | ->where('follower_id',$followed['follower_id']); 171 | 172 | $this->addFilters($lists,$filters); 173 | 174 | if (array_key_exists('paginate',$filters)) 175 | { 176 | return $lists->paginate($filters['paginate']); 177 | } 178 | 179 | return $lists->get(); 180 | } 181 | 182 | /** 183 | * @param array $followed 184 | * @return mixed 185 | */ 186 | public function countFollowers(array $followed) 187 | { 188 | return $this->follow->select($this->db->raw('Count(*) as numbers_followers')) 189 | ->where('followed_id',$followed['follower_id'])->first(); 190 | } 191 | 192 | /** 193 | * @param array $followed 194 | * @return mixed 195 | */ 196 | public function countFollowing(array $followed) 197 | { 198 | return $this->follow->select($this->db->raw('Count(*) as numbers_followers')) 199 | ->where('follower_id',$followed['follower_id'])->first(); 200 | } 201 | 202 | /** 203 | * Add filters to the selects queries 204 | * 205 | * @param $objectBuilder 206 | * @param array $filters 207 | */ 208 | public function addFilters($objectBuilder,array $filters) 209 | { 210 | if (count($filters) > 0) 211 | { 212 | foreach($filters as $key => $filter) 213 | { 214 | if ($key == "orderBy") 215 | { 216 | $field = 'created_at'; 217 | $objectBuilder->{$key}($field,$filter); 218 | } 219 | 220 | if ($key == "limit") 221 | { 222 | $objectBuilder->{$key}($filter); 223 | } 224 | } 225 | } 226 | } 227 | 228 | /** 229 | * Initialize empty query 230 | * 231 | * @param $followed 232 | * @param array $filters 233 | * @return mixed 234 | */ 235 | public function emptyQuery($followed, array $filters) 236 | { 237 | $lists = $this->follow->with('follower') 238 | ->where('followed_id',$followed['follower_id']); 239 | 240 | $this->addFilters($lists,$filters); 241 | 242 | return $lists; 243 | } 244 | } 245 | -------------------------------------------------------------------------------- /src/Fenos/Rally/Repositories/RallyRepositoryInterface.php: -------------------------------------------------------------------------------- 1 | false, 6 | 7 | 'model' => "User" 8 | 9 | ); 10 | -------------------------------------------------------------------------------- /src/migrations/2014_05_14_093849_create_followers_table.php: -------------------------------------------------------------------------------- 1 | increments('id'); 17 | $table->integer('follower_id')->index(); 18 | $table->string('follower_type')->index(); 19 | $table->integer('followed_id')->index(); 20 | $table->string('followed_type')->index(); 21 | $table->timestamps(); 22 | }); 23 | } 24 | 25 | 26 | /** 27 | * Reverse the migrations. 28 | * 29 | * @return void 30 | */ 31 | public function down() 32 | { 33 | Schema::drop('followers'); 34 | } 35 | 36 | } 37 | -------------------------------------------------------------------------------- /tests/RallyPolymorphicRepositoryTest.php: -------------------------------------------------------------------------------- 1 | mockEloquent = m::mock('Illuminate\Database\Eloquent\Model'); 34 | 35 | $this->rallyRepo = new RallyPolymorphicRepository( 36 | $this->follower = m::mock('Fenos\Rally\Models\Follower'), 37 | $this->db = m::mock('Illuminate\Database\DatabaseManager') 38 | ); 39 | } 40 | 41 | /** 42 | * TearDown 43 | */ 44 | public function tearDown() 45 | { 46 | m::close(); 47 | } 48 | 49 | public function test_is_follower() 50 | { 51 | $createFollowRelation = [ 52 | 53 | 'follower_type' => 'User', 54 | 'follower_id' => 1, 55 | 'followed_type' => 'Team', 56 | 'followed_id' => 2 57 | ]; 58 | 59 | $this->follower->shouldReceive('where') 60 | ->once() 61 | ->with('follower_type','User') 62 | ->andReturn($this->follower); 63 | 64 | $this->follower->shouldReceive('where') 65 | ->once() 66 | ->with('follower_id',1) 67 | ->andReturn($this->follower); 68 | 69 | // to reviewe test closure 70 | $this->follower->shouldReceive('where') 71 | ->once() 72 | ->andReturn($this->follower); 73 | 74 | $this->follower->shouldReceive('first') 75 | ->once() 76 | ->with() 77 | ->andReturn($this->follower); 78 | 79 | $this->rallyRepo->isFollower($createFollowRelation); 80 | } 81 | 82 | public function test_count_lists_of_followers() 83 | { 84 | $listsFollowRelation = [ 85 | 86 | 'follower_type' => 'User', 87 | 'follower_id' => 1, 88 | ]; 89 | 90 | $mockRepo = m::mock('Fenos\Rally\Repositories\RallyPolymorphicRepository[addFilters]',[$this->follower,$this->db]); 91 | 92 | $this->follower->shouldReceive('with') 93 | ->once() 94 | ->with('follower') 95 | ->andReturn($this->follower); 96 | 97 | $this->follower->shouldReceive('where') 98 | ->with('followers.followed_type','User') 99 | ->once() 100 | ->andReturn($this->follower); 101 | 102 | $this->follower->shouldReceive('where') 103 | ->once() 104 | ->with('followers.followed_id',1) 105 | ->andReturn($this->follower); 106 | 107 | $this->follower->shouldReceive('leftJoin') 108 | ->once() 109 | ->andReturn($this->follower); // when query is solve fix this test 110 | 111 | $this->follower->shouldReceive('groupBy') 112 | ->once() 113 | ->with('followers.follower_id') 114 | ->andReturn($this->follower); 115 | 116 | $this->follower->shouldReceive('groupBy') 117 | ->once() 118 | ->with('fol.follower_id') 119 | ->andReturn($this->follower); 120 | 121 | $this->follower->shouldReceive('select') 122 | ->once() 123 | ->andReturn($this->follower); 124 | 125 | $mockRepo->shouldReceive('addFilters') 126 | ->once() 127 | ->with($this->follower,[]) 128 | ->andReturn(null); 129 | 130 | $this->follower->shouldReceive('get') 131 | ->once() 132 | ->andReturn($this->follower); 133 | 134 | $result = $mockRepo->listsFollowers($listsFollowRelation,[]); 135 | 136 | $this->assertInstanceOf('Fenos\Rally\Models\Follower',$result); 137 | } 138 | 139 | public function test_count_lists_of_following() 140 | { 141 | $listsFollowRelation = [ 142 | 143 | 'follower_type' => 'User', 144 | 'follower_id' => 1, 145 | ]; 146 | 147 | $mockRepo = m::mock('Fenos\Rally\Repositories\RallyPolymorphicRepository[addFilters]',[$this->follower,$this->db]); 148 | 149 | $this->follower->shouldReceive('with') 150 | ->once() 151 | ->with('follower') 152 | ->andReturn($this->follower); 153 | 154 | $this->follower->shouldReceive('where') 155 | ->with('follower_type','User') 156 | ->once() 157 | ->andReturn($this->follower); 158 | 159 | $this->follower->shouldReceive('where') 160 | ->once() 161 | ->with('follower_id',1) 162 | ->andReturn($this->follower); 163 | 164 | $mockRepo->shouldReceive('addFilters') 165 | ->once() 166 | ->with($this->follower,[]) 167 | ->andReturn(null); 168 | 169 | $this->follower->shouldReceive('get') 170 | ->once() 171 | ->andReturn($this->follower); 172 | 173 | $result = $mockRepo->listsFollowing($listsFollowRelation,[]); 174 | 175 | $this->assertInstanceOf('Fenos\Rally\Models\Follower',$result); 176 | } 177 | 178 | public function test_count_followers() 179 | { 180 | $countFollowRelation = [ 181 | 182 | 'follower_type' => 'User', 183 | 'follower_id' => 1, 184 | ]; 185 | 186 | $this->follower->shouldReceive('select') 187 | ->once() 188 | ->andReturn($this->follower); 189 | 190 | $this->db->shouldReceive('raw') 191 | ->once() 192 | ->with('Count(*) as numbers_followers') 193 | ->andReturn($this->follower); 194 | 195 | $this->follower->shouldReceive('where') 196 | ->once() 197 | ->with('followed_type','User') 198 | ->andReturn($this->follower); 199 | 200 | $this->follower->shouldReceive('where') 201 | ->once() 202 | ->with('followed_id',1) 203 | ->andReturn($this->follower); 204 | 205 | $this->follower->shouldReceive('first') 206 | ->once() 207 | ->andReturn($this->follower); 208 | 209 | $result = $this->rallyRepo->countFollowers($countFollowRelation); 210 | 211 | $this->assertInstanceOf('Fenos\Rally\Models\Follower',$result); 212 | } 213 | 214 | public function test_count_following() 215 | { 216 | $countFollowRelation = [ 217 | 218 | 'follower_type' => 'User', 219 | 'follower_id' => 1, 220 | ]; 221 | 222 | $this->follower->shouldReceive('select') 223 | ->once() 224 | ->andReturn($this->follower); 225 | 226 | $this->db->shouldReceive('raw') 227 | ->once() 228 | ->with('Count(*) as numbers_followers') 229 | ->andReturn($this->follower); 230 | 231 | $this->follower->shouldReceive('where') 232 | ->once() 233 | ->with('follower_type','User') 234 | ->andReturn($this->follower); 235 | 236 | $this->follower->shouldReceive('where') 237 | ->once() 238 | ->with('follower_id',1) 239 | ->andReturn($this->follower); 240 | 241 | $this->follower->shouldReceive('first') 242 | ->once() 243 | ->andReturn($this->follower); 244 | 245 | $result = $this->rallyRepo->countFollowing($countFollowRelation); 246 | 247 | $this->assertInstanceOf('Fenos\Rally\Models\Follower',$result); 248 | } 249 | } 250 | 251 | -------------------------------------------------------------------------------- /tests/RallyRepositoryTest.php: -------------------------------------------------------------------------------- 1 | mockEloquent = m::mock('Illuminate\Database\Eloquent\Model'); 36 | 37 | $this->rallyRepo = new \Fenos\Rally\Repositories\RallyRepository( 38 | $this->follower = m::mock('Fenos\Rally\Models\Follower'), 39 | $this->db = m::mock('Illuminate\Database\DatabaseManager') 40 | ); 41 | } 42 | 43 | /** 44 | * TearDown 45 | */ 46 | public function tearDown() 47 | { 48 | m::close(); 49 | } 50 | 51 | public function test_follow_someone() 52 | { 53 | $createFollowRelation = [ 54 | 55 | 'follower_id' => 1, 56 | 'followed_id' => 2 57 | ]; 58 | 59 | $this->follower->shouldReceive('create') 60 | ->with($createFollowRelation) 61 | ->once() 62 | ->andReturn($this->follower); 63 | 64 | $result = $this->rallyRepo->follow($createFollowRelation); 65 | 66 | $this->assertInstanceOf('Fenos\Rally\Models\Follower',$result); 67 | } 68 | 69 | public function test_Unfollow_someone() 70 | { 71 | 72 | $this->follower->shouldReceive('delete') 73 | ->with() 74 | ->once() 75 | ->andReturn(true); 76 | 77 | $result = $this->rallyRepo->unfollow($this->follower); 78 | 79 | $this->assertTrue($result); 80 | } 81 | 82 | public function test_if_someOne_is_follower_of_somebody() 83 | { 84 | $this->follower->shouldReceive('where') 85 | ->once() 86 | ->with('follower_id',1) 87 | ->andReturn($this->follower); 88 | 89 | $this->follower->shouldReceive('where') 90 | ->once() 91 | ->with('followed_id',2) 92 | ->andReturn($this->follower); 93 | 94 | $this->follower->shouldReceive('first') 95 | ->once() 96 | ->with() 97 | ->andReturn($this->follower); 98 | 99 | $result = $this->rallyRepo->isFollower(['follower_id' =>1,'followed_id' => 2]); 100 | 101 | $this->assertInstanceOf('Fenos\Rally\Models\Follower',$result); 102 | } 103 | 104 | public function test_listsFollowers() 105 | { 106 | $createFollowRelation = [ 107 | 108 | 'follower_id' => 1 109 | ]; 110 | 111 | $mockRepo = m::mock('Fenos\Rally\Repositories\RallyRepository[addFlters]',[$this->follower,$this->db]); 112 | 113 | $this->follower->shouldReceive('with') 114 | ->once() 115 | ->with('follower') 116 | ->andReturn($this->follower); 117 | 118 | $this->follower->shouldReceive('where') 119 | ->once() 120 | ->with('followers.followed_id',1) 121 | ->andReturn($this->follower); 122 | 123 | $this->follower->shouldReceive('leftJoin') 124 | ->once() 125 | ->andReturn($this->follower); 126 | 127 | $this->follower->shouldReceive('groupBy') 128 | ->once() 129 | ->with('followers.follower_id') 130 | ->andReturn($this->follower); 131 | 132 | $this->follower->shouldReceive('groupBy') 133 | ->once() 134 | ->with('fol.follower_id') 135 | ->andReturn($this->follower); 136 | 137 | $this->follower->shouldReceive('select') 138 | ->once() 139 | ->with('followers.*','fol.follower_id as is_fan') 140 | ->andReturn($this->follower); 141 | 142 | $mockRepo->shouldReceive('addFilters') 143 | ->with($this->follower,[]) 144 | ->andReturn(null); 145 | 146 | $this->follower->shouldReceive('get') 147 | ->once() 148 | ->andReturn($this->follower); 149 | 150 | $result = $mockRepo->listsFollowers($createFollowRelation,[]); 151 | 152 | $this->assertInstanceOf('Fenos\Rally\Models\Follower',$result); 153 | 154 | } 155 | 156 | public function test_listsFollowing() 157 | { 158 | $createFollowRelation = [ 159 | 160 | 'follower_id' => 1 161 | ]; 162 | 163 | $mockRepo = m::mock('Fenos\Rally\Repositories\RallyRepository[addFlters]',[$this->follower,$this->db]); 164 | 165 | $this->follower->shouldReceive('with') 166 | ->once() 167 | ->with('follower') 168 | ->andReturn($this->follower); 169 | 170 | $this->follower->shouldReceive('where') 171 | ->once() 172 | ->with('follower_id',1) 173 | ->andReturn($this->follower); 174 | 175 | $mockRepo->shouldReceive('addFilters') 176 | ->with($this->follower,[]) 177 | ->andReturn(null); 178 | 179 | $this->follower->shouldReceive('get') 180 | ->once() 181 | ->andReturn($this->follower); 182 | 183 | $result = $mockRepo->listsFollowing($createFollowRelation,[]); 184 | 185 | $this->assertInstanceOf('Fenos\Rally\Models\Follower',$result); 186 | } 187 | 188 | public function test_count_followers() 189 | { 190 | $createFollowRelation = [ 191 | 192 | 'follower_id' => 1 193 | ]; 194 | 195 | $this->follower->shouldReceive('select') 196 | ->once() 197 | ->andReturn($this->follower); 198 | 199 | $this->db->shouldReceive('raw') 200 | ->once() 201 | ->with('Count(*) as numbers_followers') 202 | ->andReturn($this->follower); 203 | 204 | $this->follower->shouldReceive('where') 205 | ->once() 206 | ->with('followed_id',1) 207 | ->andReturn($this->follower); 208 | 209 | $this->follower->shouldReceive('first') 210 | ->once() 211 | ->andReturn($this->follower); 212 | 213 | $result = $this->rallyRepo->countFollowers($createFollowRelation); 214 | 215 | $this->assertInstanceOf('Fenos\Rally\Models\Follower',$result); 216 | } 217 | 218 | public function test_count_following() 219 | { 220 | $createFollowRelation = [ 221 | 222 | 'follower_id' => 1 223 | ]; 224 | 225 | $this->follower->shouldReceive('select') 226 | ->once() 227 | ->andReturn($this->follower); 228 | 229 | $this->db->shouldReceive('raw') 230 | ->once() 231 | ->with('Count(*) as numbers_followers') 232 | ->andReturn($this->follower); 233 | 234 | $this->follower->shouldReceive('where') 235 | ->once() 236 | ->with('follower_id',1) 237 | ->andReturn($this->follower); 238 | 239 | $this->follower->shouldReceive('first') 240 | ->once() 241 | ->andReturn($this->follower); 242 | 243 | $result = $this->rallyRepo->countFollowing($createFollowRelation); 244 | 245 | $this->assertInstanceOf('Fenos\Rally\Models\Follower',$result); 246 | } 247 | 248 | } 249 | 250 | -------------------------------------------------------------------------------- /tests/RallyTest.php: -------------------------------------------------------------------------------- 1 | rally = new Rally( 37 | $this->rallyRepo = m::mock('Fenos\Rally\Repositories\RallyRepositoryInterface'), 38 | $this->config = m::mock('Illuminate\Config\Repository') 39 | ); 40 | 41 | $this->mockEloquent = m::mock('Illuminate\Database\Eloquent\Model'); 42 | } 43 | 44 | public function tearDown() 45 | { 46 | m::close(); 47 | } 48 | 49 | public function test_follower_selector_polymorphic() 50 | { 51 | $mockRally = m::mock('Fenos\Rally\Rally[lightValidation]',[$this->rallyRepo,$this->config]); 52 | 53 | $mockRally->shouldReceive('lightValidation') 54 | ->once() 55 | ->with('User',1) 56 | ->andReturn(null); 57 | 58 | $this->config->shouldReceive('get') 59 | ->once() 60 | ->with('rally::polymorphic') 61 | ->andReturn(true); 62 | 63 | $result = $mockRally->follower('User',1); 64 | 65 | $this->assertInstanceOf('Fenos\Rally\Rally',$result); 66 | } 67 | 68 | public function test_follower_selector_NOT_polymorphic() 69 | { 70 | $mockRally = m::mock('Fenos\Rally\Rally[lightValidation]',[$this->rallyRepo,$this->config]); 71 | 72 | $mockRally->shouldReceive('lightValidation') 73 | ->once() 74 | ->with(1,false) 75 | ->andReturn(null); 76 | 77 | $this->config->shouldReceive('get') 78 | ->once() 79 | ->with('rally::polymorphic') 80 | ->andReturn(false); 81 | 82 | $result = $mockRally->follower(1); 83 | 84 | $this->assertInstanceOf('Fenos\Rally\Rally',$result); 85 | } 86 | 87 | public function test_follow_someone_polymorphic() 88 | { 89 | $createFollowRelation = [ 90 | 91 | 'follower_type' => 'User', 92 | 'follower_id' => 1, 93 | 'followed_type' => 'Team', 94 | 'followed_id' => 2 95 | ]; 96 | 97 | $mockRally = m::mock('Fenos\Rally\Rally[lightValidation,isFollowerOf,checkFollowerInformation]',[$this->rallyRepo,$this->config]); 98 | 99 | $mockRally->setFollowerPolymorpich('User',1); 100 | 101 | $mockRally->shouldReceive('lightValidation') 102 | ->once() 103 | ->with('Team',2) 104 | ->andReturn(null); 105 | 106 | $mockRally->shouldReceive('isFollowerOf') 107 | ->once() 108 | ->with('Team',2) 109 | ->andReturn(false); 110 | 111 | $mockRally->shouldReceive('checkFollowerInformation') 112 | ->once() 113 | ->andReturn(null); 114 | 115 | $this->config->shouldReceive('get') 116 | ->once() 117 | ->with('rally::polymorphic') 118 | ->andReturn(true); 119 | 120 | $this->rallyRepo->shouldReceive('follow') 121 | ->once() 122 | ->with($createFollowRelation) 123 | ->andReturn(m::mock('Fenos\Rally\Models\Follower')); 124 | 125 | $result = $mockRally->follow('Team',2); 126 | 127 | $this->assertInstanceOf('Fenos\Rally\Models\Follower',$result); 128 | } 129 | 130 | public function test_follow_someone_NOT_polymorphic() 131 | { 132 | $createFollowRelation = [ 133 | 134 | 'follower_id' => 1, 135 | 'followed_id' => 2 136 | ]; 137 | 138 | $mockRally = m::mock('Fenos\Rally\Rally[lightValidation,isFollowerOf,checkFollowerInformation]',[$this->rallyRepo,$this->config]); 139 | 140 | $mockRally->setFollower(1); 141 | 142 | $mockRally->shouldReceive('lightValidation') 143 | ->once() 144 | ->with(2,false) 145 | ->andReturn(null); 146 | 147 | $mockRally->shouldReceive('isFollowerOf') 148 | ->once() 149 | ->with(2,false) 150 | ->andReturn(false); 151 | 152 | $mockRally->shouldReceive('checkFollowerInformation') 153 | ->once() 154 | ->andReturn(null); 155 | 156 | $this->config->shouldReceive('get') 157 | ->once() 158 | ->with('rally::polymorphic') 159 | ->andReturn(false); 160 | 161 | $this->rallyRepo->shouldReceive('follow') 162 | ->once() 163 | ->with($createFollowRelation) 164 | ->andReturn(m::mock('Fenos\Rally\Models\Follower')); 165 | 166 | $result = $mockRally->follow(2,false); 167 | 168 | $this->assertInstanceOf('Fenos\Rally\Models\Follower',$result); 169 | } 170 | 171 | public function test_the_check_if_is_follower_of_someone_polymorphic() 172 | { 173 | $checkFollowRelation = [ 174 | 175 | 'follower_type' => 'User', 176 | 'follower_id' => 1, 177 | 'followed_type' => 'Team', 178 | 'followed_id' => 2 179 | ]; 180 | 181 | $mockRally = m::mock('Fenos\Rally\Rally[checkFollowerInformation]',[$this->rallyRepo,$this->config]); 182 | 183 | $mockRally->setFollowerPolymorpich('User',1); 184 | 185 | $mockRally->shouldReceive('checkFollowerInformation') 186 | ->once() 187 | ->andReturn(null); 188 | 189 | $this->config->shouldReceive('get') 190 | ->once() 191 | ->with('rally::polymorphic') 192 | ->andReturn(true); 193 | 194 | $this->rallyRepo->shouldReceive('isFollower') 195 | ->once() 196 | ->with($checkFollowRelation) 197 | ->andReturn(m::mock('Fenos\Rally\Models\Follower')); 198 | 199 | $result = $mockRally->isFollowerOf('Team',2); 200 | 201 | $this->assertInstanceOf('Fenos\Rally\Models\Follower',$result); 202 | } 203 | 204 | public function test_the_check_if_is_follower_of_someone_NOT_polymorphic() 205 | { 206 | $checkFollowRelation = [ 207 | 208 | 'follower_id' => 1, 209 | 'followed_id' => 2 210 | ]; 211 | 212 | $mockRally = m::mock('Fenos\Rally\Rally[checkFollowerInformation]',[$this->rallyRepo,$this->config]); 213 | 214 | $mockRally->setFollower(1); 215 | 216 | $mockRally->shouldReceive('checkFollowerInformation') 217 | ->once() 218 | ->andReturn(null); 219 | 220 | $this->config->shouldReceive('get') 221 | ->once() 222 | ->with('rally::polymorphic') 223 | ->andReturn(false); 224 | 225 | $this->rallyRepo->shouldReceive('isFollower') 226 | ->once() 227 | ->with($checkFollowRelation) 228 | ->andReturn(m::mock('Fenos\Rally\Models\Follower')); 229 | 230 | $result = $mockRally->isFollowerOf(2); 231 | 232 | $this->assertInstanceOf('Fenos\Rally\Models\Follower',$result); 233 | } 234 | 235 | public function test_the_check_if_is_follower_of_someone__polymorphic_on_not_found() 236 | { 237 | $checkFollowRelation = [ 238 | 239 | 'follower_id' => 1, 240 | 'followed_id' => 2 241 | ]; 242 | 243 | $mockRally = m::mock('Fenos\Rally\Rally[checkFollowerInformation]',[$this->rallyRepo,$this->config]); 244 | 245 | $mockRally->setFollower(1); 246 | 247 | $mockRally->shouldReceive('checkFollowerInformation') 248 | ->once() 249 | ->andReturn(null); 250 | 251 | $this->config->shouldReceive('get') 252 | ->once() 253 | ->with('rally::polymorphic') 254 | ->andReturn(false); 255 | 256 | $this->rallyRepo->shouldReceive('isFollower') 257 | ->once() 258 | ->with($checkFollowRelation) 259 | ->andReturn(null); 260 | 261 | $result = $mockRally->isFollowerOf(2); 262 | 263 | $this->assertFalse($result); 264 | } 265 | 266 | public function test_unfollow_someone() 267 | { 268 | $mockRally = m::mock('Fenos\Rally\Rally[isFollowerOf]',[$this->rallyRepo,$this->config]); 269 | 270 | $mockRally->setFollowerPolymorpich('User',1); 271 | 272 | $mockRally->shouldReceive('isFollowerOf') 273 | ->once() 274 | ->with('Team',2) 275 | ->andReturn(m::mock('Fenos\Rally\Models\Follower')); 276 | 277 | // test to review 278 | $this->rallyRepo->shouldReceive('unFollow') 279 | ->once() 280 | ->andReturn(true); 281 | 282 | $result = $mockRally->unFollow('Team',2); 283 | 284 | $this->assertTrue($result); 285 | } 286 | 287 | /** 288 | * @expectedException Fenos\Rally\Exceptions\FollowerNotFoundException 289 | * */ 290 | public function test_unfollow_someone_but_relation_not_found() 291 | { 292 | $mockRally = m::mock('Fenos\Rally\Rally[isFollowerOf]',[$this->rallyRepo,$this->config]); 293 | 294 | $mockRally->setFollowerPolymorpich('User',1); 295 | 296 | $mockRally->shouldReceive('isFollowerOf') 297 | ->once() 298 | ->with('Team',2) 299 | ->andReturn(false); 300 | 301 | $mockRally->unFollow('Team',2); 302 | 303 | } 304 | 305 | public function test_get_lists_followers_polymorphic() 306 | { 307 | $followerInfo = [ 308 | 'follower_type' => 'User', 309 | 'follower_id' => 1 310 | ]; 311 | 312 | $mockRally = m::mock('Fenos\Rally\Rally[checkFollowerInformation]',[$this->rallyRepo,$this->config]); 313 | 314 | $mockRally->setFollowerPolymorpich('User',1); 315 | 316 | $mockRally->shouldReceive('checkFollowerInformation') 317 | ->once() 318 | ->andReturn(null); 319 | 320 | $this->rallyRepo->shouldReceive('listsFollowers') 321 | ->once() 322 | ->with($followerInfo,[]) 323 | ->andReturn(m::mock('Fenos\Rally\Models\Follower')); 324 | 325 | $result = $mockRally->getLists(); 326 | 327 | $this->assertInstanceOf('Fenos\Rally\Models\Follower',$result); 328 | } 329 | 330 | public function test_get_lists_followers_NOT_polymorphic() 331 | { 332 | $followerInfo = [ 333 | 'follower_id' => 1 334 | ]; 335 | 336 | $mockRally = m::mock('Fenos\Rally\Rally[checkFollowerInformation]',[$this->rallyRepo,$this->config]); 337 | 338 | $mockRally->setFollower(1); 339 | 340 | $mockRally->shouldReceive('checkFollowerInformation') 341 | ->once() 342 | ->andReturn(null); 343 | 344 | $this->rallyRepo->shouldReceive('listsFollowers') 345 | ->once() 346 | ->with($followerInfo,[]) 347 | ->andReturn(m::mock('Fenos\Rally\Models\Follower')); 348 | 349 | $result = $mockRally->getLists(); 350 | 351 | $this->assertInstanceOf('Fenos\Rally\Models\Follower',$result); 352 | } 353 | 354 | public function test_get_lists_following_polymorphic() 355 | { 356 | $followerInfo = [ 357 | 'follower_type' => 'User', 358 | 'follower_id' => 1 359 | ]; 360 | 361 | $mockRally = m::mock('Fenos\Rally\Rally[checkFollowerInformation]',[$this->rallyRepo,$this->config]); 362 | 363 | $mockRally->setFollowerPolymorpich('User',1); 364 | 365 | $mockRally->shouldReceive('checkFollowerInformation') 366 | ->once() 367 | ->andReturn(null); 368 | 369 | $this->rallyRepo->shouldReceive('listsFollowing') 370 | ->once() 371 | ->with($followerInfo,[]) 372 | ->andReturn(m::mock('Fenos\Rally\Models\Follower')); 373 | 374 | $result = $mockRally->getListsFollowing(); 375 | 376 | $this->assertInstanceOf('Fenos\Rally\Models\Follower',$result); 377 | } 378 | 379 | public function test_get_lists_following_NOT_polymorphic() 380 | { 381 | $followerInfo = [ 382 | 'follower_id' => 1 383 | ]; 384 | 385 | $mockRally = m::mock('Fenos\Rally\Rally[checkFollowerInformation]',[$this->rallyRepo,$this->config]); 386 | 387 | $mockRally->setFollower(1); 388 | 389 | $mockRally->shouldReceive('checkFollowerInformation') 390 | ->once() 391 | ->andReturn(null); 392 | 393 | $this->rallyRepo->shouldReceive('listsFollowing') 394 | ->once() 395 | ->with($followerInfo,[]) 396 | ->andReturn(m::mock('Fenos\Rally\Models\Follower')); 397 | 398 | $result = $mockRally->getListsFollowing(); 399 | 400 | $this->assertInstanceOf('Fenos\Rally\Models\Follower',$result); 401 | } 402 | 403 | public function test_get_numbers_followers_polymorphic() 404 | { 405 | $followerInfo = [ 406 | 'follower_type' => 'User', 407 | 'follower_id' => 1 408 | ]; 409 | 410 | $mockRally = m::mock('Fenos\Rally\Rally[checkFollowerInformation]',[$this->rallyRepo,$this->config]); 411 | 412 | $mockRally->setFollowerPolymorpich('User',1); 413 | 414 | $mockRally->shouldReceive('checkFollowerInformation') 415 | ->once() 416 | ->andReturn(null); 417 | 418 | $this->rallyRepo->shouldReceive('countFollowers') 419 | ->once() 420 | ->with($followerInfo) 421 | ->andReturn(m::mock('Fenos\Rally\Models\Follower')); 422 | 423 | $result = $mockRally->count(); 424 | 425 | $this->assertInstanceOf('Fenos\Rally\Models\Follower',$result); 426 | } 427 | 428 | public function test_get_numbers_followers_NOT_polymorphic() 429 | { 430 | $followerInfo = [ 431 | 'follower_id' => 1 432 | ]; 433 | 434 | $mockRally = m::mock('Fenos\Rally\Rally[checkFollowerInformation]',[$this->rallyRepo,$this->config]); 435 | 436 | $mockRally->setFollower(1); 437 | 438 | $mockRally->shouldReceive('checkFollowerInformation') 439 | ->once() 440 | ->andReturn(null); 441 | 442 | $this->rallyRepo->shouldReceive('countFollowers') 443 | ->once() 444 | ->with($followerInfo) 445 | ->andReturn(m::mock('Fenos\Rally\Models\Follower')); 446 | 447 | $result = $mockRally->count(); 448 | 449 | $this->assertInstanceOf('Fenos\Rally\Models\Follower',$result); 450 | } 451 | 452 | public function test_get_numbers_following_polymorphic() 453 | { 454 | $followerInfo = [ 455 | 'follower_type' => 'User', 456 | 'follower_id' => 1 457 | ]; 458 | 459 | $mockRally = m::mock('Fenos\Rally\Rally[checkFollowerInformation]',[$this->rallyRepo,$this->config]); 460 | 461 | $mockRally->setFollowerPolymorpich('User',1); 462 | 463 | $mockRally->shouldReceive('checkFollowerInformation') 464 | ->once() 465 | ->andReturn(null); 466 | 467 | $this->rallyRepo->shouldReceive('countFollowing') 468 | ->once() 469 | ->with($followerInfo) 470 | ->andReturn(m::mock('Fenos\Rally\Models\Follower')); 471 | 472 | $result = $mockRally->countFollowing(); 473 | 474 | $this->assertInstanceOf('Fenos\Rally\Models\Follower',$result); 475 | } 476 | 477 | public function test_get_numbers_following_NOT_polymorphic() 478 | { 479 | $followerInfo = [ 480 | 'follower_id' => 1 481 | ]; 482 | 483 | $mockRally = m::mock('Fenos\Rally\Rally[checkFollowerInformation]',[$this->rallyRepo,$this->config]); 484 | 485 | $mockRally->setFollower(1); 486 | 487 | $mockRally->shouldReceive('checkFollowerInformation') 488 | ->once() 489 | ->andReturn(null); 490 | 491 | $this->rallyRepo->shouldReceive('countFollowing') 492 | ->once() 493 | ->with($followerInfo) 494 | ->andReturn(m::mock('Fenos\Rally\Models\Follower')); 495 | 496 | $result = $mockRally->countFollowing(); 497 | 498 | $this->assertInstanceOf('Fenos\Rally\Models\Follower',$result); 499 | } 500 | 501 | public function test_lightValidation_for_polymorphic() 502 | { 503 | $this->config->shouldReceive('get') 504 | ->once() 505 | ->with('rally::polymorphic') 506 | ->andReturn(true); 507 | 508 | $result = $this->rally->lightValidation('User',1); 509 | 510 | $this->assertNull($result); 511 | } 512 | 513 | /** 514 | * @expectedException \InvalidArgumentException 515 | * */ 516 | public function test_lightValidation_for_polymorphic_having_first_parameter_not_string() 517 | { 518 | $this->config->shouldReceive('get') 519 | ->once() 520 | ->with('rally::polymorphic') 521 | ->andReturn(true); 522 | 523 | $this->rally->lightValidation(10,1); 524 | 525 | } 526 | 527 | /** 528 | * @expectedException \InvalidArgumentException 529 | * */ 530 | public function test_lightValidation_for_polymorphic_having_second_parameter_not_number() 531 | { 532 | $this->config->shouldReceive('get') 533 | ->once() 534 | ->with('rally::polymorphic') 535 | ->andReturn(true); 536 | 537 | $this->rally->lightValidation('must be','a number'); 538 | } 539 | 540 | public function test_lightValidation_for_NOT_polymorphic() 541 | { 542 | $this->config->shouldReceive('get') 543 | ->once() 544 | ->with('rally::polymorphic') 545 | ->andReturn(false); 546 | 547 | $result = $this->rally->lightValidation(1); 548 | 549 | $this->assertNull($result); 550 | } 551 | 552 | public function test_check_follower_information_exists_polymorphic() 553 | { 554 | $this->rally->setFollowerPolymorpich('Team',1); 555 | 556 | $this->config->shouldReceive('get') 557 | ->once() 558 | ->with('rally::polymorphic') 559 | ->andReturn(true); 560 | 561 | $result = $this->rally->checkFollowerInformation(); 562 | 563 | $this->assertNull($result); 564 | } 565 | 566 | /** 567 | * @expectedException \InvalidArgumentException 568 | * */ 569 | public function test_check_follower_information_exists_polymorphic_not_setting_both_values() 570 | { 571 | $this->config->shouldReceive('get') 572 | ->once() 573 | ->with('rally::polymorphic') 574 | ->andReturn(true); 575 | 576 | $this->rally->checkFollowerInformation(); 577 | } 578 | 579 | public function test_check_follower_information_exists_NOT_polymorphic() 580 | { 581 | $this->rally->setFollower(1); 582 | 583 | $this->config->shouldReceive('get') 584 | ->once() 585 | ->with('rally::polymorphic') 586 | ->andReturn(false); 587 | 588 | $result = $this->rally->checkFollowerInformation(); 589 | 590 | $this->assertNull($result); 591 | } 592 | } 593 | 594 | --------------------------------------------------------------------------------