first commit
This commit is contained in:
66
tests/Post/PostConcurrentUpdateIntegrationTest.php
Normal file
66
tests/Post/PostConcurrentUpdateIntegrationTest.php
Normal file
@@ -0,0 +1,66 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Tests\Post;
|
||||
|
||||
use App\Post\Post;
|
||||
use App\Post\PostRepository;
|
||||
use App\Post\PostRepositoryInterface;
|
||||
use App\Post\PostService;
|
||||
use App\Shared\Database\Migrator;
|
||||
use App\Shared\Exception\NotFoundException;
|
||||
use App\Shared\Html\HtmlSanitizerInterface;
|
||||
use PDO;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
final class PostConcurrentUpdateIntegrationTest 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, created_at) VALUES (1, 'alice', 'alice@example.com', 'hash', 'user', '2024-01-01 00:00:00')");
|
||||
$this->db->exec("INSERT INTO posts (id, title, content, slug, author_id, created_at, updated_at) VALUES (1, 'Titre', '<p>Contenu</p>', 'titre', 1, '2024-01-01 00:00:00', '2024-01-01 00:00:00')");
|
||||
}
|
||||
|
||||
public function testUpdatePostThrowsWhenRowDisappearsBetweenReadAndWrite(): void
|
||||
{
|
||||
$realRepo = new PostRepository($this->db);
|
||||
$repo = new class($realRepo) implements PostRepositoryInterface {
|
||||
private bool $deleted = false;
|
||||
public function __construct(private readonly PostRepository $inner) {}
|
||||
public function findAll(?int $categoryId = null): array { return $this->inner->findAll($categoryId); }
|
||||
public function findRecent(int $limit): array { return $this->inner->findRecent($limit); }
|
||||
public function findByUserId(int $userId, ?int $categoryId = null): array { return $this->inner->findByUserId($userId, $categoryId); }
|
||||
public function findBySlug(string $slug): ?Post { return $this->inner->findBySlug($slug); }
|
||||
public function findById(int $id): ?Post { return $this->inner->findById($id); }
|
||||
public function create(Post $post, string $slug, int $authorId, ?int $categoryId): int { return $this->inner->create($post, $slug, $authorId, $categoryId); }
|
||||
public function update(int $id, Post $post, string $slug, ?int $categoryId): int {
|
||||
if (!$this->deleted) {
|
||||
$this->deleted = true;
|
||||
$this->inner->delete($id);
|
||||
}
|
||||
return $this->inner->update($id, $post, $slug, $categoryId);
|
||||
}
|
||||
public function delete(int $id): int { return $this->inner->delete($id); }
|
||||
public function search(string $query, ?int $categoryId = null, ?int $authorId = null): array { return $this->inner->search($query, $categoryId, $authorId); }
|
||||
public function slugExists(string $slug, ?int $excludeId = null): bool { return $this->inner->slugExists($slug, $excludeId); }
|
||||
};
|
||||
|
||||
$sanitizer = new class implements HtmlSanitizerInterface {
|
||||
public function sanitize(string $html): string { return $html; }
|
||||
};
|
||||
|
||||
$service = new PostService($repo, $sanitizer);
|
||||
|
||||
$this->expectException(NotFoundException::class);
|
||||
$service->updatePost(1, 'Titre modifié', '<p>Contenu modifié</p>');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user