Skip to content

Instantly share code, notes, and snippets.

@kbirmhrjn
Last active January 2, 2016 08:59
Show Gist options
  • Save kbirmhrjn/8280302 to your computer and use it in GitHub Desktop.
Save kbirmhrjn/8280302 to your computer and use it in GitHub Desktop.
Whenever I run phpunit, I get undefined $matches_lastupdate ? can't test properly by this
//Routes.php
Route::get('/matches',['uses' => 'MatchesController@index']);
//BaseController.php
BaseController extends Controller{
function __construct()
{
// call task for update cloudsearch if there are updated gigmor profiles
// not best place to do it.
CloudSearch::updateProfiles();
if(Auth::check()){
$counts = array_fill_keys(array_keys(\Config::get('app.profiles')), 0);
$allUserProfiles = [];
foreach(array_keys(\Config::get('app.profiles')) as $type){
$typePlural = (substr($type, -1) === 'y') ? substr($type, 0, -1).'ies' : $type.'s';
$allUserProfiles[$type] = User::profileList($typePlural);
$count = count($allUserProfiles[$type]);
if($count >= \Config::get("app.profiles.$type.max_count", 1))
View::share('userHas'.ucfirst($type).'Profile', $count);
}
View::share('allUserProfiles', $allUserProfiles);
$matches_lastupdate = null;
// retrieve matches and messages count to show it in toolbar
if(Session::has('current_profile_id')) {
$id = Session::get('current_profile_id');
$Profile = ucfirst(Session::get('current_profile_type'));
$message_count = null;
$matches_count = null;
if( $profile = $Profile::find($id))
$message_count = $profile->contacts()->whereStatus('new')->count();
if($profile = $Profile::find($id))
{
$matches_count = $profile->matches()->whereStatus('new')->count();
$matches_lastupdate = $profile->last_match_date;
}
View::share('message_count', $message_count);
View::share('matches_count', $matches_count);
}
View::share('matches_lastupdate', $matches_lastupdate);
}
}
//MatchesController.php
class MatchesController extends BaseController {
protected $matches;
protected $perPage = 5;
public function __construct(MatchesRepositoryInterface $matches)
{
$this->matches = $matches;
parent::__construct();
}
/**
* Matches Index page
*/
public function index()
{
# if we do not have 'status' we'll assign 'new'
$matchStatus = Input::get('status', 'new');
$searchMatches = $this->matches->viewData( $matchStatus, $this->perPage);
return View::make('matches.index', compact('searchMatches') )->withTitle('Matches');
}
}
///tests/MatchesControllerTest.php
class MatchesControllerTest extends TestCase
{
protected $user;
protected $profile;
public function setUp()
{
parent::setUp();
}
public function tearDown()
{
Mockery::close();
}
public function testMatches()
{
$mock = Mockery::mock('Gigmor\Repositories\Matches\MatchesRepositoryInterface');
$mock->shouldReceive('viewData')->once();
App::instance('Gigmor\Repositories\Matches\MatchesRepositoryInterface',$mock);
$this->call('GET','matches');
$this->assertViewHas('searchMatches');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment