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

declare(strict_types=1);

use BitBadger\PDODocument\{Configuration, DocumentException, Field, Mode};
use BitBadger\PDODocument\Query\Count;

pest()->group('unit');

afterEach(function () { Configuration::overrideMode(null); });

describe('::all()', function () {
    test('generates the correct SQL', function () {
        expect(Count::all('a_table'))->toBe('SELECT COUNT(*) FROM a_table');
    });
});

describe('::byFields()', function () {
    test('generates the correct SQL', function () {
        Configuration::overrideMode(Mode::SQLite);
        expect(Count::byFields('somewhere', [Field::greater('errors', 10, ':errors')]))
            ->toBe("SELECT COUNT(*) FROM somewhere WHERE data->>'errors' > :errors");
    });
});

describe('::byContains()', function () {
    test('generates the correct SQL [PostgreSQL]', function () {
        Configuration::overrideMode(Mode::PgSQL);
        expect(Count::byContains('the_table'))->toBe('SELECT COUNT(*) FROM the_table WHERE data @> :criteria');
    })->group('postgresql');
    test('throws an exception [SQLite]', function () {
        Configuration::overrideMode(Mode::SQLite);
        expect(fn () => Count::byContains(''))->toThrow(DocumentException::class);
    })->group('sqlite');
});

describe('::byJsonPath()', function () {
    test('generates the correct SQL [PostgreSQL]', function () {
        Configuration::overrideMode(Mode::PgSQL);
        expect(Count::byJsonPath('a_table'))
            ->toBe('SELECT COUNT(*) FROM a_table WHERE jsonb_path_exists(data, :path::jsonpath)');
    })->group('postgresql');
    test('throws an exception [SQLite]', function () {
        Configuration::overrideMode(Mode::SQLite);
        expect(fn () => Count::byJsonPath(''))->toThrow(DocumentException::class);
    })->group('sqlite');
});