Skip to content

Query lang.middleware

Uses

php
<?php

use Leevel\Database\Condition;
use Leevel\Kernel\Utils\Api;
use Tests\Database\DatabaseTestCase as TestCase;
use Tests\Database\Query\Database\Demo;
use Tests\Database\Query\Database\Demo2;
use Tests\Database\Query\Database\Demo3;
use Tests\Database\Query\Database\ForceMaster;

middleware 基础用法

fixture 定义

php
namespace Tests\Database\Query\Database;

use Leevel\Database\Condition;

class ForceMaster
{
    public function terminate(\Closure $next, Condition $condition, array $middlewaresConfigs, array $makeSql): array
    {
        $makeSql = array_merge(['force_master' => '/*FORCE_MASTER*/'], $makeSql);

        return $next($condition, $middlewaresConfigs, $makeSql);
    }
}
php
public function testBaseUse(): void
{
    $connect = $this->createDatabaseConnectMock();

    $sql = <<<'eot'
        [
            "\/*FORCE_MASTER*\/ SELECT `test_query`.* FROM `test_query` WHERE `test_query`.`id` = :test_query_id",
            {
                "test_query_id": [
                    5
                ]
            },
            false
        ]
        eot;

    self::assertSame(
        $sql,
        $this->varJsonSql(
            $connect
                ->table('test_query')
                ->middlewares(ForceMaster::class)
                ->where('id', '=', 5)
                ->findAll(),
            $connect
        )
    );
}

registerMiddlewares 注册查询中间件

php
public function testRegisterMiddlewares(): void
{
    $this->createDatabaseConnectMock();
    $data = Condition::registerMiddlewares([Demo::class]);

    $result = <<<'eot'
        [
            [
                "Tests\\Database\\Query\\Database\\Demo@terminate"
            ],
            [
                "Tests\\Database\\Query\\Database\\Demo@handle"
            ]
        ]
        eot;

    self::assertSame(
        $result,
        $this->varJson($data)
    );
}

middleware 支持参数传递

fixture 定义

php
namespace Tests\Database\Query\Database;

use Leevel\Database\Condition;

class Demo
{
    public function handle(\Closure $next, Condition $condition, array $middlewaresConfigs): array
    {
        $condition->where('id', '>', 5);
        $condition->where('id', '<=', 90);
        $middlewaresConfigs['hello_comment'] = 'hello comment';

        return $next($condition, $middlewaresConfigs);
    }

    public function terminate(\Closure $next, Condition $condition, array $middlewaresConfigs, array $makeSql): array
    {
        $makeSql = array_merge(['force_master' => '/*'.$middlewaresConfigs['hello_comment'].'*/'], $makeSql);

        return $next($condition, $middlewaresConfigs, $makeSql);
    }
}
php
public function testBaseUseWithArgs(): void
{
    $connect = $this->createDatabaseConnectMock();

    $sql = <<<'eot'
        [
            "\/*hello comment*\/ SELECT `test_query`.* FROM `test_query` WHERE `test_query`.`id` > :test_query_id AND `test_query`.`id` <= :test_query_id_1 AND `test_query`.`id` = :test_query_id_2",
            {
                "test_query_id": [
                    5
                ],
                "test_query_id_1": [
                    90
                ],
                "test_query_id_2": [
                    5
                ]
            },
            false
        ]
        eot;

    self::assertSame(
        $sql,
        $this->varJsonSql(
            $connect
                ->table('test_query')
                ->middlewares(Demo::class)
                ->where('id', '=', 5)
                ->findAll(),
            $connect
        )
    );
}