80 lines
2.5 KiB
PHP
80 lines
2.5 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Tests\Media;
|
|
|
|
use Netig\Netslim\Kernel\Persistence\Infrastructure\Migrator;
|
|
use PDOException;
|
|
use PHPUnit\Framework\TestCase;
|
|
use Tests\Support\TestDatabaseFactory;
|
|
use Tests\Support\TestRuntimeFactory;
|
|
|
|
final class MediaSchemaIntegrationTest extends TestCase
|
|
{
|
|
private \PDO $db;
|
|
|
|
protected function setUp(): void
|
|
{
|
|
TestRuntimeFactory::resetRuntime();
|
|
$this->db = TestDatabaseFactory::createInMemory();
|
|
|
|
$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);
|
|
}
|
|
}
|