Completed
Push — master ( 9b3ca3...174920 )
by Alex
08:09
created

ConnectionTraitUnitTest::getPdoMock()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
namespace Mezon\PdoCrud\Tests;
3
4
use Mezon\Conf\Conf;
5
6
class ConnectionTraitUnitTest extends \PHPUnit\Framework\TestCase
7
{
8
9
    /**
10
     * Method returns mock
11
     *
12
     * @return object mock
13
     */
14
    protected function getPdoMock(): object
15
    {
16
        return $this->getMockBuilder(\Mezon\PdoCrud\PdoCrud::class)
0 ignored issues
show
Deprecated Code introduced by
The function PHPUnit\Framework\MockOb...ckBuilder::setMethods() has been deprecated: https://github.com/sebastianbergmann/phpunit/pull/3687 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

16
        return /** @scrutinizer ignore-deprecated */ $this->getMockBuilder(\Mezon\PdoCrud\PdoCrud::class)

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
17
            ->setMethods([
18
            'connect'
19
        ])
20
            ->getMock();
21
    }
22
23
    /**
24
     * Method returns mock
25
     *
26
     * @return object mock
27
     */
28
    protected function getMock(): object
29
    {
30
        return $this->getMockBuilder(TraitClient::class)
0 ignored issues
show
Deprecated Code introduced by
The function PHPUnit\Framework\MockOb...ckBuilder::setMethods() has been deprecated: https://github.com/sebastianbergmann/phpunit/pull/3687 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

30
        return /** @scrutinizer ignore-deprecated */ $this->getMockBuilder(TraitClient::class)

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
31
            ->setMethods([
32
            'constructConnection'
33
        ])
34
            ->getMock();
35
    }
36
37
    /**
38
     * Method sets dsn
39
     * 
40
     * @param string $dsn dsn
41
     */
42
    protected function setDsn(string $dsn):void{
43
        Conf::setConfigValue('default-db-connection/dsn', $dsn);
0 ignored issues
show
Bug introduced by
'default-db-connection/dsn' of type string is incompatible with the type array expected by parameter $route of Mezon\Conf\Conf::setConfigValue(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

43
        Conf::setConfigValue(/** @scrutinizer ignore-type */ 'default-db-connection/dsn', $dsn);
Loading history...
44
    }
45
46
    /**
47
     * Method sets user
48
     * 
49
     * @param string $user user
50
     */
51
    protected function setUser(string $user):void{
52
        Conf::setConfigValue('default-db-connection/user', $user);
0 ignored issues
show
Bug introduced by
'default-db-connection/user' of type string is incompatible with the type array expected by parameter $route of Mezon\Conf\Conf::setConfigValue(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

52
        Conf::setConfigValue(/** @scrutinizer ignore-type */ 'default-db-connection/user', $user);
Loading history...
53
    }
54
55
    /**
56
     * Method sets password
57
     * 
58
     * @param string $password password
59
     */
60
    protected function setPassword(string $password):void{
61
        Conf::setConfigValue('default-db-connection/password', $password);
0 ignored issues
show
Bug introduced by
'default-db-connection/password' of type string is incompatible with the type array expected by parameter $route of Mezon\Conf\Conf::setConfigValue(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

61
        Conf::setConfigValue(/** @scrutinizer ignore-type */ 'default-db-connection/password', $password);
Loading history...
62
    }
63
64
    /**
65
     * Testing insertion method
66
     */
67
    public function testGetConnection(): void
68
    {
69
        // setupp
70
        $this->setDsn('dsn');
71
        $this->setUser('user');
72
        $this->setPassword('password');
73
        $mock = $this->getMock();
74
75
        $mock->expects($this->once())
76
            ->method('constructConnection')
77
            ->willReturn($this->getPdoMock());
78
79
        // test body and assertionss
80
        $mock->getConnection(); // creating connection object
81
        $mock->getConnection(); // getting created object
82
    }
83
84
    /**
85
     * Asserting exception if dsn is not set
86
     */
87
    public function testDsnException(): void
88
    {
89
        // setup
90
        \Mezon\Conf\Conf::deleteConfigValue('default-db-connection/dsn');
91
        $this->setUser('user');
92
        $this->setPassword('password');
93
        $mock = $this->getMock();
94
        $mock->setConnection(false);
95
96
        // assertions
97
        $this->expectException(\Exception::class);
98
99
        // test body
100
        $mock->getConnection();
101
    }
102
103
    /**
104
     * Asserting exception if user is not set
105
     */
106
    public function testUserException(): void
107
    {
108
        // setup
109
        $this->setDsn('dsn');
110
        \Mezon\Conf\Conf::deleteConfigValue('default-db-connection/user');
111
        $this->setPassword('password');
112
        $mock = $this->getMock();
113
        $mock->setConnection(false);
114
115
        // assertions
116
        $this->expectException(\Exception::class);
117
118
        // test body
119
        $mock->getConnection();
120
    }
121
122
    /**
123
     * Asserting exception if password is not set
124
     */
125
    public function testPasswordException(): void
126
    {
127
        // setup
128
        $this->setDsn('dsn');
129
        $this->setUser('user');
130
        \Mezon\Conf\Conf::deleteConfigValue('default-db-connection/password');
131
        $mock = $this->getMock();
132
        $mock->setConnection(false);
133
134
        // assertions
135
        $this->expectException(\Exception::class);
136
137
        // test body
138
        $mock->getConnection();
139
    }
140
}
141