2019-07-19 23:55:44 +00:00
|
|
|
<?php
|
2019-11-07 08:12:13 +00:00
|
|
|
|
2019-07-19 23:55:44 +00:00
|
|
|
use App\Entity;
|
|
|
|
|
|
|
|
class StationPlaylistTest extends \Codeception\Test\Unit
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var \UnitTester
|
|
|
|
*/
|
|
|
|
protected $tester;
|
|
|
|
|
|
|
|
public function testScheduledPlaylist()
|
|
|
|
{
|
|
|
|
/** @var Entity\Station $station */
|
|
|
|
$station = Mockery::mock(Entity\Station::class);
|
|
|
|
|
|
|
|
$playlist = new Entity\StationPlaylist($station);
|
|
|
|
|
|
|
|
// Sample playlist that plays from 10PM to 4AM the next day.
|
2020-02-18 04:32:14 +00:00
|
|
|
$scheduleEntry = new Entity\StationSchedule($playlist);
|
2019-11-07 08:12:13 +00:00
|
|
|
$scheduleEntry->setStartTime(2200);
|
|
|
|
$scheduleEntry->setEndTime(400);
|
|
|
|
$scheduleEntry->setDays([1, 2, 3]); // Monday, Tuesday, Wednesday
|
|
|
|
|
|
|
|
$playlist->getScheduleItems()->add($scheduleEntry);
|
2019-07-19 23:55:44 +00:00
|
|
|
|
|
|
|
$utc = new \DateTimeZone('UTC');
|
2020-07-08 23:50:44 +00:00
|
|
|
$test_monday = \Carbon\CarbonImmutable::create(2018, 1, 15, 0, 0, 0, $utc);
|
|
|
|
$test_thursday = \Carbon\CarbonImmutable::create(2018, 1, 18, 0, 0, 0, $utc);
|
2019-07-19 23:55:44 +00:00
|
|
|
|
|
|
|
// Sanity check: Jan 15, 2018 is a Monday, and Jan 18, 2018 is a Thursday.
|
|
|
|
$this->assertTrue($test_monday->isMonday());
|
|
|
|
$this->assertTrue($test_thursday->isThursday());
|
|
|
|
|
|
|
|
// Playlist SHOULD play Monday evening at 10:30PM.
|
|
|
|
$test_time = $test_monday->setTime(22, 30);
|
2019-07-20 21:04:42 +00:00
|
|
|
$this->assertTrue($playlist->shouldPlayNow($test_time));
|
2019-07-19 23:55:44 +00:00
|
|
|
|
|
|
|
// Playlist SHOULD play Thursday morning at 3:00AM.
|
|
|
|
$test_time = $test_thursday->setTime(3, 0);
|
2019-07-20 21:04:42 +00:00
|
|
|
$this->assertTrue($playlist->shouldPlayNow($test_time));
|
2019-07-19 23:55:44 +00:00
|
|
|
|
|
|
|
// Playlist SHOULD NOT play Monday morning at 3:00AM.
|
|
|
|
$test_time = $test_monday->setTime(3, 0);
|
2019-07-20 21:04:42 +00:00
|
|
|
$this->assertFalse($playlist->shouldPlayNow($test_time));
|
2019-07-19 23:55:44 +00:00
|
|
|
|
|
|
|
// Playlist SHOULD NOT play Thursday evening at 10:30PM.
|
|
|
|
$test_time = $test_thursday->setTime(22, 30);
|
2019-07-20 21:04:42 +00:00
|
|
|
$this->assertFalse($playlist->shouldPlayNow($test_time));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testOncePerXMinutesPlaylist()
|
|
|
|
{
|
|
|
|
/** @var Entity\Station $station */
|
|
|
|
$station = Mockery::mock(Entity\Station::class);
|
|
|
|
|
|
|
|
$playlist = new Entity\StationPlaylist($station);
|
|
|
|
$playlist->setType(Entity\StationPlaylist::TYPE_ONCE_PER_X_MINUTES);
|
|
|
|
$playlist->setPlayPerMinutes(30);
|
|
|
|
|
|
|
|
$utc = new \DateTimeZone('UTC');
|
2020-07-08 23:50:44 +00:00
|
|
|
$test_day = \Carbon\CarbonImmutable::create(2018, 1, 15, 0, 0, 0, $utc);
|
2019-07-20 21:04:42 +00:00
|
|
|
|
|
|
|
// Last played 20 minutes ago, SHOULD NOT play again.
|
2019-11-07 08:12:13 +00:00
|
|
|
$last_played = $test_day->addMinutes(0 - 20);
|
2019-07-20 21:04:42 +00:00
|
|
|
$playlist->setPlayedAt($last_played->getTimestamp());
|
|
|
|
|
|
|
|
$this->assertFalse($playlist->shouldPlayNow($test_day));
|
|
|
|
|
|
|
|
// Last played 40 minutes ago, SHOULD play again.
|
2019-11-07 08:12:13 +00:00
|
|
|
$last_played = $test_day->addMinutes(0 - 40);
|
2019-07-20 21:04:42 +00:00
|
|
|
$playlist->setPlayedAt($last_played->getTimestamp());
|
|
|
|
|
|
|
|
$this->assertTrue($playlist->shouldPlayNow($test_day));
|
2019-07-19 23:55:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testOncePerHourPlaylist()
|
|
|
|
{
|
|
|
|
/** @var Entity\Station $station */
|
|
|
|
$station = Mockery::mock(Entity\Station::class);
|
|
|
|
|
|
|
|
$playlist = new Entity\StationPlaylist($station);
|
|
|
|
$playlist->setType(Entity\StationPlaylist::TYPE_ONCE_PER_HOUR);
|
|
|
|
$playlist->setPlayPerHourMinute(50);
|
|
|
|
|
|
|
|
$utc = new \DateTimeZone('UTC');
|
2020-07-08 23:50:44 +00:00
|
|
|
$test_day = \Carbon\CarbonImmutable::create(2018, 1, 15, 0, 0, 0, null, $utc);
|
2019-07-19 23:55:44 +00:00
|
|
|
|
|
|
|
// Playlist SHOULD try to play at 11:59 PM.
|
|
|
|
$test_time = $test_day->setTime(23, 59);
|
2019-07-20 21:04:42 +00:00
|
|
|
$this->assertTrue($playlist->shouldPlayNow($test_time));
|
2019-07-19 23:55:44 +00:00
|
|
|
|
|
|
|
// Playlist SHOULD try to play at 12:04 PM.
|
|
|
|
$test_time = $test_day->setTime(12, 4);
|
2019-07-20 21:04:42 +00:00
|
|
|
$this->assertTrue($playlist->shouldPlayNow($test_time));
|
2019-07-19 23:55:44 +00:00
|
|
|
|
|
|
|
// Playlist SHOULD NOT try to play at 11:49 PM.
|
|
|
|
$test_time = $test_day->setTime(23, 49);
|
2019-07-20 21:04:42 +00:00
|
|
|
$this->assertFalse($playlist->shouldPlayNow($test_time));
|
2019-07-19 23:55:44 +00:00
|
|
|
|
|
|
|
// Playlist SHOULD NOT try to play at 12:06 PM.
|
|
|
|
$test_time = $test_day->setTime(12, 6);
|
2019-07-20 21:04:42 +00:00
|
|
|
$this->assertFalse($playlist->shouldPlayNow($test_time));
|
2019-07-19 23:55:44 +00:00
|
|
|
}
|
|
|
|
}
|