|  | 
| 4 | 4 | 
 | 
| 5 | 5 | namespace Yiisoft\Db\Pgsql\Tests; | 
| 6 | 6 | 
 | 
|  | 7 | +use InvalidArgumentException; | 
|  | 8 | +use LogicException; | 
| 7 | 9 | use Throwable; | 
| 8 | 10 | use Yiisoft\Db\Exception\Exception; | 
| 9 | 11 | use Yiisoft\Db\Exception\InvalidConfigException; | 
| 10 | 12 | use Yiisoft\Db\Exception\NotSupportedException; | 
| 11 | 13 | use Yiisoft\Db\Expression\JsonExpression; | 
|  | 14 | +use Yiisoft\Db\Pgsql\Command; | 
| 12 | 15 | use Yiisoft\Db\Pgsql\Connection; | 
| 13 |  | -use Yiisoft\Db\Pgsql\Dsn; | 
| 14 | 16 | use Yiisoft\Db\Pgsql\Driver; | 
|  | 17 | +use Yiisoft\Db\Pgsql\Dsn; | 
| 15 | 18 | use Yiisoft\Db\Pgsql\Tests\Support\TestTrait; | 
| 16 | 19 | use Yiisoft\Db\Tests\Common\CommonCommandTest; | 
| 17 | 20 | use Yiisoft\Db\Tests\Support\DbHelper; | 
| @@ -330,4 +333,76 @@ public function testShowDatabases(): void | 
| 330 | 333 |         $this->assertSame('pgsql:host=127.0.0.1;dbname=postgres;port=5432', $db->getDriver()->getDsn()); | 
| 331 | 334 |         $this->assertSame(['yiitest'], $command->showDatabases()); | 
| 332 | 335 |     } | 
|  | 336 | + | 
|  | 337 | +    public function testRefreshMaterializesView(): void | 
|  | 338 | +    { | 
|  | 339 | +        $db = $this->getConnection(true); | 
|  | 340 | +        /** @var Command $command */ | 
|  | 341 | +        $command = $db->createCommand(); | 
|  | 342 | + | 
|  | 343 | +        $this->assertTrue($command->refreshMaterializedView('mat_view_without_unique')); | 
|  | 344 | +        $this->assertTrue($command->refreshMaterializedView('mat_view_without_unique', false, true)); | 
|  | 345 | +        $this->assertTrue($command->refreshMaterializedView('mat_view_without_unique', false, true)); | 
|  | 346 | +    } | 
|  | 347 | + | 
|  | 348 | +    public static function materializedViewExceptionsDataProvider(): array | 
|  | 349 | +    { | 
|  | 350 | +        return [ | 
|  | 351 | +            [ | 
|  | 352 | +                'mat_view_without_unique', | 
|  | 353 | +                true, | 
|  | 354 | +                null, | 
|  | 355 | +                LogicException::class, | 
|  | 356 | +                'CONCURRENTLY refresh is not allowed without unique index.' | 
|  | 357 | +            ], | 
|  | 358 | + | 
|  | 359 | +            [ | 
|  | 360 | +                'mat_view_with_unique', | 
|  | 361 | +                true, | 
|  | 362 | +                false, | 
|  | 363 | +                LogicException::class, | 
|  | 364 | +                'CONCURRENTLY and WITH NO DATA may not be specified together.' | 
|  | 365 | +            ], | 
|  | 366 | + | 
|  | 367 | +            [ | 
|  | 368 | +                'mat_view_with_unique', | 
|  | 369 | +                null, | 
|  | 370 | +                false, | 
|  | 371 | +                LogicException::class, | 
|  | 372 | +                'CONCURRENTLY and WITH NO DATA may not be specified together.' | 
|  | 373 | +            ], | 
|  | 374 | + | 
|  | 375 | +            [ | 
|  | 376 | +                'not_exists_mat_view', | 
|  | 377 | +                null, | 
|  | 378 | +                null, | 
|  | 379 | +                InvalidArgumentException::class, | 
|  | 380 | +                '"not_exists_mat_view" not found in DB' | 
|  | 381 | +            ] | 
|  | 382 | +        ]; | 
|  | 383 | +    } | 
|  | 384 | + | 
|  | 385 | +    /** | 
|  | 386 | +     * @dataProvider materializedViewExceptionsDataProvider | 
|  | 387 | +     * @param string $viewName | 
|  | 388 | +     * @param bool|null $concurrently | 
|  | 389 | +     * @param bool|null $withData | 
|  | 390 | +     * @param string $exception | 
|  | 391 | +     * @param string $message | 
|  | 392 | +     * @return void | 
|  | 393 | +     * @throws Exception | 
|  | 394 | +     * @throws InvalidConfigException | 
|  | 395 | +     */ | 
|  | 396 | +    public function testRefreshMaterializesViewExceptions(string $viewName, ?bool $concurrently, ?bool $withData, string $exception, string $message): void | 
|  | 397 | +    { | 
|  | 398 | +        $db = $this->getConnection(true); | 
|  | 399 | + | 
|  | 400 | +        /** @var Command $command */ | 
|  | 401 | +        $command = $db->createCommand(); | 
|  | 402 | + | 
|  | 403 | +        $this->expectException($exception); | 
|  | 404 | +        $this->expectExceptionMessage($message); | 
|  | 405 | + | 
|  | 406 | +        $command->refreshMaterializedView($viewName, $concurrently, $withData); | 
|  | 407 | +    } | 
| 333 | 408 | } | 
0 commit comments