getConnectionForArrayExceptionDataProvider()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 3
c 1
b 0
f 1
nc 1
nop 0
dl 0
loc 10
rs 10
1
<?php
2
namespace Mezon\PdoCrud\Tests;
3
4
use Mezon\Conf\Conf;
5
use Mezon\PdoCrud\PdoCrud;
6
use PHPUnit\Framework\TestCase;
7
8
class ConnectionTraitUnitTest extends ConnectionTraitTests
9
{
10
11
    /**
12
     * Testing insertion method
13
     */
14
    public function testGetConnection(): void
15
    {
16
        // setupp
17
        $this->setDsn('dsn');
18
        $this->setUser('user');
19
        $this->setPassword('password');
20
        $mock = $this->getMock();
21
        $mock->setConnection(false);
22
23
        $mock->expects($this->once())
24
            ->method('constructConnection')
25
            ->willReturn($this->getPdoMock());
26
27
        // test body and assertionss
28
        $mock->getConnection();
29
    }
30
31
    /**
32
     * Testing data provider
33
     *
34
     * @return array testing data
35
     */
36
    public function getConnectionForArrayDataProvider(): array
37
    {
38
        return [
39
            // #0, the first case, two connections the first one is fetched
40
            [
41
                function (): void {
42
                    // setup method
43
                    $this->setConnection();
44
                    $this->setConnection('exact-connection');
45
                }
46
            ],
47
            // #1, the first case, two connections the second one is fetched
48
            [
49
                function (): void {
50
                    // setup method
51
                    $this->setConnection('exact-connection');
52
                    $this->setConnection();
53
                }
54
            ],
55
            // #2, the third case, connection was not found
56
            [
57
                function (): void {
58
                    // setup method
59
                    $this->setConnection('first-connection');
60
                    $this->setConnection('second-connection');
61
62
                    $this->expectException(\Exception::class);
63
                }
64
            ]
65
        ];
66
    }
67
68
    /**
69
     * Testing method
70
     *
71
     * @param callable $setup
72
     *            setup method
73
     * @dataProvider getConnectionForArrayDataProvider
74
     */
75
    public function testGetConnectionForArray(callable $setup): void
76
    {
77
        // setup and assertions
78
        $setup();
79
        $mock = $this->getMock();
80
        $mock->setConnection(false);
81
        $mock->expects($this->once())
82
            ->method('constructConnection')
83
            ->willReturn($this->getPdoMock());
84
85
        // test body
86
        $mock->getConnection([
87
            'exact-connection'
88
        ]);
89
    }
90
91
    /**
92
     * Data provider for the test testGetConnectionForArrayException
93
     *
94
     * @return array testing data
95
     */
96
    public function getConnectionForArrayExceptionDataProvider(): array
97
    {
98
        return [
99
            [
100
                [
101
                    'exact-connection'
102
                ]
103
            ],
104
            [
105
                new \stdClass()
106
            ]
107
        ];
108
    }
109
110
    /**
111
     * Testing exception for array type connection name
112
     *
113
     * @param mixed $connectionNAme
114
     *            connection name
115
     * @dataProvider getConnectionForArrayExceptionDataProvider
116
     */
117
    public function testGetConnectionForArrayException($connectionName): void
118
    {
119
        // TODO add snippet for testing exception with data provider
120
        // assertions
121
        $this->expectException(\Exception::class);
122
123
        // setup
124
        Conf::deleteConfigValue('exact-connection/dsn');
125
        $this->setConnection('first-connection');
126
        $this->setConnection('second-connection');
127
        $mock = $this->getMock();
128
        $mock->setConnection(false);
129
130
        // test body
131
        $mock->getConnection($connectionName);
132
    }
133
134
    /**
135
     * Testing method cached getConnection
136
     */
137
    public function testGetConnectionCached(): void
138
    {
139
        // setup
140
        $mock = $this->getMock();
141
        $mock->setConnection(new PdoCrudMock());
142
143
        // test body and assertions
144
        $this->assertInstanceOf(PdoCrudMock::class, $mock->getConnection('some-connection-wich-does-not-exists'));
145
    }
146
}
147