Add more functional and unit tests.

This commit is contained in:
Buster "Silver Eagle" Neece 2021-06-13 22:30:17 -05:00
parent f7f2755f58
commit f1d33f4104
No known key found for this signature in database
GPG Key ID: 6D9E12FF03411F4E
10 changed files with 164 additions and 36 deletions

View File

@ -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

View File

@ -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 . '%');

View File

@ -0,0 +1,16 @@
<?php
class Api_Admin_AuditLogCest extends CestAbstract
{
/**
* @before setupComplete
* @before login
*/
public function viewAuditLog(FunctionalTester $I): void
{
$I->wantTo('View audit log via API.');
$I->sendGet('/api/admin/auditlog');
$I->seeResponseCodeIs(200);
}
}

View File

@ -0,0 +1,26 @@
<?php
class Api_Admin_StorageLocationsCest extends CestAbstract
{
/**
* @before setupComplete
* @before login
*/
public function manageStorageLocations(FunctionalTester $I): void
{
$I->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',
]
);
}
}

View File

@ -0,0 +1,30 @@
<?php
class Api_Frontend_DashboardCest extends CestAbstract
{
/**
* @before setupComplete
* @before login
*/
public function checkDashboard(FunctionalTester $I): void
{
$I->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);
}
}

View File

@ -1,10 +1,10 @@
<?php
use App\Utilities;
class ExportsTest extends \Codeception\Test\Unit
{
/**
* @var \UnitTester
*/
protected $tester;
protected UnitTester $tester;
public function testExports()
{
@ -15,15 +15,15 @@ class ExportsTest extends \Codeception\Test\Unit
]
];
$csv = \App\Utilities\Csv::arrayToCsv($raw_data, false);
$csv = Utilities\Csv::arrayToCsv($raw_data, false);
$this->assertStringContainsString('"test_field_a","test_field_b"', $csv);
$raw_data = '<test><subtest>Contents</subtest></test>';
$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);
}

View File

@ -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));
}
}

View File

@ -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));
}
}

View File

@ -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);
}
}

50
tests/unit/XmlTest.php Normal file
View File

@ -0,0 +1,50 @@
<?php
use App\Xml\Reader;
use App\Xml\Writer;
class XmlTest extends \Codeception\Test\Unit
{
protected UnitTester $tester;
public function testXml(): void
{
$arrayValue = [
'mounts' => [
'mount' => [
[
'@type' => 'normal',
'path' => '/radio.mp3',
],
[
'@type' => 'special',
'path' => '/special.mp3',
],
],
],
];
$xmlString = (new Writer())->toString($arrayValue, 'icecast');
$xmlExpected = <<<'XML'
<?xml version="1.0" encoding="UTF-8"?>
<icecast>
<mounts>
<mount type="normal">
<path>/radio.mp3</path>
</mount>
<mount type="special">
<path>/special.mp3</path>
</mount>
</mounts>
</icecast>
XML;
self::assertEquals($xmlString, $xmlExpected);
$backToArray = (new Reader())->fromString($xmlString);
self::assertEquals($arrayValue, $backToArray);
}
}