Ejemplo n.º 1
0
 /**
  * Test for scopeTrending
  */
 public function testScopeTrending()
 {
     // get trending time limit
     $trendingperiod = config('rap-battle.trendingperiod', 168);
     $timeoldest = new Carbon();
     $timeoldest->subHours($trendingperiod);
     // create users
     $user1 = factory(App\Models\User::class)->create();
     $user2 = factory(App\Models\User::class)->create();
     // create battles
     for ($i = 0; $i < ($max_battles = 40); ++$i) {
         $battle = new Battle();
         $battle->rapper1_id = $user1->id;
         $battle->rapper2_id = $user2->id;
         $battle->votes_rapper1 = rand(0, 2 * $i);
         $battle->votes_rapper2 = rand(0, 2 * $i);
         // creation date: random value in valid range, every second out of range
         $date = Carbon::now()->subHours(rand(0, $trendingperiod - 1) + ($i & 0x1 ? 0 : $trendingperiod));
         $battle->created_at = $date->toDateTimeString();
         // fake votes
         if ($i < 20) {
             // each second of these battles will be trending
             $battle->votes_rapper1 = 1000 + $i;
             $battle->votes_rapper2 = 500 + 2 * $i;
         } else {
             // these battles won't be trending
             $battle->votes_rapper1 = 100 + $i;
             $battle->votes_rapper2 = 50 + 2 * $i;
         }
         $battle->save();
     }
     // checks
     $trending = Battle::trending()->get()->take(10)->keyBy('id');
     // 10 trending battles out of 20 possible in time range
     $this->assertEquals(10, $trending->count());
     foreach ($trending as $battle) {
         // check time range
         $timediff = Carbon::parse($battle->created_at)->gt($timeoldest);
         $this->assertTrue($timediff);
         // check vote counts
         $this->assertTrue(1000 <= $battle->votes_rapper1 && 1000 + $max_battles / 2 > $battle->votes_rapper1);
         $this->assertTrue(500 <= $battle->votes_rapper2 && 500 + $max_battles > $battle->votes_rapper2);
     }
 }