Merge pull request #334 from ArabCoders/dev

Added more tests coverage.
This commit is contained in:
Abdulmohsen
2023-08-31 02:14:59 +03:00
committed by GitHub
6 changed files with 249 additions and 23 deletions

23
src/Libs/TestCase.php Normal file
View File

@@ -0,0 +1,23 @@
<?php
declare(strict_types=1);
namespace App\Libs;
use Monolog\Handler\TestHandler;
class TestCase extends \PHPUnit\Framework\TestCase
{
protected TestHandler|null $handler = null;
protected function printLogs(): void
{
if (null === $this->handler) {
return;
}
foreach ($this->handler->getRecords() as $logs) {
fwrite(STDOUT, $logs['formatted'] . PHP_EOL);
}
}
}

View File

@@ -10,12 +10,12 @@ use App\Libs\Database\PDO\PDOAdapter;
use App\Libs\Entity\StateEntity;
use App\Libs\Entity\StateInterface;
use App\Libs\Guid;
use App\Libs\TestCase;
use DateTimeImmutable;
use Error;
use Monolog\Handler\TestHandler;
use Monolog\Logger;
use PDO;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\NullOutput;

72
tests/Libs/ConfigTest.php Normal file
View File

@@ -0,0 +1,72 @@
<?php
declare(strict_types=1);
namespace Tests\Libs;
use App\Libs\Config;
use App\Libs\TestCase;
class ConfigTest extends TestCase
{
private array $data = ['foo' => 'bar', 'baz' => 'kaz', 'sub' => ['key' => 'val']];
protected function setUp(): void
{
Config::init($this->data);
parent::setUp();
}
public function test_config_init_getAll(): void
{
Config::init($this->data);
$this->assertSame($this->data, Config::getAll());
}
public function test_config_get(): void
{
$this->assertSame($this->data['foo'], Config::get('foo'));
}
public function test_get_config_value_default(): void
{
$this->assertSame('not_set', Config::get('key_not_set', 'not_set'));
}
public function test_config_append(): void
{
$data = $this->data;
$data['taz'] = 'maz';
Config::append($data);
$this->assertSame($data, Config::getAll());
}
public function test_config_save(): void
{
Config::save('sub.key', 'updated');
Config::save('foo', 'updated');
$this->assertSame('updated', Config::get('foo'));
$this->assertSame('updated', Config::get('sub.key'));
}
public function test_config_has(): void
{
$this->assertTrue(Config::has('foo'));
$this->assertFalse(Config::has('taz'));
}
public function test_config_delete(): void
{
Config::remove('sub');
$data = $this->data;
unset($data['sub']);
$this->assertSame($data, Config::getAll());
$this->assertFalse(Config::has('sub'));
}
}

View File

