ClientFactoryTest   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 22
dl 0
loc 81
rs 10
c 1
b 0
f 0
wmc 9

9 Methods

Rating   Name   Duplication   Size   Complexity  
A testFromDsnAcceptsOptionOfValidType() 0 3 1
A testFromOptionsCreatesClientWithCustomPacker() 0 6 1
A testFromOptionsAcceptsOptionOfValidType() 0 3 1
A testFromDsnCreatesClientWithPurePacker() 0 5 1
A testFromOptionsCreatesClientWithPurePacker() 0 5 1
A testFromOptionsRejectsOptionOfInvalidType() 0 6 1
A testFromDsnCreatesClientWithCustomPacker() 0 6 1
A testFromDsnRejectsOptionOfInvalidType() 0 6 1
A testFromDefaultsCreatesClientWithPurePacker() 0 5 1
1
<?php
2
3
/**
4
 * This file is part of the tarantool/client package.
5
 *
6
 * (c) Eugene Leonovich <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace Tarantool\Client\Tests\Unit;
15
16
use PHPUnit\Framework\TestCase;
17
use Tarantool\Client\Client;
18
use Tarantool\Client\Packer\Packer;
19
use Tarantool\Client\Packer\PurePacker;
20
use Tarantool\Client\Tests\PhpUnitCompat;
21
22
final class ClientFactoryTest extends TestCase
23
{
24
    use PhpUnitCompat;
25
26
    public function testFromDefaultsCreatesClientWithPurePacker() : void
27
    {
28
        $client = Client::fromDefaults();
29
30
        self::assertInstanceOf(PurePacker::class, $client->getHandler()->getPacker());
31
    }
32
33
    public function testFromOptionsCreatesClientWithPurePacker() : void
34
    {
35
        $client = Client::fromOptions(['uri' => 'tcp://tnt']);
36
37
        self::assertInstanceOf(PurePacker::class, $client->getHandler()->getPacker());
38
    }
39
40
    public function testFromOptionsCreatesClientWithCustomPacker() : void
41
    {
42
        $packer = $this->createMock(Packer::class);
43
        $client = Client::fromOptions(['uri' => 'tcp://tnt'], $packer);
44
45
        self::assertSame($packer, $client->getHandler()->getPacker());
46
    }
47
48
    public function testFromDsnCreatesClientWithPurePacker() : void
49
    {
50
        $client = Client::fromDsn('tcp://tnt');
51
52
        self::assertInstanceOf(PurePacker::class, $client->getHandler()->getPacker());
53
    }
54
55
    public function testFromDsnCreatesClientWithCustomPacker() : void
56
    {
57
        $packer = $this->createMock(Packer::class);
58
        $client = Client::fromDsn('tcp://tnt', $packer);
59
60
        self::assertSame($packer, $client->getHandler()->getPacker());
61
    }
62
63
    /**
64
     * @dataProvider \Tarantool\Client\Tests\Unit\OptionsProvider::provideClientArrayOptionsOfValidTypes
65
     * @dataProvider \Tarantool\Client\Tests\Unit\OptionsProvider::provideTcpExtraConnectionArrayOptionsOfValidTypes
66
     * @doesNotPerformAssertions
67
     */
68
    public function testFromOptionsAcceptsOptionOfValidType(string $optionName, $optionValue, array $extraOptions = []) : void
69
    {
70
        Client::fromOptions([$optionName => $optionValue] + $extraOptions);
71
    }
72
73
    /**
74
     * @dataProvider \Tarantool\Client\Tests\Unit\OptionsProvider::provideClientArrayOptionsOfInvalidTypes
75
     * @dataProvider \Tarantool\Client\Tests\Unit\OptionsProvider::provideTcpExtraConnectionArrayOptionsOfInvalidTypes
76
     */
77
    public function testFromOptionsRejectsOptionOfInvalidType(string $optionName, $optionValue, string $expectedType, array $extraOptions = []) : void
78
    {
79
        $this->expectException(\TypeError::class);
80
        $this->expectExceptionMessageMatches("/must be of(?: the)? type $expectedType/");
81
82
        Client::fromOptions([$optionName => $optionValue] + $extraOptions);
83
    }
84
85
    /**
86
     * @dataProvider \Tarantool\Client\Tests\Unit\OptionsProvider::provideClientDsnOptionsOfValidTypes
87
     * @doesNotPerformAssertions
88
     */
89
    public function testFromDsnAcceptsOptionOfValidType(string $query) : void
90
    {
91
        Client::fromDsn("tcp://tnt/?$query");
92
    }
93
94
    /**
95
     * @dataProvider \Tarantool\Client\Tests\Unit\OptionsProvider::provideClientDsnOptionsOfInvalidTypes
96
     */
97
    public function testFromDsnRejectsOptionOfInvalidType(string $query, string $optionName, string $expectedType) : void
98
    {
99
        $this->expectException(\TypeError::class);
100
        $this->expectExceptionMessageMatches("/\b$optionName\b.+?must be of(?: the)? type $expectedType/");
101
102
        Client::fromDsn("tcp://tnt/?$query");
103
    }
104
}
105