<?php
/**
 * @author Daniel J. Summers <daniel@bitbadger.solutions>
 * @license MIT
 */

declare(strict_types=1);

namespace Test\Integration\PostgreSQL;

use BitBadger\PDODocument\{Exists, Field};
use PHPUnit\Framework\Attributes\TestDox;
use PHPUnit\Framework\TestCase;

/**
 * PostgreSQL integration tests for the Exists class
 */
#[TestDox('Exists (PostgreSQL integration)')]
class ExistsTest extends TestCase
{
    /** @var string Database name for throwaway database */
    private string $dbName;

    protected function setUp(): void
    {
        parent::setUp();
        $this->dbName = ThrowawayDb::create();
    }

    protected function tearDown(): void
    {
        ThrowawayDb::destroy($this->dbName);
        parent::tearDown();
    }

    #[TestDox('byId() succeeds when a document exists')]
    public function testByIdSucceedsWhenADocumentExists(): void
    {
        $this->assertTrue(Exists::byId(ThrowawayDb::TABLE, 'three'), 'There should have been an existing document');
    }

    #[TestDox('byId() succeeds when a document does not exist')]
    public function testByIdSucceedsWhenADocumentDoesNotExist(): void
    {
        $this->assertFalse(Exists::byId(ThrowawayDb::TABLE, 'seven'),
            'There should not have been an existing document');
    }

    #[TestDox('byFields() succeeds when documents exist')]
    public function testByFieldsSucceedsWhenDocumentsExist(): void
    {
        $this->assertTrue(Exists::byFields(ThrowawayDb::TABLE, [Field::equal('num_value', 10)]),
            'There should have been existing documents');
    }

    #[TestDox('byFields() succeeds when no matching documents exist')]
    public function testByFieldsSucceedsWhenNoMatchingDocumentsExist(): void
    {
        $this->assertFalse(Exists::byFields(ThrowawayDb::TABLE, [Field::less('nothing', 'none')]),
            'There should not have been any existing documents');
    }

    #[TestDox('byContains() succeeds when documents exist')]
    public function testByContainsSucceedsWhenDocumentsExist(): void
    {
        $this->assertTrue(Exists::byContains(ThrowawayDb::TABLE, ['value' => 'purple']),
            'There should have been existing documents');
    }

    #[TestDox('byContains() succeeds when no matching documents exist')]
    public function testByContainsSucceedsWhenNoMatchingDocumentsExist(): void
    {
        $this->assertFalse(Exists::byContains(ThrowawayDb::TABLE, ['value' => 'violet']),
            'There should not have been existing documents');
    }

    #[TestDox('byJsonPath() succeeds when documents exist')]
    public function testByJsonPathSucceedsWhenDocumentsExist(): void
    {
        $this->assertTrue(Exists::byJsonPath(ThrowawayDb::TABLE, '$.num_value ? (@ == 10)'),
            'There should have been existing documents');
    }

    #[TestDox('byJsonPath() succeeds when no matching documents exist')]
    public function testByJsonPathSucceedsWhenNoMatchingDocumentsExist(): void
    {
        $this->assertFalse(Exists::byJsonPath(ThrowawayDb::TABLE, '$.num_value ? (@ == 10.1)'),
            'There should have been existing documents');
    }
}