Skip to content

Commit

Permalink
updated tests for new features
Browse files Browse the repository at this point in the history
  • Loading branch information
brainfoolong committed Jan 27, 2024
1 parent 613a593 commit ee9aa81
Show file tree
Hide file tree
Showing 11 changed files with 87 additions and 60 deletions.
9 changes: 1 addition & 8 deletions appdata/modules/Framelix/src/Db/StorableSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,7 @@ public function parseClassData(): void
if ($typeIsArray) {
throw new FatalError(
'Array\'s are not supported in @property at ' . $reflectionClass->getName(
) . ' annotations of Storables. Consider using a separate storable and add a reference property in that new storable. For example the ' . User::class . ' and ' . UserRole::class
);
) . ' annotations of Storables. Consider using StorableArray instead.');
}
if (!in_array($type, ["bool", "int", "float", "double", "string", "mixed"])) {
$possibleClassNames[] = $namespace . "\\" . $type;
Expand Down Expand Up @@ -216,12 +215,6 @@ public function parseClassData(): void
// mixed is considered to be json of any length, so use a big data type that can hold lots of data
$storableSchemaProperty->databaseType = 'longtext';
}
if ($type === 'array') {
// typed arrays have a special meaning and later become an extra table in the database
// but the column itself hold just an integer of number of entries in the array
$storableSchemaProperty->databaseType = 'int';
$storableSchemaProperty->length = 11;
}
}
if (!$storableSchemaProperty->databaseType) {
throw new FatalError(
Expand Down
2 changes: 1 addition & 1 deletion appdata/modules/Framelix/src/Storable/StorableFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ public function getImageSize(?int $thumbnailSize = null): ?array
$thumbnailWidth = $thumbnailSize;
$thumbnailHeight = $thumbnailSize;
}
return ['width' => $thumbnailWidth, 'height' => $thumbnailHeight];
return ['width' => (int)$thumbnailWidth, 'height' => (int)$thumbnailHeight];
}
return $imageSize;
}
Expand Down
14 changes: 9 additions & 5 deletions appdata/modules/Framelix/src/Utils/Mutex.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use Throwable;

