|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | +/** |
| 5 | + * This file is part of Hyperf. |
| 6 | + * |
| 7 | + * @link https://www.hyperf.io |
| 8 | + * @document https://hyperf.wiki |
| 9 | + |
| 10 | + * @license https://github.com/hyperf/hyperf/blob/master/LICENSE |
| 11 | + */ |
| 12 | +namespace HyperfTest\Database\PgSQL\Cases; |
| 13 | + |
| 14 | +use Hyperf\Database\Connection; |
| 15 | +use Hyperf\Database\Connectors\ConnectionFactory; |
| 16 | +use Hyperf\Database\Exception\QueryException; |
| 17 | +use Hyperf\Database\PgSQL\Connectors\PostgresSqlSwooleExtConnector; |
| 18 | +use Hyperf\Database\PgSQL\PostgreSqlSwooleExtConnection; |
| 19 | +use Hyperf\Database\Query\Builder; |
| 20 | +use PHPUnit\Framework\TestCase; |
| 21 | +use Psr\Container\ContainerInterface; |
| 22 | + |
| 23 | +/** |
| 24 | + * @internal |
| 25 | + * @coversNothing |
| 26 | + */ |
| 27 | +class PostgreSqlSwooleExtConnectionTest extends TestCase |
| 28 | +{ |
| 29 | + protected ConnectionFactory $connectionFactory; |
| 30 | + |
| 31 | + public function setUp(): void |
| 32 | + { |
| 33 | + $container = \Mockery::mock(ContainerInterface::class); |
| 34 | + $container->shouldReceive('has')->andReturn(true); |
| 35 | + $container->shouldReceive('get')->with('db.connector.pgsql-swoole')->andReturn(new PostgresSqlSwooleExtConnector()); |
| 36 | + |
| 37 | + $this->connectionFactory = new ConnectionFactory($container); |
| 38 | + |
| 39 | + Connection::resolverFor('pgsql-swoole', static function ($connection, $database, $prefix, $config) { |
| 40 | + return new PostgreSqlSwooleExtConnection($connection, $database, $prefix, $config); |
| 41 | + }); |
| 42 | + } |
| 43 | + |
| 44 | + public function testSelectMethodDuplicateKeyValueException() |
| 45 | + { |
| 46 | + if (SWOOLE_MAJOR_VERSION < 5) { |
| 47 | + $this->markTestSkipped('PostgreSql requires Swoole version >= 5.0.0'); |
| 48 | + } |
| 49 | + |
| 50 | + $connection = $this->connectionFactory->make([ |
| 51 | + 'driver' => 'pgsql-swoole', |
| 52 | + 'host' => '127.0.0.1', |
| 53 | + 'port' => 5432, |
| 54 | + 'database' => 'postgres', |
| 55 | + 'username' => 'postgres', |
| 56 | + 'password' => 'postgres', |
| 57 | + ]); |
| 58 | + |
| 59 | + $builder = new Builder($connection); |
| 60 | + |
| 61 | + $this->expectException(QueryException::class); |
| 62 | + $this->expectExceptionMessage('ERROR: duplicate key value violates unique constraint "users_email"'); |
| 63 | + |
| 64 | + $id = $builder-> from( 'users')-> insertGetId([ 'email' => '[email protected]', 'name' => 'hyperf'], 'id'); |
| 65 | + $id2 = $builder-> from( 'users')-> insertGetId([ 'email' => '[email protected]', 'name' => 'hyperf'], 'id'); |
| 66 | + |
| 67 | + // Never here |
| 68 | + $this->assertIsNumeric($id); |
| 69 | + $this->assertIsNumeric($id2); |
| 70 | + } |
| 71 | +} |
0 commit comments