68 lines
1.8 KiB
PHP
68 lines
1.8 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Tests\Post;
|
|
|
|
use App\Post\Infrastructure\PdoTaxonUsageChecker;
|
|
use PDO;
|
|
use PDOStatement;
|
|
use PHPUnit\Framework\MockObject\MockObject;
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
#[\PHPUnit\Framework\Attributes\AllowMockObjectsWithoutExpectations]
|
|
final class TaxonUsageCheckerTest extends TestCase
|
|
{
|
|
/** @var PDO&MockObject */
|
|
private PDO $db;
|
|
|
|
private PdoTaxonUsageChecker $checker;
|
|
|
|
protected function setUp(): void
|
|
{
|
|
$this->db = $this->createMock(PDO::class);
|
|
$this->checker = new PdoTaxonUsageChecker($this->db);
|
|
}
|
|
|
|
private function stmtForScalar(mixed $value): MockObject&PDOStatement
|
|
{
|
|
$stmt = $this->createMock(PDOStatement::class);
|
|
$stmt->method('execute')->willReturn(true);
|
|
$stmt->method('fetchColumn')->willReturn($value);
|
|
|
|
return $stmt;
|
|
}
|
|
|
|
public function testReturnsTrueWhenTaxonIsReferencedByAtLeastOnePost(): void
|
|
{
|
|
$stmt = $this->stmtForScalar(1);
|
|
$this->db->method('prepare')->willReturn($stmt);
|
|
|
|
self::assertTrue($this->checker->isTaxonInUse(5));
|
|
}
|
|
|
|
public function testReturnsFalseWhenTaxonIsUnused(): void
|
|
{
|
|
$stmt = $this->stmtForScalar(0);
|
|
$this->db->method('prepare')->willReturn($stmt);
|
|
|
|
self::assertFalse($this->checker->isTaxonInUse(5));
|
|
}
|
|
|
|
public function testQueriesPostsTableWithTaxonIdentifier(): void
|
|
{
|
|
$stmt = $this->stmtForScalar(0);
|
|
|
|
$this->db->expects($this->once())
|
|
->method('prepare')
|
|
->with($this->stringContains('FROM posts'))
|
|
->willReturn($stmt);
|
|
|
|
$stmt->expects($this->once())
|
|
->method('execute')
|
|
->with([':category_id' => 5]);
|
|
|
|
$this->checker->isTaxonInUse(5);
|
|
}
|
|
}
|