first commit
This commit is contained in:
80
tests/Media/MediaSchemaIntegrationTest.php
Normal file
80
tests/Media/MediaSchemaIntegrationTest.php
Normal file
@@ -0,0 +1,80 @@
|
||||
<?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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user