testFactoryThrowsExceptionIfUserSettingsDoesNotExist()   B
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 43
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 43
rs 8.8571
cc 1
eloc 31
nc 1
nop 0
1
<?php
2
3
namespace JhFlexiTimeTest\Entity\Factory;
4
5
use JhFlexiTime\Entity\Factory\UserSettingsFactory;
6
use JhFlexiTime\Entity\UserSettings;
7
use JhUser\Entity\User;
8
9
/**
10
 * Class UserSettingsFactoryTest
11
 * @package JhFlexiTimeTest\Entity\Factory
12
 * @author Aydin Hassan <[email protected]>
13
 */
14
class UserSettingsFactoryTest extends \PHPUnit_Framework_TestCase
15
{
16
    public function testFactoryThrowsExceptionIfUserSettingsDoesNotExist()
17
    {
18
        $serviceLocator   = $this->getMock('Zend\ServiceManager\ServiceLocatorInterface');
19
        $mockUserService  = $this->getMock('Zend\Authentication\AuthenticationService');
20
21
        $mockUserService
22
            ->expects($this->once())
23
            ->method('hasIdentity')
24
            ->will($this->returnValue(true));
25
26
        $serviceLocator
27
            ->expects($this->at(0))
28
            ->method('get')
29
            ->with('zfcuser_auth_service')
30
            ->will($this->returnValue($mockUserService));
31
32
        $user = new User();
33
        $mockUserService
34
            ->expects($this->once())
35
            ->method('getIdentity')
36
            ->will($this->returnValue($user));
37
38
39
        $userSettingsRepository = $this->getMock('JhFlexiTime\Repository\UserSettingsRepositoryInterface');
40
        $userSettingsRepository
41
            ->expects($this->once())
42
            ->method('findOneByUser')
43
            ->with($user)
44
            ->will($this->returnValue(null));
45
46
        $serviceLocator
47
            ->expects($this->at(1))
48
            ->method('get')
49
            ->with('JhFlexiTime\Repository\UserSettingsRepository')
50
            ->will($this->returnValue($userSettingsRepository));
51
52
        $this->setExpectedException('InvalidArgumentException', 'User does not have a settings row');
53
54
        $factory = new UserSettingsFactory();
55
        $factory->createService($serviceLocator);
56
57
        //$this->assertInstanceOf('JhFlexiTime\Entity\UserSettings', $factory->createService($serviceLocator));*/
58
    }
59
60
    public function testFactoryThrowsExceptionIfUserNotAuthenticated()
61
    {
62
        $serviceLocator   = $this->getMock('Zend\ServiceManager\ServiceLocatorInterface');
63
        $mockUserService  = $this->getMock('Zend\Authentication\AuthenticationServiceInterface');
64
65
        $mockUserService
66
            ->expects($this->once())
67
            ->method('hasIdentity')
68
            ->will($this->returnValue(false));
69
70
        $serviceLocator
71
            ->expects($this->once())
72
            ->method('get')
73
            ->with('zfcuser_auth_service')
74
            ->will($this->returnValue($mockUserService));
75
76
        $this->setExpectedException('InvalidArgumentException', 'User is not authenticated');
77
78
        $factory = new UserSettingsFactory();
79
        $factory->createService($serviceLocator);
80
    }
81
82
    public function testFactoryReturnsInstanceIfUserAndSettingsExist()
83
    {
84
        $serviceLocator   = $this->getMock('Zend\ServiceManager\ServiceLocatorInterface');
85
        $mockUserService  = $this->getMock('Zend\Authentication\AuthenticationService');
86
87
        $mockUserService
88
            ->expects($this->once())
89
            ->method('hasIdentity')
90
            ->will($this->returnValue(true));
91
92
        $serviceLocator
93
            ->expects($this->at(0))
94
            ->method('get')
95
            ->with('zfcuser_auth_service')
96
            ->will($this->returnValue($mockUserService));
97
98
        $user = new User();
99
        $mockUserService
100
            ->expects($this->once())
101
            ->method('getIdentity')
102
            ->will($this->returnValue($user));
103
104
        $userSettings = new UserSettings();
105
        $userSettingsRepository = $this->getMock('JhFlexiTime\Repository\UserSettingsRepositoryInterface');
106
        $userSettingsRepository
107
            ->expects($this->once())
108
            ->method('findOneByUser')
109
            ->with($user)
110
            ->will($this->returnValue($userSettings));
111
112
        $serviceLocator
113
            ->expects($this->at(1))
114
            ->method('get')
115
            ->with('JhFlexiTime\Repository\UserSettingsRepository')
116
            ->will($this->returnValue($userSettingsRepository));
117
118
        $factory = new UserSettingsFactory();
119
        $this->assertSame($userSettings, $factory->createService($serviceLocator));
120
    }
121
}
122