diff --git a/codeception.yml b/codeception.yml index bab1217a1..6f6e33ee4 100644 --- a/codeception.yml +++ b/codeception.yml @@ -15,16 +15,25 @@ extensions: enabled: - Codeception\Extension\RunFailed modules: -coverage: - enabled: true - include: +coverage : + enabled : true + include : - src/* - exclude: + exclude : + # Application bootstrapping + - src/AppFactory.php + - src/EventDispatcher.php + - src/Exception.php + - src/Plugins.php + # Used in application, but not used in tests - src/Radio/Frontend/ShoutCast2.php + - src/Console/*.php - src/Console/Command/*.php - src/Entity/Fixture/* - src/Entity/Migration/* + - src/Service/IpGeolocator/GeoLite.php + - src/RateLimit.php # Exceptions - src/Exception/*.php diff --git a/src/Controller/Api/Admin/AuditLogController.php b/src/Controller/Api/Admin/AuditLogController.php index f743e6334..83bcc03b7 100644 --- a/src/Controller/Api/Admin/AuditLogController.php +++ b/src/Controller/Api/Admin/AuditLogController.php @@ -41,7 +41,7 @@ class AuditLogController ->setParameter('start', $start->getTimestamp()) ->setParameter('end', $end->getTimestamp()); - $search_phrase = trim($params['searchPhrase']); + $search_phrase = trim($params['searchPhrase'] ?? ''); if (!empty($search_phrase)) { $qb->andWhere('(a.user LIKE :query OR a.identifier LIKE :query OR a.target LIKE :query)') ->setParameter('query', '%' . $search_phrase . '%'); diff --git a/tests/functional/Api_Admin_AuditLogCest.php b/tests/functional/Api_Admin_AuditLogCest.php new file mode 100644 index 000000000..d8562d514 --- /dev/null +++ b/tests/functional/Api_Admin_AuditLogCest.php @@ -0,0 +1,16 @@ +wantTo('View audit log via API.'); + + $I->sendGet('/api/admin/auditlog'); + $I->seeResponseCodeIs(200); + } +} diff --git a/tests/functional/Api_Admin_StorageLocationsCest.php b/tests/functional/Api_Admin_StorageLocationsCest.php new file mode 100644 index 000000000..ec1cba708 --- /dev/null +++ b/tests/functional/Api_Admin_StorageLocationsCest.php @@ -0,0 +1,26 @@ +wantTo('Manage storage locations via API.'); + + $this->testCrudApi( + $I, + '/api/admin/storage_locations', + [ + 'type' => \App\Entity\StorageLocation::TYPE_STATION_MEDIA, + 'adapter' => \App\Entity\StorageLocation::ADAPTER_LOCAL, + 'path' => '/tmp/test_storage_location', + ], + [ + 'path' => '/tmp/test_storage_location_2', + ] + ); + } +} diff --git a/tests/functional/Api_Frontend_DashboardCest.php b/tests/functional/Api_Frontend_DashboardCest.php new file mode 100644 index 000000000..fd378dcb3 --- /dev/null +++ b/tests/functional/Api_Frontend_DashboardCest.php @@ -0,0 +1,30 @@ +wantTo('Check dashboard API functions.'); + + $I->sendGet('/api/frontend/dashboard/charts'); + + $I->seeResponseCodeIs(200); + $I->canSeeResponseContainsJson( + [ + 'metrics' => [], + ] + ); + + $I->sendGet('/api/frontend/dashboard/notifications'); + + $I->seeResponseCodeIs(200); + + $I->sendGet('/api/frontend/dashboard/stations'); + + $I->seeResponseCodeIs(200); + } +} diff --git a/tests/unit/ExportsTest.php b/tests/unit/ExportsTest.php index 0d7324870..c30717891 100644 --- a/tests/unit/ExportsTest.php +++ b/tests/unit/ExportsTest.php @@ -1,10 +1,10 @@ assertStringContainsString('"test_field_a","test_field_b"', $csv); $raw_data = 'Contents'; - $xml_array = \App\Utilities\Xml::xmlToArray($raw_data); + $xml_array = Utilities\Xml::xmlToArray($raw_data); $this->assertArrayHasKey('test', $xml_array); - $xml = \App\Utilities\Xml::arrayToXml($xml_array); + $xml = Utilities\Xml::arrayToXml($xml_array); $this->assertStringContainsString($raw_data, $xml); } diff --git a/tests/unit/ListenerIntervalTest.php b/tests/unit/ListenerIntervalTest.php index 8b0dfc073..4482c9ee5 100644 --- a/tests/unit/ListenerIntervalTest.php +++ b/tests/unit/ListenerIntervalTest.php @@ -5,7 +5,7 @@ use Carbon\CarbonImmutable; class ListenerIntervalTest extends \Codeception\Test\Unit { - public function testListenerIntervals() + public function testListenerIntervals(): void { $utc = new \DateTimeZone('UTC'); @@ -29,6 +29,6 @@ class ListenerIntervalTest extends \Codeception\Test\Unit ]; $expected = 6 * 60 * 60; - $this->assertEquals($expected, Entity\Listener::getListenerSeconds($intervals)); + self::assertEquals($expected, Entity\Listener::getListenerSeconds($intervals)); } } diff --git a/tests/unit/StationPlaylistTest.php b/tests/unit/StationPlaylistTest.php index b01825ee3..4a0eee287 100644 --- a/tests/unit/StationPlaylistTest.php +++ b/tests/unit/StationPlaylistTest.php @@ -8,13 +8,13 @@ class StationPlaylistTest extends \Codeception\Test\Unit protected App\Radio\AutoDJ\Scheduler $scheduler; - protected function _inject(App\Tests\Module $tests_module) + protected function _inject(App\Tests\Module $tests_module): void { $di = $tests_module->container; $this->scheduler = $di->get(App\Radio\AutoDJ\Scheduler::class); } - public function testScheduledPlaylist() + public function testScheduledPlaylist(): void { /** @var Entity\Station $station */ $station = Mockery::mock(Entity\Station::class); @@ -35,24 +35,24 @@ class StationPlaylistTest extends \Codeception\Test\Unit $test_thursday = \Carbon\CarbonImmutable::create(2018, 1, 18, 0, 0, 0, $utc); // 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()); + self::assertTrue($test_monday->isMonday()); + self::assertTrue($test_thursday->isThursday()); // Playlist SHOULD play Monday evening at 10:30PM. $test_time = $test_monday->setTime(22, 30); - $this->assertTrue($this->scheduler->shouldPlaylistPlayNow($playlist, $test_time)); + self::assertTrue($this->scheduler->shouldPlaylistPlayNow($playlist, $test_time)); // Playlist SHOULD play Thursday morning at 3:00AM. $test_time = $test_thursday->setTime(3, 0); - $this->assertTrue($this->scheduler->shouldPlaylistPlayNow($playlist, $test_time)); + self::assertTrue($this->scheduler->shouldPlaylistPlayNow($playlist, $test_time)); // Playlist SHOULD NOT play Monday morning at 3:00AM. $test_time = $test_monday->setTime(3, 0); - $this->assertFalse($this->scheduler->shouldPlaylistPlayNow($playlist, $test_time)); + self::assertFalse($this->scheduler->shouldPlaylistPlayNow($playlist, $test_time)); // Playlist SHOULD NOT play Thursday evening at 10:30PM. $test_time = $test_thursday->setTime(22, 30); - $this->assertFalse($this->scheduler->shouldPlaylistPlayNow($playlist, $test_time)); + self::assertFalse($this->scheduler->shouldPlaylistPlayNow($playlist, $test_time)); } public function testOncePerXMinutesPlaylist() @@ -72,13 +72,13 @@ class StationPlaylistTest extends \Codeception\Test\Unit $last_played = $test_day->addMinutes(0 - 20); $playlist->setPlayedAt($last_played->getTimestamp()); - $this->assertFalse($this->scheduler->shouldPlaylistPlayNow($playlist, $test_day)); + self::assertFalse($this->scheduler->shouldPlaylistPlayNow($playlist, $test_day)); // Last played 40 minutes ago, SHOULD play again. $last_played = $test_day->addMinutes(0 - 40); $playlist->setPlayedAt($last_played->getTimestamp()); - $this->assertTrue($this->scheduler->shouldPlaylistPlayNow($playlist, $test_day)); + self::assertTrue($this->scheduler->shouldPlaylistPlayNow($playlist, $test_day)); } public function testOncePerHourPlaylist() @@ -96,18 +96,18 @@ class StationPlaylistTest extends \Codeception\Test\Unit // Playlist SHOULD try to play at 11:59 PM. $test_time = $test_day->setTime(23, 59); - $this->assertTrue($this->scheduler->shouldPlaylistPlayNow($playlist, $test_time)); + self::assertTrue($this->scheduler->shouldPlaylistPlayNow($playlist, $test_time)); // Playlist SHOULD try to play at 12:04 PM. $test_time = $test_day->setTime(12, 4); - $this->assertTrue($this->scheduler->shouldPlaylistPlayNow($playlist, $test_time)); + self::assertTrue($this->scheduler->shouldPlaylistPlayNow($playlist, $test_time)); // Playlist SHOULD NOT try to play at 11:49 PM. $test_time = $test_day->setTime(23, 49); - $this->assertFalse($this->scheduler->shouldPlaylistPlayNow($playlist, $test_time)); + self::assertFalse($this->scheduler->shouldPlaylistPlayNow($playlist, $test_time)); // Playlist SHOULD NOT try to play at 12:06 PM. $test_time = $test_day->setTime(12, 6); - $this->assertFalse($this->scheduler->shouldPlaylistPlayNow($playlist, $test_time)); + self::assertFalse($this->scheduler->shouldPlaylistPlayNow($playlist, $test_time)); } } diff --git a/tests/unit/UtilitiesTest.php b/tests/unit/UtilitiesTest.php index 798ec58d5..a4b3b4e19 100644 --- a/tests/unit/UtilitiesTest.php +++ b/tests/unit/UtilitiesTest.php @@ -4,24 +4,21 @@ use App\Utilities\Strings; class UtilitiesTest extends \Codeception\Test\Unit { - /** - * @var \UnitTester - */ - protected $tester; + protected UnitTester $tester; - public function testUtilities() + public function testUtilities(): void { $test_result = Strings::generatePassword(10); - $this->assertTrue(strlen($test_result) == 10); + self::assertEquals(10, strlen($test_result)); $test_string = 'Lorem ipsum dolor sit amet lorem ipsum dolor sit amet lorem ipsum dolor sit amet'; $test_result = Strings::truncateText($test_string, 15); $expected_result = 'Lorem ipsum...'; - $this->assertEquals($test_result, $expected_result); + self::assertEquals($test_result, $expected_result); $test_url = 'https://www.twitter.com/'; $test_result = Strings::truncateUrl($test_url); $expected_result = 'twitter.com'; - $this->assertEquals($test_result, $expected_result); + self::assertEquals($test_result, $expected_result); } } diff --git a/tests/unit/XmlTest.php b/tests/unit/XmlTest.php new file mode 100644 index 000000000..a3c499b31 --- /dev/null +++ b/tests/unit/XmlTest.php @@ -0,0 +1,50 @@ + [ + 'mount' => [ + [ + '@type' => 'normal', + 'path' => '/radio.mp3', + ], + [ + '@type' => 'special', + 'path' => '/special.mp3', + ], + ], + ], + ]; + + $xmlString = (new Writer())->toString($arrayValue, 'icecast'); + + $xmlExpected = <<<'XML' + + + + + /radio.mp3 + + + /special.mp3 + + + + + XML; + + self::assertEquals($xmlString, $xmlExpected); + + $backToArray = (new Reader())->fromString($xmlString); + + self::assertEquals($arrayValue, $backToArray); + } +}