Skip to content

Commit

Permalink
#16 created automated tests
Browse files Browse the repository at this point in the history
  • Loading branch information
almostengr committed Jun 1, 2024
1 parent 18ece2d commit 29524ee
Show file tree
Hide file tree
Showing 8 changed files with 156 additions and 25 deletions.
8 changes: 6 additions & 2 deletions ShowPulseBase.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
<?php

require_once "/opt/fpp/www/common.php";
namespace App;

$testing = false;
$commonFile = $testing ? "/opt/fpp/www/common.php" : __DIR__ . "/tests/OptFppWwwCommonMock.php";
require_once $commonFile;

abstract class ShowPulseBase
{
Expand Down Expand Up @@ -118,7 +122,7 @@ protected function executeFppCommand($command, $data = array())
}
}

protected function logError($data)
public function logError($data)
{
$currentDateTime = date('Y-m-d h:i:s A');
error_log("$currentDateTime: $data");
Expand Down
3 changes: 3 additions & 0 deletions ShowPulseSettingForm.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
<?php

namespace App;
use Exception;

require_once "ShowPulseBase.php";

final class ShowPulseSettingForm extends ShowPulseBase
Expand Down
15 changes: 9 additions & 6 deletions ShowPulseWorker.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
<?php

namespace App;
use Exception;

require_once "ShowPulseBase.php";

final class StatusDto
{
private $warnings;
private $sequence;
private $song_title;
private $song_artist;
private $status;
public $warnings;
public $sequence;
public $song_title;
public $song_artist;
public $status;

public function __construct($hasErrors, $sequence, $status)
{
Expand Down Expand Up @@ -185,7 +188,7 @@ public function sleepDelay()
}

$worker = new ShowPulseWorker();
while (true) {
while ($testing) {
try {
$worker->getWebsiteApiKey();
$worker->getFppStatus();
Expand Down
23 changes: 23 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd"
bootstrap="vendor/autoload.php"
cacheDirectory=".phpunit.cache"
executionOrder="depends,defects"
requireCoverageMetadata="true"
beStrictAboutCoverageMetadata="true"
beStrictAboutOutputDuringTests="true"
failOnRisky="true"
failOnWarning="true">
<testsuites>
<testsuite name="default">
<directory>tests</directory>
</testsuite>
</testsuites>

<source restrictDeprecations="true" restrictNotices="true" restrictWarnings="true">
<include>
<directory>.</directory>
</include>
</source>
</phpunit>
66 changes: 66 additions & 0 deletions tests/OptFppWwwCommonMock.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

function WriteSettingToFile($settingName, $setting, $plugin = "")
{
global $settingsFile;
global $settings;
$filename = $settingsFile;

if ($plugin != "") {
$filename = $settings['configDirectory'] . "/plugin." . $plugin;
}

$settingsStr = "";
if (file_exists($filename)) {
$tmpSettings = parse_ini_file($filename);
}
$tmpSettings[$settingName] = $setting;
foreach ($tmpSettings as $key => $value) {
$settingsStr .= $key . " = \"" . $value . "\"\n";
}
file_put_contents($filename, $settingsStr, LOCK_EX);
}

function ReadSettingFromFile($settingName, $plugin = "")
{
global $settingsFile;
global $settings;
$filename = $settingsFile;

if ($plugin != "") {
$filename = "test" . "/plugin." . $plugin;
// $filename = $settings["configDirectory"] . "/plugin." . $plugin;
}
if (!file_exists($filename)) {
return false;
}
$fd = @fopen($filename, "r");
$settingsStr = "";
if ($fd) {
flock($fd, LOCK_SH);
$settingsStr = file_get_contents($filename);
flock($fd, LOCK_UN);
fclose($fd);
}
if (!empty($settingsStr)) {
if (preg_match("/^" . $settingName . "/m", $settingsStr)) {
$result = preg_match("/^" . $settingName . "\s*=(\s*\S*\w*)/m", $settingsStr, $output_array);
if ($result == 0) {
// error_log("The setting " . $settingName . " could not be found in " . $filename);
return false;
}
return trim($output_array[1], " \t\n\r\0\x0B\"");
} else {
// error_log("The setting " . $settingName . " could not be found in " . $filename);
return false;
}
} else {
error_log("The setting file:" . $filename . " could not be found.");
return false;
}
}

function GetDirSetting($dir)
{
return "";
}
6 changes: 0 additions & 6 deletions tests/ShowPulseSettingFormTest.php

This file was deleted.

6 changes: 0 additions & 6 deletions tests/ShowPulseWorkerTest.php

This file was deleted.

54 changes: 49 additions & 5 deletions tests/StatusDtoTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,60 @@
namespace App\Tests;

use PHPUnit\Framework\TestCase;
use \App\StatusDto;

include_once "src/ShowPulseWorker.php";
require_once "ShowPulseWorker.php";

class StatusDtoTest extends TestCase
/**
* @covers \App\StatusDto
*/
final class StatusDtoTest extends TestCase
{
public function testAssignMediaMetaWithNull()
public function testConstructor()
{
$dto = new \App\StatusDto("", false, 24, "Test sequence.fseq", false);
$dto->assignMedia("Testing Title", "Demo Artist");
$dto = new StatusDto(4, "test.fseq", "playing");
$this->assertEquals("test.fseq", $dto->sequence);
}

public function testConstructorWithSpecialCharacterSequence()
{
$dto = new StatusDto(0, 'test-flight_rainbow.fseq', 'playing');

$this->assertEquals('test-flight rainbow', $dto->song_title);
$this->assertEquals('test-flight_rainbow.fseq', $dto->sequence);
}

public function testConstructorWithErrors()
{
$dto = new StatusDto(5, 'testing.fseq', 'idle');

$this->assertEquals(5, $dto->warnings);
$this->assertNotEquals(10, $dto->warnings);
}

public function testAssignMediaWithTitleAndArtist()
{
$dto = new StatusDto(0, "test.fseq", "playing");
$dto->assignMedia("Foo", "Bar");

$this->assertEquals("Foo", $dto->song_title);
$this->assertEquals("Bar", $dto->song_artist);
}

public function testAssignMediaWithTitleAndNoArtist()
{
$dto = new StatusDto(0, "test.fseq", "playing");

$dto->assignMedia("Foo");

$this->assertEquals("Foo", $dto->song_title);
$this->assertEquals(null, $dto->song_artist);
}

public function testAssignMediaWithNoTitleNorArtist()
{
$dto = new StatusDto(0, "test.fseq", "playing");

$this->assertEquals("test", $dto->song_title);
}
}

0 comments on commit 29524ee

Please sign in to comment.