Skip to content

Commit

Permalink
Improve tests and require strict metadata coverage (#359)
Browse files Browse the repository at this point in the history
  • Loading branch information
VerifiedJoseph authored Sep 30, 2024
1 parent 303bb6c commit ad65344
Show file tree
Hide file tree
Showing 38 changed files with 1,323 additions and 153 deletions.
5 changes: 5 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@
"App\\": "include/"
}
},
"autoload-dev": {
"classmap": [
"tests/"
]
},
"scripts": {
"lint": "phpstan --memory-limit=150M && phpcs",
"lint-phpstan": "phpstan",
Expand Down
2 changes: 1 addition & 1 deletion include/FeedFormat/FeedFormat.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ protected function buildTitle(array $video): string
if ($video['streamStatus'] === 'upcoming') {
$scheduled = $this->getFormattedScheduledDate($video['scheduled']);

return sprintf('[Live Stream %s] %s ', $scheduled, $video['title']);
return sprintf('[Live Stream %s] %s', $scheduled, $video['title']);
}

if ($video['streamStatus'] === 'live') {
Expand Down
3 changes: 1 addition & 2 deletions include/FeedFormat/HtmlFormat.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace App\FeedFormat;

use App\Template;
use App\Helper\Url;

class HtmlFormat extends FeedFormat
{
Expand All @@ -30,7 +29,7 @@ public function build(): void
'items' => $this->buildItems()
]);

$this->feed = $html->render(minify: true);
$this->feed = $html->render();
}

/**
Expand Down
8 changes: 2 additions & 6 deletions include/Page/Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ private function checkInputs(array $inputs): void
}

if (isset($inputs['embed_videos'])) {
$this->embedVideos = true;
$this->embedVideos = filter_var($inputs['embed_videos'], FILTER_VALIDATE_BOOLEAN);
}

if (isset($inputs['ignore_premieres'])) {
Expand Down Expand Up @@ -212,15 +212,11 @@ private function generate(): void
*/
private function validateFeedId(string $query): bool
{
if ($this->feedType === 'channel') {
return Validate::channelId($query);
}

if ($this->feedType === 'playlist') {
return Validate::playlistId($query);
}

return false;
return Validate::channelId($query);
}

/**
Expand Down
21 changes: 15 additions & 6 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" bootstrap="./vendor/autoload.php" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.3/phpunit.xsd" displayDetailsOnTestsThatTriggerWarnings="true" cacheDirectory=".phpunit.cache">
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/11.2/phpunit.xsd"
bootstrap="./vendor/autoload.php"
cacheDirectory=".phpunit.cache"
executionOrder="depends,defects"
beStrictAboutOutputDuringTests="true"
failOnRisky="true" failOnWarning="true"
displayDetailsOnTestsThatTriggerWarnings="true"
beStrictAboutCoverageMetadata="true"
requireCoverageMetadata="true">
<testsuites>
<testsuite name="better-video-rss">
<directory suffix=".php">./tests/</directory>
<testsuite name="default">
<directory>./tests/</directory>
</testsuite>
</testsuites>
<source>
<source restrictNotices="true" restrictWarnings="true">
<include>
<directory suffix=".php">./include/</directory>
<directory>./include/</directory>
</include>
</source>
</phpunit>
</phpunit>
27 changes: 27 additions & 0 deletions tests/AbstractTestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

use PHPUnit\Framework\TestCase as TestCase;
use App\Config;

abstract class AbstractTestCase extends TestCase
{
/**
* Create config class stub
*
* Array example: `['getSelfUrl' => 'https://example.com/', 'getTimezone' => 'UTC']`
*
* @param array<string, mixed> $methods
* @return PHPUnit\Framework\MockObject\Stub&Config
*/
protected static function createConfigStub(array $methods): Config
{
/** @var PHPUnit\Framework\MockObject\Stub&Config */
$config = self::createStub(Config::class);

foreach ($methods as $method => $value) {
$config->method($method)->willReturn($value);
}

return $config;
}
}
5 changes: 5 additions & 0 deletions tests/ApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@
use App\Http\Response;

