Files
netslim-core/tests/Media/MediaSchemaIntegrationTest.php
2026-03-20 22:13:41 +01:00

81 lines
2.5 KiB
PHP

<?php
declare(strict_types=1);
namespace Tests\Media;
use Netig\Netslim\Kernel\Persistence\Infrastructure\Migrator;
use PDO;
use PDOException;
use PHPUnit\Framework\TestCase;
final class MediaSchemaIntegrationTest extends TestCase
{
private PDO $db;
protected function setUp(): void
{
$this->db = new PDO('sqlite::memory:', options: [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
]);
$this->db->sqliteCreateFunction('strip_tags', 'strip_tags', 1);
Migrator::run($this->db);
$this->db->exec("INSERT INTO users (id, username, email, password_hash, role) VALUES (1, 'alice', 'alice@example.com', 'hash', 'user')");
$this->db->exec("INSERT INTO users (id, username, email, password_hash, role) VALUES (2, 'bob', 'bob@example.com', 'hash', 'user')");
}
public function testMediaHashIsUniquePerUser(): void
{
$stmt = $this->db->prepare(
'INSERT INTO media (filename, url, hash, user_id, created_at) VALUES (:filename, :url, :hash, :user_id, :created_at)',
);
$stmt->execute([
':filename' => 'first.webp',
':url' => '/media/first.webp',
':hash' => 'same-hash',
':user_id' => 1,
':created_at' => '2026-03-19 10:00:00',
]);
$this->expectException(PDOException::class);
$stmt->execute([
':filename' => 'second.webp',
':url' => '/media/second.webp',
':hash' => 'same-hash',
':user_id' => 1,
':created_at' => '2026-03-19 10:01:00',
]);
}
public function testMediaHashCanBeSharedAcrossDifferentUsers(): void
{
$stmt = $this->db->prepare(
'INSERT INTO media (filename, url, hash, user_id, created_at) VALUES (:filename, :url, :hash, :user_id, :created_at)',
);
$stmt->execute([
':filename' => 'alice.webp',
':url' => '/media/alice.webp',
':hash' => 'same-hash',
':user_id' => 1,
':created_at' => '2026-03-19 10:00:00',
]);
$stmt->execute([
':filename' => 'bob.webp',
':url' => '/media/bob.webp',
':hash' => 'same-hash',
':user_id' => 2,
':created_at' => '2026-03-19 10:01:00',
]);
$count = (int) $this->db->query("SELECT COUNT(*) FROM media WHERE hash = 'same-hash'")->fetchColumn();
self::assertSame(2, $count);
}
}