use function file_exists;
use function file_get_contents;
use function file_put_contents;
use function filemtime;
use function sleep;
Expand All @@ -18,10 +17,12 @@
*/
class Mutex
{

/**
* Create a mutex for given name
* If mutex with this name already exist, it does update the mutex start time
* @param string $name The name is in the namespace of the FRAMELIX_MODULE, so each module can have the same name without an interferance
* @param string $name The name is in the namespace of the FRAMELIX_MODULE, so each module can have the same name
* without an interferance
*/
public static function create(string $name): void
{
Expand All @@ -41,9 +42,11 @@ public static function create(string $name): void
/**
* Check if a mutex with given name exist
* @param string $name
* @param int|null $maxLifetime Seconds from start of mutex - If set, it will return true when mutex is older than max lifetime
* Should be used when you not want a deadlock if a job cant release a mutex because of errors and mutex is never released
* @return int 0 = not locked, -1 = inifinite locked (not maxlifetime given), > 0 Seconds how long the mutex is still locked
* @param int|null $maxLifetime Seconds from start of mutex - If set, it will return true when mutex is older than
* max lifetime Should be used when you not want a deadlock if a job cant release a mutex because of errors and
* mutex is never released
* @return int 0 = not locked, -1 = inifinite locked (not maxlifetime given), > 0 Seconds how long the mutex is
* still locked
*/
public static function isLocked(string $name, ?int $maxLifetime = null): int
{
Expand All @@ -70,4 +73,5 @@ public static function release(string $name): void
unlink($file);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,9 @@ protected static function setupStorableSchema(StorableSchema $selfStorableSchema
$selfStorableSchema->connectionId = "test";
}

public function __construct()
public static function getUserdataFolderPath(bool $public = false): string
{
parent::__construct();
$this->relativePathOnDisk = "tmp/storablefiletest";
return parent::getUserdataFolderPath(true);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,29 @@
*/
class TestStorableSystemValue extends SystemValue
{

public static function setupStorableMeta(\Framelix\Framelix\StorableMeta\SystemValue $meta): void
{
}

public function isDeletable(): bool
{
// just to extend coverage
parent::isDeletable();
return true;
}

public function isEditable(): bool
{
// just to extend coverage
parent::isEditable();
return true;
}

public function isReadable(): bool
{
// just to extend coverage
parent::isReadable();
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

abstract class StorableArrayTestBase extends TestCaseDbTypes
{

public function test(): void
{
$this->setupDatabase(true);
Expand All @@ -28,22 +29,32 @@ public function test(): void
$testValue2 = ['foo', ['12302']];

StorableArray::setValue($parent, 'test', $testValue);
StorableArray::setValue($parent2, 'test', $testValue);
StorableArray::setValue($parent, 'test2', $testValue2);
StorableArray::setValue($parent2, 'test2', $testValue2);
StorableArray::setValues($parent2, ['test' => $testValue, 'test2' => $testValue2, 'test3' => $testValue2]);
$this->assertSame($testValue, StorableArray::getValue($parent, 'test'));
$this->assertSame($testValue, StorableArray::getValue($parent, 'test'));
$this->assertSame($testValue, StorableArray::getValue($parent, 'test', true));
$this->assertSame($testValue, StorableArray::getValue($parent2, 'test'));
$this->assertSame($testValue2, StorableArray::getValue($parent, 'test2'));
$this->assertSame($testValue2, StorableArray::getValue($parent2, 'test2'));
$this->assertSame($testValue2, StorableArray::getValue($parent2, 'test3'));

// using setValues will delete all other keys that have existed
StorableArray::setValues($parent2, ['test' => $testValue, 'test2' => $testValue2]);
$this->assertNull(StorableArray::getValue($parent2, 'test3'));

StorableArray::deleteValue($parent, 'test');
$this->assertTrue(StorableArray::deleteValue($parent, 'test'));
$this->assertFalse(StorableArray::deleteValue($parent, 'test'));
$this->assertNull(StorableArray::getValue($parent, 'test'));
$this->assertSame($testValue2, StorableArray::getValue($parent, 'test2'));

StorableArray::deleteValues($parent2);
// null will just delete that one key
StorableArray::setValue($parent2, 'test', null);
$this->assertNull(StorableArray::getValue($parent2, 'test'));
$this->assertNull(StorableArray::getValue($parent2, 'test2'));
$this->assertSame($testValue2, StorableArray::getValue($parent2, 'test2'));

StorableArray::deleteValues($parent2);
$this->assertCount(0, StorableArray::getValues($parent2));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,25 @@

namespace Db\Storables;

use Framelix\Framelix\Html\TableCell;
use Framelix\Framelix\Network\UploadedFile;
use Framelix\Framelix\Url;
use Framelix\Framelix\Utils\FileUtils;
use Framelix\FramelixTests\Storable\TestStorableFile;
use Framelix\FramelixTests\TestCaseDbTypes;

use function mkdir;

abstract class StorableFileTestBase extends TestCaseDbTypes
{

public function test(): void
{
$storableFile = new TestStorableFile();
FileUtils::deleteDirectory(FRAMELIX_USERDATA_FOLDER . "/" . $storableFile->relativePathOnDisk);
mkdir(FRAMELIX_USERDATA_FOLDER . "/" . $storableFile->relativePathOnDisk, recursive: true);

FileUtils::deleteDirectory(FileUtils::getUserdataFilepath("storablefile", true));
$this->setupDatabase();
$this->addSimulatedFile("test.txt", "foobar", false);

$uploadedPdfFile = UploadedFile::createFromFile(__DIR__ . "/../../../test-files/imageutils/test-pdf.pdf");
$uploadedImageFile = UploadedFile::createFromFile(__DIR__ . "/../../../test-files/imageutils/test-image.jpg");

// simulate not existing file
$this->assertExceptionOnCall(function () {
$uploadedFile = UploadedFile::createFromSubmitData("test.txt")[0];
Expand All @@ -41,54 +41,55 @@ public function test(): void
$storableFile->store(false, "foobar");
});

$storableFile = new TestStorableFile();
$storableFile->filename = "test.txt";
$storableFile->store(false, "test");
$storableFilePdf = new TestStorableFile();
$storableFilePdf->store(false, $uploadedPdfFile, true);

$storableFile2 = new TestStorableFile();
$storableFile2->filename = "test.txt";
$storableFile2->store(false, "test");
$storableFilePdf2 = new TestStorableFile();
$storableFilePdf2->store(false, $uploadedPdfFile, true);

$storableFile3 = new TestStorableFile();
$storableFile3->filename = "test.jpg";
$storableFile3->store(false, "test");
$storableFileImg = new TestStorableFile();
$storableFileImg->store(false, $uploadedImageFile, true);
$this->assertStringContainsString(
'<framelix-image',
$storableFile3->getImageTag(true, true, 1000, 1200, true, true)
$storableFileImg->getImageTag(true, true, 1000, 1200, true, true)
);

$this->assertInstanceOf(Url::class, $storableFilePdf->getDownloadUrl());
$this->assertIsString($storableFilePdf->getHtmlString());
$this->assertSame($uploadedPdfFile->getFileContents(), $storableFilePdf->getFileContents());
$this->assertSame($uploadedPdfFile->getFileContents(), $storableFilePdf2->getFileContents());

$this->assertInstanceOf(Url::class, $storableFile->getDownloadUrl());
$this->assertIsString($storableFile->getHtmlString());
$this->assertSame('test', $storableFile->getFileContents());
$this->assertSame('test', $storableFile2->getFileContents());
$storableFile->delete();
$storableFile2->delete();
$this->assertNull($storableFile2->getDownloadUrl());
$this->assertIsArray($storableFilePdf->getMetadata());
$this->assertSame(['width' => 275, 'height' => 183], $storableFileImg->getImageSize());
$this->assertSame(['width' => 100, 'height' => 67], $storableFileImg->getImageSize(100));
$this->assertInstanceOf(TableCell::class, $storableFileImg->getHtmlTableValue());

$storableFilePdf->delete();
$storableFilePdf2->delete();
$this->assertNull($storableFilePdf2->getDownloadUrl());
// deleted file return html string anyway
$this->assertIsString($storableFile->getHtmlString());
$this->assertIsString($storableFilePdf->getHtmlString());

$this->addSimulatedFile("test.txt", "foobar", false);
$uploadedFile = UploadedFile::createFromSubmitData("test.txt")[0];
$storableFile = new TestStorableFile();
$storableFile->store(false, $uploadedFile);
$storableFilePdf = new TestStorableFile();
$storableFilePdf->store(false, $uploadedFile);

// restore to test update functionality
$this->addSimulatedFile("test.txt", "foobar", false);
$uploadedFile = UploadedFile::createFromSubmitData("test.txt")[0];
$storableFile->store(false, $uploadedFile);
$storableFile->store(false, "foobar2");
$this->assertSame('foobar2', $storableFile->getFileContents());
$storableFilePdf->store(false, $uploadedFile);
$storableFilePdf->store(false, "foobar2");
$this->assertSame('foobar2', $storableFilePdf->getFileContents());

// only update metadata without file changes
$storableFile->filename = "foo";
$storableFile->store();

$storableFile->delete();
$storableFilePdf->filename = "foo";
$storableFilePdf->store();

$this->assertNull($storableFile->getFileContents());
$storableFilePdf->delete();

$storableFile = new TestStorableFile();
FileUtils::deleteDirectory(FRAMELIX_USERDATA_FOLDER . "/" . $storableFile->relativePathOnDisk);
$this->assertNull($storableFilePdf->getFileContents());
FileUtils::deleteDirectory(FileUtils::getUserdataFilepath("storablefile", true));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Db\Storables;

final class StorableFileTestMysqlTest extends StorableFolderTestBase
final class StorableFileTestMysqlTest extends StorableFileTestBase
{

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Db\Storables;

final class StorableFileTestSqliteTest extends StorableFolderTestBase
final class StorableFileTestSqliteTest extends StorableFileTestBase
{

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

abstract class SystemValueTestBase extends TestCaseDbTypes
{

public function test(): void
{
$this->setupDatabase(true);
Expand Down Expand Up @@ -48,6 +49,15 @@ public function test(): void
TestStorableSystemValue::getEntries()
);

$this->assertSame(
[],
TestStorableSystemValue::getEntries(additionalCondition: "0")
);

$this->assertStorableDefaultGetters($storable3);

$meta = new \Framelix\Framelix\StorableMeta\SystemValue($storable3);
TestStorableSystemValue::setupStorableMeta($meta);
}

}
2 changes: 2 additions & 0 deletions appdata/modules/FramelixTests/tests/Utils/HtmlUtilsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Utils;

use Framelix\Framelix\Config;
use Framelix\Framelix\Url;
use Framelix\Framelix\Utils\HtmlUtils;
use Framelix\FramelixTests\TestCase;
Expand All @@ -16,6 +17,7 @@ public function tests(): void
$this->assertSame("&amp;<br />\n", HtmlUtils::escape("&\n", true));
$this->assertIsString(HtmlUtils::getIncludeTagForUrl(Url::create()->appendPath(".css")));
$this->assertIsString(HtmlUtils::getIncludeTagForUrl(Url::create()->appendPath(".js")));
$this->assertIsString(HtmlUtils::getIncludeTagsForBundles(Config::$compilerFileBundles));

$this->assertExceptionOnCall(function () {
HtmlUtils::getIncludeTagForUrl(Url::create()->appendPath(".jpeg"));
Expand Down

0 comments on commit ee9aa81

Please sign in to comment.