#[CoversClass(Api::class)]
#[UsesClass(Config::class)]
#[UsesClass(Request::class)]
#[UsesClass(Response::class)]
#[UsesClass(App\Helper\Json::class)]
#[UsesClass(App\Helper\Url::class)]
class ApiTest extends TestCase
{
private static Config $config;
Expand Down
36 changes: 20 additions & 16 deletions tests/CacheTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<?php

use PHPUnit\Framework\TestCase;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\UsesClass;
use MockFileSystem\MockFileSystem as mockfs;
Expand All @@ -9,7 +8,9 @@

#[CoversClass(Cache::class)]
#[UsesClass(Config::class)]
class CacheTest extends TestCase
#[UsesClass(App\Helper\File::class)]
#[UsesClass(App\Helper\Json::class)]
class CacheTest extends AbstractTestCase
{
private static Config $config;

Expand All @@ -21,23 +22,26 @@ class CacheTest extends TestCase

public static function setUpBeforeClass(): void
{
mockfs::create();
self::$cacheFilepath = mockfs::getUrl('/' . hash('sha256', self::$channelId) . '.cache');

self::$data = (array) json_decode(
(string)
file_get_contents('tests/files/channel-cache-data.json'),
associative: true
);

mockfs::create();
self::$cacheFilepath = mockfs::getUrl('/' . hash('sha256', self::$channelId) . '.cache');
self::$config = self::createConfigStub([
'getCacheDisableStatus' => false,
'getCacheDirectory' => mockfs::getUrl('/'),
'getCacheFormatVersion' => 1
]);
}

public function setUp(): void
{
// Reset cache file data
copy('tests/files/channel-cache-data.json', self::$cacheFilepath);

/** @var PHPUnit\Framework\MockObject\Stub&Config */
$config = self::createStub(Config::class);
$config->method('getCacheDisableStatus')->willReturn(false);
$config->method('getCacheDirectory')->willReturn(mockfs::getUrl('/'));
$config->method('getCacheFormatVersion')->willReturn(1);
self::$config = $config;
}

/**
Expand Down Expand Up @@ -70,11 +74,11 @@ public function testLoadWithFeedIdNotInCache(): void
*/
public function testLoadWithNoVersionMatch(): void
{
/** @var PHPUnit\Framework\MockObject\Stub&Config */
$config = self::createStub(Config::class);
$config->method('getCacheDisableStatus')->willReturn(false);
$config->method('getCacheDirectory')->willReturn(mockfs::getUrl('/'));
$config->method('getCacheFormatVersion')->willReturn(2);
$config = self::createConfigStub([
'getCacheDisableStatus' => false,
'getCacheDirectory' => mockfs::getUrl('/'),
'getCacheFormatVersion' => 2
]);

$cache = new Cache(self::$channelId, $config);
$cache->load();
Expand Down
3 changes: 3 additions & 0 deletions tests/Config/ValidateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@

#[CoversClass(Validate::class)]
#[UsesClass(Config::class)]
#[UsesClass(ConfigException::class)]
#[UsesClass(\App\Config\Base::class)]
#[UsesClass(\App\Helper\Validate::class)]
class ValidateTest extends TestCase
{
/** @var array<string, mixed> $defaults */
Expand Down
4 changes: 4 additions & 0 deletions tests/ConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
use App\Version;

#[CoversClass(Config::class)]
#[CoversClass(Version::class)]
#[UsesClass(App\Config\Base::class)]
#[UsesClass(App\Config\Validate::class)]
#[UsesClass(App\Helper\Validate::class)]
class ConfigTest extends TestCase
{
public function setUp(): void
Expand Down
Loading

0 comments on commit ad65344

Please sign in to comment.