InstallerTest::setUp()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 15
rs 9.4285
cc 1
eloc 11
nc 1
nop 0
1
<?php
2
3
namespace JhFlexiTimeTest\Install;
4
5
use JhFlexiTime\Install\Installer;
6
use ZfcUser\Entity\User;
7
use JhFlexiTime\Entity\UserSettings;
8
use JhFlexiTime\Entity\RunningBalance;
9
10
/**
11
 * Class InstallerTest
12
 * @package JhFlexiTimeTest\Install\Factory
13
 * @author Aydin Hassan <[email protected]>
14
 */
15
class InstallerTest extends \PHPUnit_Framework_TestCase
16
{
17
    protected $installer;
18
    protected $userRepository;
19
    protected $userSettingsRepository;
20
    protected $balanceRepository;
21
    protected $objectManager;
22
    protected $console;
23
24
25
    public function setUp()
26
    {
27
        $this->userRepository = $this->getMock('Jhuser\Repository\UserRepositoryInterface');
28
        $this->userSettingsRepository = $this->getMock('JhFlexiTime\Repository\UserSettingsRepositoryInterface');
29
        $this->balanceRepository = $this->getMock('JhFlexiTime\Repository\BalanceRepositoryInterface');
30
        $this->objectManager = $this->getMock('Doctrine\Common\Persistence\ObjectManager');
31
        $this->console = $this->getMock('Zend\Console\Adapter\AdapterInterface');
32
33
        $this->installer = new Installer(
34
            $this->userRepository,
35
            $this->userSettingsRepository,
36
            $this->balanceRepository,
37
            $this->objectManager
38
        );
39
    }
40
41
    public function testInstallerCallsAllCreateFunctionsForEachUser()
42
    {
43
        $installer = $this->getMock(
44
            'JhFlexiTime\Install\Installer',
45
            ['createSettingsRow', 'createRunningBalanceRow'],
46
            [
47
                $this->userRepository,
48
                $this->userSettingsRepository,
49
                $this->balanceRepository,
50
                $this->objectManager
51
            ]
52
        );
53
54
        $users = $this->getUsers();
55
56
        $this->userRepository
57
            ->expects($this->once())
58
            ->method('findAll')
59
            ->will($this->returnValue($users));
60
61
        $installer
62
            ->expects($this->at(0))
63
            ->method('createSettingsRow')
64
            ->with($users[0], $this->console);
65
66
        $installer
67
            ->expects($this->at(1))
68
            ->method('createRunningBalanceRow')
69
            ->with($users[0], $this->console);
70
71
        $installer
72
            ->expects($this->at(2))
73
            ->method('createSettingsRow')
74
            ->with($users[1], $this->console);
75
76
        $installer
77
            ->expects($this->at(3))
78
            ->method('createRunningBalanceRow')
79
            ->with($users[1], $this->console);
80
81
        $this->objectManager
82
            ->expects($this->once())
83
            ->method('flush');
84
85
        $installer->install($this->console);
86
87
    }
88
89
    public function testUserSettingsRowCreationIsSkippedIfRowExists()
90
    {
91
        $user = new User();
92
        $user->setEmail("[email protected]");
93
94
        $userSettings = new UserSettings();
95
96
        $this->console
97
            ->expects($this->at(0))
98
            ->method('writeLine')
99
            ->with("Checking if user '[email protected]' has a user_flex_settings row..", 4);
100
101
        $this->userSettingsRepository
102
            ->expects($this->once())
103
            ->method('findOneByUser')
104
            ->with($user)
105
            ->will($this->returnValue($userSettings));
106
107
        $this->console
108
             ->expects($this->at(1))
109
             ->method('writeLine')
110
             ->with('Settings row exists. Skipping', 4);
111
112
        $this->installer->createSettingsRow($user, $this->console);
113
    }
114
115
    public function testUserSettingsRowIsCreatedIfNotExist()
116
    {
117
        $user = new User();
118
        $user->setEmail("[email protected]");
119
120
        $this->console
121
            ->expects($this->at(0))
122
            ->method('writeLine')
123
            ->with("Checking if user '[email protected]' has a user_flex_settings row..", 4);
124
125
        $this->userSettingsRepository
126
            ->expects($this->once())
127
            ->method('findOneByUser')
128
            ->with($user)
129
            ->will($this->returnValue(null));
130
131
        $this->console
132
            ->expects($this->at(1))
133
            ->method('writeLine')
134
            ->with("Settings row does not exist. Creating... ", 4);
135
136
        $this->objectManager
137
             ->expects($this->once())
138
             ->method('persist')
139
             ->with($this->isInstanceOf('JhFlexiTime\Entity\UserSettings'));
140
141
        $this->installer->createSettingsRow($user, $this->console);
142
    }
143
144
    public function testExceptionIsThrownIfUserFlexSettingsTableDoesNotExist()
145
    {
146
        $user = new User();
147
        $user->setEmail("[email protected]");
148
149
        $e = new \Doctrine\DBAL\DBALException("Some Message");
150
151
        $this->console
152
            ->expects($this->at(0))
153
            ->method('writeLine')
154
            ->with("Checking if user '[email protected]' has a user_flex_settings row..", 4);
155
156
        $this->userSettingsRepository
157
            ->expects($this->once())
158
            ->method('findOneByUser')
159
            ->with($user)
160
            ->will($this->throwException($e));
161
162
        $this->setExpectedException('JhInstaller\Install\Exception');
163
        $this->installer->createSettingsRow($user, $this->console);
164
165
        $errors = [
166
            "The Database table has not been created. Be sure to run the schema tool. Message: Some Message"
167
        ];
168
169
        $this->assertEquals($errors, $this->installer->getErrors());
170
    }
171
172
    public function testRunningBalanceRowCreationIsSkippedIfRowExists()
173
    {
174
        $user = new User();
175
        $user->setEmail("[email protected]");
176
177
        $balance = new RunningBalance();
178
179
        $this->console
180
            ->expects($this->at(0))
181
            ->method('writeLine')
182
            ->with("Checking if user '[email protected]' has a running_balance row..", 4);
183
184
        $this->balanceRepository
185
            ->expects($this->once())
186
            ->method('findOneByUser')
187
            ->with($user)
188
            ->will($this->returnValue($balance));
189
190
        $this->console
191
            ->expects($this->at(1))
192
            ->method('writeLine')
193
            ->with('Running Balance row exists. Skipping', 4);
194
195
        $this->installer->createRunningBalanceRow($user, $this->console);
196
    }
197
198
    public function testRunningBalanceRowIsCreatedIfNotExist()
199
    {
200
        $user = new User();
201
        $user->setEmail("[email protected]");
202
203
        $this->console
204
            ->expects($this->at(0))
205
            ->method('writeLine')
206
            ->with("Checking if user '[email protected]' has a running_balance row..", 4);
207
208
        $this->balanceRepository
209
            ->expects($this->once())
210
            ->method('findOneByUser')
211
            ->with($user)
212
            ->will($this->returnValue(null));
213
214
        $this->console
215
            ->expects($this->at(1))
216
            ->method('writeLine')
217
            ->with("Running Balance row does not exist. Creating... ", 4);
218
219
        $this->objectManager
220
            ->expects($this->once())
221
            ->method('persist')
222
            ->with($this->isInstanceOf('JhFlexiTime\Entity\RunningBalance'));
223
224
        $this->installer->createRunningBalanceRow($user, $this->console);
225
    }
226
227
    public function testExceptionIsThrownIfRunningBalanceTableDoesNotExist()
228
    {
229
        $user = new User();
230
        $user->setEmail("[email protected]");
231
232
        $e = new \Doctrine\DBAL\DBALException("Some Message");
233
234
        $this->console
235
            ->expects($this->at(0))
236
            ->method('writeLine')
237
            ->with("Checking if user '[email protected]' has a running_balance row..", 4);
238
239
        $this->balanceRepository
240
            ->expects($this->once())
241
            ->method('findOneByUser')
242
            ->with($user)
243
            ->will($this->throwException($e));
244
245
        $this->setExpectedException('JhInstaller\Install\Exception');
246
        $this->installer->createRunningBalanceRow($user, $this->console);
247
248
        $errors = [
249
            "The Database table has not been created. Be sure to run the schema tool. Message: Some Message"
250
        ];
251
252
        $this->assertEquals($errors, $this->installer->getErrors());
253
    }
254
255
    public function testGetErrorsReturnsEmptyArray()
256
    {
257
        $errors = $this->installer->getErrors();
258
        $this->assertEquals([], $errors);
259
    }
260
261
    public function getUsers()
262
    {
263
        $user1 = new User();
264
        $user2 = new User();
265
266
        $user1->setEmail("[email protected]");
267
        $user2->setEmail("[email protected]");
268
269
        return [$user1, $user2];
270
    }
271
}
272