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

declare(strict_types=1);

use BitBadger\PDODocument\{Count, DocumentException, Exists, Field, Find, Patch};
use Test\Integration\SQLite\ThrowawayDb;
use Test\Integration\TestDocument;

pest()->group('integration', 'sqlite');

describe('::byId()', function () {
    test('updates an existing document', function () {
        Patch::byId(ThrowawayDb::TABLE, 'one', ['num_value' => 44]);
        $doc = Find::byId(ThrowawayDb::TABLE, 'one', TestDocument::class);
        expect($doc)->isSome->toBeTrue()->and($doc->value)->num_value->toBe(44);
    });
    test('does nothing when no document exists', function () {
        expect(Exists::byId(ThrowawayDb::TABLE, 'forty-seven'))->toBeFalse();
        Patch::byId(ThrowawayDb::TABLE, 'forty-seven', ['foo' => 'green']);
    });
});

describe('::byFields()', function () {
    test('updates matching documents', function () {
        Patch::byFields(ThrowawayDb::TABLE, [Field::equal('value', 'purple')], ['num_value' => 77]);
        expect(Count::byFields(ThrowawayDb::TABLE, [Field::equal('num_value', 77)]))->toBe(2);
    });
    test('does nothing when no documents match', function () {
        expect(Exists::byFields(ThrowawayDb::TABLE, [Field::equal('value', 'burgundy')]))->toBeFalse();
        Patch::byFields(ThrowawayDb::TABLE, [Field::equal('value', 'burgundy')], ['foo' => 'green']);
    });
});

describe('::byContains()', function () {
    test('throws an exception', function () {
        expect(fn () => Patch::byContains('', [], []))->toThrow(DocumentException::class);
    });
});

describe('::byJsonPath()', function () {
    test('throws an exception', function () {
        expect(fn () => Patch::byJsonPath('', '', []))->toThrow(DocumentException::class);
    });
});