@@ -7,15 +7,14 @@ namespace Tests\Mappers\Import;
use App\Libs\Database\DatabaseInterface as iDB;
use App\Libs\Database\PDO\PDOAdapter;
use App\Libs\Entity\StateEntity;
use App\Libs\Entity\StateInterface as iFace;
use App\Libs\Entity\StateInterface as iState;
use App\Libs\Guid;
use App\Libs\Mappers\Import\DirectMapper;
use App\Libs\Message;
use App\Libs\TestCase;
use Monolog\Handler\TestHandler;
use Monolog\Logger;
use PDO;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\NullOutput;
@@ -65,8 +64,8 @@ class DirectMapperTest extends TestCase
$this->assertSame(
[
iFace::TYPE_MOVIE => ['added' => 1, 'updated' => 0, 'failed' => 0],
iFace::TYPE_EPISODE => ['added' => 1, 'updated' => 0, 'failed' => 0],
iState::TYPE_MOVIE => ['added' => 1, 'updated' => 0, 'failed' => 0],
iState::TYPE_EPISODE => ['added' => 1, 'updated' => 0, 'failed' => 0],
],
$this->mapper->commit()
);
@@ -74,7 +73,7 @@ class DirectMapperTest extends TestCase
// -- assert 0 as we have committed the changes to the db, and the state should have been reset.
$this->assertCount(0, $this->mapper);
$testEpisode->metadata['home_plex'][iFace::COLUMN_GUIDS][Guid::GUID_TVRAGE] = '2';
$testEpisode->metadata['home_plex'][iState::COLUMN_GUIDS][Guid::GUID_TVRAGE] = '2';
$this->mapper->add($testEpisode);
@@ -82,8 +81,8 @@ class DirectMapperTest extends TestCase
$this->assertSame(
[
iFace::TYPE_MOVIE => ['added' => 0, 'updated' => 0, 'failed' => 0],
iFace::TYPE_EPISODE => ['added' => 0, 'updated' => 1, 'failed' => 0],
iState::TYPE_MOVIE => ['added' => 0, 'updated' => 0, 'failed' => 0],
iState::TYPE_EPISODE => ['added' => 0, 'updated' => 1, 'failed' => 0],
],
$this->mapper->commit()
);
@@ -143,12 +142,78 @@ class DirectMapperTest extends TestCase
$this->assertSame(0, (int)ag($obj->getMetadata($testMovie->via), iState::COLUMN_WATCHED));
}
/**
* @throws \Exception
*/
public function test_update_unwatch_conflict_no_metadata(): void
{
$this->mapper->add(new StateEntity($this->testMovie));
$this->mapper->commit();
$this->mapper->reset()->loadData();
$timeNow = time();
$testData = $this->testMovie;
$testData[iState::COLUMN_VIA] = 'fiz';
$testData[iState::COLUMN_WATCHED] = 0;
$testData[iState::COLUMN_UPDATED] = $timeNow;
$testData[iState::COLUMN_META_DATA] = [
'fiz' => [
iState::COLUMN_ID => 121,
iState::COLUMN_TYPE => iState::TYPE_MOVIE,
iState::COLUMN_WATCHED => 0,
iState::COLUMN_YEAR => '2020',
iState::COLUMN_META_DATA_EXTRA => [
iState::COLUMN_META_DATA_EXTRA_DATE => '2020-01-03',
],
iState::COLUMN_META_DATA_ADDED_AT => $timeNow,
],
];
$testMovie = new StateEntity($testData);
$this->mapper->add($testMovie, ['after' => new \DateTimeImmutable('@' . ($timeNow - 10))]);
$this->mapper->commit();
$this->mapper->reset()->loadData();
$obj = $this->mapper->get($testMovie);
$this->assertSame(1, $obj->watched);
}
/**
* @throws \Exception
*/
public function test_update_unwatch_conflict_no_date(): void
{
$testData = $this->testMovie;
$timeNow = time();
$testData[iState::COLUMN_UPDATED] = $timeNow;
$testData[iState::COLUMN_META_DATA]['home_plex'][iState::COLUMN_META_DATA_PLAYED_AT] = $timeNow;
$movie = new StateEntity($testData);
$this->mapper->add($movie);
$this->mapper->commit();
$this->mapper->reset()->loadData();
$testData[iState::COLUMN_WATCHED] = 0;
$testData[iState::COLUMN_UPDATED] = $timeNow;
$testMovie = new StateEntity($testData);
$this->mapper->add($testMovie, ['after' => new \DateTimeImmutable('@' . ($timeNow - 10))]);
$this->mapper->commit();
$this->mapper->reset()->loadData();
$obj = $this->mapper->get($testMovie);
$this->assertSame(1, $obj->watched);
}
public function test_get_conditions(): void
{
$movie = $this->testMovie;
$episode = $this->testEpisode;
foreach (iFace::ENTITY_ARRAY_KEYS as $key) {
foreach (iState::ENTITY_ARRAY_KEYS as $key) {
if (null !== ($movie[$key] ?? null)) {
ksort($movie[$key]);
}
@@ -186,25 +251,25 @@ class DirectMapperTest extends TestCase
$this->assertSame(
[
iFace::TYPE_MOVIE => ['added' => 1, 'updated' => 0, 'failed' => 0],
iFace::TYPE_EPISODE => ['added' => 1, 'updated' => 0, 'failed' => 0],
iState::TYPE_MOVIE => ['added' => 1, 'updated' => 0, 'failed' => 0],
iState::TYPE_EPISODE => ['added' => 1, 'updated' => 0, 'failed' => 0],
],
$insert
);
$testMovie->metadata['home_plex'][iFace::COLUMN_GUIDS][Guid::GUID_ANIDB] = '1920';
$testEpisode->metadata['home_plex'][iFace::COLUMN_GUIDS][Guid::GUID_ANIDB] = '1900';
$testMovie->metadata['home_plex'][iState::COLUMN_GUIDS][Guid::GUID_ANIDB] = '1920';
$testEpisode->metadata['home_plex'][iState::COLUMN_GUIDS][Guid::GUID_ANIDB] = '1900';
$this->mapper
->add($testMovie, ['diff_keys' => iFace::ENTITY_KEYS])
->add($testEpisode, ['diff_keys' => iFace::ENTITY_KEYS]);
->add($testMovie, ['diff_keys' => iState::ENTITY_KEYS])
->add($testEpisode, ['diff_keys' => iState::ENTITY_KEYS]);
$updated = $this->mapper->commit();
$this->assertSame(
[
iFace::TYPE_MOVIE => ['added' => 0, 'updated' => 1, 'failed' => 0],
iFace::TYPE_EPISODE => ['added' => 0, 'updated' => 1, 'failed' => 0],
iState::TYPE_MOVIE => ['added' => 0, 'updated' => 1, 'failed' => 0],
iState::TYPE_EPISODE => ['added' => 0, 'updated' => 1, 'failed' => 0],
],
$updated
);

View File

@@ -11,10 +11,10 @@ use App\Libs\Entity\StateInterface as iState;
use App\Libs\Guid;
use App\Libs\Mappers\Import\MemoryMapper;
use App\Libs\Message;
use App\Libs\TestCase;
use Monolog\Handler\TestHandler;
use Monolog\Logger;
use PDO;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\NullOutput;
@@ -135,8 +135,8 @@ class MemoryMapperTest extends TestCase
$this->mapper->commit();
$this->mapper->reset()->loadData();
$obj = $this->mapper->get($testMovie);
$this->assertSame(0, (int)$obj->watched);
$this->assertSame(1, (int)$obj->updated);
$this->assertSame(0, $obj->watched);
$this->assertSame(1, $obj->updated);
$this->assertSame(0, (int)ag($obj->getMetadata($testMovie->via), iState::COLUMN_WATCHED));
// -- update
@@ -153,8 +153,8 @@ class MemoryMapperTest extends TestCase
$obj = $this->mapper->get($testMovie);
$this->assertSame(1, $testMovie->watched);
$this->assertSame(1, (int)$obj->watched);
$this->assertSame(5, (int)$obj->updated);
$this->assertSame(1, $obj->watched);
$this->assertSame(5, $obj->updated);
$this->assertSame(1, (int)ag($obj->getMetadata($testMovie->via), iState::COLUMN_WATCHED));
$this->assertSame(5, (int)ag($obj->getMetadata($testMovie->via), iState::COLUMN_META_DATA_PLAYED_AT));
}
@@ -178,6 +178,72 @@ class MemoryMapperTest extends TestCase
$this->assertSame(0, (int)ag($obj->getMetadata($testMovie->via), iState::COLUMN_WATCHED));
}
/**
* @throws \Exception
*/
public function test_update_unwatch_conflict_no_metadata(): void
{
$this->mapper->add(new StateEntity($this->testMovie));
$this->mapper->commit();
$this->mapper->reset()->loadData();
$timeNow = time();
$testData = $this->testMovie;
$testData[iState::COLUMN_VIA] = 'fiz';
$testData[iState::COLUMN_WATCHED] = 0;
$testData[iState::COLUMN_UPDATED] = $timeNow;
$testData[iState::COLUMN_META_DATA] = [
'fiz' => [
iState::COLUMN_ID => 121,
iState::COLUMN_TYPE => iState::TYPE_MOVIE,
iState::COLUMN_WATCHED => 0,
iState::COLUMN_YEAR => '2020',
iState::COLUMN_META_DATA_EXTRA => [
iState::COLUMN_META_DATA_EXTRA_DATE => '2020-01-03',
],
iState::COLUMN_META_DATA_ADDED_AT => $timeNow,
],
];
$testMovie = new StateEntity($testData);
$this->mapper->add($testMovie, ['after' => new \DateTimeImmutable('@' . ($timeNow - 10))]);
$this->mapper->commit();
$this->mapper->reset()->loadData();
$obj = $this->mapper->get($testMovie);
$this->assertSame(1, $obj->watched);
}
/**
* @throws \Exception
*/
public function test_update_unwatch_conflict_no_date(): void
{
$testData = $this->testMovie;
$timeNow = time();
$testData[iState::COLUMN_UPDATED] = $timeNow;
$testData[iState::COLUMN_META_DATA]['home_plex'][iState::COLUMN_META_DATA_PLAYED_AT] = $timeNow;
$movie = new StateEntity($testData);
$this->mapper->add($movie);
$this->mapper->commit();
$this->mapper->reset()->loadData();
$testData[iState::COLUMN_WATCHED] = 0;
$testData[iState::COLUMN_UPDATED] = $timeNow;
$testMovie = new StateEntity($testData);
$this->mapper->add($testMovie, ['after' => new \DateTimeImmutable('@' . ($timeNow - 10))]);
$this->mapper->commit();
$this->mapper->reset()->loadData();
$obj = $this->mapper->get($testMovie);
$this->assertSame(1, $obj->watched);
}
public function test_get_conditions(): void
{
$movie = $this->testMovie;

View File

@@ -5,8 +5,8 @@ declare(strict_types=1);
namespace Tests\Server;
use App\Libs\Server;
use App\Libs\TestCase;
use Exception;
use PHPUnit\Framework\TestCase;
use RuntimeException;
use Symfony\Component\Process\Process;